Skip to content

muhmud/qsh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

qsh

Query SHell - improved querying from your terminal

QSH

Currently supports:

  • sqlite3 (3.37+)
  • mysql
  • psql
  • sqlcmd (for mssql)
  • sqlcl (for oracle)
  • mclient (for monetdb)

There is also a generic mode, which can potentially be used with other tools not on this list. The generic mode can be used with non-database tools as well, like the redis-cli, REPLs and even shells, such as bash. See the usage section below for more details.

Prerequisites

You'll need to install & use tmux, which is needed to manage the split panes. It should be available from your package manager. Installing jq and tree would also be a good idea. For the generic mode, you will need rlwrap and perl.

For better viewing of SQL results, the pspg pager is recommended (ensure you have the latest version), however, you could also use less -SinFX. When displaying results, qsh will try to make a sensible choice, however, you can instead explicitly choose a pager. For generic mode, bat can also work well.

To format SQL statements, you will need python 3 and sqlparse.

Note: If you have issues, make sure your local install of qsh is fully up-to-date.

Setup

Clone this repository to your home:

$ git clone https://github.com/muhmud/qsh.git ~/.qsh

And then add the ~/.qsh/bin directory to your PATH.

You now just need to setup the editor you want to use for writing SQL statements, which will be triggered from your SQL client tool. If you want to, and it's recommended, you can setup a keyboard shortcut for this in your tmux config. This will give you the same consistent shortcut for starting the editor from any tool.

The following example does this for Alt-q:

# qsh
bind-key -n M-q run-shell ~/.qsh/bin/qsh-start

You can currently use either vim/nvim or micro. Whichever you choose, make sure your QSH_EDITOR or EDITOR/VISUAL environment variable is set appropriately.

vim/nvim

vim-plug

Plug 'muhmud/qsh', { 'dir': '~/.qsh', 'branch': 'main', 'rtp': 'editors/vim' }

packer

{ "~/.qsh/editors/vim", as = "Qsh" }

The default key mappings can be found here. You can disable them by setting g:qsh_enable_key_mappings to 0.

You can add custom key mappings like this:

autocmd Filetype sql call QshCustomSqlKeyMappings()
function QshCustomSqlKeyMappings() 
   ...
endfunction

Micro

The micro plugin can be installed by executing the following:

$ mkdir -p ~/.config/micro/plug && cp -r ~/.qsh/editors/micro ~/.config/micro/plug/qsh

The following key mappings, or similar, can be added to ~/.config/micro/bindings.json:

"Alt-g": "command:QshExecute",
"Alt-G": "command:QshExecute '^---$' 0",
"Alt-e": "command:QshExecuteSelection",
"Alt-y": "command:QshExecuteAll",
"Alt-d": "command:QshExecuteNamedScript 'describe'",
"Alt-r": "command:QshExecuteNamedScript 'select-some'",
"Alt-v": "command:QshExecuteScript",
"Alt-i": "command:QshExecuteSnippet",
"Alt-t": "command:QshExecuteNamedSnippet 'format'",
"Alt-p": "command:QshSetUnsetPrefix"

Usage

From within a tmux session, prefix the invocation of your SQL client with qsh:

$ qsh psql

This will setup your SQL client environment appropriately. Now, trigger the editor using the command for your environment. For mysql, this would be \e;, and for psql, \e or if you setup a keyboard shortcut in your tmux config, as described above, you could use that also. Alternatively, use the -s option to startup the editor automatically.

You should see the editor pane created, where you can now type in queries. A default SQL file is created for you, however, you could open up any other file you need to.

Note: For sqlcl, you would start the editor by using @qsh

Generic Mode

If you invoke a tool that qsh does not know about, it will go into generic mode and will attempt to give you a usable querying experience, so you shouldn't need to do anything differently.

Generic

If your terminal looks messed up when the tool starts, try to use the REPL mode by specifying the -r option:

$ qsh -r redis-cli

You can also use the -f option to change the extension of the file opened up in the editor. This can be useful to enable language/domain specific features for the tool you are using. Setting this value to something that isn't sql implicitly enables REPL mode:

$ qsh -f js node

You can use scripts and snippets with generic mode tools by setting the QSH_SCRIPTS_PATH and QSH_SNIPPETS_PATH environment variables. See the sections below for details on how these work.

You can register the settings you use for generic tools, including certain environment variables, just as you would register a connection for a database. This makes it easier to invoke the tool in the same way in the future. See the registering connections section for more details, or just run the qsh and/or qsh-reg tools without any arguments.

For example, the following registers an invocation for using qsh with zsh:

$ QSH_EDITOR_COMMAND="\u001B\u0016" \
  QSH_NEWLINE_ON_COMMAND=1 \
  VISUAL=~/.qsh/scripts/qsh \
    qsh-reg -gisf sh zsh zsh
  • -g - Grab environment variables, which will be restored when the tool is started
  • -i - Invoke the tool as is, i.e. without rlwrap
  • -s - Go straight into editor mode
  • -f sh - Set the file extension for the default file opened up in the editor to sh
  • zsh - The name of the invocation to create
  • zsh - The actual invocation of zsh

Now this invocation of qsh can be started like this:

$ qsh zsh

Prefix

You can set qsh to add a prefix to every command you want to execute, which can be sometimes be useful in generic mode, for example, when running git or kubectl commands. This way you don't need to repeat this command every time.

Executing Queries

  • Alt-e - Highlight a query to run and execute it
  • Alt-g - Execute a query without needing to highlight it
  • Alt-G - Execute multiple statements or function/procedure definitions without needing to highlight
  • Alt-y - Execute everything in the editor buffer

For Alt-g, qsh will look for a statement delimited on either side by a semi-colon. This makes it easier to execute a large SQL statement without needing to highlight it every time.

Alternatively, using Alt-G does the same thing but changes the delimiter to be the string ---, which must be the only thing on a line. You can change this to whatever you like, this is simply the default as defined in the key mapping.

The following provides an example:

create procedure test(a int)
begin
  update test
    set a = 1;                  /* <- If the cursor is here, Alt-G will create procedure test only */
end;

---                             /* <- This is the customizable delimiter defined in the key binding */

create procedure test2(a int)
begin
  update test
    set a = 2;                  /* <- If the cursor is here, Alt-G will create procedure test2 only */
end;

Scripts