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 |
if ( my $group_by = param('lookup_col') ) { |
if ( my $group_by = param('lookup_col') ) { |
54 |
|
|
55 |
my @cols = ( $group_by, "count($group_by)" ); |
my @cols = ( $group_by, "count($group_by)" ); |
67 |
push @group_by, @join_cols; |
push @group_by, @join_cols; |
68 |
} |
} |
69 |
|
|
70 |
|
my @data = where_from_parts( @where_parts ); |
71 |
|
|
72 |
my $sql = join("\n", |
my $sql = join("\n", |
73 |
'select', join(',', @cols), qq{ |
'select', join(',', @cols), qq{ |
74 |
from $table |
from $table |
75 |
$join |
$join |
76 |
}, 'group by', join(',', @group_by), qq{ |
}, shift @data, # extract where |
77 |
|
'group by', join(',', @group_by), qq{ |
78 |
order by count($group_by) desc |
order by count($group_by) desc |
79 |
limit 10 |
limit 10 |
80 |
} |
} |
84 |
|
|
85 |
my $t = time(); |
my $t = time(); |
86 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
87 |
$sth->execute; |
$sth->execute( @data ); |
88 |
$t = time() - $t; |
$t = time() - $t; |
89 |
print qq|$t<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|; |
print qq|$t<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|; |
90 |
while ( my @row = $sth->fetchrow_array ) { |
while ( my @row = $sth->fetchrow_array ) { |
126 |
my $sql = "select $c from $table"; |
my $sql = "select $c from $table"; |
127 |
my @data; |
my @data; |
128 |
|
|
129 |
if ( @where_parts ) { |
my @where = where_from_parts( @where_parts ); |
130 |
my @w; |
$sql .= shift @where; |
131 |
foreach ( @where_parts ) { |
push @data, @where; |
|
my ( $w,$v ) = split(/\?\t/,$_,2); |
|
|
push @w, "$w ?"; |
|
|
push @data, $v; |
|
|
} |
|
|
$sql .= ' where ' . join(' and ', @w); |
|
|
} |
|
|
|
|
132 |
|
|
133 |
$sql .= ' group by ' . $group_by if $group_by; |
$sql .= ' group by ' . $group_by if $group_by; |
134 |
$sql .= ' order by ' . param('order_by') if param('order_by'); |
$sql .= ' order by ' . param('order_by') if param('order_by'); |
185 |
|
|
186 |
print |
print |
187 |
qq|</table>| |
qq|</table>| |
188 |
, start_form( -id => 'sql' ) |
, start_form( -id => 'sql', -class => 'fixed' ) |
189 |
|
|
190 |
, qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="close">| |
, qq|<input type=button value="[=]" onclick="\$('form#sql').toggleClass('fixed'); return false;" title="toggle fixed position" class="right">| |
191 |
|
, qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="right">| |
192 |
|
|
193 |
, qq|<label for=columns>select</label>| |
, qq|<label for=columns>select</label>| |
194 |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |