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 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
|
#!/usr/local/bin/perl
# stag-q
# cjm@fruitfly.org
use strict;
no strict qw(vars);
use Carp;
use DBIx::DBStag;
use Data::Stag qw(:all);
use Data::Dumper;
use Getopt::Long;
use FileHandle;
use Term::ANSIColor;
my $h = {};
$| = 1;
my $dbname = '';
my $connect;
my $term;
my @hist = ();
my $XTERMMODE = 0;
my $MLINEMODE = 0;
my $TBLMODE = 0;
my $PAGER = "less -fR";
my $cscheme =
{
'keyword'=>'cyan',
'variable'=>'magenta',
'text' => 'reset',
'comment' => 'red',
'block' => 'blue',
'property' => 'green',
};
my $dbimap;
my $schema;
GetOptions(
"dbname|d=s"=>\$dbname,
"connect|c"=>\$connect,
"dbimap=s"=>\$dbimap,
"schema|s=s"=>\$schema,
);
if ($dbimap) {
$ENV{DBSTAG_DBIMAP_FILE} = $dbimap;
}
# parent dbh
my $sdbh =
DBIx::DBStag->new;
# child dbh
my $dbh;
my $stag;
my $res;
my $loc;
my $templates = [];
my $varnames = [];
my $options = {};
my $nesting;
my $rows = [];
my $template;
my $template_name = '';
my %exec_argh = ();
my $resources = $sdbh->resources_list;
my $resources_hash = $sdbh->resources_hash;
my @dbresl = grep {$_->{type} eq 'rdb'} @$resources;
my @dbnames = (map {$_->{name}} @dbresl);
my $W = Data::Stag->getformathandler('sxpr');
my $ofh = \*STDOUT;
$W->fh($ofh);
$W->use_color(1);
if ($connect) {
db($dbname);
}
else {
setdb($dbname) if $dbname;
}
shell();
exit 0;
sub shell {
my $prompt = $ENV{STAG_SHELL} || "Stag[\$dbname]\$template_name> ";
my $quit = 0;
my @lines = ();
my $r;
my $rv;
my $outfh;
my $return;
my $echo;
my $line;
# welcome;
require Term::ReadLine;
require Shell;
welcome();
# checkoptions;
$term = shift || Term::ReadLine->new($prompt);
my $rcfile = "$ENV{HOME}/.stagshellrc";
if (-f $rcfile) {
open(F, $rcfile);
@lines = <F>;
close(F);
}
my $end_signal = ";";
while (!$quit) {
my $line;
if ($MLINEMODE) {
$line = '';
my $thisline =
$term->readline(prompt($prompt));
if ($thisline =~ /^\\/ || $thisline =~ /^\w$/ ||
$thisline =~ /^\w\s+/ || $thisline =~ /^\//) {
$line = $thisline;
}
else {
while (1) {
if($thisline !~ /(.*)$end_signal/) {
$line.= "$thisline\n";
}
else {
$line.= "$1\n";
last;
}
$thisline = $term->readline('- ');
}
}
}
else {
$line =
@lines ? shift @lines : $term->readline(prompt($prompt));
}
if ($line =~ /^\^/) {
$line =~ s/^\^//;
print "$prompt$line";
my $e = <STDIN>;
if ($e =~ /^q/) {
$line = "";
@lines = ();
}
}
if ($options->{echo} && $line !~ /[\+]wait/) {
if ($line =~ /^\#/) {
print "$line\n";
}
else {
print "$prompt$line\n";
}
if ($options->{sleep}) {
sleep $options->{sleep};
}
if ($options->{wait}) {
sleep $options->{wait};
}
}
my ($cmd, @w) = split(' ',$line);
my $rest = join(" ", @w);
$_ = $cmd;
addhist($line);
$line =~ s/\#[^\n]*\n*/\n/gs;
$line =~ s/^\s*select/:SELECT/i;
# : - sql tunnel and escape everything after in quotes
if ($line =~ /^:/) {
if ($line =~ /\!\s*$/) {
$line =~ s/\!\s*$//;
# interpolate
$line = eval("\"$line\"");
}
$line =~ s/^:/ sqlselect q\[/;
$line .= ']';
}
if ($line =~ /^\/\//) {
# $line =~ s/^\/\/(.*)/\@r = apph\-\>$1; print tree2xml\(\@r\)/;
}
if ($line =~ /^\//) {
# $line =~ s/^\//\@r = apph\-\>/;
}
if ($line =~ /^\\dd\s*(.*)/) {
my $arg = $1 ? "('$1')" : '';
$line = "dshowdbs $arg";
}
if ($line =~ /^\\d\s*(.*)/) {
my $arg = $1 ? "('$1')" : '';
$line = "showdbs $arg;";
}
if ($line =~ /^\\tt\s*(.*)/) {
my $arg = $1 ? "('$1')" : '';
$line = "dshowtemplates $arg;";
}
if ($line =~ /^\\t\s*(.*)/) {
my $arg = $1 ? "('$1')" : '';
$line = "showtemplates $arg;";
}
if ($line =~ /^\\v\s*(.*)/) {
my $arg = $1 ? "('$1')" : '';
$line = "showvars $arg;";
}
if ($line =~ /^d\s+(.*)/) {
$line = "db '$1';";
}
if ($line =~ /^w\s+(.*)/) {
$line = "writer '$1';";
}
if ($line =~ /^t\s+(.*)/) {
$line = "template '$1';";
}
if ($line =~ /^\/$/) {
ex();
next;
}
if ($line =~ /^\/\s*(.*)$/) {
ex(split(' ', $1));
next;
}
if ($line =~ /^\\l/) {
$MLINEMODE = !$MLINEMODE;
printf "MLINEMODE = %s\n", $MLINEMODE ? 'MULTI' : 'SINGLE';
$line = '';
}
if ($line =~ /^\\x/) {
$XTERMMODE = !$XTERMMODE;
printf "XTERMMODE = %s\n", $XTERMMODE ? 'ON' : 'OFF';
$line = '';
}
if ($line =~ /^\\r/) {
$TBLMODE = !$TBLMODE;
printf "TBLMODE = %s\n", $TBLMODE ? 'ON' : 'OFF';
$line = '';
}
if ($line =~ /^\\c/) {
showresourcesfile();
next;
}
if ($line =~ /^\\q/) {
$quit = 1;
last;
}
if ($line =~ /^(\w+)\s*=\s*(.*)/) {
$exec_argh{"$1"} = $2;
msg("SETTING $1 to $2");
next;
}
# ! - shell and escape everything after in quotes
if ($line =~ /^\!/) {
$line =~ s/^\!/sh q\[/;
$line .= ']';
}
# ? - show
if ($line =~ /^\?/) {
$line =~ s/^\?/help /;
}
# ? - show
if ($line =~ /^\#/) {
next;
}
# + is the set command
if ($line =~ /^\+/) {
$line =~ s/\+//;
$line = "set ".join(",", map {"q[$_]"} split(' ', $line));
}
# --- EXECUTE ---
print "Echo:$line\n" if $echo;
$rv = eval $line;
if ($@) {
print STDERR $@;
}
print "\n";
print "$rv\n" if $return;
if ($options->{sleep}) {
sleep $options->{sleep};
}
if ($options->{wait}) {
sleep $options->{wait};
$options->{wait} = 0;
}
}
}
sub trace {
$ENV{DBSTAG_TRACE} = !$ENV{DBSTAG_TRACE};
}
sub setdb {
$dbname = shift;
msg("Set dbname to $dbname");
$res = $resources_hash->{$dbname};
if ($res) {
$schema = $res->{schema};
$loc = $res->{loc} || '';
msg("loc: $loc") if $loc;
}
else {
warnmsg("Unknown $dbname");
}
if ($schema) {
$templates = $sdbh->find_templates_by_schema($schema);
msg("schema: $schema");
}
else {
msg("schema not known; templates unrestricted");
$templates = $sdbh->template_list;
}
msg("Templates available: " . scalar(@$templates));
$res;
}
sub db {
$dbname = shift;
if ($dbh) {
$dbh->disconnect;
}
eval {
$dbh = DBIx::DBStag->connect($dbname);
msg("Connected to $dbname");
setdb($dbname);
};
if ($@) {
print STDERR "Could not connect to database '$dbname'\n";
}
$dbh;
}
sub conn {
$dbname = shift if @_;
if (!$dbh) {
if (!$dbname) {
warnmsg("You need to set a database with 'd' first");
}
else {
db($dbname);
}
}
}
sub addhist {
my $line = shift;
next unless $line;
# $term->addhistory($line);
push(@hist, $line);
}
sub showhist {
print "$_\n" foreach @hist;
}
sub showdbs {
showfilter(shift, [sort @dbnames]);
}
sub dshowdbs {
my @N = filter(shift, \@dbnames);
my @R =
map {$resources_hash->{$_}} @N;
page(sub {
my $fh = shift;
my $r = shift @R;
return 0 unless $r;
printf $fh "%-20s %s\n", $r->{name}, hilite('keyword', $r->{schema});
return 1;
});
}
sub showfilter {
foreach my $item (filter(@_)) {
printf "$item\n";
}
}
sub writer {
$W = Data::Stag->getformathandler(shift);
$W->use_color(1);
$W->fh($ofh);
}
sub filter {
my $re = shift;
my $list = shift || [];
return
grep { !$re || $_ =~ /$re/ } @$list;
}
sub showresourcesfile {
`xterm -e less $ENV{DBSTAG_DBIMAP_FILE}`;
}
sub showtemplates {
if ($templates) {
showfilter(shift, [map {$_->name} @$templates]);
}
else {
warnmsg("no templates for $dbname");
}
}
sub dshowtemplates {
my @T = map {$sdbh->find_template($_)} filter(shift, [map {$_->name} @$templates]);
page(sub {
my $fh = shift;
my $t = shift @T;
return 0 unless $t;
my $n = $t->name;
my $hdr =
hilite('comment',
join("\n",
"+" x 60,
"++++ $n". (' ' x (50 - length($n))). "++++",
("+" x 60),
"\n"));
my $ftr =
hilite('comment',
"// -- END OF TEMPLATE --\n". ("=" x 60));
print $fh $hdr;
$t->show($fh, $cscheme, sub { Term::ANSIColor::color(@_)});
print $fh $ftr;
print $fh "\n";
});
# my $fn;
# my $fh = \*STDOUT;
# ($fn, $fh) = opentmp();
# foreach (@t) {
# $_->show($fh, $cscheme);
# }
# $fh->close;
# if ($XTERMMODE) {
# my ($pfn, $pfh) = opentmp();
# print $pfh "more $fn && sleep 3600";
# system("xterm -e sh $pfn");
# $pfh->close;
# }
# else {
# system("more $fn");
# }
# unlink $fn;
}
sub page {
my $sub = shift;
my $fn;
my $fh = \*STDOUT;
($fn, $fh) = opentmp();
while ($sub->($fh)) {
#
}
# print $fh "\n\L";
$fh->close;
if ($XTERMMODE) {
my ($pfn, $pfh) = opentmp();
print $pfh "$PAGER $fn";
$pfh->close;
system("xterm -e sh $pfn &");
sleep(1);
unlink $pfn;
}
else {
system("$PAGER $fn");
}
unlink $fn;
}
sub prompt {
my $p = shift;
$p =~ s/(\$\w+)/eval($1)/eg;
$p;
}
sub template {
my $n = shift;
my @matches = grep {$_->name eq $n} @$templates;
if (@matches) {
if (@matches > 1) {
msg("not set - these are the possibilities");
showfilter(undef, \@matches);
}
elsif (!@matches) {
warnmsg("No templates match: $n");
}
else {
$template = shift @matches;
$varnames = $template->get_varnames;
$template_name = $n;
msg("Set template to \"$n\"");
msg("varnames:");
%exec_argh = ();
showvars();
}
}
}
sub showvars {
foreach my $vn (@$varnames) {
printf("%-20s => %s\n",
hilite('keyword', $vn),
defined($exec_argh{$vn}) ?
hilite('variable', $exec_argh{$vn}) : ' - NOT SET - ');
}
}
sub ex {
my @args = @_;
my $bind = {%exec_argh};
if (@args) {
foreach my $arg (@args) {
if ($arg =~ /(\w+)=(\S+)/) {
$bind->{$1} = $2;
}
else {
$bind = [] unless ref($bind) eq 'ARRAY';
push(@$bind, $arg);
}
}
}
conn();
$stag =
$dbh->selectall_stag(-template=>$template,
-bind=>$bind,
-nesting=>$nesting,
);
showstag();
}
sub sqlselect {
my $sql = shift;
conn();
if ($TBLMODE) {
$rows =
$dbh->selectall_arrayref($sql);
if ($rows) {
showrows();
}
else {
print STDERR "Query failed\n";
}
}
else {
$stag =
$dbh->selectall_stag(-sql=>$sql,
-nesting=>$nesting,
);
showstag();
}
}
sub sql {
my $sql = shift;
$dbh->do($sql);
}
sub hilite {
my $cn = shift;
my $str = shift;
color($cscheme->{$cn}) . $str . color('reset');
}
sub showstag {
my @kids = $stag->kids;
if (!@kids) {
msg("NO DATA");
return;
}
page(sub {
my $fh = shift;
$W->fh($fh);
$stag->sax($W);
return 0;
});
}
sub showrows {
my @R = @$rows;
page(sub {
my $fh = shift;
my $r = shift @R;
return 0 unless $r;
my @C = values %$cscheme;
for (my $i=0; $i<@$r; $i++) {
printf $fh "%s%s\t", color($C[$i%3]), $r->[$i];
# printf $fh $r->[$i], "\t";
}
print $fh color('reset') . "\n";
return 1;
});
}
sub x {
print Dumper shift;
}
sub msg {
print "@_\n";
}
sub warnmsg {
print "WARNING:@_\n";
}
sub welcome {
msg("Hello. This is the command line interface to DBStag.");
msg("Type ? or help if you are ever confused.\n");
}
sub make_offering {
print "Thank you! You are very kind\n";
`xv /users/cjm/stag/stag-god.jpg`;
}
sub help {
my $s =
join("\n",
'COMMAND SUMMARY',
'===============',
' ? displays this help message',
' \d displays available databases',
' \d MATCH available databases containing regexp MATCH',
' \dd displays available databases - DETAILED',
' \dd MATCH available databases containing regexp MATCH - DETAILED',
' \t [MATCH] lists available templates (optionally matching MATCH regexp)',
' \tt [MATCH] as above, but detailed view',
' \x toggle pager - from shell to popup xterm',
' \r toggle queryresult mode - rows vs trees',
' \c show resources conf file',
' d DBNAME set the database',
' w WRITER set the tree writer (xml, sxpr or itext)',
' t TEMPLATE set the template',
' x VARIABLE show perl variable using Data::Dump',
' trace toggle DBSTAG_TRACE',
' \v show template variable bindings',
' \l toggle multi/single line mode',
' \q QUIT',
' :SQL runs SQL',
' / [ARGS] execute the template; bind using ARGS',
'INTRO',
'This is the dbstag interactive query shell',
'You can issue SQL queries or call dbstag parameterized',
'canned query templates here. This is also a perl interpreter',
'',
'TEMPLATED QUERIES',
'See DBIx::DBStag::SQLTemplate for more details',
'You can execute the current template using forward-slash',
'Use the t command to set a template by name',
'',
'Binding to template args:',
'',
'You can bind any template argument on the command line by saying',
'VARIABLE = VALUE',
'(spaces around the = are optional)',
' you do not need a $ prefix',
' you do not need quotes around the value',
'',
'you can also bind variables when you execute the query, like this',
'',
' /VAR1=VAL1 VAR2=VAL2',
'',
'or sequentially:',
' / VAL1 VAL2',
'single value:',
' /VAL1',
'example with wildcard:',
' /*foo*',
'',
'if you have already bound the variables using =, then just type "q"',
'',
"\n\n\n",
"Other possibilities",
"-------------------",
'SQL',
' type in any SQL SELECT statement, and the results will be turned',
' into a Stag tree and displayed as either xml, sxpr or itext',
' the resulting tree goes into the $stag global variable',
' You can also switch to table mode using the \\r toggle',
' (in this case results go into the $rows variable)',
' perl variable will not be interpolated by default - you',
' can force interpolation by ending your SQL with !',
' $x="foo"',
' select * from bar where name=\'$x\'!',
'',
'Perl',
' type in any perl and it will be evaluated. don\'t use my to declare',
' variables though!',
'',
'Weird stuff',
'-----------',
' the xterm pager acts kind of weird depending on the stag god\'s whims',
' try running the same command again',
'',
'you will need xhost+ set if you want to use the xterm pager',
'',
'have fun and be careful out there',
);
page(sub {
my $fh = shift;
print $fh $s, "\n";
return 0;
});
}
our $ID = 0;
sub opentmp {
my $fn = "/tmp/stag-tmp-$ID-$$";
$ID++;
my $fh = new FileHandle "> $fn";
$fh || die($fn);
return ($fn, $fh);
}
sub closetmp {
my ($fn, $fh) = shift;
$fh->close || die($fn);
unlink($fn);
}
=head1 NAME
stag-qsh - DBStag query shell
=head1 SYNOPSIS
stag-qsh
help
=head1 DESCRIPTION
This is an interactive database query shell. For a full description,
execute this script and type 'help'
=head2 ARGUMENTS
=head3 -d B<DBNAME>
This is either a DBI locator or the logical name of a database in the
DBSTAG_DBIMAP_FILE config file
=cut
|