/[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 32 by dpavlin, Tue Dec 8 20:08:29 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 $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 28  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 {
38            return unless @_;
39            my @where_parts = @_;
40    
41            warn "# where_from_parts ",dump( @where_parts );
42    
43            my @w;
44            my @data;
45            foreach ( @where_parts ) {
46                    my ( $w,$v ) = split(/\?\t/,$_,2);
47                    push @w, "$w ?";
48                    push @data, $v;
49            }
50            unshift @data, ' where ' . join(' and ', @w);
51            warn "# ",dump( @data );
52            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    
# Line 47  if ( my $group_by = param('lookup_col') Line 76  if ( my $group_by = param('lookup_col')
76                  push @group_by, @join_cols;                  push @group_by, @join_cols;
77          }          }
78    
79            my @data = where_from_parts( @where_parts );
80    
81          my $sql = join("\n",          my $sql = join("\n",
82                  'select', join(',', @cols), qq{                  'select', join(',', @cols), qq{
83                  from $table                  from $table
84                  $join                  $join
85                  }, 'group by', join(',', @group_by), qq{                  }, shift @data, # extract where
86                    '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 61  if ( my $group_by = param('lookup_col') Line 93  if ( my $group_by = param('lookup_col')
93    
94          my $t = time();          my $t = time();
95          my $sth = $dbh->prepare( $sql );          my $sth = $dbh->prepare( $sql );
96          $sth->execute;          $sth->execute( @data );
97          $t = time() - $t;          $t = time() - $t;
98          print header, 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    
110  print header, q|  print q|
111    <!DOCTYPE html>
112  <html>  <html>
113  <head>  <head>
114    <meta charset="utf-8">
115  <title>SQL Web Session</title>  <title>SQL Web Session</title>
116  <link rel="stylesheet" type="text/css" href="style.css">  <link rel="stylesheet" type="text/css" href="style.css">
117  <!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js -->  <!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js -->
# Line 103  my $c = join(',', @columns); Line 137  my $c = join(',', @columns);
137  my $sql = "select $c from $table";  my $sql = "select $c from $table";
138  my @data;  my @data;
139    
140  if ( @where_parts ) {  my @where = where_from_parts( @where_parts );
141          my @w;  $sql .= shift @where;
142          foreach ( @where_parts ) {  push @data, @where;
                 my ( $w,$v ) = split(/\?\t/,$_,2);  
                 push @w, "$w ?";  
                 push @data, $v;  
         }  
         $sql .= ' where ' . join(' and ', @w);  
 }  
   
143    
144  $sql .= ' group by ' . $group_by if $group_by;  $sql .= ' group by ' . $group_by if $group_by;
145  $sql .= ' order by ' . param('order_by') if param('order_by');  $sql .= ' order by ' . param('order_by') if param('order_by');
# Line 120  $sql .= ' limit ? offset ?'; Line 147  $sql .= ' limit ? offset ?';
147    
148  push @data, ( $limit, $offset );  push @data, ( $limit, $offset );
149    
150  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|;  
151    
152  my $t = time();  my $t = time();
153    
# Line 137  $t = time() - $t; Line 159  $t = time() - $t;
159    
160  print $sth->rows, qq| rows in $t s</code>|;  print $sth->rows, qq| rows in $t s</code>|;
161    
 #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>  
 };  
   
162  @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';  @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
163    
 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 );  
 }  
   
164  print  print
165          qq|</table>|            start_form( -id => 'sql', -class => 'fixed' )
         , start_form( -id => 'sql' )  
166    
167          , 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">|
168            , qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="right">|
169    
170          , qq|<label for=columns>select</label>|          , qq|<label for=columns>select</label>|
171          , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )          , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
# Line 189  print Line 183  print
183                  <input type=button name=lookup_col title="lookup column details">                  <input type=button name=lookup_col title="lookup column details">
184                  <input type=button name=close_group_by value="[x]" disabled=1>                  <input type=button name=close_group_by value="[x]" disabled=1>
185                  <div id="lookup"></div>                  <div id="lookup"></div>
186                  <span>                  </span>
187          |          |
188    
189          , qq|<label for=group_by>group by</label>|          , qq|<label for=group_by>group by</label>|
# Line 207  print Line 201  print
201          , submit( -name => 'execute', -value => 'execute' )          , submit( -name => 'execute', -value => 'execute' )
202    
203          , end_form          , end_form
204            ;
205    
206    #my @types = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} };
207    my $types = dump( $sth->{TYPE} );
208    print qq{
209    <script type="text/javascript">
210    var column_type = $types ;
211    </script>
212    };
213    
214    print qq|<table id="results">|;
215    
216    my $counter = 0;
217    sub table_row {
218            my $cell = shift;
219            my $class = $counter++ % 2 == 0 ? ' class=o' : '';
220            return
221                      qq|<tr $class><$cell>|
222                    . join( qq|</$cell><$cell>|, @_ )
223                    . qq|</$cell></tr>|
224                    ;
225    
226    }
227    
228    print table_row( 'th', @columns );
229    
230    while ( my @row = $sth->fetchrow_array ) {
231            print table_row( 'td', @row );
232    }
233    
234    print qq|</table>|
235          , qq|</body></html>|          , qq|</body></html>|
236          ;          ;

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

  ViewVC Help
Powered by ViewVC 1.1.26