--- index.cgi 2009/04/13 14:26:46 1 +++ index.cgi 2009/04/18 14:18:48 22 @@ -7,89 +7,147 @@ use CGI::Carp qw/fatalsToBrowser/; # FIXME remove for production use DBI; use Data::Dump qw/dump/; +use Time::HiRes qw/time/; + +print qq{Content-type: text/html\r\n\r\n}; our $dsn = 'DBI:Pg:dbname=syslog'; our $user = 'dpavlin'; +our $table = 'log'; +our $limit = 1000; + +our $group_by_join = { + feed_id => [ 'feeds', 'id', 'title', 'link', 'timestamp' ], +}; + +require 'config.pl' if -e 'config.pl'; +$table = param('table') || $table; my @columns = param('columns'); @columns = ('*') unless @columns; -my $table = param('table') || 'log'; -my $order_by = param('order_by') || 'timestamp desc'; -my $limit = param('limit') || 1000; +$limit = param('limit') || $limit; my $offset = param('offset') || 0; -print header, q| +my @where_parts = param('where_parts'); - - -SQL Session - - - - + + - |; -my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; +my $group_by = param('group_by'); + +if ( param('where_operator') && length( param('where_value') ) > 0 ) { + my $where_value = param('where_value'); + push @where_parts, param('where_column') . ' ' . param('where_operator') . " ?\t$where_value"; + param('where_value',''); +} -my @data = ( $limit, $offset ); my $c = join(',', @columns); -my $sql = "select $c from $table "; -if ( param('where_operator') && length( param('where_value') ) > 0 ) { - $sql .= " where " . param('where_column') . ' ' . param('where_operator') . ' ?'; - unshift @data, param('where_value'); + +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); +} + + +$sql .= ' group by ' . $group_by if $group_by; +$sql .= ' order by ' . param('order_by') if param('order_by'); +$sql .= ' limit ? offset ?'; + +push @data, ( $limit, $offset ); + +my $sql_html = $sql; +{ + my @d = @data; + $sql_html =~ s{\?}{dump( shift @d )}ge; } -$sql .= " limit ? offset ?"; +print qq|$sql_html
\n\r\n\r|; -print qq|$sql
|, dump( @data ), qq|
|; +my $t = time(); my $sth = $dbh->prepare( $sql ); $sth->execute( @data ); -@columns = @{ $sth->{NAME} }; +$t = time() - $t; + +print $sth->rows, qq| rows in $t s
|; + +#my @types = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} }; +my $types = dump( $sth->{TYPE} ); +print qq{ + +}; + +@columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*'; print qq||; @@ -113,24 +171,38 @@ print qq|
| - , start_form( -id => 'sql' ) + , start_form( -id => 'sql', -class => 'fixed' ) + + , qq|| + , qq|| , qq|| - , checkbox_group( -name => 'columns', -values => [ @columns ], -default => [ @columns ] ) + , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) , qq|| - , textfield( -name => 'from', -value => $table, -default => 'log' ) + , textfield( -name => 'from', -value => $table, -default => $table ) , qq|| + , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] ) , popup_menu( -name => 'where_column', -values => [ @columns ] ), - , popup_menu( -name => 'where_operator', -values => [ 'like', 'not like', '=', '!=' ]) + , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ]) , textfield( -name => 'where_value' ) + , qq| + + + +
+ + | + + , qq|| + , textfield( -name => 'group_by' ) , qq|| - , textfield( -name => 'order_by' -value => $order_by ) + , textfield( -name => 'order_by' ) , qq|| - , textfield( -name=> 'limit', -default => 1000, -size => 4 ) + , textfield( -name=> 'limit', -default => $limit, -size => 4 ) , qq|| , textfield( -name=> 'offset', -default => 0, -size => 4 )