/[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 4 by dpavlin, Mon Apr 13 21:26:43 2009 UTC revision 17 by dpavlin, Sat Apr 18 12:08:07 2009 UTC
# Line 11  use Time::HiRes qw/time/; Line 11  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    our $table  = 'log';
15    
16  require 'config.pl' if -e 'config.pl';  require 'config.pl' if -e 'config.pl';
17    
18    $table  = param('table') || $table;
19  my @columns = param('columns');  my @columns = param('columns');
20  @columns = ('*') unless @columns;  @columns = ('*') unless @columns;
 my $table = param('table') || 'log';  
21  my $limit = param('limit') || 1000;  my $limit = param('limit') || 1000;
22  my $offset = param('offset') || 0;  my $offset = param('offset') || 0;
23    
24  my @where_parts = param('where_parts');  my @where_parts = param('where_parts');
25    
26    my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;
27    
28    if ( my $group_by = param('add_group_by') ) {
29            my $sth = $dbh->prepare(qq{
30                    select $group_by,count($group_by)
31                    from $table
32                    group by $group_by
33                    order by count($group_by) desc
34                    limit 10
35            });
36            $sth->execute;
37            print header, qq|<table><tr><th>count</th><th>$group_by</th><tr>|;
38            while ( my @row = $sth->fetchrow_array ) {
39                    my ( $n, $c ) = @row;
40                    $n = 'NULL' unless defined $n;
41                    print qq|<tr><td>$c</td><td><a href="#">$n</a></td></tr>|;
42            }
43            print qq|</table>|;
44            exit;
45    }
46    
47  print header, q|  print header, q|
48    
49  <html>  <html>
# Line 30  print header, q| Line 52  print header, q|
52  <link rel="stylesheet" type="text/css" href="style.css">  <link rel="stylesheet" type="text/css" href="style.css">
53  <!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js -->  <!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js -->
54  <script type="text/javascript" src="jquery-1.3.2.min.js"></script>  <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
 <script type="text/javascript">  
   
 $(document).ready( function() {  
   
         function click_on_cell(e) {  
   
                 var tag = e.originalTarget.tagName;  
   
                 var col_nr = e.originalTarget.cellIndex;  
   
                 var column = $('table#results th:nth-child(' + ( col_nr + 1 ) + ')').text();  
                 var where_operator = '=';  
                 var where_value = window.getSelection().getRangeAt(0).cloneContents().textContent;  
                 if ( where_value.length == 0 )  
                         where_value = e.originalTarget.textContent;  
                 else  
                         where_value = '%' + where_value + '%';  
   
                 console.debug('click on ', this, e,  
                         e.originalTarget,  
                         column, where_operator, where_value  
                 );  
   
                 if ( tag == 'TH' ) {  
                         console.info('header', column);  
                         $('form#sql input[name=order_by]').attr('value', where_value + ' desc');  
                 } else if ( tag = 'TD' ) {  
                         console.info('column', column, where_operator, where_value);  
                         $('form#sql input[name=where_value]').attr('value', where_value);  
                         $('form#sql select[name=where_column]').attr('options').selectedIndex = col_nr;  
                         $('form#sql input[name=add_group_by]').attr('value', column).css('display','block');  
                 } else {  
                         console.error('unknown click on ', tag, e);  
                 }  
           
                 $('form#sql').addClass('visible');  
         };  
   
         $('table#results').bind('mouseup', click_on_cell);  
55    
56          $('#status').bind('click', function() {  <script type="text/javascript" src="sql-editor.js"></script>
                 $('form#sql').toggleClass('visible');  
         });  
   
         console.info('ready');  
 });  
57    
 </script>  
58  </head>  </head>
59  <body>  <body>
60  |;  |;
61    
62  my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;  my $group_by = param('group_by');
   
   
 my $group_by = param('add_group_by');  
   
 if ( $group_by ) {  
         @columns = ( "count($group_by)", $group_by );  
         my $g;  
         foreach my $c ( @where_parts, param('order_by'), $group_by ) {  
                 $c =~ s/\s.+$//;  
                 $g->{$c}++ if length($c) > 0;  
         }  
         $group_by = join( ',', keys %$g );  
         print "# $group_by g = ",dump( $g );  
         param('where_value','');  
         param('group_by', $group_by);  
         param('columns', [ @columns ], [ @columns ]);  
         param('order_by', $columns[0] . ' desc');  
 } else {  
         $group_by = param('group_by');  
 }  
           
63    
64  if ( param('where_operator') && length( param('where_value') ) > 0 ) {  if ( param('where_operator') && length( param('where_value') ) > 0 ) {
65          my $where_value = param('where_value');          my $where_value = param('where_value');
# Line 151  $t = time() - $t; Line 107  $t = time() - $t;
107    
108  print $sth->rows, qq| rows in $t s</code>|;  print $sth->rows, qq| rows in $t s</code>|;
109    
110    #my @types = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} };
111    my $types = dump( $sth->{TYPE} );
112    print qq{
113    <script type="text/javascript">
114    var column_type = $types ;
115    </script>
116    };
117    
118  @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';  @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
119    
# Line 178  print Line 141  print
141          qq|</table>|          qq|</table>|
142          , start_form( -id => 'sql' )          , start_form( -id => 'sql' )
143    
144            , qq|<a href="#" onclick="\$('form#sql').toggleClass('visible'); return false;" class=close title="hide sql editor">[x]</a>|
145    
146          , qq|<label for=columns>select</label>|          , qq|<label for=columns>select</label>|
147          , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )          , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
148    
# Line 189  print Line 154  print
154          , popup_menu( -name => 'where_column', -values => [ @columns ] ),          , popup_menu( -name => 'where_column', -values => [ @columns ] ),
155          , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ])          , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ])
156          , textfield( -name => 'where_value' )          , textfield( -name => 'where_value' )
157            , qq|
158                    <span>
159                    <input type=button name=add_group_by>
160                    <input type=button name=close_group_by value="[x]" disabled=1>
161                    <div id="lookup"></div>
162                    <span>
163            |
164    
165          , qq|<label for=group_by>group by</label>|          , qq|<label for=group_by>group by</label>|
166          , textfield( -name => 'group_by' )          , textfield( -name => 'group_by' )
         , submit( -name => 'add_group_by' )  
167    
168          , qq|<label for=order_by>order by</label>|          , qq|<label for=order_by>order by</label>|
169          , textfield( -name => 'order_by' )          , textfield( -name => 'order_by' )

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

  ViewVC Help
Powered by ViewVC 1.1.26