/[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 21 - (hide annotations)
Sat Apr 18 14:08:52 2009 UTC (10 years, 2 months ago) by dpavlin
File size: 5246 byte(s)
send header without charset

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26