diff --git a/doc/FUTURE_IDEAS b/doc/FUTURE_IDEAS index c6a327ce..2dc23b02 100644 --- a/doc/FUTURE_IDEAS +++ b/doc/FUTURE_IDEAS @@ -1,3 +1,15 @@ +Annoncer foredrag til FSCONS + +I will be at FSCONS 2010-11-07 talking about GNU Parallel. +http://www.fscons.org/fs/gnu-parallel + +== SQL == + +Example with %0a as newline +sql :pg_foo?'\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)' + == FEX == fex syntax for splitting fields diff --git a/src/parallel b/src/parallel index b21ca985..bf42a022 100755 --- a/src/parallel +++ b/src/parallel @@ -1870,9 +1870,12 @@ B is also a tool for running jobs in parallel. It does not support running jobs on remote computers. B encourages using BASH aliases and BASH functions instead of -scripts. GNU B will never support running aliases and -functions (see why http://www.perlmonks.org/index.pl?node_id=484296) -but scripts or composed commands work just fine. +scripts. GNU B can use the aliases and functions that are +defined at login (using: B) but it will +never support running aliases and functions that are defined defined +later (see why +http://www.perlmonks.org/index.pl?node_id=484296). However, scripts or +composed commands work just fine. B generates a lot of status information on STDERR which makes it harder to use the STDERR output of the job directly as input for diff --git a/src/sql b/src/sql index 057d54fa..cd34d27d 100755 --- a/src/sql +++ b/src/sql @@ -355,6 +355,7 @@ B(1), B(1), B(1), B(1), B(1), B(1 use Getopt::Long; use strict; +use File::Temp qw/tempfile tempdir/; parse_options(); @@ -401,14 +402,19 @@ if(defined $::opt_tablesize) { unshift @ARGV, tablesize($database_driver,%dburl); } +my $queryfile = ""; if($dburl{'query'}) { - unshift @ARGV,$dburl{'query'}; + my $fh; + ($fh,$queryfile) = tempfile(SUFFIX => ".sql"); + print $fh $dburl{'query'}; + close $fh; + $batch_command = "cat $queryfile - | $batch_command"; } do { if(@ARGV) { $::opt_debug and print "$batch_command\n"; - open(M,"|$batch_command") || die("mysql/psql/sqlplus not in path"); + open(M,"| $batch_command") || die("mysql/psql/sqlplus not in path"); print M "@ARGV"; close M; } elsif (is_stdin_terminal()) { @@ -421,11 +427,13 @@ do { $err = $?>>8; } while (--$retries and $err); +$queryfile and unlink $queryfile; + $Global::Initfile && unlink $Global::Initfile; exit ($err); sub parse_options { - $Global::version = 20100906; + $Global::version = 20100914; $Global::progname = 'sql'; # This must be done first as this may exec myself @@ -828,6 +836,7 @@ sub parse_dburl { sub uri_unescape { # Copied from http://cpansearch.perl.org/src/GAAS/URI-1.55/URI/Escape.pm # to avoid depending on URI::Escape + # This section is (C) Gisle Aas. # Note from RFC1630: "Sequences which start with a percent sign # but are not followed by two hexadecimal characters are reserved # for future extension" diff --git a/unittest/tests-to-run/sql03.sh b/unittest/tests-to-run/sql03.sh index 446db25d..68334a92 100644 --- a/unittest/tests-to-run/sql03.sh +++ b/unittest/tests-to-run/sql03.sh @@ -8,15 +8,15 @@ sql mysql://root@ "CREATE USER 'sqlunittest'@'localhost' IDENTIFIED BY 'CB5A1FFF sql mysql://root@ "GRANT ALL PRIVILEGES ON sqlunittest.* TO 'sqlunittest'@'localhost';" echo '### Test reading sql from url command line' -sql "mysql:///tange?SELECT 'Yes it works' as 'Test reading SQL from command line';" +echo | sql "mysql:///tange?SELECT 'Yes it works' as 'Test reading SQL from command line';" echo '### Test reading sql from url command line %-quoting' -sql "mysql:///tange?SELECT 'Yes it%20works' as 'Test%20%-quoting%20SQL from command line';" +echo | sql "mysql:///tange?SELECT 'Yes it%20works' as 'Test%20%-quoting%20SQL from command line';" echo "### Test .sql/aliases with url on commandline" echo :sqlunittest mysql://sqlunittest:CB5A1FFFA5A@localhost:3306/sqlunittest >> ~/.sql/aliases perl -i -ne '$seen{$_}++ || print' ~/.sql/aliases -sql ":sqlunittest?SELECT 'Yes it%20works' as 'Test if .sql/aliases with %-quoting works';" +echo | sql ":sqlunittest?SELECT 'Yes it%20works' as 'Test if .sql/aliases with %-quoting works';" echo "### Test cyclic alias .sql/aliases" echo :cyclic :cyclic2 >> ~/.sql/aliases @@ -28,7 +28,21 @@ stdout sql ":cyclic3?SELECT 'NO IT DID NOT' as 'Test if :cyclic is found works'; echo "### Test alias with statement .sql/aliases" echo ":testselect sqlite:////tmp/file.sqlite?SELECT 'It works' AS 'Test statement in alias';" >> ~/.sql/aliases perl -i -ne '$seen{$_}++ || print' ~/.sql/aliases -stdout sql :testselect +echo | stdout sql :testselect +echo ":testselectmysql mysql://sqlunittest:CB5A1FFFA5A@localhost:3306/sqlunittest?SELECT 'It works' AS 'Test statement in alias';" >> ~/.sql/aliases +perl -i -ne '$seen{$_}++ || print' ~/.sql/aliases +echo | stdout sql :testselect +echo | stdout sql :testselectmysql + +echo "### Test alias followed by SQL as arg" +echo ignored | stdout sql :testselect "select 'Arg on cmdline';" + +echo "### Test alias with query followed by SQL as arg" +echo ignored | stdout sql :testselect" select 'Query added to alias';" "select 'Arg on cmdline';" + +echo "### Test alias with statement .sql/aliases" +echo "select 'Query from stdin';" | sql :testselect" select 'Query added to alias';" +echo "select 'Query from stdin';" | sql :testselectmysql" select 'Query added to alias';" echo "### Test empty dburl" stdout sql '' diff --git a/unittest/tests-to-run/test25.sh b/unittest/tests-to-run/test25.sh index 52fbe851..7b327deb 100644 --- a/unittest/tests-to-run/test25.sh +++ b/unittest/tests-to-run/test25.sh @@ -25,7 +25,7 @@ echo no output |parallel -kv ::: 'echo a' 'cat' EOF chmod 755 /tmp/parallel-script-for-script2 echo via pseudotty | script -q -f -c /tmp/parallel-script-for-script2 /dev/null -sleep 1 +sleep 2 echo '### Test stdin goes to first command only' echo via cat |parallel --arg-sep .--- -kv .--- 'cat' 'echo b' echo via cat |parallel -kv ::: 'cat' 'echo b' diff --git a/unittest/wanted-results/sql03 b/unittest/wanted-results/sql03 index 417aa31e..554c8f26 100644 --- a/unittest/wanted-results/sql03 +++ b/unittest/wanted-results/sql03 @@ -12,6 +12,30 @@ Yes it works ### Test alias with statement .sql/aliases Test statement in alias It works +Test statement in alias +It works +Test statement in alias +It works +### Test alias followed by SQL as arg +Test statement in alias +It works +Arg on cmdline +### Test alias with query followed by SQL as arg +Test statement in alias +It works +Query added to alias +Arg on cmdline +### Test alias with statement .sql/aliases +Test statement in alias +It works +Query added to alias +Query from stdin +Test statement in alias +It works +Query added to alias +Query added to alias +Query from stdin +Query from stdin ### Test empty dburl Error: is not a valid DBURL