sql: alias can now contain statement

This commit is contained in:
Ole Tange 2010-09-10 00:37:32 +02:00
parent c9b2a3f727
commit 944a4fdd4f
2 changed files with 17 additions and 6 deletions

16
src/sql
View file

@ -149,18 +149,21 @@ For this to work B<--shebang> or B<-Y> must be set as the first option.
=head1 DBURL =head1 DBURL
A DBURL has the following syntax: A DBURL has the following syntax:
vendor://[user[:password]@][host][:port]/[database] [sql:]vendor://[user[:password]@][host][:port]/[database][?sqlstatement]
Examples: Examples:
mysql://user:pa55w0rd@mysqlserver/database mysql://user:pa55w0rd@mysqlserver/database
oracle://scott:tiger@oracleserver/xe sql:oracle://scott:tiger@oracleserver/xe
postgresql://user:pa55w0rd@pg.example.com/db postgresql://user:pa55w0rd@pg.example.com/db
pg:/// pg:///
sqlite2:////tmp/db.sqlite?select * from foo;
sqlite3:///../db.sqlite3?select%20*%20from%20foo;
Currently supported vendors: MySQL (mysql), MySQL with SSL (mysqls, Currently supported vendors: MySQL (mysql), MySQL with SSL (mysqls,
mysqlssl), Oracle (oracle, ora), PostgreSQL (postgresql, pg, pgsql, mysqlssl), Oracle (oracle, ora), PostgreSQL (postgresql, pg, pgsql,
postgres), PostgreSQL with SSL (postgresqlssl, pgs, pgsqlssl, postgres), PostgreSQL with SSL (postgresqlssl, pgs, pgsqlssl,
postgresssl, pgssl, postgresqls, pgsqls, postgress) postgresssl, pgssl, postgresqls, pgsqls, postgress), SQLite (sqlite2,
sqlite3).
Aliases must start with ':' and are read from Aliases must start with ':' and are read from
/etc/sql/aliases and ~/.sql/aliases. The user's own /etc/sql/aliases and ~/.sql/aliases. The user's own
@ -177,7 +180,9 @@ Example of aliases:
# Alias for an alias # Alias for an alias
:m :myalias4 :m :myalias4
# the sortest alias possible # the sortest alias possible
: sqlite:////tmp/mydefault.sqlite : sqlite2:////tmp/mydefault.sqlite
# Including an SQL statement
:stm sqlite:////tmp/file.sqlite?select * from foo;
=head1 EXAMPLES =head1 EXAMPLES
@ -743,7 +748,7 @@ sub get_alias {
my $dburl; my $dburl;
for (@urlalias) { for (@urlalias) {
/^$alias_part\s+(\S+)/ and do { $dburl = $1; last; } /^$alias_part\s+(\S+.*)/ and do { $dburl = $1; last; }
} }
if($dburl) { if($dburl) {
@ -806,6 +811,7 @@ sub parse_dburl {
$options{port} = undef_if_empty($5); $options{port} = undef_if_empty($5);
$options{database} = undef_if_empty($6); $options{database} = undef_if_empty($6);
$options{statement} = undef_if_empty(uri_unescape($7)); $options{statement} = undef_if_empty(uri_unescape($7));
debug("dburl $url\n");
debug("databasedriver ",$options{databasedriver}, " user ", $options{user}, debug("databasedriver ",$options{databasedriver}, " user ", $options{user},
" password ", $options{password}, " host ", $options{host}, " password ", $options{password}, " host ", $options{host},
" port ", $options{port}, " database ", $options{database}, " port ", $options{port}, " database ", $options{database},

View file

@ -25,6 +25,11 @@ echo :cyclic3 :cyclic >> ~/.sql/aliases
perl -i -ne '$seen{$_}++ || print' ~/.sql/aliases perl -i -ne '$seen{$_}++ || print' ~/.sql/aliases
stdout sql ":cyclic3?SELECT 'NO IT DID NOT' as 'Test if :cyclic is found works';" 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 "### Test empty dburl" echo "### Test empty dburl"
stdout sql '' stdout sql ''