/[sql-web-session]/index.cgi
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /index.cgi

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by dpavlin, Mon Apr 13 14:26:46 2009 UTC revision 2 by dpavlin, Mon Apr 13 17:55:51 2009 UTC
# Line 7  use CGI qw/:standard/; Line 7  use CGI qw/:standard/;
7  use CGI::Carp qw/fatalsToBrowser/; # FIXME remove for production  use CGI::Carp qw/fatalsToBrowser/; # FIXME remove for production
8  use DBI;  use DBI;
9  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
10    use Time::HiRes qw/time/;
11    
12  our $dsn    = 'DBI:Pg:dbname=syslog';  our $dsn    = 'DBI:Pg:dbname=syslog';
13  our $user   = 'dpavlin';  our $user   = 'dpavlin';
14    
15    require 'config.pl' if -e 'config.pl';
16    
17  my @columns = param('columns');  my @columns = param('columns');
18  @columns = ('*') unless @columns;  @columns = ('*') unless @columns;
19  my $table = param('table') || 'log';  my $table = param('table') || 'log';
 my $order_by = param('order_by') || 'timestamp desc';  
20  my $limit = param('limit') || 1000;  my $limit = param('limit') || 1000;
21  my $offset = param('offset') || 0;  my $offset = param('offset') || 0;
22    
23    my @where_parts = param('where_parts');
24    
25  print header, q|  print header, q|
26    
27  <html>  <html>
28  <head>  <head>
29  <title>SQL Session</title>  <title>SQL Web Session</title>
30  <link rel="stylesheet" type="text/css" href="style.css">  <link rel="stylesheet" type="text/css" href="style.css">
31  <!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js -->  <!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js -->
32  <script type="text/javascript" src="jquery-1.3.2.min.js"></script>  <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
# Line 51  $(document).ready( function() { Line 55  $(document).ready( function() {
55    
56                  if ( tag == 'TH' ) {                  if ( tag == 'TH' ) {
57                          console.info('header', column);                          console.info('header', column);
58                            $('form#sql input[name=order_by]').attr('value', where_value);
59                  } else if ( tag = 'TD' ) {                  } else if ( tag = 'TD' ) {
60                          console.info('column', column, where_operator, where_value);                          console.info('column', column, where_operator, where_value);
61                          $('form#sql input[name=where_value]').attr('value', where_value);                          $('form#sql input[name=where_value]').attr('value', where_value);
# Line 59  $(document).ready( function() { Line 64  $(document).ready( function() {
64                          console.error('unknown click on ', tag, e);                          console.error('unknown click on ', tag, e);
65                  }                  }
66                    
67                    $('form#sql').addClass('visible');
68          };          };
69    
70          $('table#results').bind('mouseup', click_on_cell);          $('table#results').bind('mouseup', click_on_cell);
71    
72            $('#status').bind('click', function() {
73                    $('form#sql').toggleClass('visible');
74            });
75    
76          console.info('ready');          console.info('ready');
77  });  });
78    
# Line 73  $(document).ready( function() { Line 83  $(document).ready( function() {
83    
84  my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;  my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;
85    
 my @data = ( $limit, $offset );  
86    
 my $c = join(',', @columns);  
 my $sql = "select $c from $table ";  
87  if ( param('where_operator') && length( param('where_value') ) > 0 ) {  if ( param('where_operator') && length( param('where_value') ) > 0 ) {
88          $sql .= " where " . param('where_column') . ' ' . param('where_operator') . ' ?';          my $where_value = param('where_value');
89          unshift @data, param('where_value');          push @where_parts, param('where_column') . ' ' . param('where_operator') . " ?\t$where_value";
90            param('where_value','');
91  }  }
 $sql .= " limit ? offset ?";  
92    
93  print qq|<code>$sql<br>|, dump( @data ), qq|</code>|;  my $c = join(',', @columns);
94    
95    my $sql = "select $c from $table";
96    my @data;
97    
98    if ( @where_parts ) {
99            my @w;
100            foreach ( @where_parts ) {
101                    my ( $w,$v ) = split(/\?\t/,$_,2);
102                    push @w, "$w ?";
103                    push @data, $v;
104            }
105            $sql .= ' where ' . join(' and ', @w);
106    }
107    
108    $sql .= ' order by ' . param('order_by') if param('order_by');
109    $sql .= ' limit ? offset ?';
110    
111    push @data, ( $limit, $offset );
112    
113    my $t = time();
114    
115  my $sth = $dbh->prepare( $sql );  my $sth = $dbh->prepare( $sql );
116    
117  $sth->execute( @data );  $sth->execute( @data );
118    
119    $t = time() - $t;
120    
121    my $sql_html = $sql;
122    $sql_html =~ s{\?}{dump( shift @data )}ge;
123    
124    print qq|<code id="status">$sql_html<br>|, $sth->rows, qq| rows in $t s</code>|;
125    
126    
127  @columns = @{ $sth->{NAME} };  @columns = @{ $sth->{NAME} };
128    
129  print qq|<table id="results">|;  print qq|<table id="results">|;
# Line 116  print Line 151  print
151          , start_form( -id => 'sql' )          , start_form( -id => 'sql' )
152    
153          , qq|<label for=columns>select</label>|          , qq|<label for=columns>select</label>|
154          , checkbox_group( -name => 'columns', -values => [ @columns ], -default => [ @columns ] )          , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
155    
156          , qq|<label for=from>from</label>|          , qq|<label for=from>from</label>|
157          , textfield( -name => 'from', -value => $table, -default => 'log' )          , textfield( -name => 'from', -value => $table, -default => 'log' )
158    
159          , qq|<label for=where>where</label>|          , qq|<label for=where>where</label>|
160            , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] )
161          , popup_menu( -name => 'where_column', -values => [ @columns ] ),          , popup_menu( -name => 'where_column', -values => [ @columns ] ),
162          , popup_menu( -name => 'where_operator', -values => [ 'like', 'not like', '=', '!=' ])          , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ])
163          , textfield( -name => 'where_value' )          , textfield( -name => 'where_value' )
164    
165          , qq|<label for=order_by>order by</label>|          , qq|<label for=order_by>order by</label>|
166          , textfield( -name => 'order_by' -value => $order_by )          , textfield( -name => 'order_by' )
167    
168          , qq|<label for=limit>limit</label>|          , qq|<label for=limit>limit</label>|
169          , textfield( -name=> 'limit', -default => 1000, -size => 4 )          , textfield( -name=> 'limit', -default => 1000, -size => 4 )

Legend:
Removed from v.1  
changed lines
  Added in v.2

  ViewVC Help
Powered by ViewVC 1.1.26