/[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 17 by dpavlin, Sat Apr 18 12:08:07 2009 UTC revision 26 by dpavlin, Sat Apr 18 23:54:30 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';
17    our $limit  = 1000;
18    
19    our $group_by_join = {
20            feed_id => [ 'feeds', 'id', 'title', 'link', 'timestamp' ],
21    };
22    
23  require 'config.pl' if -e 'config.pl';  require 'config.pl' if -e 'config.pl';
24    
25  $table  = param('table') || $table;  $table  = param('table') || $table;
26  my @columns = param('columns');  my @columns = param('columns');
27  @columns = ('*') unless @columns;  @columns = ('*') unless @columns;
28  my $limit = param('limit') || 1000;  $limit = param('limit') || $limit;
29  my $offset = param('offset') || 0;  my $offset = param('offset') || 0;
30    
31  my @where_parts = param('where_parts');  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  if ( my $group_by = param('add_group_by') ) {  sub where_from_parts {
36          my $sth = $dbh->prepare(qq{          return unless @_;
37                  select $group_by,count($group_by)          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') ) {
54    
55            my @cols = ( $group_by, "count($group_by)" );
56            my @group_by = ( $group_by );
57    
58            my $join = '';
59            my @join = @{ $group_by_join->{$group_by} } if defined $group_by_join->{$group_by};
60            if ( @join ) {
61                    warn "## join ",dump( @join );
62                    my $join_table = shift @join;
63                    my $col = shift @join;
64                    $join = qq{ join $join_table on $table.$group_by = $join_table.$col };
65                    my @join_cols = map { $join_table . '.' . $_ } @join;
66                    push @cols, @join_cols;
67                    push @group_by, @join_cols;
68            }
69    
70            my @data = where_from_parts( @where_parts );
71    
72            my $sql = join("\n",
73                    'select', join(',', @cols), qq{
74                  from $table                  from $table
75                  group by $group_by                  $join
76                    }, 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          $sth->execute;          );
82          print header, qq|<table><tr><th>count</th><th>$group_by</th><tr>|;  
83            warn "# join SQL: $sql\n";
84    
85            my $t = time();
86            my $sth = $dbh->prepare( $sql );
87            $sth->execute( @data );
88            $t = time() - $t;
89            print qq|<code>$sql<code>|;
90            print qq|<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|;
91          while ( my @row = $sth->fetchrow_array ) {          while ( my @row = $sth->fetchrow_array ) {
92                  my ( $n, $c ) = @row;                  my $n = shift @row;
93                  $n = 'NULL' unless defined $n;                  $n = 'NULL' unless defined $n;
94                  print qq|<tr><td>$c</td><td><a href="#">$n</a></td></tr>|;                  print qq|<tr><td><a href="#">$n</a></td><td>|, join(qq|</td><td>|, @row), qq|</td></tr>|;
95          }          }
96          print qq|</table>|;          print qq|</table>|;
97            print $sth->rows, qq| rows in $t s|;
98          exit;          exit;
99  }  }
100    
101  print header, q|  print q|
102    
103  <html>  <html>
104  <head>  <head>
# Line 73  my $c = join(',', @columns); Line 127  my $c = join(',', @columns);
127  my $sql = "select $c from $table";  my $sql = "select $c from $table";
128  my @data;  my @data;
129    
130  if ( @where_parts ) {  my @where = where_from_parts( @where_parts );
131          my @w;  $sql .= shift @where;
132          foreach ( @where_parts ) {  push @data, @where;
                 my ( $w,$v ) = split(/\?\t/,$_,2);  
                 push @w, "$w ?";  
                 push @data, $v;  
         }  
         $sql .= ' where ' . join(' and ', @w);  
 }  
   
133    
134  $sql .= ' group by ' . $group_by if $group_by;  $sql .= ' group by ' . $group_by if $group_by;
135  $sql .= ' order by ' . param('order_by') if param('order_by');  $sql .= ' order by ' . param('order_by') if param('order_by');
# Line 107  $t = time() - $t; Line 154  $t = time() - $t;
154    
155  print $sth->rows, qq| rows in $t s</code>|;  print $sth->rows, qq| rows in $t s</code>|;
156    
 #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>  
 };  
   
157  @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';  @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
158    
 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 );  
 }  
   
159  print  print
160          qq|</table>|            start_form( -id => 'sql', -class => 'fixed' )
         , start_form( -id => 'sql' )  
161    
162          , qq|<a href="#" onclick="\$('form#sql').toggleClass('visible'); return false;" class=close title="hide sql editor">[x]</a>|          , qq|<input type=button value="[=]" onclick="\$('form#sql').toggleClass('fixed'); return false;" title="toggle fixed position" class="right">|
163            , qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="right">|
164    
165          , qq|<label for=columns>select</label>|          , qq|<label for=columns>select</label>|
166          , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )          , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
167    
168          , qq|<label for=from>from</label>|          , qq|<label for=from>from</label>|
169          , textfield( -name => 'from', -value => $table, -default => 'log' )          , textfield( -name => 'from', -value => $table, -default => $table )
170    
171          , qq|<label for=where>where</label>|          , qq|<label for=where>where</label>|
172          , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] )          , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] )
# Line 156  print Line 175  print
175          , textfield( -name => 'where_value' )          , textfield( -name => 'where_value' )
176          , qq|          , qq|
177                  <span>                  <span>
178                  <input type=button name=add_group_by>                  <input type=button name=lookup_col title="lookup column details">
179                  <input type=button name=close_group_by value="[x]" disabled=1>                  <input type=button name=close_group_by value="[x]" disabled=1>
180                  <div id="lookup"></div>                  <div id="lookup"></div>
181                  <span>                  </span>
182          |          |
183    
184          , qq|<label for=group_by>group by</label>|          , qq|<label for=group_by>group by</label>|
# Line 169  print Line 188  print
188          , textfield( -name => 'order_by' )          , textfield( -name => 'order_by' )
189    
190          , qq|<label for=limit>limit</label>|          , qq|<label for=limit>limit</label>|
191          , textfield( -name=> 'limit', -default => 1000, -size => 4 )          , textfield( -name=> 'limit', -default => $limit, -size => 4 )
192    
193          , qq|<label for=offset>offset</label>|          , qq|<label for=offset>offset</label>|
194          , textfield( -name=> 'offset', -default => 0, -size => 4 )          , textfield( -name=> 'offset', -default => 0, -size => 4 )
# Line 177  print Line 196  print
196          , submit( -name => 'execute', -value => 'execute' )          , submit( -name => 'execute', -value => 'execute' )
197    
198          , end_form          , end_form
199            ;
200    
201    #my @types = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} };
202    my $types = dump( $sth->{TYPE} );
203    print qq{
204    <script type="text/javascript">
205    var column_type = $types ;
206    </script>
207    };
208    
209    print qq|<table id="results">|;
210    
211    my $counter = 0;
212    sub table_row {
213            my $cell = shift;
214            my $class = $counter++ % 2 == 0 ? ' class=o' : '';
215            return
216                      qq|<tr $class><$cell>|
217                    . join( qq|</$cell><$cell>|, @_ )
218                    . qq|</$cell></tr>|
219                    ;
220    
221    }
222    
223    print table_row( 'th', @columns );
224    
225    while ( my @row = $sth->fetchrow_array ) {
226            print table_row( 'td', @row );
227    }
228    
229    print qq|</table>|
230          , qq|</body></html>|          , qq|</body></html>|
231          ;          ;

Legend:
Removed from v.17  
changed lines
  Added in v.26

  ViewVC Help
Powered by ViewVC 1.1.26