/[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 5 - (show annotations)
Mon Apr 13 22:12:38 2009 UTC (10 years, 4 months ago) by dpavlin
File size: 5194 byte(s)
don't include order_by which we overwrite anyway
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 <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 $('form#sql input[name=order_by]').attr('value', where_value + ' desc');
59 } 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 $('form#sql input[name=add_group_by]').attr('value', column).css('display','block');
64 } else {
65 console.error('unknown click on ', tag, e);
66 }
67
68 $('form#sql').addClass('visible');
69 };
70
71 $('table#results').bind('mouseup', click_on_cell);
72
73 $('#status').bind('click', function() {
74 $('form#sql').toggleClass('visible');
75 });
76
77 console.info('ready');
78 });
79
80 </script>
81 </head>
82 <body>
83 |;
84
85 my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;
86
87
88 my $group_by = param('add_group_by');
89
90 if ( $group_by ) {
91 @columns = ( "count($group_by)", $group_by );
92 my $g;
93 foreach my $c ( @where_parts, $group_by ) {
94 $c =~ s/\s.+$//;
95 $g->{$c}++ if length($c) > 0;
96 }
97 $group_by = join( ',', keys %$g );
98 print "# $group_by g = ",dump( $g );
99 param('where_value','');
100 param('group_by', $group_by);
101 param('columns', [ @columns ], [ @columns ]);
102 param('order_by', $columns[0] . ' desc');
103 } else {
104 $group_by = param('group_by');
105 }
106
107
108 if ( param('where_operator') && length( param('where_value') ) > 0 ) {
109 my $where_value = param('where_value');
110 push @where_parts, param('where_column') . ' ' . param('where_operator') . " ?\t$where_value";
111 param('where_value','');
112 }
113
114
115 my $c = join(',', @columns);
116
117 my $sql = "select $c from $table";
118 my @data;
119
120 if ( @where_parts ) {
121 my @w;
122 foreach ( @where_parts ) {
123 my ( $w,$v ) = split(/\?\t/,$_,2);
124 push @w, "$w ?";
125 push @data, $v;
126 }
127 $sql .= ' where ' . join(' and ', @w);
128 }
129
130
131 $sql .= ' group by ' . $group_by if $group_by;
132 $sql .= ' order by ' . param('order_by') if param('order_by');
133 $sql .= ' limit ? offset ?';
134
135 push @data, ( $limit, $offset );
136
137 my $sql_html = $sql;
138 {
139 my @d = @data;
140 $sql_html =~ s{\?}{dump( shift @d )}ge;
141 }
142 print qq|<code id="status">$sql_html<br>\n\r\n\r|;
143
144 my $t = time();
145
146 my $sth = $dbh->prepare( $sql );
147
148 $sth->execute( @data );
149
150 $t = time() - $t;
151
152 print $sth->rows, qq| rows in $t s</code>|;
153
154
155 @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
156
157 print qq|<table id="results">|;
158
159 my $counter = 0;
160 sub table_row {
161 my $cell = shift;
162 my $class = $counter++ % 2 == 0 ? ' class=o' : '';
163 return
164 qq|<tr $class><$cell>|
165 . join( qq|</$cell><$cell>|, @_ )
166 . qq|</$cell></tr>|
167 ;
168
169 }
170
171 print table_row( 'th', @columns );
172
173 while ( my @row = $sth->fetchrow_array ) {
174 print table_row( 'td', @row );
175 }
176
177 print
178 qq|</table>|
179 , start_form( -id => 'sql' )
180
181 , qq|<label for=columns>select</label>|
182 , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
183
184 , qq|<label for=from>from</label>|
185 , textfield( -name => 'from', -value => $table, -default => 'log' )
186
187 , qq|<label for=where>where</label>|
188 , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] )
189 , popup_menu( -name => 'where_column', -values => [ @columns ] ),
190 , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ])
191 , textfield( -name => 'where_value' )
192
193 , qq|<label for=group_by>group by</label>|
194 , textfield( -name => 'group_by' )
195 , submit( -name => 'add_group_by' )
196
197 , qq|<label for=order_by>order by</label>|
198 , textfield( -name => 'order_by' )
199
200 , qq|<label for=limit>limit</label>|
201 , textfield( -name=> 'limit', -default => 1000, -size => 4 )
202
203 , qq|<label for=offset>offset</label>|
204 , textfield( -name=> 'offset', -default => 0, -size => 4 )
205
206 , submit( -name => 'execute', -value => 'execute' )
207
208 , end_form
209
210
211 , qq|</body></html>|
212 ;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26