32 |
|
|
33 |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
34 |
|
|
35 |
|
sub where_from_parts { |
36 |
|
return unless @_; |
37 |
|
my @where_parts = @_; |
38 |
|
|
39 |
|
warn "# where_from_parts ",dump( @where_parts ); |
40 |
|
|
41 |
|
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 |
|
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') ) { |
if ( my $group_by = param('lookup_col') ) { |
61 |
|
|
62 |
my @cols = ( $group_by, "count($group_by)" ); |
my @cols = ( $group_by, "count($group_by)" ); |
74 |
push @group_by, @join_cols; |
push @group_by, @join_cols; |
75 |
} |
} |
76 |
|
|
77 |
|
my @data = where_from_parts( @where_parts ); |
78 |
|
|
79 |
my $sql = join("\n", |
my $sql = join("\n", |
80 |
'select', join(',', @cols), qq{ |
'select', join(',', @cols), qq{ |
81 |
from $table |
from $table |
82 |
$join |
$join |
83 |
}, 'group by', join(',', @group_by), qq{ |
}, shift @data, # extract where |
84 |
|
'group by', join(',', @group_by), qq{ |
85 |
order by count($group_by) desc |
order by count($group_by) desc |
86 |
limit 10 |
limit $limit |
87 |
} |
} |
88 |
); |
); |
89 |
|
|
91 |
|
|
92 |
my $t = time(); |
my $t = time(); |
93 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
94 |
$sth->execute; |
$sth->execute( @data ); |
95 |
$t = time() - $t; |
$t = time() - $t; |
96 |
print qq|$t<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|; |
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 ) { |
while ( my @row = $sth->fetchrow_array ) { |
99 |
my $n = shift @row; |
my $n = shift @row; |
100 |
$n = 'NULL' unless defined $n; |
$n = 'NULL' unless defined $n; |
101 |
print qq|<tr><td><a href="#">$n</a></td><td>|, join(qq|</td><td>|, @row), qq|</td></tr>|; |
print qq|<tr><td><a href="#">$n</a></td><td>|, join(qq|</td><td>|, @row), qq|</td></tr>|; |
102 |
} |
} |
103 |
print qq|</table>|; |
print qq|</table>|; |
104 |
print qq|<code>$sql</code>|; |
print $sth->rows, qq| rows in $t s|; |
105 |
exit; |
exit; |
106 |
} |
} |
107 |
|
|
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 |
|
|
|
#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> |
|
|
}; |
|
|
|
|
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', -class => 'fixed' ) |
|
163 |
|
|
164 |
, qq|<input type=button value="[=]" onclick="\$('form#sql').toggleClass('fixed'); return false;" title="toggle fixed position" class="right">| |
, 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">| |
, qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="right">| |
180 |
<input type=button name=lookup_col title="lookup column details"> |
<input type=button name=lookup_col title="lookup column details"> |
181 |
<input type=button name=close_group_by value="[x]" disabled=1> |
<input type=button name=close_group_by value="[x]" disabled=1> |
182 |
<div id="lookup"></div> |
<div id="lookup"></div> |
183 |
<span> |
</span> |
184 |
| |
| |
185 |
|
|
186 |
, qq|<label for=group_by>group by</label>| |
, qq|<label for=group_by>group by</label>| |
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 |
; |
; |