sql - execute a command on a database determined by a dburl
sql [-hnr] [--table-size] [--db-size] [-p pass-through] [-s string] dburl [sqlcommand]
#!/usr/bin/sql --shebang [options] dburl
GNU sql aims to give a simple, unified interface for accessing databases through all the different databases' command line clients. So far the focus has been on giving a common way to specify login information (protocol, username, password, hostname, and port number), size (database and table size), and running queries.
The database is addressed using a dburl. If command is left out you will get that database's interactive shell.
GNU sql is often used in combination with GNU parallel.
A DBURL has the following syntax: vendor://[user[:password]@][host][:port]/[database]
See the section DBURL below.
The SQL command to run.
Example: select 1+2
If you use * or ? remember to quote them, as the shell may otherwise expand them.
If no command is given SQL is read from the keyboard or STDIN.
Example: echo 'select 1+2' | sql mysql:///
Size of database. Show the size of the database on disk.
Print a summary of the options to GNU sql and exit.
HTML output. Turn on HTML tabular output.
Show the list of running queries.
Remove headers and footers and print only tuples. Bug in Oracle: it still prints number of rows found.
The string following -p will be given to the database connection program as arguments. Multiple -p's will be joined with space. Example:
-p "-U username" can also be written -p -U -p username.
Try 3 times. Short version of --retries 3.
Try ntimes times. If the client program returns with an error, retry the command. Default is --retries 1.
Field separator. Use string as seperator between columns.
Do not use the first line of input (used by GNU sql itself when called with --shebang).
Size of tables. Show the size of the tables in the database.
Print the version GNU sql and exit.
GNU sql can be called as a shebang (#!) command as the first line of a script. Like this:
#!/usr/bin/sql -Y mysql:///
select * from users;
For this to work --shebang or -Y must be set as the first option.
A DBURL has the following syntax: vendor://[user[:password]@][host][:port]/[database]
Examples: mysql://user:pa55w0rd@mysqlserver/database oracle://scott:tiger@oracleserver/xe postgresql://user:pa55w0rd@pg.example.com/db pg:///
Currently supported vendors: MySQL (mysql), MySQL with SSL (mysqls, mysqlssl), Oracle (oracle, ora), PostgreSQL (postgresql, pg, pgsql, postgres), PostgreSQL with SSL (postgresqlssl, pgs, pgsqlssl, postgresssl, pgssl, postgresqls, pgsqls, postgress)
Aliases must start with ':' and is read from dburl.aliases.dist, dburl.aliases and ~/.dburl.aliases. The user's own ~/.dburl.aliases should only be readable by the user.
Example of dburl.aliases:
:myalias1 pg://user:pa55w0rd@pg.example.com/db :myalias2 ora://scott:tiger@ora1.example.com/xe # Short form of mysql://`whoami`:nopassword@localhost:3306/`whoami` :myalias3 mysql:/// # Short form of mysql://`whoami`:nopassword@localhost:33333/mydb :myalias4 mysql://:33333/mydb # Alias for an alias :m :myalias4
pg_dump my_database | sql pg://user:pass@pgserver/my_new_db
sql -n mysql:/// 'show tables' | parallel sql mysql:/// delete from {};
sql -n pg:/// '\dt' | awk '{print $3}' | parallel -r sql pg:/// drop table {};
Create a script called demosql:
#!/usr/bin/sql -Y mysql:///
select * from users;
Then do:
chmod 755 demosql; ./demosql
Use GNU parallel's --colsep to separate columns:
sql :mydburl 'select * from bar' | parallel --colsep '\t' do_stuff {4} {1}
GNU sql is part of GNU parallel. Report bugs to <bug-parallel@gnu.org>.
Copyright (C) 2008,2009,2010 Ole Tange http://ole.tange.dk
Copyright (C) 2007,2008,2009,2010 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or at your option any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
Permission is granted to copy, distribute and/or modify this documentation under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the file fdl.txt.
You are free:
to copy, distribute and transmit the work
to adapt the work
Under the following conditions:
You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.
With the understanding that:
Any of the above conditions can be waived if you get permission from the copyright holder.
Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license.
In no way are any of the following rights affected by the license:
Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations;
The author's moral rights;
Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights.
For any reuse or distribution, you must make clear to others the license terms of this work.
A copy of the full license is included in the file as cc-by-sa.txt.
GNU sql uses Perl. If mysql is installed, MySQL dburls will work. If psql is installed, PostgreSQL dburls will work. If sqlplus is installed, Oracle dburls will work. If rlwrap is installed, GNU sql will have a command history for Oracle.
~/.dburl.aliases - user's own aliases with DBURLs
dburl.aliases - user's own aliases with DBURLs
dburl.aliases.dist - common aliases with DBURLs
mysql(1), psql(1), sqlplus(1), rlwrap(1)