From 6f03760b567ccf60ed539eef62b946cc4d73b734 Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Wed, 22 Sep 2010 23:54:42 +0200 Subject: [PATCH] sql: \n and \x0a in arguments is replaced with newline. Passes unittest. Added more examples. --- doc/FUTURE_IDEAS | 6 +- doc/release_new_version | 1 + src/sql | 104 ++++++++++++++++++++++++++++----- unittest/tests-to-run/sql03.sh | 3 + unittest/wanted-results/sql03 | 16 +++++ 5 files changed, 115 insertions(+), 15 deletions(-) diff --git a/doc/FUTURE_IDEAS b/doc/FUTURE_IDEAS index b0066a0a..5c428689 100644 --- a/doc/FUTURE_IDEAS +++ b/doc/FUTURE_IDEAS @@ -1,9 +1,13 @@ +== Bug == + +(echo ; echo abc ; echo abc; echo ; echo bbc) | parallel --colsep b -v echo {1}{2} + == SQL == Example with %0a as newline sql :my_postgres?'\dt %0a SELECT * FROM users' -cat ~/.sql/aliases | parallel --colsep '\s' sql {1} '"select 0.14159+3;" | grep -q 3.14159 || (echo dead: {1}; exit 1)' +cat ~/.sql/aliases | parallel --colsep '\s' sql {1} '"select 0.14+3;" | grep -q 3.14 || (echo dead: {1}; exit 1)' == FEX == diff --git a/doc/release_new_version b/doc/release_new_version index 61417c68..9d75e79e 100644 --- a/doc/release_new_version +++ b/doc/release_new_version @@ -95,6 +95,7 @@ http://www.gnu.org/software/parallel/ http://www.gnu.org/software/parallel/man.html pod2html src/parallel > ../parallel-web/parallel/man.html +pod2html src/sql > ../parallel-web/parallel/sql.html cd ../parallel-web/parallel cvs up cvs ci diff --git a/src/sql b/src/sql index f5478e9d..0ae33ee0 100755 --- a/src/sql +++ b/src/sql @@ -6,11 +6,9 @@ sql - execute a command on a database determined by a dburl =head1 SYNOPSIS -B [B<-hnr>] [B<--table-size>] [B<--db-size>] [B<-p> -I] [B<-s> I] I [I] +B [options] I [I] -B [B<-hnr>] [B<--table-size>] [B<--db-size>] [B<-p> -I] [B<-s> I] I < commandfile +B [options] I < commandfile B<#!/usr/bin/sql> B<--shebang> [options] I @@ -38,10 +36,16 @@ See the section DBURL below. =item I -The SQL commands to run. Each argument will have a newline appended. +The SQL commands to run. Each argument will have a newline +appended. Example: "SELECT 1+2;" "SELECT 'SQL';" +If the arguments contain '\n' or '\x0a' this will be replaced with a +newline: + +Example: "SELECT 1+2;\n SELECT 'SQL';" + If no commands are given SQL is read from the keyboard or STDIN. Example: echo 'SELECT 1+2;' | sql mysql:/// @@ -195,39 +199,109 @@ Example of aliases: # the sortest alias possible : sqlite2:////tmp/mydefault.sqlite # Including an SQL query - :query sqlite:////tmp/file.sqlite?select * from foo; + :query sqlite:////tmp/file.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 -pg_dump my_database | sql pg://user:pass@pgserver/my_new_db +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 -sql -n mysql:/// 'show tables' | parallel sql mysql:/// delete from {}; +Using GNU B it is easy to empty all tables without dropping them: + +B + =head2 Drop all tables in a PostgreSQL database -sql -n pg:/// '\dt' | awk '{print $3}' | parallel -r sql pg:/// drop table {}; +To drop all tables in a PostgreSQL database do: + +B + =head2 Run as a script -Create a script called I: +Instead of doing: - #!/usr/bin/sql -Y mysql:/// +B - select * from users; +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