/[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 23 - (hide annotations)
Sat Apr 18 23:14:41 2009 UTC (10 years, 5 months ago) by dpavlin
File size: 5738 byte(s)
pass checked where filters into lookup

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 dpavlin 21 print qq{Content-type: text/html\r\n\r\n};
13    
14 dpavlin 1 our $dsn = 'DBI:Pg:dbname=syslog';
15     our $user = 'dpavlin';
16 dpavlin 17 our $table = 'log';
17 dpavlin 18 our $limit = 1000;
18 dpavlin 1
19 dpavlin 19 our $group_by_join = {
20     feed_id => [ 'feeds', 'id', 'title', 'link', 'timestamp' ],
21     };
22    
23 dpavlin 2 require 'config.pl' if -e 'config.pl';
24    
25 dpavlin 17 $table = param('table') || $table;
26 dpavlin 1 my @columns = param('columns');
27     @columns = ('*') unless @columns;
28 dpavlin 18 $limit = param('limit') || $limit;
29 dpavlin 1 my $offset = param('offset') || 0;
30    
31 dpavlin 2 my @where_parts = param('where_parts');
32    
33 dpavlin 10 my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;
34    
35 dpavlin 23 sub where_from_parts {
36     return unless @_;
37     my @where_parts = @_;
38    
39     warn "# where_from_parts ",dump( @where_parts );
40    
41     my @w;
42     my @data;
43     foreach ( @where_parts ) {
44     my ( $w,$v ) = split(/\?\t/,$_,2);
45     push @w, "$w ?";
46     push @data, $v;
47     }
48     unshift @data, ' where ' . join(' and ', @w);
49     warn "# ",dump( @data );
50     return @data;
51     }
52    
53 dpavlin 20 if ( my $group_by = param('lookup_col') ) {
54 dpavlin 19
55     my @cols = ( $group_by, "count($group_by)" );
56     my @group_by = ( $group_by );
57    
58     my $join = '';
59     my @join = @{ $group_by_join->{$group_by} } if defined $group_by_join->{$group_by};
60     if ( @join ) {
61     warn "## join ",dump( @join );
62     my $join_table = shift @join;
63     my $col = shift @join;
64     $join = qq{ join $join_table on $table.$group_by = $join_table.$col };
65     my @join_cols = map { $join_table . '.' . $_ } @join;
66     push @cols, @join_cols;
67     push @group_by, @join_cols;
68     }
69    
70 dpavlin 23 my @data = where_from_parts( @where_parts );
71    
72 dpavlin 19 my $sql = join("\n",
73     'select', join(',', @cols), qq{
74 dpavlin 10 from $table
75 dpavlin 19 $join
76 dpavlin 23 }, shift @data, # extract where
77     'group by', join(',', @group_by), qq{
78 dpavlin 10 order by count($group_by) desc
79     limit 10
80 dpavlin 19 }
81     );
82    
83     warn "# join SQL: $sql\n";
84    
85     my $t = time();
86     my $sth = $dbh->prepare( $sql );
87 dpavlin 23 $sth->execute( @data );
88 dpavlin 19 $t = time() - $t;
89 dpavlin 21 print qq|$t<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|;
90 dpavlin 10 while ( my @row = $sth->fetchrow_array ) {
91 dpavlin 19 my $n = shift @row;
92 dpavlin 10 $n = 'NULL' unless defined $n;
93 dpavlin 19 print qq|<tr><td><a href="#">$n</a></td><td>|, join(qq|</td><td>|, @row), qq|</td></tr>|;
94 dpavlin 10 }
95     print qq|</table>|;
96 dpavlin 19 print qq|<code>$sql</code>|;
97 dpavlin 10 exit;
98     }
99    
100 dpavlin 21 print q|
101 dpavlin 1
102     <html>
103     <head>
104 dpavlin 2 <title>SQL Web Session</title>
105 dpavlin 1 <link rel="stylesheet" type="text/css" href="style.css">
106     <!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js -->
107     <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
108 dpavlin 7
109 dpavlin 10 <script type="text/javascript" src="sql-editor.js"></script>
110 dpavlin 7
111 dpavlin 1 </head>
112     <body>
113     |;
114    
115 dpavlin 10 my $group_by = param('group_by');
116 dpavlin 1
117     if ( param('where_operator') && length( param('where_value') ) > 0 ) {
118 dpavlin 2 my $where_value = param('where_value');
119     push @where_parts, param('where_column') . ' ' . param('where_operator') . " ?\t$where_value";
120     param('where_value','');
121 dpavlin 1 }
122    
123 dpavlin 3
124 dpavlin 2 my $c = join(',', @columns);
125 dpavlin 1
126 dpavlin 2 my $sql = "select $c from $table";
127     my @data;
128    
129 dpavlin 23 my @where = where_from_parts( @where_parts );
130     $sql .= shift @where;
131     push @data, @where;
132 dpavlin 2
133 dpavlin 3 $sql .= ' group by ' . $group_by if $group_by;
134 dpavlin 2 $sql .= ' order by ' . param('order_by') if param('order_by');
135     $sql .= ' limit ? offset ?';
136    
137     push @data, ( $limit, $offset );
138    
139 dpavlin 3 my $sql_html = $sql;
140     {
141     my @d = @data;
142     $sql_html =~ s{\?}{dump( shift @d )}ge;
143     }
144     print qq|<code id="status">$sql_html<br>\n\r\n\r|;
145    
146 dpavlin 2 my $t = time();
147    
148 dpavlin 1 my $sth = $dbh->prepare( $sql );
149    
150     $sth->execute( @data );
151    
152 dpavlin 2 $t = time() - $t;
153    
154 dpavlin 3 print $sth->rows, qq| rows in $t s</code>|;
155 dpavlin 2
156 dpavlin 14 #my @types = map { scalar $dbh->type_info($_)->{TYPE_NAME} } @{ $sth->{TYPE} };
157     my $types = dump( $sth->{TYPE} );
158     print qq{
159     <script type="text/javascript">
160     var column_type = $types ;
161     </script>
162     };
163    
164 dpavlin 3 @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
165 dpavlin 2
166 dpavlin 1 print qq|<table id="results">|;
167    
168     my $counter = 0;
169     sub table_row {
170     my $cell = shift;
171     my $class = $counter++ % 2 == 0 ? ' class=o' : '';
172     return
173     qq|<tr $class><$cell>|
174     . join( qq|</$cell><$cell>|, @_ )
175     . qq|</$cell></tr>|
176     ;
177    
178     }
179    
180     print table_row( 'th', @columns );
181    
182     while ( my @row = $sth->fetchrow_array ) {
183     print table_row( 'td', @row );
184     }
185    
186     print
187     qq|</table>|
188 dpavlin 22 , start_form( -id => 'sql', -class => 'fixed' )
189 dpavlin 1
190 dpavlin 22 , qq|<input type=button value="[=]" onclick="\$('form#sql').toggleClass('fixed'); return false;" title="toggle fixed position" class="right">|
191     , qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="right">|
192 dpavlin 6
193 dpavlin 1 , qq|<label for=columns>select</label>|
194 dpavlin 2 , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
195 dpavlin 1
196     , qq|<label for=from>from</label>|
197 dpavlin 19 , textfield( -name => 'from', -value => $table, -default => $table )
198 dpavlin 1
199     , qq|<label for=where>where</label>|
200 dpavlin 2 , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] )
201 dpavlin 1 , popup_menu( -name => 'where_column', -values => [ @columns ] ),
202 dpavlin 2 , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ])
203 dpavlin 1 , textfield( -name => 'where_value' )
204 dpavlin 10 , qq|
205     <span>
206 dpavlin 20 <input type=button name=lookup_col title="lookup column details">
207 dpavlin 10 <input type=button name=close_group_by value="[x]" disabled=1>
208 dpavlin 12 <div id="lookup"></div>
209 dpavlin 10 <span>
210     |
211 dpavlin 1
212 dpavlin 3 , qq|<label for=group_by>group by</label>|
213     , textfield( -name => 'group_by' )
214    
215 dpavlin 1 , qq|<label for=order_by>order by</label>|
216 dpavlin 2 , textfield( -name => 'order_by' )
217 dpavlin 1
218     , qq|<label for=limit>limit</label>|
219 dpavlin 19 , textfield( -name=> 'limit', -default => $limit, -size => 4 )
220 dpavlin 1
221     , qq|<label for=offset>offset</label>|
222     , textfield( -name=> 'offset', -default => 0, -size => 4 )
223    
224     , submit( -name => 'execute', -value => 'execute' )
225    
226     , end_form
227    
228    
229     , qq|</body></html>|
230     ;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26