sql: Test of %-quoting.

sql: Test of cyclic aliases.
This commit is contained in:
Ole Tange 2010-09-10 00:18:43 +02:00
parent 9711803e61
commit c9b2a3f727
5 changed files with 100 additions and 17 deletions

View file

@ -1,16 +1,3 @@
== SQL ==
Unittest:
dburl = :
dburl = null
Denne med '?' skal vel også virke:
sql mysql:///mydb?'SELECT * FROM foo'
el.
sql 'mysql:///mydb?SELECT * FROM foo'
== FEX ==
fex syntax for splitting fields

49
src/sql
View file

@ -176,7 +176,8 @@ Example of aliases:
:myalias4 mysql://:33333/mydb
# Alias for an alias
:m :myalias4
# the sortest alias possible
: sqlite:////tmp/mydefault.sqlite
=head1 EXAMPLES
@ -451,6 +452,7 @@ sub parse_options {
if(defined $::opt_help) { die_usage(); }
if(defined $::opt_version) { version(); exit(0); }
$Global::debug = $::opt_debug;
}
sub database_driver_alias {
@ -703,6 +705,7 @@ sub get_alias {
if ($alias !~ /^:/) {
return $alias;
}
# Find the alias
my $path;
if (-l $0) {
@ -729,7 +732,15 @@ sub get_alias {
push @urlalias, `cat "$alias_file"`;
}
}
my ($alias_part,$rest) = $alias=~/(:\w+)(.*)/;
my ($alias_part,$rest) = $alias=~/(:\w*)(.*)/;
# If we saw this before: we have an alias loop
if(grep {$_ eq $alias_part } @Private::seen_aliases) {
print STDERR "$alias_part is a cyclic alias\n";
exit -1;
} else {
push @Private::seen_aliases, $alias_part;
}
my $dburl;
for (@urlalias) {
/^$alias_part\s+(\S+)/ and do { $dburl = $1; last; }
@ -739,6 +750,7 @@ sub get_alias {
return get_alias($dburl.$rest);
} else {
Usage("$alias is not defined in @search");
exit(-1);
}
}
@ -768,7 +780,7 @@ sub parse_dburl {
((?:oracle|ora|mysql|pg|postgres|postgresql)(?:s|ssl|)|
(?:sqlite|sqlite2|sqlite3)):// # Databasedriver ($1)
(?:
([^:@]*) # Username ($2)
([^:@/][^:@]*|) # Username ($2)
(?:
:([^@]*) # Password ($3)
)?
@ -793,7 +805,11 @@ sub parse_dburl {
$options{host} = undef_if_empty($4);
$options{port} = undef_if_empty($5);
$options{database} = undef_if_empty($6);
$options{statement} = undef_if_empty($7);
$options{statement} = undef_if_empty(uri_unescape($7));
debug("databasedriver ",$options{databasedriver}, " user ", $options{user},
" password ", $options{password}, " host ", $options{host},
" port ", $options{port}, " database ", $options{database},
" statement ",$options{statement}, "\n");
} else {
Usage("$url is not a valid DBURL");
exit -1;
@ -801,6 +817,25 @@ sub parse_dburl {
return %options;
}
sub uri_unescape {
# Copied from http://cpansearch.perl.org/src/GAAS/URI-1.55/URI/Escape.pm
# to avoid depending on URI::Escape
# Note from RFC1630: "Sequences which start with a percent sign
# but are not followed by two hexadecimal characters are reserved
# for future extension"
my $str = shift;
if (@_ && wantarray) {
# not executed for the common case of a single argument
my @str = ($str, @_); # need to copy
foreach (@str) {
s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
}
return @str;
}
$str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg if defined $str;
$str;
}
sub undef_if_empty {
if(defined($_[0]) and $_[0] eq "") {
return undef;
@ -841,6 +876,12 @@ sub usage {
print "See 'man $Global::progname' for the options\n";
}
sub debug {
# Returns: N/A
$Global::debug or return;
@_ = grep { defined $_ ? $_ : "" } @_;
print @_;
}
# TODO --list-databases: psql -l eller '\l'. mysql show databases. Oracle ?
$::opt_skip_first_line = $::opt_shebang = 0;

View file

@ -46,6 +46,7 @@ sql mysql://sqlunittest:CB5A1FFFA5A@localhost:3306/sqlunittest </tmp/unittest.sq
echo "### Test .sql/aliases"
echo :sqlunittest mysql://sqlunittest:CB5A1FFFA5A@localhost:3306/sqlunittest >> ~/.sql/aliases
perl -i -ne '$seen{$_}++ || print' ~/.sql/aliases
sql :sqlunittest "SELECT 'Yes it does' as 'Test if .sql/aliases works';"
echo "### Test --noheaders --no-headers -n"

View file

@ -0,0 +1,33 @@
#!/bin/bash
# Setup
sql mysql://root@ "drop user 'sqlunittest'@'localhost'"
sql mysql://root@ DROP DATABASE sqlunittest;
sql mysql://root@ CREATE DATABASE sqlunittest;
sql mysql://root@ "CREATE USER 'sqlunittest'@'localhost' IDENTIFIED BY 'CB5A1FFFA5A';"
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 '### Test reading sql from url command line %-quoting'
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 "### Test cyclic alias .sql/aliases"
echo :cyclic :cyclic2 >> ~/.sql/aliases
echo :cyclic2 :cyclic3 >> ~/.sql/aliases
echo :cyclic3 :cyclic >> ~/.sql/aliases
perl -i -ne '$seen{$_}++ || print' ~/.sql/aliases
stdout sql ":cyclic3?SELECT 'NO IT DID NOT' as 'Test if :cyclic is found works';"
echo "### Test empty dburl"
stdout sql ''
echo "### Test dburl :"
stdout sql ':'

View file

@ -0,0 +1,21 @@
### Test reading sql from url command line
Test reading SQL from command line
Yes it works
### Test reading sql from url command line %-quoting
Test %-quoting SQL from command line
Yes it works
### Test .sql/aliases with url on commandline
Test if .sql/aliases with %-quoting works
Yes it works
### Test cyclic alias .sql/aliases
:cyclic3 is a cyclic alias
### Test empty dburl
Error:
is not a valid DBURL
sql [-hnr] [--table-size] [--db-size] [-p pass-through] [-s string] dburl [command]
### Test dburl :
Error:
: is not defined in /home/tange/.sql/aliases /home/tange/.dburl.aliases /etc/sql/aliases /usr/local/bin/dburl.aliases /usr/local/bin/dburl.aliases.dist
sql [-hnr] [--table-size] [--db-size] [-p pass-through] [-s string] dburl [command]