/[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

Annotation of /index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations)
Mon Apr 13 17:55:51 2009 UTC (10 years, 6 months ago) by dpavlin
File size: 4378 byte(s)
added where_parts so you now can drill down in data
1 dpavlin 1 #!/usr/bin/perl
2    
3     use warnings;
4     use strict;
5    
6     use CGI qw/:standard/;
7     use CGI::Carp qw/fatalsToBrowser/; # FIXME remove for production
8     use DBI;
9     use Data::Dump qw/dump/;
10 dpavlin 2 use Time::HiRes qw/time/;
11 dpavlin 1
12     our $dsn = 'DBI:Pg:dbname=syslog';
13     our $user = 'dpavlin';
14    
15 dpavlin 2 require 'config.pl' if -e 'config.pl';
16    
17 dpavlin 1 my @columns = param('columns');
18     @columns = ('*') unless @columns;
19     my $table = param('table') || 'log';
20     my $limit = param('limit') || 1000;
21     my $offset = param('offset') || 0;
22    
23 dpavlin 2 my @where_parts = param('where_parts');
24    
25 dpavlin 1 print header, q|
26    
27     <html>
28     <head>
29 dpavlin 2 <title>SQL Web Session</title>
30 dpavlin 1 <link rel="stylesheet" type="text/css" href="style.css">
31     <!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js -->
32     <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
33     <script type="text/javascript">
34    
35     $(document).ready( function() {
36    
37     function click_on_cell(e) {
38    
39     var tag = e.originalTarget.tagName;
40    
41     var col_nr = e.originalTarget.cellIndex;
42    
43     var column = $('table#results th:nth-child(' + ( col_nr + 1 ) + ')').text();
44     var where_operator = '=';
45     var where_value = window.getSelection().getRangeAt(0).cloneContents().textContent;
46     if ( where_value.length == 0 )
47     where_value = e.originalTarget.textContent;
48     else
49     where_value = '%' + where_value + '%';
50    
51     console.debug('click on ', this, e,
52     e.originalTarget,
53     column, where_operator, where_value
54     );
55    
56     if ( tag == 'TH' ) {
57     console.info('header', column);
58 dpavlin 2 $('form#sql input[name=order_by]').attr('value', where_value);
59 dpavlin 1 } else if ( tag = 'TD' ) {
60     console.info('column', column, where_operator, where_value);
61     $('form#sql input[name=where_value]').attr('value', where_value);
62     $('form#sql select[name=where_column]').attr('options').selectedIndex = col_nr;
63     } else {
64     console.error('unknown click on ', tag, e);
65     }
66    
67 dpavlin 2 $('form#sql').addClass('visible');
68 dpavlin 1 };
69    
70     $('table#results').bind('mouseup', click_on_cell);
71    
72 dpavlin 2 $('#status').bind('click', function() {
73     $('form#sql').toggleClass('visible');
74     });
75    
76 dpavlin 1 console.info('ready');
77     });
78    
79     </script>
80     </head>
81     <body>
82     |;
83    
84     my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;
85    
86    
87     if ( param('where_operator') && length( param('where_value') ) > 0 ) {
88 dpavlin 2 my $where_value = param('where_value');
89     push @where_parts, param('where_column') . ' ' . param('where_operator') . " ?\t$where_value";
90     param('where_value','');
91 dpavlin 1 }
92    
93 dpavlin 2 my $c = join(',', @columns);
94 dpavlin 1
95 dpavlin 2 my $sql = "select $c from $table";
96     my @data;
97    
98     if ( @where_parts ) {
99     my @w;
100     foreach ( @where_parts ) {
101     my ( $w,$v ) = split(/\?\t/,$_,2);
102     push @w, "$w ?";
103     push @data, $v;
104     }
105     $sql .= ' where ' . join(' and ', @w);
106     }
107    
108     $sql .= ' order by ' . param('order_by') if param('order_by');
109     $sql .= ' limit ? offset ?';
110    
111     push @data, ( $limit, $offset );
112    
113     my $t = time();
114    
115 dpavlin 1 my $sth = $dbh->prepare( $sql );
116    
117     $sth->execute( @data );
118    
119 dpavlin 2 $t = time() - $t;
120    
121     my $sql_html = $sql;
122     $sql_html =~ s{\?}{dump( shift @data )}ge;
123    
124     print qq|<code id="status">$sql_html<br>|, $sth->rows, qq| rows in $t s</code>|;
125    
126    
127 dpavlin 1 @columns = @{ $sth->{NAME} };
128    
129     print qq|<table id="results">|;
130    
131     my $counter = 0;
132     sub table_row {
133     my $cell = shift;
134     my $class = $counter++ % 2 == 0 ? ' class=o' : '';
135     return
136     qq|<tr $class><$cell>|
137     . join( qq|</$cell><$cell>|, @_ )
138     . qq|</$cell></tr>|
139     ;
140    
141     }
142    
143     print table_row( 'th', @columns );
144    
145     while ( my @row = $sth->fetchrow_array ) {
146     print table_row( 'td', @row );
147     }
148    
149     print
150     qq|</table>|
151     , start_form( -id => 'sql' )
152    
153     , qq|<label for=columns>select</label>|
154 dpavlin 2 , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
155 dpavlin 1
156     , qq|<label for=from>from</label>|
157     , textfield( -name => 'from', -value => $table, -default => 'log' )
158    
159     , qq|<label for=where>where</label>|
160 dpavlin 2 , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] )
161 dpavlin 1 , popup_menu( -name => 'where_column', -values => [ @columns ] ),
162 dpavlin 2 , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ])
163 dpavlin 1 , textfield( -name => 'where_value' )
164    
165     , qq|<label for=order_by>order by</label>|
166 dpavlin 2 , textfield( -name => 'order_by' )
167 dpavlin 1
168     , qq|<label for=limit>limit</label>|
169     , textfield( -name=> 'limit', -default => 1000, -size => 4 )
170    
171     , qq|<label for=offset>offset</label>|
172     , textfield( -name=> 'offset', -default => 0, -size => 4 )
173    
174     , submit( -name => 'execute', -value => 'execute' )
175    
176     , end_form
177    
178    
179     , qq|</body></html>|
180     ;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26