/[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 27 - (hide annotations)
Sat Apr 18 23:58:42 2009 UTC (10 years, 2 months ago) by dpavlin
File size: 5863 byte(s)
display SQL with embedded parametars using sql_html

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26