/[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 23 by dpavlin, Sat Apr 18 23:14:41 2009 UTC revision 29 by dpavlin, Tue Dec 8 20:03:35 2009 UTC
# Line 12  use Time::HiRes qw/time/; Line 12  use Time::HiRes qw/time/;
12  print qq{Content-type: text/html\r\n\r\n};  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 $database = ''; # if not in $dsn
16  our $user   = 'dpavlin';  our $user   = 'dpavlin';
17  our $table  = 'log';  our $table  = 'log';
18  our $limit  = 1000;  our $limit  = 1000;
19    our $passwd = '';
20    
21  our $group_by_join = {  our $group_by_join = {
22          feed_id => [ 'feeds', 'id', 'title', 'link', 'timestamp' ],          feed_id => [ 'feeds', 'id', 'title', 'link', 'timestamp' ],
# Line 30  my $offset = param('offset') || 0; Line 32  my $offset = param('offset') || 0;
32    
33  my @where_parts = param('where_parts');  my @where_parts = param('where_parts');
34    
35  my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;  my $dbh = DBI->connect( $dsn . $database, $user, $passwd, { RaiseError => 1 } ) || die $DBI::errstr;
36    
37  sub where_from_parts {  sub where_from_parts {
38          return unless @_;          return unless @_;
# Line 50  sub where_from_parts { Line 52  sub where_from_parts {
52          return @data;          return @data;
53  }  }
54    
55    sub sql_html {
56            my @d = @_;
57            my $sql_html = shift @d;
58            $sql_html =~ s{\?}{dump( shift @d )}ge;
59            return $sql_html;
60    }
61    
62  if ( my $group_by = param('lookup_col') ) {  if ( my $group_by = param('lookup_col') ) {
63    
64          my @cols = ( $group_by, "count($group_by)" );          my @cols = ( $group_by, "count($group_by)" );
# Line 76  if ( my $group_by = param('lookup_col') Line 85  if ( my $group_by = param('lookup_col')
85                  }, shift @data, # extract where                  }, shift @data, # extract where
86                  'group by', join(',', @group_by), qq{                  'group by', join(',', @group_by), qq{
87                  order by count($group_by) desc                  order by count($group_by) desc
88                  limit 10                  limit $limit
89                  }                  }
90          );          );
91    
# Line 86  if ( my $group_by = param('lookup_col') Line 95  if ( my $group_by = param('lookup_col')
95          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
96          $sth->execute( @data );          $sth->execute( @data );
97          $t = time() - $t;          $t = time() - $t;
98          print qq|$t<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|;          print qq|<code>|, sql_html( $sql, @data ), qq|<code>|;
99            print qq|<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|;
100          while ( my @row = $sth->fetchrow_array ) {          while ( my @row = $sth->fetchrow_array ) {
101                  my $n = shift @row;                  my $n = shift @row;
102                  $n = 'NULL' unless defined $n;                  $n = 'NULL' unless defined $n;
103                  print qq|<tr><td><a href="#">$n</a></td><td>|, join(qq|</td><td>|, @row), qq|</td></tr>|;                  print qq|<tr><td><a href="#">$n</a></td><td>|, join(qq|</td><td>|, @row), qq|</td></tr>|;
104          }          }
105          print qq|</table>|;          print qq|</table>|;
106          print qq|<code>$sql</code>|;          print $sth->rows, qq| rows in $t s|;
107          exit;          exit;
108  }  }
109    
# Line 136  $sql .= ' limit ? offset ?'; Line 146  $sql .= ' limit ? offset ?';
146    
147  push @data, ( $limit, $offset );  push @data, ( $limit, $offset );
148    
149  my $sql_html = $sql;  print qq|<code id="status">|, sql_html( $sql, @data ), qq|<br>\n|;
 {  
         my @d = @data;  
         $sql_html =~ s{\?}{dump( shift @d )}ge;  
 }  
 print qq|<code id="status">$sql_html<br>\n\r\n\r|;  
150    
151  my $t = time();  my $t = time();
152    
# Line 153  $t = time() - $t; Line 158  $t = time() - $t;
158    
159  print $sth->rows, qq| rows in $t s</code>|;  print $sth->rows, qq| rows in $t s</code>|;
160    
 #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>  
 };  
   
161  @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';  @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
162    
 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 );  
 }  
   
163  print  print
164          qq|</table>|            start_form( -id => 'sql', -class => 'fixed' )
         , start_form( -id => 'sql', -class => 'fixed' )  
165    
166          , qq|<input type=button value="[=]" onclick="\$('form#sql').toggleClass('fixed'); return false;" title="toggle fixed position" class="right">|          , qq|<input type=button value="[=]" onclick="\$('form#sql').toggleClass('fixed'); return false;" title="toggle fixed position" class="right">|
167          , qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="right">|          , qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="right">|
# Line 206  print Line 182  print
182                  <input type=button name=lookup_col title="lookup column details">                  <input type=button name=lookup_col title="lookup column details">
183                  <input type=button name=close_group_by value="[x]" disabled=1>                  <input type=button name=close_group_by value="[x]" disabled=1>
184                  <div id="lookup"></div>                  <div id="lookup"></div>
185                  <span>                  </span>
186          |          |
187    
188          , qq|<label for=group_by>group by</label>|          , qq|<label for=group_by>group by</label>|
# Line 224  print Line 200  print
200          , submit( -name => 'execute', -value => 'execute' )          , submit( -name => 'execute', -value => 'execute' )
201    
202          , end_form          , end_form
203            ;
204    
205    #my @types = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} };
206    my $types = dump( $sth->{TYPE} );
207    print qq{
208    <script type="text/javascript">
209    var column_type = $types ;
210    </script>
211    };
212    
213    print qq|<table id="results">|;
214    
215    my $counter = 0;
216    sub table_row {
217            my $cell = shift;
218            my $class = $counter++ % 2 == 0 ? ' class=o' : '';
219            return
220                      qq|<tr $class><$cell>|
221                    . join( qq|</$cell><$cell>|, @_ )
222                    . qq|</$cell></tr>|
223                    ;
224    
225    }
226    
227    print table_row( 'th', @columns );
228    
229    while ( my @row = $sth->fetchrow_array ) {
230            print table_row( 'td', @row );
231    }
232    
233    print qq|</table>|
234          , qq|</body></html>|          , qq|</body></html>|
235          ;          ;

Legend:
Removed from v.23  
changed lines
  Added in v.29

  ViewVC Help
Powered by ViewVC 1.1.26