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

Contents of /index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations)
Tue Apr 14 17:01:09 2009 UTC (14 years, 11 months ago) by dpavlin
File size: 5546 byte(s)
use firebug lite because we use console.* calls

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 use Time::HiRes qw/time/;
11
12 our $dsn = 'DBI:Pg:dbname=syslog';
13 our $user = 'dpavlin';
14
15 require 'config.pl' if -e 'config.pl';
16
17 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 my @where_parts = param('where_parts');
24
25 print header, q|
26
27 <html>
28 <head>
29 <title>SQL Web Session</title>
30 <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
34 <!-- insert firebug lite because we use console.* all over -->
35 <script type="text/javascript" src="http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js"></script>
36 <script type="text/javascript">
37 firebug.env.height = 100;
38 </script>
39
40 <script type="text/javascript">
41
42 $(document).ready( function() {
43
44 function click_on_cell(e) {
45
46 var tag = e.originalTarget.tagName;
47
48 var col_nr = e.originalTarget.cellIndex;
49
50 var column = $('table#results th:nth-child(' + ( col_nr + 1 ) + ')').text();
51 var where_operator = '=';
52 var where_value = window.getSelection().getRangeAt(0).cloneContents().textContent;
53 if ( where_value.length == 0 )
54 where_value = e.originalTarget.textContent;
55 else
56 where_value = '%' + where_value + '%';
57
58 console.debug('click on ', this, e,
59 e.originalTarget,
60 column, where_operator, where_value
61 );
62
63 if ( tag == 'TH' ) {
64 console.info('header', column);
65 $('form#sql input[name=order_by]').attr('value', where_value + ' desc');
66 } else if ( tag = 'TD' ) {
67 console.info('column', column, where_operator, where_value);
68 $('form#sql input[name=where_value]').attr('value', where_value);
69 $('form#sql select[name=where_column]').attr('options').selectedIndex = col_nr;
70 $('form#sql input[name=add_group_by]').attr('value', column).css('display','block');
71 } else {
72 console.error('unknown click on ', tag, e);
73 }
74
75 $('form#sql').addClass('visible');
76 };
77
78 $('table#results').bind('mouseup', click_on_cell);
79
80 $('#status').bind('click', function() {
81 $('form#sql').toggleClass('visible');
82 });
83
84 console.info('ready');
85 });
86
87 </script>
88 </head>
89 <body>
90 |;
91
92 my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;
93
94
95 my $group_by = param('add_group_by');
96
97 if ( $group_by ) {
98 @columns = ( "count($group_by)", $group_by );
99 my $g;
100 foreach my $c ( @where_parts, $group_by ) {
101 $c =~ s/\s.+$//;
102 $g->{$c}++ if length($c) > 0;
103 }
104 $group_by = join( ',', keys %$g );
105 print "# $group_by g = ",dump( $g );
106 param('where_value','');
107 param('group_by', $group_by);
108 param('columns', [ @columns ], [ @columns ]);
109 param('order_by', $columns[0] . ' desc');
110 } else {
111 $group_by = param('group_by');
112 }
113
114
115 if ( param('where_operator') && length( param('where_value') ) > 0 ) {
116 my $where_value = param('where_value');
117 push @where_parts, param('where_column') . ' ' . param('where_operator') . " ?\t$where_value";
118 param('where_value','');
119 }
120
121
122 my $c = join(',', @columns);
123
124 my $sql = "select $c from $table";
125 my @data;
126
127 if ( @where_parts ) {
128 my @w;
129 foreach ( @where_parts ) {
130 my ( $w,$v ) = split(/\?\t/,$_,2);
131 push @w, "$w ?";
132 push @data, $v;
133 }
134 $sql .= ' where ' . join(' and ', @w);
135 }
136
137
138 $sql .= ' group by ' . $group_by if $group_by;
139 $sql .= ' order by ' . param('order_by') if param('order_by');
140 $sql .= ' limit ? offset ?';
141
142 push @data, ( $limit, $offset );
143
144 my $sql_html = $sql;
145 {
146 my @d = @data;
147 $sql_html =~ s{\?}{dump( shift @d )}ge;
148 }
149 print qq|<code id="status">$sql_html<br>\n\r\n\r|;
150
151 my $t = time();
152
153 my $sth = $dbh->prepare( $sql );
154
155 $sth->execute( @data );
156
157 $t = time() - $t;
158
159 print $sth->rows, qq| rows in $t s</code>|;
160
161
162 @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
163
164 print qq|<table id="results">|;
165
166 my $counter = 0;
167 sub table_row {
168 my $cell = shift;
169 my $class = $counter++ % 2 == 0 ? ' class=o' : '';
170 return
171 qq|<tr $class><$cell>|
172 . join( qq|</$cell><$cell>|, @_ )
173 . qq|</$cell></tr>|
174 ;
175
176 }
177
178 print table_row( 'th', @columns );
179
180 while ( my @row = $sth->fetchrow_array ) {
181 print table_row( 'td', @row );
182 }
183
184 print
185 qq|</table>|
186 , start_form( -id => 'sql' )
187
188 , qq|<a href="#" onclick="\$('form#sql').toggleClass('visible'); return false;" class=close>close</a>|
189
190 , qq|<label for=columns>select</label>|
191 , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
192
193 , qq|<label for=from>from</label>|
194 , textfield( -name => 'from', -value => $table, -default => 'log' )
195
196 , qq|<label for=where>where</label>|
197 , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] )
198 , popup_menu( -name => 'where_column', -values => [ @columns ] ),
199 , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ])
200 , textfield( -name => 'where_value' )
201
202 , qq|<label for=group_by>group by</label>|
203 , textfield( -name => 'group_by' )
204 , submit( -name => 'add_group_by' )
205
206 , qq|<label for=order_by>order by</label>|
207 , textfield( -name => 'order_by' )
208
209 , qq|<label for=limit>limit</label>|
210 , textfield( -name=> 'limit', -default => 1000, -size => 4 )
211
212 , qq|<label for=offset>offset</label>|
213 , textfield( -name=> 'offset', -default => 0, -size => 4 )
214
215 , submit( -name => 'execute', -value => 'execute' )
216
217 , end_form
218
219
220 , qq|</body></html>|
221 ;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26