Skip to content

Commit 883e5d1

Browse files
committed
Initial commit
0 parents  commit 883e5d1

File tree

6 files changed

+186
-0
lines changed

6 files changed

+186
-0
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Brandon Romano
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

ash_config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: SQL
2+
package: github.com/ash-shell/sql
3+
default_alias: sql

lib/drivers/interface.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
#
3+
# This file is a blank interface that all sql drivers must adhere to.
4+
#
5+
# Some of these methods in this actual interface have error messages,
6+
# as this interface is actually imported before any specific driver is
7+
# loaded.
8+
#
9+
# When creating a driver, you must override all methods here.
10+
11+
#################################################
12+
# Executes a database statement and outputs the
13+
# result as a space delimited string of columns.
14+
#
15+
# @param $1: The statement to be executed
16+
#################################################
17+
Sql__execute() {
18+
Logger__error "No sql driver selected, must call 'Sql__open' before calling 'Sql__execute'"
19+
}
20+
21+
#################################################
22+
# Here is an opportunity to provide any driver
23+
# specific logic for when the database is opened.
24+
#################################################
25+
Sql_driver_open() {
26+
:
27+
}
28+
29+
#################################################
30+
# Here is an opportunity to provide any driver
31+
# specific logic for when the database is closed.
32+
#################################################
33+
Sql_driver_close() {
34+
:
35+
}

lib/drivers/mysql.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
# Temporary config file location
4+
Sql_MYSQL_CONFIG_FILE="/tmp/ash_sql_mysql_config.cnf"
5+
6+
#################################################
7+
# Executes a database statement and
8+
# gives the output
9+
#
10+
# @param $1: the statement to be executed
11+
#################################################
12+
Sql__execute() {
13+
"$SQL_MYSQL_PATH" \
14+
--defaults-extra-file="$Sql_MYSQL_CONFIG_FILE" \
15+
--database="$SQL_DATABASE_NAME" \
16+
--silent \
17+
--skip-column-names \
18+
--batch \
19+
--execute="$1"
20+
}
21+
22+
#################################################
23+
# Generates the MySQL config file to be used
24+
#################################################
25+
Sql_driver_open() {
26+
# Deleting old mysql config file if it existed
27+
if [[ -e "$Sql_MYSQL_CONFIG_FILE" ]]; then
28+
rm "$Sql_MYSQL_CONFIG_FILE"
29+
fi
30+
31+
# Creating new MySQL config file
32+
touch "$Sql_MYSQL_CONFIG_FILE"
33+
chmod 600 "$Sql_MYSQL_CONFIG_FILE"
34+
35+
echo "[client]" >> "$Sql_MYSQL_CONFIG_FILE"
36+
echo "user = $SQL_USER" >> "$Sql_MYSQL_CONFIG_FILE"
37+
echo "password = $SQL_PASSWORD" >> "$Sql_MYSQL_CONFIG_FILE"
38+
echo "host = $SQL_HOST" >> "$Sql_MYSQL_CONFIG_FILE"
39+
echo "port = $SQL_PORT" >> "$Sql_MYSQL_CONFIG_FILE"
40+
}
41+
42+
#################################################
43+
# Deletes the temporary database config file
44+
#################################################
45+
Sql_driver_close() {
46+
# Deleting old mysql config file if it existed
47+
if [[ -e "$Sql_MYSQL_CONFIG_FILE" ]]; then
48+
rm "$Sql_MYSQL_CONFIG_FILE"
49+
fi
50+
}

lib/drivers/postgres.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
#################################################
4+
# Executes a database statement and
5+
# gives the output
6+
#
7+
# @param $1: The statement to be executed
8+
#################################################
9+
Sql__execute() {
10+
output=$("$SQL_POSTGRES_PATH" \
11+
--username "$SQL_USER" \
12+
--dbname "$SQL_DATABASE_NAME" \
13+
--host "$SQL_HOST" \
14+
--command "$1" \
15+
--no-align \
16+
--field-separator=" " \
17+
--record-separator="\r\n" \
18+
--tuples-only 2>&1)
19+
result="$?"
20+
printf "$output"
21+
return $result
22+
}
23+
24+
#################################################
25+
# Do nothing, no driver specific setup
26+
#################################################
27+
Sql_driver_open() {
28+
:
29+
}
30+
31+
#################################################
32+
# Do nothing, no driver specific shutdown
33+
#################################################
34+
Sql_driver_close() {
35+
:
36+
}

lib/sql.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
Sql__PACKAGE_LOCATION="$(Ash__find_module_directory "github.com/ash-shell/sql")"
4+
Sql__DRIVER_MYSQL='mysql'
5+
Sql__DRIVER_POSTGRES='postgres'
6+
7+
# Load the interface for appropriate non loaded messages
8+
. "$Sql__PACKAGE_LOCATION/lib/drivers/interface.sh"
9+
10+
#################################################
11+
# Loads the appropriate database driver handles
12+
# all other setup operations
13+
#
14+
# @param $1: The driver to setup
15+
#################################################
16+
Sql__open() {
17+
local driver="$1"
18+
if [[ "$driver" = "" ]]; then
19+
Logger__error "Sql__open must be passed a database driver name"
20+
fi
21+
22+
# Loading the appropriate database driver
23+
if [[ "$driver" = "$Sql__DRIVER_MYSQL" ]]; then
24+
. "$Sql__PACKAGE_LOCATION/lib/drivers/mysql.sh"
25+
elif [[ "$driver" = "$Sql__DRIVER_POSTGRES" ]]; then
26+
. "$Sql__PACKAGE_LOCATION/lib/drivers/postgres.sh"
27+
else
28+
Logger__error "Invalid sql driver name '$driver'"
29+
return
30+
fi
31+
32+
# Run any driver specific logic
33+
Sql_driver_open
34+
}
35+
36+
#################################################
37+
# Shuts down the database
38+
#################################################
39+
Sql__close() {
40+
Sql_driver_close
41+
}

0 commit comments

Comments
 (0)