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 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.
B<prll> encourages using BASH aliases and BASH functions instead of
scripts. GNU B<parallel> 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<parallel> can use the aliases and functions that are
defined at login (using: B<parallel bash -ci myalias>) 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<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

13
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 strict;
use File::Temp qw/tempfile tempdir/;
parse_options();
@ -401,8 +402,13 @@ 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 {
@ -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"

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';"
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 ''

View file

@ -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'

View file

@ -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