sql: Deal with :aliases containing ?query parts in batch mode.

:aliases?query is still a problem in interactive mode.
Passes sql-part of unittest.
This commit is contained in:
Ole Tange 2010-09-14 18:37:26 +02:00
parent 77f0254543
commit 22934a4bcd
6 changed files with 73 additions and 11 deletions

View file

@ -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 ==
fex syntax for splitting fields fex syntax for splitting fields

View file

@ -1870,9 +1870,12 @@ B<prll> is also a tool for running jobs in parallel. It does not
support running jobs on remote computers. support running jobs on remote computers.
B<prll> encourages using BASH aliases and BASH functions instead of B<prll> encourages using BASH aliases and BASH functions instead of
scripts. GNU B<parallel> will never support running aliases and scripts. GNU B<parallel> can use the aliases and functions that are
functions (see why http://www.perlmonks.org/index.pl?node_id=484296) defined at login (using: B<parallel bash -ci myalias>) but it will
but scripts or composed commands work just fine. 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<prll> generates a lot of status information on STDERR which makes it B<prll> generates a lot of status information on STDERR which makes it
harder to use the STDERR output of the job directly as input for harder to use the STDERR output of the job directly as input for

15
src/sql
View file

@ -355,6 +355,7 @@ B<mysql>(1), B<psql>(1), B<rlwrap>(1), B<sqlite>(1), B<sqlite3>(1), B<sqlplus>(1
use Getopt::Long; use Getopt::Long;
use strict; use strict;
use File::Temp qw/tempfile tempdir/;
parse_options(); parse_options();
@ -401,14 +402,19 @@ if(defined $::opt_tablesize) {
unshift @ARGV, tablesize($database_driver,%dburl); unshift @ARGV, tablesize($database_driver,%dburl);
} }
my $queryfile = "";
if($dburl{'query'}) { 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 { do {
if(@ARGV) { if(@ARGV) {
$::opt_debug and print "$batch_command\n"; $::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"; print M "@ARGV";
close M; close M;
} elsif (is_stdin_terminal()) { } elsif (is_stdin_terminal()) {
@ -421,11 +427,13 @@ do {
$err = $?>>8; $err = $?>>8;
} while (--$retries and $err); } while (--$retries and $err);
$queryfile and unlink $queryfile;
$Global::Initfile && unlink $Global::Initfile; $Global::Initfile && unlink $Global::Initfile;
exit ($err); exit ($err);
sub parse_options { sub parse_options {
$Global::version = 20100906; $Global::version = 20100914;
$Global::progname = 'sql'; $Global::progname = 'sql';
# This must be done first as this may exec myself # This must be done first as this may exec myself
@ -828,6 +836,7 @@ sub parse_dburl {
sub uri_unescape { sub uri_unescape {
# Copied from http://cpansearch.perl.org/src/GAAS/URI-1.55/URI/Escape.pm # Copied from http://cpansearch.perl.org/src/GAAS/URI-1.55/URI/Escape.pm
# to avoid depending on URI::Escape # to avoid depending on URI::Escape
# This section is (C) Gisle Aas.
# Note from RFC1630: "Sequences which start with a percent sign # Note from RFC1630: "Sequences which start with a percent sign
# but are not followed by two hexadecimal characters are reserved # but are not followed by two hexadecimal characters are reserved
# for future extension" # for future extension"

View file

@ -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';" sql mysql://root@ "GRANT ALL PRIVILEGES ON sqlunittest.* TO 'sqlunittest'@'localhost';"
echo '### Test reading sql from url command line' 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' 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 "### Test .sql/aliases with url on commandline"
echo :sqlunittest mysql://sqlunittest:CB5A1FFFA5A@localhost:3306/sqlunittest >> ~/.sql/aliases echo :sqlunittest mysql://sqlunittest:CB5A1FFFA5A@localhost:3306/sqlunittest >> ~/.sql/aliases
perl -i -ne '$seen{$_}++ || print' ~/.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 "### Test cyclic alias .sql/aliases"
echo :cyclic :cyclic2 >> ~/.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 "### Test alias with statement .sql/aliases"
echo ":testselect sqlite:////tmp/file.sqlite?SELECT 'It works' AS 'Test statement in alias';" >> ~/.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 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" echo "### Test empty dburl"
stdout sql '' stdout sql ''

View file

@ -25,7 +25,7 @@ echo no output |parallel -kv ::: 'echo a' 'cat'
EOF EOF
chmod 755 /tmp/parallel-script-for-script2 chmod 755 /tmp/parallel-script-for-script2
echo via pseudotty | script -q -f -c /tmp/parallel-script-for-script2 /dev/null 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 '### Test stdin goes to first command only'
echo via cat |parallel --arg-sep .--- -kv .--- 'cat' 'echo b' echo via cat |parallel --arg-sep .--- -kv .--- 'cat' 'echo b'
echo via cat |parallel -kv ::: 'cat' 'echo b' echo via cat |parallel -kv ::: 'cat' 'echo b'

View file

@ -12,6 +12,30 @@ Yes it works
### Test alias with statement .sql/aliases ### Test alias with statement .sql/aliases
Test statement in alias Test statement in alias
It works 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 ### Test empty dburl
Error: Error:
is not a valid DBURL is not a valid DBURL