#!/usr/bin/perl -w =head1 NAME sql - execute a command on a database determined by a dburl =head1 SYNOPSIS B [options] I [I] B [options] I < commandfile B<#!/usr/bin/sql> B<--shebang> [options] I =head1 DESCRIPTION GNU B 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 I are left out you will get that database's interactive shell. GNU B is often used in combination with GNU B. =over 9 =item I A DBURL has the following syntax: [sql:]vendor:// [[user][:password]@][host][:port]/[database][?sqlquery] See the section DBURL below. =item I The SQL commands to run. Each argument will have a newline appended. Example: "SELECT * FROM foo;" "SELECT * FROM bar;" If the arguments contain '\n' or '\x0a' this will be replaced with a newline: Example: "SELECT * FROM foo;\n SELECT * FROM bar;" If no commands are given SQL is read from the keyboard or STDIN. Example: echo 'SELECT * FROM foo;' | sql mysql:/// =item B<--db-size> =item B<--dbsize> Size of database. Show the size of the database on disk. For Oracle this requires access to read the table I - the user I has that. =item B<--help> =item B<-h> Print a summary of the options to GNU B and exit. =item B<--html> HTML output. Turn on HTML tabular output. =item B<--show-processlist> =item B<--proclist> =item B<--listproc> Show the list of running queries. =item B<--show-databases> =item B<--showdbs> =item B<--list-databases> =item B<--listdbs> List the databases (table spaces) in the database. =item B<--show-tables> =item B<--list-tables> =item B<--table-list> List the tables in the database. =item B<--noheaders> =item B<--no-headers> =item B<-n> Remove headers and footers and print only tuples. Bug in Oracle: it still prints number of rows found. =item B<-p> I The string following -p will be given to the database connection program as arguments. Multiple -p's will be joined with space. Example: pass '-U' and the user name to the program: I<-p "-U scott"> can also be written I<-p -U -p scott>. =item B<-r> Try 3 times. Short version of I<--retries 3>. =item B<--retries> I Try I times. If the client program returns with an error, retry the command. Default is I<--retries 1>. =item B<--sep> I =item B<-s> I Field separator. Use I as separator between columns. =item B<--skip-first-line> Do not use the first line of input (used by GNU B itself when called with B<--shebang>). =item B<--table-size> =item B<--tablesize> Size of tables. Show the size of the tables in the database. =item B<--verbose> =item B<-v> Print which command is sent. =item B<--version> =item B<-V> Print the version GNU B and exit. =item B<--shebang> =item B<-Y> GNU B can be called as a shebang (#!) command as the first line of a script. Like this: #!/usr/bin/sql -Y mysql:/// SELECT * FROM foo; For this to work B<--shebang> or B<-Y> must be set as the first option. =back =head1 DBURL A DBURL has the following syntax: [sql:]vendor:// [[user][:password]@][host][:port]/[database][?sqlquery] To quote special characters use %-encoding specified in http://tools.ietf.org/html/rfc3986#section-2.1 (E.g. a password containing '/' would contain '%2F'). Examples: mysql://scott:tiger@my.example.com/mydb sql:oracle://scott:tiger@ora.example.com/xe postgresql://scott:tiger@pg.example.com/pgdb pg:/// postgresqlssl://scott@pg.example.com:3333/pgdb sql:sqlite2:////tmp/db.sqlite?SELECT * FROM foo; sqlite3:///../db.sqlite3?SELECT%20*%20FROM%20foo; 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), SQLite2 (sqlite, sqlite2), SQLite3 (sqlite3). Aliases must start with ':' and are read from /etc/sql/aliases and ~/.sql/aliases. The user's own ~/.sql/aliases should only be readable by the user. Example of aliases: :myalias1 pg://scott:tiger@pg.example.com/pgdb :myalias2 ora://scott:tiger@ora.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 # the sortest alias possible : sqlite2:////tmp/db.sqlite # Including an SQL query :query sqlite:////tmp/db.sqlite?SELECT * FROM foo; =head1 EXAMPLES =head2 Get an interactive prompt The most basic use of GNU B is to get an interactive prompt: B If you have setup an alias you can do: B =head2 Run a query To run a query directly from the command line: B Oracle requires newlines after each statement. This can be done like this: B Or this: B =head2 Copy a PostgreSQL database To copy a PostgreSQL database use pg_dump to generate the dump and GNU B to import it: B =head2 Empty all tables in a MySQL database Using GNU B it is easy to empty all tables without dropping them: B =head2 Drop all tables in a PostgreSQL database To drop all tables in a PostgreSQL database do: B =head2 Run as a script Instead of doing: B you can combine the sqlfile with the DBURL to make a UNIX-script. Create a script called I: B<#!/usr/bin/sql -Y mysql:///> B