diff --git a/README.md b/README.md index da91505..b7fd934 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,28 @@ if [[ $? -ne 0 ]]; then fi ``` +> Note: You can't run this function in a subshell, as it is responsible for sourcing in specific driver files. + +### Testing Connections + +After you open a connection, you'll likely want to verify we can actually start executing queries against it. + +You can test a connection by calling `Sql__ping`. + +```sh +# Test connection +local ping_output +ping_output=$(Sql__ping) +if [[ $? -ne 0 ]]; then + Sql__close + echo "Error connecting to database:" + echo "$ping_output" + return 1 +fi +``` + +> 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. + ### Closing Connections After you are done running all of your queries against the database, you will now have to close your database. diff --git a/lib/drivers/interface.sh b/lib/drivers/interface.sh index 6df82a4..7c09904 100644 --- a/lib/drivers/interface.sh +++ b/lib/drivers/interface.sh @@ -15,20 +15,35 @@ # @param $1: The statement to be executed ################################################# Sql__execute() { - Logger__error "No sql driver selected, must call 'Sql__open' before calling 'Sql__execute'" + echo "No sql driver selected, must call 'Sql__open' before calling 'Sql__execute'" + return 1 } + +################################################# +# Pings the database to verify if we have a +# valid connection. + +# @returns: 0 if we have a valid connection, +# 1 otherwise +################################################# +Sql__ping() { + echo "No sql driver selected, must call 'Sql__open' before calling 'Sql__ping'" + return 1 +} + + ################################################# # Checks if a specific table exists in the # database. # # @param $1: The name of the table to check if # if it exists. -# @returns: 0 if a table exists, 1 if no table -# exists +# @returns: 0 if a table exists, 1 otherwise ################################################# Sql__table_exists() { - Logger__error "No sql driver selected, must call 'Sql__open' before calling 'Sql__table_exists'" + echo "No sql driver selected, must call 'Sql__open' before calling 'Sql__table_exists'" + return 1 } ################################################# diff --git a/lib/drivers/mysql.sh b/lib/drivers/mysql.sh index 1ed8bc8..f64410e 100644 --- a/lib/drivers/mysql.sh +++ b/lib/drivers/mysql.sh @@ -20,6 +20,24 @@ Sql__execute() { 2>&1 } +################################################# +# Pings the database to verify if we have a +# valid connection. + +# @returns: 0 if we have a valid connection, +# 1 otherwise +################################################# +Sql__ping() { + local out + out=$(Sql__execute "SELECT 1;") + if [[ $? -eq 0 ]]; then + return 0 + else + echo "$out" + return 1 + fi +} + ################################################# # Checks if a specific table exists in the # database. diff --git a/lib/drivers/postgres.sh b/lib/drivers/postgres.sh index e74585e..5977123 100644 --- a/lib/drivers/postgres.sh +++ b/lib/drivers/postgres.sh @@ -22,6 +22,24 @@ Sql__execute() { return $result } +################################################# +# Pings the database to verify if we have a +# valid connection. + +# @returns: 0 if we have a valid connection, +# 1 otherwise +################################################# +Sql__ping() { + local out + out=$(Sql__execute "SELECT 1;") + if [[ $? -eq 0 ]]; then + return 0 + else + echo "$out" + return 1 + fi +} + ################################################# # Checks if a specific table exists in the # database. diff --git a/lib/sql.sh b/lib/sql.sh index a3dd079..02d7b4a 100644 --- a/lib/sql.sh +++ b/lib/sql.sh @@ -12,11 +12,12 @@ Sql__DRIVER_POSTGRES='postgres' # all other setup operations # # @param $1: The driver to setup +# @returns: 1 if the driver is invalid, +# 0 otherwise ################################################# Sql__open() { local driver="$1" if [[ "$driver" = "" ]]; then - echo "Sql__open must be passed a database driver name" return 1 fi @@ -26,7 +27,6 @@ Sql__open() { elif [[ "$driver" = "$Sql__DRIVER_POSTGRES" ]]; then . "$Sql__PACKAGE_LOCATION/lib/drivers/postgres.sh" else - echo "Invalid sql driver name '$driver'" return 1 fi diff --git a/test.sh b/test.sh index 11fb0f4..a394260 100644 --- a/test.sh +++ b/test.sh @@ -3,7 +3,7 @@ ################################################# # Tests the failure cases of Sql__open ################################################# -Sql__test_open_failure(){ +Sql__test_open_failure() { # Trying to run 'Sql__open' with an invalid driver open_hello=$(Sql__open "hello") if [[ $? -eq 0 ]]; then @@ -11,6 +11,15 @@ Sql__test_open_failure(){ return 1; fi + # Test connection + local ping_output + ping_output=$(Sql__ping) + if [[ $? -eq 0 ]]; then + Sql__close + echo "Should not be connected to a database on an open failure" + return 1 + fi + # Trying to run 'Sql__open' with no driver open_no_driver=$(Sql__open) if [[ $? -eq 0 ]]; then @@ -22,104 +31,90 @@ Sql__test_open_failure(){ ################################################# # Tests the success case of Sql__open for PostgreSQL ################################################# -Sql__test_postgres_open(){ - # Open DB - Sql__open "$Sql__DRIVER_POSTGRES" - if [[ $? -ne 0 ]]; then - echo "'Sql__open' returned an error" - return 1 - fi - - # Close DB - Sql__close - if [[ $? -ne 0 ]]; then - echo "'Sql__close' returned an error" - return 1 - fi +Sql__test_postgres_open() { + Sql_test_generic_open "$Sql__DRIVER_POSTGRES" } ################################################# # Tests the success case of Sql__open for MySQL -# -# This tests differs from PostgreSQL because -# this actually tests an implementation detail -# that users don't particularly need to worry -# about, but is important to test from the -# testing end. The two interfaces for both -# drivers are still exactly the same. -################################################# -Sql__test_mysql_open(){ - # Open DB - Sql__open "$Sql__DRIVER_MYSQL" - if [[ $? -ne 0 ]]; then - echo "'Sql__open' returned an error" - return 1 - fi - - # Verify MySQL config file was created - if [[ ! -f "$SQL_MYSQL_CONFIG_FILE" ]]; then - echo "MySQL config file was not created" - echo "Make sure you have permissions to write to $SQL_MYSQL_CONFIG_FILE" - return 1 - fi - - # Close DB - Sql__close - if [[ $? -ne 0 ]]; then - echo "'Sql__close' returned an error" - return 1 - fi - - # Verify MySQL config file was deleted - if [[ -f "$SQL_MYSQL_CONFIG_FILE" ]]; then - echo "MySQL config file was not deleted" - echo "Make sure you have permissions to delete $SQL_MYSQL_CONFIG_FILE" - return 1 - fi +################################################# +Sql__test_mysql_open() { + Sql_test_generic_open "$Sql__DRIVER_MYSQL" } ################################################# # Tests a successful PostgreSQL query ################################################# -Sql__test_postgres_query(){ - Sql__generic_test_query "$Sql__DRIVER_POSTGRES" +Sql__test_postgres_query() { + Sql_test_generic_query "$Sql__DRIVER_POSTGRES" } ################################################# # Tests a successful MySQL query ################################################# -Sql__test_mysql_query(){ - Sql__generic_test_query "$Sql__DRIVER_MYSQL" +Sql__test_mysql_query() { + Sql_test_generic_query "$Sql__DRIVER_MYSQL" } ################################################# # Tests the a failed PostgreSQL query ################################################# -Sql__test_postgres_query_failure(){ - Sql__generic_test_query_failure "$Sql__DRIVER_POSTGRES" +Sql__test_postgres_query_failure() { + Sql_test_generic_query_failure "$Sql__DRIVER_POSTGRES" } ################################################# # Tests the a failed MySQL query ################################################# -Sql__test_mysql_query_failure(){ - Sql__generic_test_query_failure "$Sql__DRIVER_MYSQL" +Sql__test_mysql_query_failure() { + Sql_test_generic_query_failure "$Sql__DRIVER_MYSQL" } ################################################# # Test the successful and unsuccessful case for # the Sql__table_exists function for PostgreSQL ################################################# -Sql__test_postgres_table_exists(){ - Sql__generic_test_table_exists "$Sql__DRIVER_POSTGRES" +Sql__test_postgres_table_exists() { + Sql_test_generic_table_exists "$Sql__DRIVER_POSTGRES" } ################################################# # Test the successful and unsuccessful case for # the Sql__table_exists function for MySQL ################################################# -Sql__test_mysql_table_exists(){ - Sql__generic_test_table_exists "$Sql__DRIVER_MYSQL" +Sql__test_mysql_table_exists() { + Sql_test_generic_table_exists "$Sql__DRIVER_MYSQL" +} + +################################################# +# Tests the success case of Sql__open for a driver +# +# @param $1: The driver to test +################################################# +Sql_test_generic_open() { + # Open DB + Sql__open "$1" + if [[ $? -ne 0 ]]; then + echo "'Sql__open' returned an error" + return 1 + fi + + # Test connection + local ping_output + ping_output=$(Sql__ping) + if [[ $? -ne 0 ]]; then + Sql__close + echo -e "Error connecting to database:" + echo "$ping_output" + return 1 + fi + + # Close DB + Sql__close + if [[ $? -ne 0 ]]; then + echo "'Sql__close' returned an error" + return 1 + fi } ################################################# @@ -128,7 +123,7 @@ Sql__test_mysql_table_exists(){ # # @param $1: The driver to test ################################################# -Sql__generic_test_query(){ +Sql_test_generic_query() { # Open DB Sql__open "$1" @@ -174,7 +169,7 @@ Sql__generic_test_query(){ # # @param $1: The driver to test ################################################# -Sql__generic_test_query_failure() { +Sql_test_generic_query_failure() { # Open DB Sql__open "$1" if [[ $? -ne 0 ]]; then @@ -203,7 +198,7 @@ Sql__generic_test_query_failure() { # # @param $1: The driver to test ################################################# -Sql__generic_test_table_exists() { +Sql_test_generic_table_exists() { # Open DB Sql__open "$1" if [[ $? -ne 0 ]]; then