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 $user = 'dpavlin'; |
our $user = 'dpavlin'; |
16 |
|
our $table = 'log'; |
17 |
|
our $limit = 1000; |
18 |
|
|
19 |
|
our $group_by_join = { |
20 |
|
feed_id => [ 'feeds', 'id', 'title', 'link', 'timestamp' ], |
21 |
|
}; |
22 |
|
|
23 |
require 'config.pl' if -e 'config.pl'; |
require 'config.pl' if -e 'config.pl'; |
24 |
|
|
25 |
|
$table = param('table') || $table; |
26 |
my @columns = param('columns'); |
my @columns = param('columns'); |
27 |
@columns = ('*') unless @columns; |
@columns = ('*') unless @columns; |
28 |
my $table = param('table') || 'log'; |
$limit = param('limit') || $limit; |
|
my $limit = param('limit') || 1000; |
|
29 |
my $offset = param('offset') || 0; |
my $offset = param('offset') || 0; |
30 |
|
|
31 |
my @where_parts = param('where_parts'); |
my @where_parts = param('where_parts'); |
32 |
|
|
33 |
print header, q| |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
34 |
|
|
35 |
<html> |
sub where_from_parts { |
36 |
<head> |
return unless @_; |
37 |
<title>SQL Web Session</title> |
my @where_parts = @_; |
|
<link rel="stylesheet" type="text/css" href="style.css"> |
|
|
<!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js --> |
|
|
<script type="text/javascript" src="jquery-1.3.2.min.js"></script> |
|
38 |
|
|
39 |
<!-- insert firebug lite because we use console.* all over --> |
warn "# where_from_parts ",dump( @where_parts ); |
|
<script type="text/javascript" src="http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js"></script> |
|
|
<script type="text/javascript"> |
|
|
firebug.env.height = 100; |
|
|
</script> |
|
40 |
|
|
41 |
<script type="text/javascript"> |
my @w; |
42 |
|
my @data; |
43 |
|
foreach ( @where_parts ) { |
44 |
|
my ( $w,$v ) = split(/\?\t/,$_,2); |
45 |
|
push @w, "$w ?"; |
46 |
|
push @data, $v; |
47 |
|
} |
48 |
|
unshift @data, ' where ' . join(' and ', @w); |
49 |
|
warn "# ",dump( @data ); |
50 |
|
return @data; |
51 |
|
} |
52 |
|
|
53 |
$(document).ready( function() { |
sub sql_html { |
54 |
|
my @d = @_; |
55 |
|
my $sql_html = shift @d; |
56 |
|
$sql_html =~ s{\?}{dump( shift @d )}ge; |
57 |
|
return $sql_html; |
58 |
|
} |
59 |
|
|
60 |
|
if ( my $group_by = param('lookup_col') ) { |
61 |
|
|
62 |
function click_on_cell(e) { |
my @cols = ( $group_by, "count($group_by)" ); |
63 |
|
my @group_by = ( $group_by ); |
64 |
|
|
65 |
var tag = e.originalTarget.tagName; |
my $join = ''; |
66 |
|
my @join = @{ $group_by_join->{$group_by} } if defined $group_by_join->{$group_by}; |
67 |
|
if ( @join ) { |
68 |
|
warn "## join ",dump( @join ); |
69 |
|
my $join_table = shift @join; |
70 |
|
my $col = shift @join; |
71 |
|
$join = qq{ join $join_table on $table.$group_by = $join_table.$col }; |
72 |
|
my @join_cols = map { $join_table . '.' . $_ } @join; |
73 |
|
push @cols, @join_cols; |
74 |
|
push @group_by, @join_cols; |
75 |
|
} |
76 |
|
|
77 |
var col_nr = e.originalTarget.cellIndex; |
my @data = where_from_parts( @where_parts ); |
78 |
|
|
79 |
var column = $('table#results th:nth-child(' + ( col_nr + 1 ) + ')').text(); |
my $sql = join("\n", |
80 |
var where_operator = '='; |
'select', join(',', @cols), qq{ |
81 |
var where_value = window.getSelection().getRangeAt(0).cloneContents().textContent; |
from $table |
82 |
if ( where_value.length == 0 ) |
$join |
83 |
where_value = e.originalTarget.textContent; |
}, shift @data, # extract where |
84 |
else |
'group by', join(',', @group_by), qq{ |
85 |
where_value = '%' + where_value + '%'; |
order by count($group_by) desc |
86 |
|
limit $limit |
|
console.debug('click on ', this, e, |
|
|
e.originalTarget, |
|
|
column, where_operator, where_value |
|
|
); |
|
|
|
|
|
if ( tag == 'TH' ) { |
|
|
console.info('header', column); |
|
|
$('form#sql input[name=order_by]').attr('value', where_value + ' desc'); |
|
|
} else if ( tag = 'TD' ) { |
|
|
console.info('column', column, where_operator, where_value); |
|
|
$('form#sql input[name=where_value]').attr('value', where_value); |
|
|
$('form#sql select[name=where_column]').attr('options').selectedIndex = col_nr; |
|
|
$('form#sql input[name=add_group_by]').attr('value', column).css('display','block'); |
|
|
} else { |
|
|
console.error('unknown click on ', tag, e); |
|
87 |
} |
} |
88 |
|
); |
|
$('form#sql').addClass('visible'); |
|
|
}; |
|
|
|
|
|
$('table#results').bind('mouseup', click_on_cell); |
|
|
|
|
|
$('#status').bind('click', function() { |
|
|
$('form#sql').toggleClass('visible'); |
|
|
}); |
|
89 |
|
|
90 |
console.info('ready'); |
warn "# join SQL: $sql\n"; |
|
}); |
|
91 |
|
|
92 |
</script> |
my $t = time(); |
93 |
</head> |
my $sth = $dbh->prepare( $sql ); |
94 |
<body> |
$sth->execute( @data ); |
95 |
|; |
$t = time() - $t; |
96 |
|
print qq|<code>|, sql_html( $sql, @data ), qq|<code>|; |
97 |
|
print qq|<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|; |
98 |
|
while ( my @row = $sth->fetchrow_array ) { |
99 |
|
my $n = shift @row; |
100 |
|
$n = 'NULL' unless defined $n; |
101 |
|
print qq|<tr><td><a href="#">$n</a></td><td>|, join(qq|</td><td>|, @row), qq|</td></tr>|; |
102 |
|
} |
103 |
|
print qq|</table>|; |
104 |
|
print $sth->rows, qq| rows in $t s|; |
105 |
|
exit; |
106 |
|
} |
107 |
|
|
108 |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
print q| |
109 |
|
|
110 |
|
<html> |
111 |
|
<head> |
112 |
|
<title>SQL Web Session</title> |
113 |
|
<link rel="stylesheet" type="text/css" href="style.css"> |
114 |
|
<!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js --> |
115 |
|
<script type="text/javascript" src="jquery-1.3.2.min.js"></script> |
116 |
|
|
117 |
my $group_by = param('add_group_by'); |
<script type="text/javascript" src="sql-editor.js"></script> |
118 |
|
|
119 |
if ( $group_by ) { |
</head> |
120 |
@columns = ( "count($group_by)", $group_by ); |
<body> |
121 |
my $g; |
|; |
122 |
foreach my $c ( @where_parts, $group_by ) { |
|
123 |
$c =~ s/\s.+$//; |
my $group_by = param('group_by'); |
|
$g->{$c}++ if length($c) > 0; |
|
|
} |
|
|
$group_by = join( ',', keys %$g ); |
|
|
print "# $group_by g = ",dump( $g ); |
|
|
param('where_value',''); |
|
|
param('group_by', $group_by); |
|
|
param('columns', [ @columns ], [ @columns ]); |
|
|
param('order_by', $columns[0] . ' desc'); |
|
|
} else { |
|
|
$group_by = param('group_by'); |
|
|
} |
|
|
|
|
124 |
|
|
125 |
if ( param('where_operator') && length( param('where_value') ) > 0 ) { |
if ( param('where_operator') && length( param('where_value') ) > 0 ) { |
126 |
my $where_value = param('where_value'); |
my $where_value = param('where_value'); |
134 |
my $sql = "select $c from $table"; |
my $sql = "select $c from $table"; |
135 |
my @data; |
my @data; |
136 |
|
|
137 |
if ( @where_parts ) { |
my @where = where_from_parts( @where_parts ); |
138 |
my @w; |
$sql .= shift @where; |
139 |
foreach ( @where_parts ) { |
push @data, @where; |
|
my ( $w,$v ) = split(/\?\t/,$_,2); |
|
|
push @w, "$w ?"; |
|
|
push @data, $v; |
|
|
} |
|
|
$sql .= ' where ' . join(' and ', @w); |
|
|
} |
|
|
|
|
140 |
|
|
141 |
$sql .= ' group by ' . $group_by if $group_by; |
$sql .= ' group by ' . $group_by if $group_by; |
142 |
$sql .= ' order by ' . param('order_by') if param('order_by'); |
$sql .= ' order by ' . param('order_by') if param('order_by'); |
144 |
|
|
145 |
push @data, ( $limit, $offset ); |
push @data, ( $limit, $offset ); |
146 |
|
|
147 |
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|; |
|
148 |
|
|
149 |
my $t = time(); |
my $t = time(); |
150 |
|
|
156 |
|
|
157 |
print $sth->rows, qq| rows in $t s</code>|; |
print $sth->rows, qq| rows in $t s</code>|; |
158 |
|
|
|
|
|
159 |
@columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*'; |
@columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*'; |
160 |
|
|
|
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 ); |
|
|
} |
|
|
|
|
161 |
print |
print |
162 |
qq|</table>| |
start_form( -id => 'sql', -class => 'fixed' ) |
|
, start_form( -id => 'sql' ) |
|
163 |
|
|
164 |
, qq|<a href="#" onclick="\$('form#sql').toggleClass('visible'); return false;" class=close>close</a>| |
, qq|<input type=button value="[=]" onclick="\$('form#sql').toggleClass('fixed'); return false;" title="toggle fixed position" class="right">| |
165 |
|
, qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="right">| |
166 |
|
|
167 |
, qq|<label for=columns>select</label>| |
, qq|<label for=columns>select</label>| |
168 |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
169 |
|
|
170 |
, qq|<label for=from>from</label>| |
, qq|<label for=from>from</label>| |
171 |
, textfield( -name => 'from', -value => $table, -default => 'log' ) |
, textfield( -name => 'from', -value => $table, -default => $table ) |
172 |
|
|
173 |
, qq|<label for=where>where</label>| |
, qq|<label for=where>where</label>| |
174 |
, checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] ) |
, checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] ) |
175 |
, popup_menu( -name => 'where_column', -values => [ @columns ] ), |
, popup_menu( -name => 'where_column', -values => [ @columns ] ), |
176 |
, popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ]) |
, popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ]) |
177 |
, textfield( -name => 'where_value' ) |
, textfield( -name => 'where_value' ) |
178 |
|
, qq| |
179 |
|
<span> |
180 |
|
<input type=button name=lookup_col title="lookup column details"> |
181 |
|
<input type=button name=close_group_by value="[x]" disabled=1> |
182 |
|
<div id="lookup"></div> |
183 |
|
</span> |
184 |
|
| |
185 |
|
|
186 |
, qq|<label for=group_by>group by</label>| |
, qq|<label for=group_by>group by</label>| |
187 |
, textfield( -name => 'group_by' ) |
, textfield( -name => 'group_by' ) |
|
, submit( -name => 'add_group_by' ) |
|
188 |
|
|
189 |
, qq|<label for=order_by>order by</label>| |
, qq|<label for=order_by>order by</label>| |
190 |
, textfield( -name => 'order_by' ) |
, textfield( -name => 'order_by' ) |
191 |
|
|
192 |
, qq|<label for=limit>limit</label>| |
, qq|<label for=limit>limit</label>| |
193 |
, textfield( -name=> 'limit', -default => 1000, -size => 4 ) |
, textfield( -name=> 'limit', -default => $limit, -size => 4 ) |
194 |
|
|
195 |
, qq|<label for=offset>offset</label>| |
, qq|<label for=offset>offset</label>| |
196 |
, textfield( -name=> 'offset', -default => 0, -size => 4 ) |
, textfield( -name=> 'offset', -default => 0, -size => 4 ) |
198 |
, submit( -name => 'execute', -value => 'execute' ) |
, submit( -name => 'execute', -value => 'execute' ) |
199 |
|
|
200 |
, end_form |
, end_form |
201 |
|
; |
202 |
|
|
203 |
|
#my @types = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} }; |
204 |
|
my $types = dump( $sth->{TYPE} ); |
205 |
|
print qq{ |
206 |
|
<script type="text/javascript"> |
207 |
|
var column_type = $types ; |
208 |
|
</script> |
209 |
|
}; |
210 |
|
|
211 |
|
print qq|<table id="results">|; |
212 |
|
|
213 |
|
my $counter = 0; |
214 |
|
sub table_row { |
215 |
|
my $cell = shift; |
216 |
|
my $class = $counter++ % 2 == 0 ? ' class=o' : ''; |
217 |
|
return |
218 |
|
qq|<tr $class><$cell>| |
219 |
|
. join( qq|</$cell><$cell>|, @_ ) |
220 |
|
. qq|</$cell></tr>| |
221 |
|
; |
222 |
|
|
223 |
|
} |
224 |
|
|
225 |
|
print table_row( 'th', @columns ); |
226 |
|
|
227 |
|
while ( my @row = $sth->fetchrow_array ) { |
228 |
|
print table_row( 'td', @row ); |
229 |
|
} |
230 |
|
|
231 |
|
print qq|</table>| |
232 |
, qq|</body></html>| |
, qq|</body></html>| |
233 |
; |
; |