Skip to content

Commit 1a5af9f

Browse files
author
adrianbartyczak
committed
Update and rename mysqlutils
1 parent f608732 commit 1a5af9f

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Shell provides solutions for the following application layer paradigms:
1313

1414
Solutions for applications
1515

16-
* [RDBMS functions for MySQL](functions_scripts/applications/database/mysqlutils)
16+
* [Generic RDBMS utility functions for a MySQL database](functions_scripts/applications/database/mysqldbutils)
1717
* [Check if a PostgreSQL database exists.](one-liners/applications/database/postgresql-database.one-liners)
1818

1919
## Programming languages

functions_scripts/applications/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
## database/
55

66
* [**mongodbutils**](database/mongodbutils): RDBMS functions for MongoDB
7-
* [**mysqlutils**](database/mysqlutils): NoSQL functions for MySQL
7+
* [**mysqldbutils**](database/mysqldbutils): Generic RDBMS utility functions for a MySQL database

functions_scripts/applications/database/mysqlutils renamed to functions_scripts/applications/database/mysqldbutils

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,48 @@
11
#!/usr/bin/env bash
22
#
3-
# RDBMS functions for MySQL
3+
# Generic RDBMS utility functions for a MySQL database
44
#
55
# Usage:
6-
# mysqlutils <databaes user> <database name> <action>
6+
# mysqldbutils <databaes_user> <database_name> <action>
77
#
88

99
if [ "$#" -lt 3 ]; then
10-
echo -e 'mysqlutils: invalid number of arguments; database user,\n' \
10+
echo -e 'mysqldbutils: invalid number of arguments; database user,\n'\
1111
' database name and action required' 1>&2
1212
exit 1
1313
fi
1414

15-
DB_USER="${1}"
16-
DB_NAME="${2}"
17-
ACTION="${3}"
15+
readonly DB_USER="${1}"
16+
readonly DB_NAME="${2}"
17+
readonly ACTION="${3}"
1818
shift 3
1919

2020
case "${ACTION}" in
2121
# Create the database from an SQL file.
2222
# Usage:
23-
# ... create <create SQL file>
24-
'create')
25-
if [ "$#" -eq 0 ]; then
26-
echo "mysqlutils: no SQL file specified" 1>&2
27-
exit 1
28-
fi
29-
if [ ! -f "${1}" ]; then
30-
echo "mysqlutils: file \"${1}\" not found" 1>&2
31-
exit 1
32-
fi
23+
# ... create_from_file <sql_file>
24+
'create_from_file')
25+
[ "$#" -eq 0 ] && \
26+
echo "mysqldbutils: no SQL file specified" 1>&2 && \
27+
exit 1
28+
[ ! -f "${1}" ] && \
29+
echo "mysqldbutils: file \"${1}\" not found" 1>&2 && \
30+
exit 1
3331

3432
mysql -u"${DB_USER}" -e "CREATE DATABASE IF NOT EXISTS ${DB_NAME}"
35-
3633
if mysql -u"${DB_USER}" -e "use ${DB_NAME}" 2>/dev/null; then
37-
bash ${0} 'droptables' "${DB_USER}" "${DB_NAME}"
34+
bash "${0}" 'drop_all_tables' "${DB_USER}" "${DB_NAME}"
3835
mysql -u"${DB_USER}" "${DB_NAME}" < "${1}"
3936
else
40-
echo "mysqlutils: database \"${DB_NAME}\" not found" 1>&2
37+
echo "mysqldbutils: database \"${DB_NAME}\" not found" 1>&2
4138
fi
4239
;;
4340

4441
# Drop all tables in the database.
4542
# Usage:
46-
# ... droptables
47-
'droptables')
48-
Q1="
43+
# ... drop_all_tables
44+
'drop_all_tables')
45+
query="
4946
USE ${DB_NAME};
5047
SET FOREIGN_KEY_CHECKS = 0;
5148
SET GROUP_CONCAT_MAX_LEN=32768;
@@ -61,13 +58,13 @@ case "${ACTION}" in
6158
DEALLOCATE PREPARE stmt;
6259
SET FOREIGN_KEY_CHECKS = 1;
6360
"
64-
mysql -u"${DB_USER}" -e "${Q1}"
61+
mysql -u"${DB_USER}" -e "${query}"
6562
;;
6663

67-
# Clear data from all tables in the database.
64+
# Clear all tables in the database.
6865
# Usage:
69-
# ... cleardata
70-
'cleardata')
66+
# ... clear_all_tables
67+
'clear_all_tables')
7168
mysqldump -d -u"${DB_USER}" --add-drop-table "${DB_NAME}" > \
7269
./db_export_tmp.sql
7370
mysql -u"${DB_USER}" "${DB_NAME}" < ./db_export_tmp.sql
@@ -80,9 +77,9 @@ case "${ACTION}" in
8077
'export')
8178
mysqldump -u"${DB_USER}" "${DB_NAME}" > ./db_"${DB_NAME}"_export.sql
8279
;;
83-
80+
8481
*)
85-
echo "mysqlutils: unknown action \"${ACTION}\"" 1>&2
82+
echo "mysqldbutils: unknown action \"${ACTION}\"" 1>&2
8683
;;
8784

8885
esac

0 commit comments

Comments
 (0)