Skip to content

Commit be8ab98

Browse files
Add SQL ping function (#11)
* Add SQL ping function * Add sql ping tests * Update README for ping
1 parent 92c3899 commit be8ab98

File tree

6 files changed

+140
-72
lines changed

6 files changed

+140
-72
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ if [[ $? -ne 0 ]]; then
132132
fi
133133
```
134134

135+
> Note: You can't run this function in a subshell, as it is responsible for sourcing in specific driver files.
136+
137+
### Testing Connections
138+
139+
After you open a connection, you'll likely want to verify we can actually start executing queries against it.
140+
141+
You can test a connection by calling `Sql__ping`.
142+
143+
```sh
144+
# Test connection
145+
local ping_output
146+
ping_output=$(Sql__ping)
147+
if [[ $? -ne 0 ]]; then
148+
Sql__close
149+
echo "Error connecting to database:"
150+
echo "$ping_output"
151+
return 1
152+
fi
153+
```
154+
155+
> The reason why this doesn't just automatically happen in `Sql__open` is because you can't run `Sql__open` in a subshell (as it sources other files), and this function has output that we want to capture.
156+
135157
### Closing Connections
136158

137159
After you are done running all of your queries against the database, you will now have to close your database.

lib/drivers/interface.sh

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,35 @@
1515
# @param $1: The statement to be executed
1616
#################################################
1717
Sql__execute() {
18-
Logger__error "No sql driver selected, must call 'Sql__open' before calling 'Sql__execute'"
18+
echo "No sql driver selected, must call 'Sql__open' before calling 'Sql__execute'"
19+
return 1
1920
}
2021

22+
23+
#################################################
24+
# Pings the database to verify if we have a
25+
# valid connection.
26+
27+
# @returns: 0 if we have a valid connection,
28+
# 1 otherwise
29+
#################################################
30+
Sql__ping() {
31+
echo "No sql driver selected, must call 'Sql__open' before calling 'Sql__ping'"
32+
return 1
33+
}
34+
35+
2136
#################################################
2237
# Checks if a specific table exists in the
2338
# database.
2439
#
2540
# @param $1: The name of the table to check if
2641
# if it exists.
27-
# @returns: 0 if a table exists, 1 if no table
28-
# exists
42+
# @returns: 0 if a table exists, 1 otherwise
2943
#################################################
3044
Sql__table_exists() {
31-
Logger__error "No sql driver selected, must call 'Sql__open' before calling 'Sql__table_exists'"
45+
echo "No sql driver selected, must call 'Sql__open' before calling 'Sql__table_exists'"
46+
return 1
3247
}
3348

3449
#################################################

lib/drivers/mysql.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ Sql__execute() {
2020
2>&1
2121
}
2222

23+
#################################################
24+
# Pings the database to verify if we have a
25+
# valid connection.
26+
27+
# @returns: 0 if we have a valid connection,
28+
# 1 otherwise
29+
#################################################
30+
Sql__ping() {
31+
local out
32+
out=$(Sql__execute "SELECT 1;")
33+
if [[ $? -eq 0 ]]; then
34+
return 0
35+
else
36+
echo "$out"
37+
return 1
38+
fi
39+
}
40+
2341
#################################################
2442
# Checks if a specific table exists in the
2543
# database.

lib/drivers/postgres.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ Sql__execute() {
2222
return $result
2323
}
2424

25+
#################################################
26+
# Pings the database to verify if we have a
27+
# valid connection.
28+
29+
# @returns: 0 if we have a valid connection,
30+
# 1 otherwise
31+
#################################################
32+
Sql__ping() {
33+
local out
34+
out=$(Sql__execute "SELECT 1;")
35+
if [[ $? -eq 0 ]]; then
36+
return 0
37+
else
38+
echo "$out"
39+
return 1
40+
fi
41+
}
42+
2543
#################################################
2644
# Checks if a specific table exists in the
2745
# database.

lib/sql.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ Sql__DRIVER_POSTGRES='postgres'
1212
# all other setup operations
1313
#
1414
# @param $1: The driver to setup
15+
# @returns: 1 if the driver is invalid,
16+
# 0 otherwise
1517
#################################################
1618
Sql__open() {
1719
local driver="$1"
1820
if [[ "$driver" = "" ]]; then
19-
echo "Sql__open must be passed a database driver name"
2021
return 1
2122
fi
2223

@@ -26,7 +27,6 @@ Sql__open() {
2627
elif [[ "$driver" = "$Sql__DRIVER_POSTGRES" ]]; then
2728
. "$Sql__PACKAGE_LOCATION/lib/drivers/postgres.sh"
2829
else
29-
echo "Invalid sql driver name '$driver'"
3030
return 1
3131
fi
3232

test.sh

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@
33
#################################################
44
# Tests the failure cases of Sql__open
55
#################################################
6-
Sql__test_open_failure(){
6+
Sql__test_open_failure() {
77
# Trying to run 'Sql__open' with an invalid driver
88
open_hello=$(Sql__open "hello")
99
if [[ $? -eq 0 ]]; then
1010
echo "'Sql__open' should return an error when trying to open driver 'hello'"
1111
return 1;
1212
fi
1313

14+
# Test connection
15+
local ping_output
16+
ping_output=$(Sql__ping)
17+
if [[ $? -eq 0 ]]; then
18+
Sql__close
19+
echo "Should not be connected to a database on an open failure"
20+
return 1
21+
fi
22+
1423
# Trying to run 'Sql__open' with no driver
1524
open_no_driver=$(Sql__open)
1625
if [[ $? -eq 0 ]]; then
@@ -22,104 +31,90 @@ Sql__test_open_failure(){
2231
#################################################
2332
# Tests the success case of Sql__open for PostgreSQL
2433
#################################################
25-
Sql__test_postgres_open(){
26-
# Open DB
27-
Sql__open "$Sql__DRIVER_POSTGRES"
28-
if [[ $? -ne 0 ]]; then
29-
echo "'Sql__open' returned an error"
30-
return 1
31-
fi
32-
33-
# Close DB
34-
Sql__close
35-
if [[ $? -ne 0 ]]; then
36-
echo "'Sql__close' returned an error"
37-
return 1
38-
fi
34+
Sql__test_postgres_open() {
35+
Sql_test_generic_open "$Sql__DRIVER_POSTGRES"
3936
}
4037

4138
#################################################
4239
# Tests the success case of Sql__open for MySQL
43-
#
44-
# This tests differs from PostgreSQL because
45-
# this actually tests an implementation detail
46-
# that users don't particularly need to worry
47-
# about, but is important to test from the
48-
# testing end. The two interfaces for both
49-
# drivers are still exactly the same.
50-
#################################################
51-
Sql__test_mysql_open(){
52-
# Open DB
53-
Sql__open "$Sql__DRIVER_MYSQL"
54-
if [[ $? -ne 0 ]]; then
55-
echo "'Sql__open' returned an error"
56-
return 1
57-
fi
58-
59-
# Verify MySQL config file was created
60-
if [[ ! -f "$SQL_MYSQL_CONFIG_FILE" ]]; then
61-
echo "MySQL config file was not created"
62-
echo "Make sure you have permissions to write to $SQL_MYSQL_CONFIG_FILE"
63-
return 1
64-
fi
65-
66-
# Close DB
67-
Sql__close
68-
if [[ $? -ne 0 ]]; then
69-
echo "'Sql__close' returned an error"
70-
return 1
71-
fi
72-
73-
# Verify MySQL config file was deleted
74-
if [[ -f "$SQL_MYSQL_CONFIG_FILE" ]]; then
75-
echo "MySQL config file was not deleted"
76-
echo "Make sure you have permissions to delete $SQL_MYSQL_CONFIG_FILE"
77-
return 1
78-
fi
40+
#################################################
41+
Sql__test_mysql_open() {
42+
Sql_test_generic_open "$Sql__DRIVER_MYSQL"
7943
}
8044

8145
#################################################
8246
# Tests a successful PostgreSQL query
8347
#################################################
84-
Sql__test_postgres_query(){
85-
Sql__generic_test_query "$Sql__DRIVER_POSTGRES"
48+
Sql__test_postgres_query() {
49+
Sql_test_generic_query "$Sql__DRIVER_POSTGRES"
8650
}
8751

8852
#################################################
8953
# Tests a successful MySQL query
9054
#################################################
91-
Sql__test_mysql_query(){
92-
Sql__generic_test_query "$Sql__DRIVER_MYSQL"
55+
Sql__test_mysql_query() {
56+
Sql_test_generic_query "$Sql__DRIVER_MYSQL"
9357
}
9458

9559
#################################################
9660
# Tests the a failed PostgreSQL query
9761
#################################################
98-
Sql__test_postgres_query_failure(){
99-
Sql__generic_test_query_failure "$Sql__DRIVER_POSTGRES"
62+
Sql__test_postgres_query_failure() {
63+
Sql_test_generic_query_failure "$Sql__DRIVER_POSTGRES"
10064
}
10165

10266
#################################################
10367
# Tests the a failed MySQL query
10468
#################################################
105-
Sql__test_mysql_query_failure(){
106-
Sql__generic_test_query_failure "$Sql__DRIVER_MYSQL"
69+
Sql__test_mysql_query_failure() {
70+
Sql_test_generic_query_failure "$Sql__DRIVER_MYSQL"
10771
}
10872

10973
#################################################
11074
# Test the successful and unsuccessful case for
11175
# the Sql__table_exists function for PostgreSQL
11276
#################################################
113-
Sql__test_postgres_table_exists(){
114-
Sql__generic_test_table_exists "$Sql__DRIVER_POSTGRES"
77+
Sql__test_postgres_table_exists() {
78+
Sql_test_generic_table_exists "$Sql__DRIVER_POSTGRES"
11579
}
11680

11781
#################################################
11882
# Test the successful and unsuccessful case for
11983
# the Sql__table_exists function for MySQL
12084
#################################################
121-
Sql__test_mysql_table_exists(){
122-
Sql__generic_test_table_exists "$Sql__DRIVER_MYSQL"
85+
Sql__test_mysql_table_exists() {
86+
Sql_test_generic_table_exists "$Sql__DRIVER_MYSQL"
87+
}
88+
89+
#################################################
90+
# Tests the success case of Sql__open for a driver
91+
#
92+
# @param $1: The driver to test
93+
#################################################
94+
Sql_test_generic_open() {
95+
# Open DB
96+
Sql__open "$1"
97+
if [[ $? -ne 0 ]]; then
98+
echo "'Sql__open' returned an error"
99+
return 1
100+
fi
101+
102+
# Test connection
103+
local ping_output
104+
ping_output=$(Sql__ping)
105+
if [[ $? -ne 0 ]]; then
106+
Sql__close
107+
echo -e "Error connecting to database:"
108+
echo "$ping_output"
109+
return 1
110+
fi
111+
112+
# Close DB
113+
Sql__close
114+
if [[ $? -ne 0 ]]; then
115+
echo "'Sql__close' returned an error"
116+
return 1
117+
fi
123118
}
124119

125120
#################################################
@@ -128,7 +123,7 @@ Sql__test_mysql_table_exists(){
128123
#
129124
# @param $1: The driver to test
130125
#################################################
131-
Sql__generic_test_query(){
126+
Sql_test_generic_query() {
132127
# Open DB
133128
Sql__open "$1"
134129

@@ -174,7 +169,7 @@ Sql__generic_test_query(){
174169
#
175170
# @param $1: The driver to test
176171
#################################################
177-
Sql__generic_test_query_failure() {
172+
Sql_test_generic_query_failure() {
178173
# Open DB
179174
Sql__open "$1"
180175
if [[ $? -ne 0 ]]; then
@@ -203,7 +198,7 @@ Sql__generic_test_query_failure() {
203198
#
204199
# @param $1: The driver to test
205200
#################################################
206-
Sql__generic_test_table_exists() {
201+
Sql_test_generic_table_exists() {
207202
# Open DB
208203
Sql__open "$1"
209204
if [[ $? -ne 0 ]]; then

0 commit comments

Comments
 (0)