/[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 19 - (hide annotations)
Sat Apr 18 13:55:20 2009 UTC (10 years, 6 months ago) by dpavlin
File size: 5178 byte(s)
added joins for lookups by group_by
fixed defaults for form fields

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26