9 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
10 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
11 |
|
|
12 |
|
print qq{Content-type: text/html\r\n\r\n}; |
13 |
|
|
14 |
our $dsn = 'DBI:Pg:dbname=syslog'; |
our $dsn = 'DBI:Pg:dbname=syslog'; |
15 |
|
our $database = ''; # if not in $dsn |
16 |
our $user = 'dpavlin'; |
our $user = 'dpavlin'; |
17 |
our $table = 'log'; |
our $table = 'log'; |
18 |
|
our $limit = 1000; |
19 |
|
our $passwd = ''; |
20 |
|
|
21 |
|
our $group_by_join = { |
22 |
|
feed_id => [ 'feeds', 'id', 'title', 'link', 'timestamp' ], |
23 |
|
}; |
24 |
|
|
25 |
require 'config.pl' if -e 'config.pl'; |
require 'config.pl' if -e 'config.pl'; |
26 |
|
|
27 |
$table = param('table') || $table; |
$table = param('table') || $table; |
28 |
my @columns = param('columns'); |
my @columns = param('columns'); |
29 |
@columns = ('*') unless @columns; |
@columns = ('*') unless @columns; |
30 |
my $limit = param('limit') || 1000; |
$limit = param('limit') || $limit; |
31 |
my $offset = param('offset') || 0; |
my $offset = param('offset') || 0; |
32 |
|
|
33 |
my @where_parts = param('where_parts'); |
my @where_parts = param('where_parts'); |
34 |
|
|
35 |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
my $dbh = DBI->connect( $dsn . $database, $user, $passwd, { RaiseError => 1 } ) || die $DBI::errstr; |
36 |
|
|
37 |
|
sub where_from_parts { |
38 |
|
return unless @_; |
39 |
|
my @where_parts = @_; |
40 |
|
|
41 |
|
warn "# where_from_parts ",dump( @where_parts ); |
42 |
|
|
43 |
|
my @w; |
44 |
|
my @data; |
45 |
|
foreach ( @where_parts ) { |
46 |
|
my ( $w,$v ) = split(/\?\t/,$_,2); |
47 |
|
push @w, "$w ?"; |
48 |
|
push @data, $v; |
49 |
|
} |
50 |
|
unshift @data, ' where ' . join(' and ', @w); |
51 |
|
warn "# ",dump( @data ); |
52 |
|
return @data; |
53 |
|
} |
54 |
|
|
55 |
|
sub sql_html { |
56 |
|
my @d = @_; |
57 |
|
my $sql_html = shift @d; |
58 |
|
$sql_html =~ s{\?}{dump( shift @d )}ge; |
59 |
|
return $sql_html; |
60 |
|
} |
61 |
|
|
62 |
|
if ( my $group_by = param('lookup_col') ) { |
63 |
|
|
64 |
if ( my $group_by = param('add_group_by') ) { |
my @cols = ( $group_by, "count($group_by)" ); |
65 |
my $sth = $dbh->prepare(qq{ |
my @group_by = ( $group_by ); |
66 |
select $group_by,count($group_by) |
|
67 |
|
my $join = ''; |
68 |
|
my @join = @{ $group_by_join->{$group_by} } if defined $group_by_join->{$group_by}; |
69 |
|
if ( @join ) { |
70 |
|
warn "## join ",dump( @join ); |
71 |
|
my $join_table = shift @join; |
72 |
|
my $col = shift @join; |
73 |
|
$join = qq{ join $join_table on $table.$group_by = $join_table.$col }; |
74 |
|
my @join_cols = map { $join_table . '.' . $_ } @join; |
75 |
|
push @cols, @join_cols; |
76 |
|
push @group_by, @join_cols; |
77 |
|
} |
78 |
|
|
79 |
|
my @data = where_from_parts( @where_parts ); |
80 |
|
|
81 |
|
my $sql = join("\n", |
82 |
|
'select', join(',', @cols), qq{ |
83 |
from $table |
from $table |
84 |
group by $group_by |
$join |
85 |
|
}, shift @data, # extract where |
86 |
|
'group by', join(',', @group_by), qq{ |
87 |
order by count($group_by) desc |
order by count($group_by) desc |
88 |
limit 10 |
limit $limit |
89 |
}); |
} |
90 |
$sth->execute; |
); |
91 |
print header, qq|<table><tr><th>count</th><th>$group_by</th><tr>|; |
|
92 |
|
warn "# join SQL: $sql\n"; |
93 |
|
|
94 |
|
my $t = time(); |
95 |
|
my $sth = $dbh->prepare( $sql ); |
96 |
|
$sth->execute( @data ); |
97 |
|
$t = time() - $t; |
98 |
|
print qq|<code>|, sql_html( $sql, @data ), qq|<code>|; |
99 |
|
print qq|<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|; |
100 |
while ( my @row = $sth->fetchrow_array ) { |
while ( my @row = $sth->fetchrow_array ) { |
101 |
my ( $n, $c ) = @row; |
my $n = shift @row; |
102 |
$n = 'NULL' unless defined $n; |
$n = 'NULL' unless defined $n; |
103 |
print qq|<tr><td>$c</td><td><a href="#">$n</a></td></tr>|; |
print qq|<tr><td><a href="#">$n</a></td><td>|, join(qq|</td><td>|, @row), qq|</td></tr>|; |
104 |
} |
} |
105 |
print qq|</table>|; |
print qq|</table>|; |
106 |
|
print $sth->rows, qq| rows in $t s|; |
107 |
exit; |
exit; |
108 |
} |
} |
109 |
|
|
110 |
print header, q| |
print q| |
111 |
|
|
112 |
<html> |
<html> |
113 |
<head> |
<head> |
136 |
my $sql = "select $c from $table"; |
my $sql = "select $c from $table"; |
137 |
my @data; |
my @data; |
138 |
|
|
139 |
if ( @where_parts ) { |
my @where = where_from_parts( @where_parts ); |
140 |
my @w; |
$sql .= shift @where; |
141 |
foreach ( @where_parts ) { |
push @data, @where; |
|
my ( $w,$v ) = split(/\?\t/,$_,2); |
|
|
push @w, "$w ?"; |
|
|
push @data, $v; |
|
|
} |
|
|
$sql .= ' where ' . join(' and ', @w); |
|
|
} |
|
|
|
|
142 |
|
|
143 |
$sql .= ' group by ' . $group_by if $group_by; |
$sql .= ' group by ' . $group_by if $group_by; |
144 |
$sql .= ' order by ' . param('order_by') if param('order_by'); |
$sql .= ' order by ' . param('order_by') if param('order_by'); |
146 |
|
|
147 |
push @data, ( $limit, $offset ); |
push @data, ( $limit, $offset ); |
148 |
|
|
149 |
my $sql_html = $sql; |
print qq|<code id="status">|, sql_html( $sql, @data ), qq|<br>\n|; |
|
{ |
|
|
my @d = @data; |
|
|
$sql_html =~ s{\?}{dump( shift @d )}ge; |
|
|
} |
|
|
print qq|<code id="status">$sql_html<br>\n\r\n\r|; |
|
150 |
|
|
151 |
my $t = time(); |
my $t = time(); |
152 |
|
|
158 |
|
|
159 |
print $sth->rows, qq| rows in $t s</code>|; |
print $sth->rows, qq| rows in $t s</code>|; |
160 |
|
|
|
#my @types = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} }; |
|
|
my $types = dump( $sth->{TYPE} ); |
|
|
print qq{ |
|
|
<script type="text/javascript"> |
|
|
var column_type = $types ; |
|
|
</script> |
|
|
}; |
|
|
|
|
161 |
@columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*'; |
@columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*'; |
162 |
|
|
|
print qq|<table id="results">|; |
|
|
|
|
|
my $counter = 0; |
|
|
sub table_row { |
|
|
my $cell = shift; |
|
|
my $class = $counter++ % 2 == 0 ? ' class=o' : ''; |
|
|
return |
|
|
qq|<tr $class><$cell>| |
|
|
. join( qq|</$cell><$cell>|, @_ ) |
|
|
. qq|</$cell></tr>| |
|
|
; |
|
|
|
|
|
} |
|
|
|
|
|
print table_row( 'th', @columns ); |
|
|
|
|
|
while ( my @row = $sth->fetchrow_array ) { |
|
|
print table_row( 'td', @row ); |
|
|
} |
|
|
|
|
163 |
print |
print |
164 |
qq|</table>| |
start_form( -id => 'sql', -class => 'fixed' ) |
|
, start_form( -id => 'sql' ) |
|
165 |
|
|
166 |
, qq|<a href="#" onclick="\$('form#sql').toggleClass('visible'); return false;" class=close title="hide sql editor">[x]</a>| |
, qq|<input type=button value="[=]" onclick="\$('form#sql').toggleClass('fixed'); return false;" title="toggle fixed position" class="right">| |
167 |
|
, qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="right">| |
168 |
|
|
169 |
, qq|<label for=columns>select</label>| |
, qq|<label for=columns>select</label>| |
170 |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
171 |
|
|
172 |
, qq|<label for=from>from</label>| |
, qq|<label for=from>from</label>| |
173 |
, textfield( -name => 'from', -value => $table, -default => 'log' ) |
, textfield( -name => 'from', -value => $table, -default => $table ) |
174 |
|
|
175 |
, qq|<label for=where>where</label>| |
, qq|<label for=where>where</label>| |
176 |
, checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] ) |
, checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] ) |
179 |
, textfield( -name => 'where_value' ) |
, textfield( -name => 'where_value' ) |
180 |
, qq| |
, qq| |
181 |
<span> |
<span> |
182 |
<input type=button name=add_group_by> |
<input type=button name=lookup_col title="lookup column details"> |
183 |
<input type=button name=close_group_by value="[x]" disabled=1> |
<input type=button name=close_group_by value="[x]" disabled=1> |
184 |
<div id="lookup"></div> |
<div id="lookup"></div> |
185 |
<span> |
</span> |
186 |
| |
| |
187 |
|
|
188 |
, qq|<label for=group_by>group by</label>| |
, qq|<label for=group_by>group by</label>| |
192 |
, textfield( -name => 'order_by' ) |
, textfield( -name => 'order_by' ) |
193 |
|
|
194 |
, qq|<label for=limit>limit</label>| |
, qq|<label for=limit>limit</label>| |
195 |
, textfield( -name=> 'limit', -default => 1000, -size => 4 ) |
, textfield( -name=> 'limit', -default => $limit, -size => 4 ) |
196 |
|
|
197 |
, qq|<label for=offset>offset</label>| |
, qq|<label for=offset>offset</label>| |
198 |
, textfield( -name=> 'offset', -default => 0, -size => 4 ) |
, textfield( -name=> 'offset', -default => 0, -size => 4 ) |
200 |
, submit( -name => 'execute', -value => 'execute' ) |
, submit( -name => 'execute', -value => 'execute' ) |
201 |
|
|
202 |
, end_form |
, end_form |
203 |
|
; |
204 |
|
|
205 |
|
#my @types = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} }; |
206 |
|
my $types = dump( $sth->{TYPE} ); |
207 |
|
print qq{ |
208 |
|
<script type="text/javascript"> |
209 |
|
var column_type = $types ; |
210 |
|
</script> |
211 |
|
}; |
212 |
|
|
213 |
|
print qq|<table id="results">|; |
214 |
|
|
215 |
|
my $counter = 0; |
216 |
|
sub table_row { |
217 |
|
my $cell = shift; |
218 |
|
my $class = $counter++ % 2 == 0 ? ' class=o' : ''; |
219 |
|
return |
220 |
|
qq|<tr $class><$cell>| |
221 |
|
. join( qq|</$cell><$cell>|, @_ ) |
222 |
|
. qq|</$cell></tr>| |
223 |
|
; |
224 |
|
|
225 |
|
} |
226 |
|
|
227 |
|
print table_row( 'th', @columns ); |
228 |
|
|
229 |
|
while ( my @row = $sth->fetchrow_array ) { |
230 |
|
print table_row( 'td', @row ); |
231 |
|
} |
232 |
|
|
233 |
|
print qq|</table>| |
234 |
, qq|</body></html>| |
, qq|</body></html>| |
235 |
; |
; |