1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
#!/usr/local/bin/perl -w
# POD docs at end of file
use strict;
use Carp;
use DBIx::DBStag;
use Data::Dumper;
use Getopt::Long;
my $debug;
my $help;
my $db;
my $nesting;
my $show;
my $file;
my $user;
my $pass;
my $template_name;
my $where;
my $select;
my $rows;
my $writer;
my $verbose;
my @order;
my $color;
my $out;
my $sgml;
my $pre_sql;
my $aliaspolicy;
my $metadata;
my @matrixcols;
my @matrixcells;
# cmd line interpreter gets rid of quotes; need to use backspace
my @ARGV2 = ();
while (my $arg = shift @ARGV) {
while (substr($arg,-1) eq '\\' && @ARGV) {
my $next = shift @ARGV;
substr($arg,-1,1," $next");
}
push(@ARGV2,$arg);
}
@ARGV = @ARGV2;
GetOptions(
"help|h"=>\$help,
"rows"=>\$rows,
"show"=>\$show,
"sgml"=>\$sgml,
"nesting|n=s"=>\$nesting,
"file|f=s"=>\$file,
"db|d=s"=>\$db,
"user|u=s"=>\$user,
"pass|p=s"=>\$pass,
"template|t=s"=>\$template_name,
"where|wh=s"=>\$where,
"matrixcol|mcol=s@"=>\@matrixcols,
"matrixcell|mcell=s@"=>\@matrixcells,
"writer|w=s"=>\$writer,
"select|s=s"=>\$select,
"order=s@"=>\@order,
"verbose|v"=>\$verbose,
"aliaspolicy|alias=s"=>\$aliaspolicy,
"colour|color"=>\$color,
"out|o=s"=>\$out,
"pre=s"=>\$pre_sql,
"metadata"=>\$metadata,
"trace"=>\$ENV{DBSTAG_TRACE},
);
@ARGV = map { if (/^\/(.*)/) {$template_name=$1;()} else {$_} } @ARGV;
if ($help && !$template_name && !$db) {
system("perldoc $0");
exit 0;
}
if ((@matrixcols && !@matrixcells) ||
(!@matrixcols && @matrixcells)) {
print STDERR "-matrixcol and -matrixcell must be set together!\n";
exit 1;
}
if (@matrixcols) {
$rows = 1;
}
my $H = Data::Stag->getformathandler($writer || $ENV{STAG_WRITER} || 'xml');
$H->use_color(1) if $color;
if ($sgml) {
$rows = 1;
$H = Data::Stag->getformathandler('xml');
}
my $sql;
if ($file) {
open(F, $file) || die $file;
$sql = join('', <F>);
close(F);
}
elsif ($template_name) {
# No SQL required if template provided
}
elsif ($help) {
# deal with this later...
}
else {
$sql = shift @ARGV;
if ($sql eq '-') {
print STDERR "Reading SQL from STDIN...\n";
$sql = <STDIN>;
}
# if ($sql =~ /^\/(.*)/) {
# # shorthand for a template
# $template_name = $1;
# $sql = '';
# }
}
my $template;
if ($template_name) {
$template =
DBIx::DBStag->new->find_template($template_name);
}
if ($help) {
if ($template) {
my $varnames = $template->get_varnames;
my $desc = $template->desc;
# $desc =~ s/\s+/ /;
if ($verbose) {
require "Term/ANSIColor.pm";
$template->show(\*STDOUT,
undef,
sub { Term::ANSIColor::color(@_)}
);
}
else {
$desc =~ s/\n */\n /mg;
print "DESC:\n $desc\n";
}
print "PARAMETERS:\n";
foreach my $vn (@$varnames) {
print " $vn\n";
}
my $nesting = $template->nesting;
if ($nesting) {
print "QUERY RESULT STRUCTURE (NESTING):\n";
print $nesting->sxpr;
}
}
else {
# show templates
my $dbh =
DBIx::DBStag->new;
my $templates = $dbh->find_templates_by_dbname($db);
foreach my $template (@$templates) {
if ($verbose) {
require "Term/ANSIColor.pm";
$template->show(\*STDOUT,
undef,
sub { Term::ANSIColor::color(@_)},
);
}
else {
my $desc = $template->desc || '';
$desc =~ s/\s*$//;
printf "NAME: %s\nDESC: %s\n//\n",
$template->name, $desc;
}
}
}
exit 0;
}
if (!$db) {
die "you must specify a database name (logical name or dbi path) with -d";
}
# QUERY DB
my $dbh =
DBIx::DBStag->connect($db, $user, $pass);
$dbh->include_metadata($metadata);
if ($pre_sql) {
$dbh->do($pre_sql);
}
my $xml;
my @sel_args = (-sql=>$sql, -nesting=>$nesting);
if ($template) {
if ($where) {
$template->set_clause(where => $where);
}
if ($select) {
$template->set_clause(select => $select);
}
if (@order) {
$template->set_clause(order => join(", ",@order));
}
my @args = ();
my %argh = ();
while (my $arg = shift @ARGV) {
# print "ARG:$arg;;\n";
if ($arg =~ /(.*)\@=(.*)/) {
my ($k,$v) = ($1,$2);
$v = [split(/\,/,$v)];
$argh{$k} = $v;
}
elsif ($arg =~ /(.*)=(.*)/) {
my ($k,$v) = ($1,$2);
$argh{$k} = $v;
}
else {
push(@args, $arg);
}
}
my $bind = \@args;
if (%argh) {
$bind = \%argh;
if (@args) {
die("can't used mixed argument passing");
}
}
@sel_args =
(-template=>$template, -nesting=>$nesting, -bind=>$bind);
}
if ($aliaspolicy) {
push(@sel_args, -aliaspolicy=>$aliaspolicy);
}
eval {
if ($rows) {
my $count = 0;
my $prep_h = $dbh->prepare_stag(@sel_args);
my $cols = $prep_h->{cols};
my $sth = $prep_h->{sth};
my $exec_args = $prep_h->{exec_args};
my $rv = $sth->execute(@$exec_args);
if (@matrixcols) {
my @COL = ();
my @CELL = ();
for (my $i=0;$i<@$cols;$i++) {
my $col = $cols->[$i];
foreach (@matrixcols) {
if ($_ eq $col) {
$COL[$i]=1;
}
}
foreach (@matrixcells) {
if ($_ eq $col) {
$CELL[$i]=1;
}
}
}
while (my $r = $sth->fetchrow_arrayref) {
my @row = ();
for (my $i=0;$i<@$cols;$i++) {
if ($COL[$i]) {
}
elsif ($COL[$i]) {
}
else {
}
}
}
}
while (my $r = $sth->fetchrow_arrayref) {
# TODO: html
if ($sgml) {
if (!$count) {
$H->start_event('table');
$H->event(title=>"Query Results");
$H->start_event('tgroup');
$H->event('@'=>[
[cols=>scalar(@$r)]]);
$H->event(thead=>[
[row=>[
map {[entry=>$_]} @$cols]]]);
$H->start_event('tbody');
}
$H->event(row=>[map {[entry=>$_]} @$r]);
}
else {
# ASCII
printf "%s\n",
join("\t", map {esc_col_val($_)} @$r);
}
$count++;
}
} # end of ROWS mode
else {
# HIERARCHICAL
my $fh;
if ($out) {
my $fh = FileHandle->new(">$out") || die "cannot write to $out";
$H->fh($fh);
}
else {
$H->fh(\*STDOUT);
}
my $stag = $dbh->selectall_stag(@sel_args);
$stag->events($H);
$fh->close if $fh;
}
};
if ($@) {
print "FAILED\n$@";
}
$dbh->disconnect;
if ($show) {
my ($sql, @exec_args) = $dbh->last_sql_and_args;
print "DBI SQL:\n$sql\n\nARGUMENT BINDINGS: @exec_args\n";
}
#print $xml;
exit 0;
sub esc_col_val {
my $str = shift;
return '\\NULL' unless defined $str;
$str =~ s/\t/\\t/g;
$str =~ s/\n/\\n/g;
$str;
}
__END__
=head1 NAME
selectall_xml.pl - Query all elements from database with an XML output
=head1 SYNOPSIS
selectall_xml.pl [-d <dbi>] [-f file of sql] [-nesting|n <nesting>] SQL
=head1 DESCRIPTION
This script will query a database using either SQL provided by the
script user, or using an SQL templates; the query results will be
turned into XML using the L<DBIx::DBStag> module. The nesting of the
XML can be controlled by the DBStag SQL extension "USE NESTING..."
=head2 EXAMPLES
selectall_xml.pl -d "dbi:Pg:dbname=mydb;host=localhost"\
"SELECT * FROM a NATURAL JOIN b"
=head2 TEMPLATES
A parameterized SQL template (canned query) can be used instead of
specifying the full SQL
For example:
selectall_xml.pl -d genedb /genedb-gene gene_symbol=Adh
Or:
selectall_xml.pl -d genedb /genedb-gene Adh
Or:
selectall_xml.pl -d genedb /genedb-gene gene_symbol@=Adh,dpp,bam,indy
A template is indicated by the syntactic shorthand of using a slash to
precede the template name; in this case the template is called
B<genedb-gene>. the -t option can also be used.
All the remaining arguments are passed in as SQL template
parameters. They can be passed in as either name=value pairs, or as a
simple list of arguments which get passed into the template in order
To use templates, you should have the environment variable
B<DBSTAG_TEMPLATE_DIRS> set. See B<DBIx::DBStag> for details.
=head2 LISTING AVAILABLE TEMPLATES FOR A DB
selectall_xml.pl -d mydb -h
=head2 LISTING VARIABLES FOR A TEMPLATE
selectall_xml.pl /genedb-gene -h
=head1 ENVIRONMENT VARIABLES
=over
=item DBSTAG_DBIMAP_FILE
A file containing configuration details for local databases
=item DBSTAG_TEMPLATE_DIRS
list of directories (separated by B<:>s) to be searched when templates
are requested
=back
=head1 COMMAND LINE ARGUMENTS
=over
=item -h|help
shows this page if no other arguments are given
if a template is specified, gives template details
if a db is specified, lists templates for that db
use in conjunction with -v for full descriptions
=item -d|dbname DBNAME
this is either a full DBI locator string (eg
B<dbi:Pg:dbname=mydb;host=localhost>) or it can also be a shortened
"nickname", which is then looked up in the file pointed at by the
environment variable B<DBSTAG_DBIMAP_FILE>
=item -u|user USER
database user identity
=item -p|password PASS
database password
=item -f|file SQLFILE
this is a path to a file containing SQL that will be executed, as an
alternative to writing the SQL on the command line
=item -n|nesting NESTING-EXPRESSIONS
a bracketed expression indicating how to the resulting objects/XML
should be nested. See L<DBIx::DBStag> for details.
=item -t|template TEMPLATE-NAME
the name of a template; see above
=item -wh|where WHERE-CLAUSE
used to override the WHERE clause of the query; useful for combining
with templates
You can append to an existing where clause by using the prefix B<+>
=item -s|select SELECT-COLS
used to override the SELECT clause of the query; useful for combining
with templates
=item -rows
sometimes it is preferable to return the results as a table rather
than xml or a similar nested structure. specifying -rows will fetch a
table, one line per row, and columns separated by tabs
=item -pre SQL
a piece of SQL is that is executed immediately before the main query; e.g.:
-pre "SET search_path=myschema,public"
=item -o|out FILE
a file to output the results to
=item -w|writer WRITER
writer class; can be any perl class, or one of these
=over
=item xml [default]
=item sxpr
lisp S-Expressions
=item itext
indented text
=back
=item -color
shows results in color (sxpr and itext only)
=item -show
will show the parse of the SQL statement
=back
=cut
|