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
|
#!/usr/local/bin/perl -w
=head1 NAME
selectall_html.pl - select all elements from database
=head1 SYNOPSIS
selectall_html.pl -d "dbi:Pg:dbname=mydb;host=localhost" "SELECT * FROM a NATURAL JOIN b"
=head1 DESCRIPTION
=head1 ARGUMENTS
=cut
use strict;
use Carp;
use DBIx::DBStag;
use Data::Stag;
use Getopt::Long;
my $debug;
my $help;
my $db;
my $nesting;
my $show;
my $remove = "";
my $highlight = "";
GetOptions(
"help|h"=>\$help,
"db|d=s"=>\$db,
"show"=>\$show,
"remove|r=s"=>\$remove,
"highlight=s"=>\$highlight,
"nesting|p=s"=>\$nesting,
);
if ($help) {
system("perldoc $0");
exit 0;
}
my $dbh =
DBIx::DBStag->connect($db);
my $sql = shift @ARGV;
if (!$sql) {
print STDERR "Reading SQL from STDIN...\n";
$sql = <STDIN>;
}
my $stag = $dbh->selectall_stag($sql, $nesting);
my @rmlist = split(/\;\s*/, $remove);
foreach (@rmlist) {
my @was = $stag->findnode($_, []);
}
my %hih = map {$_=>1} split(/\;\s*/, $highlight);
our $BORDER = 0;
our $TDARGS = 'VALIGN="top"';
our $THARGS = 'BGCOLOR=#9999FF';
our $TERMCOLOUR = 'BGCOLOR=#DDDDFF';
print to_html($stag);
$dbh->disconnect;
sub to_html {
my $stag = shift;
my $row = 0;
return sprintf("<table border=$BORDER>%s</table>",
join('',
map {
_to_html($_, $row++)
} $stag->kids));
}
sub _to_html {
my $stag = shift;
my $thisrow = shift;
my @kids = $stag->kids;
my $el = $stag->element;
# my $type = $types{$el};
my $type = 'tr';
my $out =
"<$type>";
my $pre = '';
my @hdrs = ();
foreach my $kid (@kids) {
next unless $kid->isterminal;
my $data = $kid->data;
if ($hih{$kid->element}) {
$data = "<b>$data</b>";
}
$out .= sprintf("<td $TERMCOLOUR $TDARGS>%s</td>",
$data);
push(@hdrs, $kid->element);
}
my @ntkids = $stag->ntnodes;
my %elh = ();
my @elts = ();
# get unique ordered list
foreach (@ntkids) {
if (!$elh{$_->element}) {
$elh{$_->element} = 1;
push(@elts, $_->element);
}
}
foreach my $subel (@elts) {
push(@hdrs, $subel);
my @stags = $stag->getnode($subel);
my $inner;
my @ntstags = grep {!$_->isterminal} @stags;
if (@ntstags) {
my $row = 0;
$inner = sprintf("<table border=$BORDER>%s</table>",
join('',
map {
_to_html($_, $row++)
} @stags));
}
else {
$inner =
join('<br>',
map {
_to_html($_) . '\n'
} @stags);
}
$out .= sprintf("<td $TDARGS>$inner</td>");
}
$out .= "</$type>\n";
if (!$thisrow) {
$pre = '<tr>' . join('',
map {"<th $THARGS>$_</th>"} @hdrs) . '</tr>';
}
return $pre . $out;
}
|