/[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 32 - (hide annotations)
Tue Dec 8 20:08:29 2009 UTC (9 years, 8 months ago) by dpavlin
File size: 5973 byte(s)
added utf-8 charset and declare us html5 page

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26