Skip to main content

Simple SQL REPL Command Handler

Project description

squelch

Squelch is a package providing a Simple SQL REPL Command Handler. Squelch uses SQLAlchemy for database access and so can support any database engine that SQLAlchemy supports, thereby providing a common database client experience for any of those database engines. Squelch is modelled on a simplified psql, the PostgreSQL command line client. The Squelch CLI supports readline history and basic SQL statement tab completions.

Install

The package can be installed from PyPI:

$ pip install squelch

From the command line

The package comes with a functional CLI called squelch, which just calls the package main, hence the following two invocations are equivalent:

$ python3 -m squelch
$ squelch

The only required argument is a database connection URL. This can either be passed on the command line, via the --url option, or specified in a JSON configuration file given by the --conf-file option. The form of the JSON configuration file is as follows:

{
  "url": "<URL>"
}

where the <URL> follows the SQLAlchemy database connection URL syntax. An advantage of using a configuration file is that it avoids providing database login credentials in plain text on the command line.

Running queries

When running the CLI in a terminal, the user is dropped into an interactive REPL. From here, the user is prompted for input, which can be an SQL statement to be sent to the database engine, or a CLI command (backslash command) such as \q to quit the CLI:

$ python -m squelch -c tests/data/test.json 
squelch (0.3.0)
Type "help" for help.

tests/data/test.db => select * from data;
 id   | name   | status   | key
------+--------+----------+-----------
 1    | pmb    | 0        | 0000-0000
 2    | abc    | 0        | 0000-0001
 3    | def    | 0        | 0000-0002
 4    | ghi    | 1        | 0000-0003
(4 rows)

tests/data/test.db => \q

Alternatively, the CLI can be called as a one-shot by providing a query on stdin, thereby allowing it to be called in scripts.

For example, using echo to pipe a query to the CLI:

$ echo "select * from data" | python -m squelch -c tests/data/test.json
 id   | name   | status   | key
------+--------+----------+-----------
 1    | pmb    | 0        | 0000-0000
 2    | abc    | 0        | 0000-0001
 3    | def    | 0        | 0000-0002
 4    | ghi    | 1        | 0000-0003
(4 rows)

Or redirecting from a file. Given the following queries in a file:

$ cat tests/data/queries.sql
select * from data;
select * from data where id = 1;
select * from status where status = 1;

the result would be:

$ python -m squelch -c tests/data/test.json < tests/data/queries.sql
 id   | name   | status   | key
------+--------+----------+-----------
 1    | pmb    | 0        | 0000-0000
 2    | abc    | 0        | 0000-0001
 3    | def    | 0        | 0000-0002
 4    | ghi    | 1        | 0000-0003
(4 rows)

 id   | name   | status   | key
------+--------+----------+-----------
 1    | pmb    | 0        | 0000-0000
(1 row)

 name   | status
--------+----------
 ghi    | 1
(1 row)

Machine-readable data in scripts

It's likely that when calling the CLI from a script, the user is less interested in the data being laid out in a human-readable table, rather, they probably want it as machine-readable data. The table format can be set (using the --pset option) to csv so that the table is printed as CSV. Additionally, the table footer can be turned off (again using --pset) so that the result is just a simple CSV table. Taking our example from earlier, the result would be:

$ echo "select * from data;" | python -m squelch -c tests/data/test.json --pset format=csv --pset footer=off
id,name,status,key
1,pmb,0,0000-0000
2,abc,0,0000-0001
3,def,0,0000-0002
4,ghi,1,0000-0003

Command line usage

usage: squelch [-h] [-c CONF_FILE] [-u URL] [-S [NAME=VALUE [NAME=VALUE ...]]]
               [-P [NAME=VALUE [NAME=VALUE ...]]] [-v] [-V]

Squelch is a Simple SQL REPL Command Handler.

optional arguments:
  -h, --help            show this help message and exit
  -c CONF_FILE, --conf-file CONF_FILE
                        The full path to a JSON configuration file. It
                        defaults to ./squelch.json.
  -u URL, --url URL     The database connection URL, as required by
                        sqlalchemy.create_engine().
  -S [NAME=VALUE [NAME=VALUE ...]], --set [NAME=VALUE [NAME=VALUE ...]]
                        Set state variable NAME to VALUE.
  -P [NAME=VALUE [NAME=VALUE ...]], --pset [NAME=VALUE [NAME=VALUE ...]]
                        Set printing state variable NAME to VALUE.
  -v, --verbose         Turn verbose messaging on. The effects of this option
                        are incremental.
  -V, --version         show program's version number and exit

Database Connection URL

The database connection URL can either be passed on the command line, via the --url option, or specified in a JSON configuration file given by the --conf-file option.  The form of the JSON configuration file is as follows:

{
  "url": "<URL>"
}

From the SQLAlchemy documentation:

"The string form of the URL is dialect[+driver]://user:password@host/dbname[?key=value..], where dialect is a database name such as mysql, oracle, postgresql, etc., and driver the name of a DBAPI, such as psycopg2, pyodbc, cx_oracle, etc. Alternatively, the URL can be an instance of URL."

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

squelch-0.3.0.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

squelch-0.3.0-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file squelch-0.3.0.tar.gz.

File metadata

  • Download URL: squelch-0.3.0.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.8.10 Linux/5.15.0-91-generic

File hashes

Hashes for squelch-0.3.0.tar.gz
Algorithm Hash digest
SHA256 22d7726672f1d45893f6b267ca13c04278358689d146f3289ee8b1a691f4e2b3
MD5 4ef1f8d491a6e6483a92547265336459
BLAKE2b-256 cedd66eb8f0467db981be9287648ca93469ae0dcc676b5761fc33e460831a65d

See more details on using hashes here.

File details

Details for the file squelch-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: squelch-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.8.10 Linux/5.15.0-91-generic

File hashes

Hashes for squelch-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 165b60fcbdf536f3fdc53352b9b7ea0c45810cac2a5d830e2d9684dac2743e90
MD5 fde3aa0b67901a19995af5f536792c1d
BLAKE2b-256 541adea8b47d6bb5e1ba572da5641206a1ead496aa575725bd8edf47fa50fc5f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page