/[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 20 by dpavlin, Sat Apr 18 14:05:13 2009 UTC revision 25 by dpavlin, Sat Apr 18 23:35:57 2009 UTC
# Line 9  use DBI; Line 9  use DBI;
9  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
10  use Time::HiRes qw/time/;  use Time::HiRes qw/time/;
11    
12    print qq{Content-type: text/html\r\n\r\n};
13    
14  our $dsn    = 'DBI:Pg:dbname=syslog';  our $dsn    = 'DBI:Pg:dbname=syslog';
15  our $user   = 'dpavlin';  our $user   = 'dpavlin';
16  our $table  = 'log';  our $table  = 'log';
# Line 30  my @where_parts = param('where_parts'); Line 32  my @where_parts = param('where_parts');
32    
33  my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;  my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;
34    
35    sub where_from_parts {
36            return unless @_;
37            my @where_parts = @_;
38    
39            warn "# where_from_parts ",dump( @where_parts );
40    
41            my @w;
42            my @data;
43            foreach ( @where_parts ) {
44                    my ( $w,$v ) = split(/\?\t/,$_,2);
45                    push @w, "$w ?";
46                    push @data, $v;
47            }
48            unshift @data, ' where ' . join(' and ', @w);
49            warn "# ",dump( @data );
50            return @data;
51    }
52    
53  if ( my $group_by = param('lookup_col') ) {  if ( my $group_by = param('lookup_col') ) {
54    
55          my @cols = ( $group_by, "count($group_by)" );          my @cols = ( $group_by, "count($group_by)" );
# Line 47  if ( my $group_by = param('lookup_col') Line 67  if ( my $group_by = param('lookup_col')
67                  push @group_by, @join_cols;                  push @group_by, @join_cols;
68          }          }
69    
70            my @data = where_from_parts( @where_parts );
71    
72          my $sql = join("\n",          my $sql = join("\n",
73                  'select', join(',', @cols), qq{                  'select', join(',', @cols), qq{
74                  from $table                  from $table
75                  $join                  $join
76                  }, 'group by', join(',', @group_by), qq{                  }, shift @data, # extract where
77                    'group by', join(',', @group_by), qq{
78                  order by count($group_by) desc                  order by count($group_by) desc
79                  limit 10                  limit $limit
80                  }                  }
81          );          );
82    
# Line 61  if ( my $group_by = param('lookup_col') Line 84  if ( my $group_by = param('lookup_col')
84    
85          my $t = time();          my $t = time();
86          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
87          $sth->execute;          $sth->execute( @data );
88          $t = time() - $t;          $t = time() - $t;
89          print header, qq|$t<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|;          print qq|$t<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|;
90          while ( my @row = $sth->fetchrow_array ) {          while ( my @row = $sth->fetchrow_array ) {
91                  my $n = shift @row;                  my $n = shift @row;
92                  $n = 'NULL' unless defined $n;                  $n = 'NULL' unless defined $n;
# Line 74  if ( my $group_by = param('lookup_col') Line 97  if ( my $group_by = param('lookup_col')
97          exit;          exit;
98  }  }
99    
100  print header, q|  print q|
101    
102  <html>  <html>
103  <head>  <head>
# Line 103  my $c = join(',', @columns); Line 126  my $c = join(',', @columns);
126  my $sql = "select $c from $table";  my $sql = "select $c from $table";
127  my @data;  my @data;
128    
129  if ( @where_parts ) {  my @where = where_from_parts( @where_parts );
130          my @w;  $sql .= shift @where;
131          foreach ( @where_parts ) {  push @data, @where;
                 my ( $w,$v ) = split(/\?\t/,$_,2);  
                 push @w, "$w ?";  
                 push @data, $v;  
         }  
         $sql .= ' where ' . join(' and ', @w);  
 }  
   
132    
133  $sql .= ' group by ' . $group_by if $group_by;  $sql .= ' group by ' . $group_by if $group_by;
134  $sql .= ' order by ' . param('order_by') if param('order_by');  $sql .= ' order by ' . param('order_by') if param('order_by');
# Line 137  $t = time() - $t; Line 153  $t = time() - $t;
153    
154  print $sth->rows, qq| rows in $t s</code>|;  print $sth->rows, qq| rows in $t s</code>|;
155    
 #my @types = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} };  
 my $types = dump( $sth->{TYPE} );  
 print qq{  
 <script type="text/javascript">  
 var column_type = $types ;  
 </script>  
 };  
   
156  @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';  @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
157    
 print qq|<table id="results">|;  
   
 my $counter = 0;  
 sub table_row {  
         my $cell = shift;  
         my $class = $counter++ % 2 == 0 ? ' class=o' : '';  
         return  
                   qq|<tr $class><$cell>|  
                 . join( qq|</$cell><$cell>|, @_ )  
                 . qq|</$cell></tr>|  
                 ;  
   
 }  
   
 print table_row( 'th', @columns );  
   
 while ( my @row = $sth->fetchrow_array ) {  
         print table_row( 'td', @row );  
 }  
   
158  print  print
159          qq|</table>|            start_form( -id => 'sql', -class => 'fixed' )
         , start_form( -id => 'sql' )  
160    
161          , qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="close">|          , qq|<input type=button value="[=]" onclick="\$('form#sql').toggleClass('fixed'); return false;" title="toggle fixed position" class="right">|
162            , qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="right">|
163    
164          , qq|<label for=columns>select</label>|          , qq|<label for=columns>select</label>|
165          , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )          , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
# Line 189  print Line 177  print
177                  <input type=button name=lookup_col title="lookup column details">                  <input type=button name=lookup_col title="lookup column details">
178                  <input type=button name=close_group_by value="[x]" disabled=1>                  <input type=button name=close_group_by value="[x]" disabled=1>
179                  <div id="lookup"></div>                  <div id="lookup"></div>
180                  <span>                  </span>
181          |          |
182    
183          , qq|<label for=group_by>group by</label>|          , qq|<label for=group_by>group by</label>|
# Line 207  print Line 195  print
195          , submit( -name => 'execute', -value => 'execute' )          , submit( -name => 'execute', -value => 'execute' )
196    
197          , end_form          , end_form
198            ;
199    
200    #my @types = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} };
201    my $types = dump( $sth->{TYPE} );
202    print qq{
203    <script type="text/javascript">
204    var column_type = $types ;
205    </script>
206    };
207    
208    print qq|<table id="results">|;
209    
210    my $counter = 0;
211    sub table_row {
212            my $cell = shift;
213            my $class = $counter++ % 2 == 0 ? ' class=o' : '';
214            return
215                      qq|<tr $class><$cell>|
216                    . join( qq|</$cell><$cell>|, @_ )
217                    . qq|</$cell></tr>|
218                    ;
219    
220    }
221    
222    print table_row( 'th', @columns );
223    
224    while ( my @row = $sth->fetchrow_array ) {
225            print table_row( 'td', @row );
226    }
227    
228    print qq|</table>|
229          , qq|</body></html>|          , qq|</body></html>|
230          ;          ;

Legend:
Removed from v.20  
changed lines
  Added in v.25

  ViewVC Help
Powered by ViewVC 1.1.26