14 |
our $table = 'log'; |
our $table = 'log'; |
15 |
our $limit = 1000; |
our $limit = 1000; |
16 |
|
|
17 |
|
our $group_by_join = { |
18 |
|
feed_id => [ 'feeds', 'id', 'title', 'link', 'timestamp' ], |
19 |
|
}; |
20 |
|
|
21 |
require 'config.pl' if -e 'config.pl'; |
require 'config.pl' if -e 'config.pl'; |
22 |
|
|
23 |
$table = param('table') || $table; |
$table = param('table') || $table; |
31 |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
32 |
|
|
33 |
if ( my $group_by = param('add_group_by') ) { |
if ( my $group_by = param('add_group_by') ) { |
34 |
my $sth = $dbh->prepare(qq{ |
|
35 |
select $group_by,count($group_by) |
my @cols = ( $group_by, "count($group_by)" ); |
36 |
|
my @group_by = ( $group_by ); |
37 |
|
|
38 |
|
my $join = ''; |
39 |
|
my @join = @{ $group_by_join->{$group_by} } if defined $group_by_join->{$group_by}; |
40 |
|
if ( @join ) { |
41 |
|
warn "## join ",dump( @join ); |
42 |
|
my $join_table = shift @join; |
43 |
|
my $col = shift @join; |
44 |
|
$join = qq{ join $join_table on $table.$group_by = $join_table.$col }; |
45 |
|
my @join_cols = map { $join_table . '.' . $_ } @join; |
46 |
|
push @cols, @join_cols; |
47 |
|
push @group_by, @join_cols; |
48 |
|
} |
49 |
|
|
50 |
|
my $sql = join("\n", |
51 |
|
'select', join(',', @cols), qq{ |
52 |
from $table |
from $table |
53 |
group by $group_by |
$join |
54 |
|
}, 'group by', join(',', @group_by), qq{ |
55 |
order by count($group_by) desc |
order by count($group_by) desc |
56 |
limit 10 |
limit 10 |
57 |
}); |
} |
58 |
|
); |
59 |
|
|
60 |
|
warn "# join SQL: $sql\n"; |
61 |
|
|
62 |
|
my $t = time(); |
63 |
|
my $sth = $dbh->prepare( $sql ); |
64 |
$sth->execute; |
$sth->execute; |
65 |
print header, qq|<table><tr><th>count</th><th>$group_by</th><tr>|; |
$t = time() - $t; |
66 |
|
print header, qq|$t<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|; |
67 |
while ( my @row = $sth->fetchrow_array ) { |
while ( my @row = $sth->fetchrow_array ) { |
68 |
my ( $n, $c ) = @row; |
my $n = shift @row; |
69 |
$n = 'NULL' unless defined $n; |
$n = 'NULL' unless defined $n; |
70 |
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>|; |
71 |
} |
} |
72 |
print qq|</table>|; |
print qq|</table>|; |
73 |
|
print qq|<code>$sql</code>|; |
74 |
exit; |
exit; |
75 |
} |
} |
76 |
|
|
177 |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
178 |
|
|
179 |
, qq|<label for=from>from</label>| |
, qq|<label for=from>from</label>| |
180 |
, textfield( -name => 'from', -value => $table, -default => 'log' ) |
, textfield( -name => 'from', -value => $table, -default => $table ) |
181 |
|
|
182 |
, qq|<label for=where>where</label>| |
, qq|<label for=where>where</label>| |
183 |
, checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] ) |
, checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] ) |
199 |
, textfield( -name => 'order_by' ) |
, textfield( -name => 'order_by' ) |
200 |
|
|
201 |
, qq|<label for=limit>limit</label>| |
, qq|<label for=limit>limit</label>| |
202 |
, textfield( -name=> 'limit', -default => 1000, -size => 4 ) |
, textfield( -name=> 'limit', -default => $limit, -size => 4 ) |
203 |
|
|
204 |
, qq|<label for=offset>offset</label>| |
, qq|<label for=offset>offset</label>| |
205 |
, textfield( -name=> 'offset', -default => 0, -size => 4 ) |
, textfield( -name=> 'offset', -default => 0, -size => 4 ) |