Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 19 additions & 4 deletions lib/drivers/interface.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

#################################################
Expand Down
18 changes: 18 additions & 0 deletions lib/drivers/mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions lib/drivers/postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions lib/sql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
127 changes: 61 additions & 66 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
#################################################
# 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
echo "'Sql__open' should return an error when trying to open driver 'hello'"
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
Expand All @@ -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
}

#################################################
Expand All @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down