--- index.cgi 2009/04/13 14:26:46 1 +++ index.cgi 2009/04/15 15:26:38 13 @@ -7,89 +7,106 @@ use CGI::Carp qw/fatalsToBrowser/; # FIXME remove for production use DBI; use Data::Dump qw/dump/; +use Time::HiRes qw/time/; our $dsn = 'DBI:Pg:dbname=syslog'; our $user = 'dpavlin'; +require 'config.pl' if -e 'config.pl'; + +my $table = param('table') || 'log'; 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; my $offset = param('offset') || 0; +my @where_parts = param('where_parts'); + +my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; + +if ( my $group_by = param('add_group_by') ) { + my $sth = $dbh->prepare(qq{ + select $group_by,count($group_by) + from $table + group by $group_by + order by count($group_by) desc + limit 10 + }); + $sth->execute; + print header, qq||; + while ( my @row = $sth->fetchrow_array ) { + my ( $n, $c ) = @row; + $n = 'NULL' unless defined $n; + print qq||; + } + print qq|
count$group_by
$c$n
|; + exit; +} + print header, q| -SQL Session +SQL Web Session - - function click_on_cell(e) { + + +|; - var tag = e.originalTarget.tagName; +my $group_by = param('group_by'); - var col_nr = e.originalTarget.cellIndex; - - var column = $('table#results th:nth-child(' + ( col_nr + 1 ) + ')').text(); - var where_operator = '='; - var where_value = window.getSelection().getRangeAt(0).cloneContents().textContent; - if ( where_value.length == 0 ) - where_value = e.originalTarget.textContent; - else - where_value = '%' + where_value + '%'; - - console.debug('click on ', this, e, - e.originalTarget, - column, where_operator, where_value - ); - - if ( tag == 'TH' ) { - console.info('header', column); - } else if ( tag = 'TD' ) { - console.info('column', column, where_operator, where_value); - $('form#sql input[name=where_value]').attr('value', where_value); - $('form#sql select[name=where_column]').attr('options').selectedIndex = col_nr; - } else { - console.error('unknown click on ', tag, e); - } - - }; +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',''); +} - $('table#results').bind('mouseup', click_on_cell); - console.info('ready'); -}); +my $c = join(',', @columns); - - - -|; +my $sql = "select $c from $table"; +my @data; -my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; +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 @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'); +$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
|; + +@columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*'; print qq||; @@ -115,19 +132,32 @@ qq|
| , start_form( -id => 'sql' ) + , qq|[x]| + , 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' ) , 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 )