Skip to content

Repository files navigation

bash_builtin_SQL_DB

Bash extensions for Postgresql and SQLite SQL databases in Bash scripts.

Summary

Bash is the most widely used UNIX shell. Using SQL databases intensively in Bash scripts using commands like psql or sqlite3 is slow.

Since the CLI programs /usr/bin/psql or /usr/bin/sqlite3 are called for each query, there is a large overhead.

cg_sqlite and cg_psql are fast bash-builtins for SQLite and Postgresql with less overhead. The result is captured in a bash array to avoid expensive invocations of fork().

Status: Working. Already using cg_psql in production.

Usage

db=/tmp/my_test_sqlite3.db
result=''
cg_sqlite  aa  -D <database file>  'SQL_Statements;'")

echo "Results: ${#aa[@]}    First: ${aa[0]:-}   Second: ${aa[1]:-}"

Options

-D  <database file or connection info, respectively>

-d  $'\t'                     Delimiter character of query result for columns
-d  '|'
-d  $'\t\n'                   ... and delimit records by 2nd character

-l  <Max number of results>   There is a default value DEFAULT_MAX_RESULTS for entries captured in a variable.

-V                            Print version.  Can be used to check available of the builtin

-v                            Increase verbosity. Can be repeated.

Example

./sqlite_test.sh

Benchmarks

Run SQL_benchmark.sh without parameter for instructions.

10,000 simple SELECT queries - lower values are better:

/usr/bin/pgsql          811 seconds
/usr/bin/sqlite          59 seconds

cg_psql   bashbuiltin   1.3 seconds
cg_sqlite bashbuiltin   0.5 seconds

Compilation and Installation

First Install dependencies

The enclosed compile script generates shared object files with the ending .so.

compile_builtin.sh bashbuiltin_psql.c

or

compile_builtin.sh bashbuiltin_sqlite.c

Builtins need to be loaded into the Bash instance with enable. The following commands can be run in the current interactive shell or can be placed at the top of a Bash script.

enable -f ~/compiled/bashbuiltin_sqlite.so   cg_sqlite

or

enable -f ~/compiled/bashbuiltin_psql.so   cg_psql

When they are loaded successfully, the help command will print the full documentation

help cg_psql

or

help cg_sqlite

Limitations

  • Other DBs like MySQL and DB2 are not (yet) supported. You can send a request-for-feature.

Related:

About

Bash builtins for using Postgresql and SQLite SQL databases in Bash script

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages