--- index.cgi 2009/04/18 14:18:48 22 +++ index.cgi 2009/04/18 23:14:41 23 @@ -32,6 +32,24 @@ my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; +sub where_from_parts { + return unless @_; + my @where_parts = @_; + + warn "# where_from_parts ",dump( @where_parts ); + + my @w; + my @data; + foreach ( @where_parts ) { + my ( $w,$v ) = split(/\?\t/,$_,2); + push @w, "$w ?"; + push @data, $v; + } + unshift @data, ' where ' . join(' and ', @w); + warn "# ",dump( @data ); + return @data; +} + if ( my $group_by = param('lookup_col') ) { my @cols = ( $group_by, "count($group_by)" ); @@ -49,11 +67,14 @@ push @group_by, @join_cols; } + my @data = where_from_parts( @where_parts ); + my $sql = join("\n", 'select', join(',', @cols), qq{ from $table $join - }, 'group by', join(',', @group_by), qq{ + }, shift @data, # extract where + 'group by', join(',', @group_by), qq{ order by count($group_by) desc limit 10 } @@ -63,7 +84,7 @@ my $t = time(); my $sth = $dbh->prepare( $sql ); - $sth->execute; + $sth->execute( @data ); $t = time() - $t; print qq|$t|; while ( my @row = $sth->fetchrow_array ) { @@ -105,16 +126,9 @@ my $sql = "select $c from $table"; my @data; -if ( @where_parts ) { - my @w; - foreach ( @where_parts ) { - my ( $w,$v ) = split(/\?\t/,$_,2); - push @w, "$w ?"; - push @data, $v; - } - $sql .= ' where ' . join(' and ', @w); -} - +my @where = where_from_parts( @where_parts ); +$sql .= shift @where; +push @data, @where; $sql .= ' group by ' . $group_by if $group_by; $sql .= ' order by ' . param('order_by') if param('order_by');
|, join(qq||, @cols), qq|