/[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

Contents of /index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 20 - (show annotations)
Sat Apr 18 14:05:13 2009 UTC (10 years, 2 months ago) by dpavlin
File size: 5218 byte(s)
rename add_group_by into lookup_col

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 use Time::HiRes qw/time/;
11
12 our $dsn = 'DBI:Pg:dbname=syslog';
13 our $user = 'dpavlin';
14 our $table = 'log';
15 our $limit = 1000;
16
17 our $group_by_join = {
18 feed_id => [ 'feeds', 'id', 'title', 'link', 'timestamp' ],
19 };
20
21 require 'config.pl' if -e 'config.pl';
22
23 $table = param('table') || $table;
24 my @columns = param('columns');
25 @columns = ('*') unless @columns;
26 $limit = param('limit') || $limit;
27 my $offset = param('offset') || 0;
28
29 my @where_parts = param('where_parts');
30
31 my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr;
32
33 if ( my $group_by = param('lookup_col') ) {
34
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 from $table
53 $join
54 }, 'group by', join(',', @group_by), qq{
55 order by count($group_by) desc
56 limit 10
57 }
58 );
59
60 warn "# join SQL: $sql\n";
61
62 my $t = time();
63 my $sth = $dbh->prepare( $sql );
64 $sth->execute;
65 $t = time() - $t;
66 print header, qq|$t<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|;
67 while ( my @row = $sth->fetchrow_array ) {
68 my $n = shift @row;
69 $n = 'NULL' unless defined $n;
70 print qq|<tr><td><a href="#">$n</a></td><td>|, join(qq|</td><td>|, @row), qq|</td></tr>|;
71 }
72 print qq|</table>|;
73 print qq|<code>$sql</code>|;
74 exit;
75 }
76
77 print header, q|
78
79 <html>
80 <head>
81 <title>SQL Web Session</title>
82 <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
86 <script type="text/javascript" src="sql-editor.js"></script>
87
88 </head>
89 <body>
90 |;
91
92 my $group_by = param('group_by');
93
94 if ( param('where_operator') && length( param('where_value') ) > 0 ) {
95 my $where_value = param('where_value');
96 push @where_parts, param('where_column') . ' ' . param('where_operator') . " ?\t$where_value";
97 param('where_value','');
98 }
99
100
101 my $c = join(',', @columns);
102
103 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
117 $sql .= ' group by ' . $group_by if $group_by;
118 $sql .= ' order by ' . param('order_by') if param('order_by');
119 $sql .= ' limit ? offset ?';
120
121 push @data, ( $limit, $offset );
122
123 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 my $t = time();
131
132 my $sth = $dbh->prepare( $sql );
133
134 $sth->execute( @data );
135
136 $t = time() - $t;
137
138 print $sth->rows, qq| rows in $t s</code>|;
139
140 #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 @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
149
150 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 , qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="close">|
175
176 , qq|<label for=columns>select</label>|
177 , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
178
179 , qq|<label for=from>from</label>|
180 , textfield( -name => 'from', -value => $table, -default => $table )
181
182 , qq|<label for=where>where</label>|
183 , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] )
184 , popup_menu( -name => 'where_column', -values => [ @columns ] ),
185 , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ])
186 , textfield( -name => 'where_value' )
187 , qq|
188 <span>
189 <input type=button name=lookup_col title="lookup column details">
190 <input type=button name=close_group_by value="[x]" disabled=1>
191 <div id="lookup"></div>
192 <span>
193 |
194
195 , qq|<label for=group_by>group by</label>|
196 , textfield( -name => 'group_by' )
197
198 , qq|<label for=order_by>order by</label>|
199 , textfield( -name => 'order_by' )
200
201 , qq|<label for=limit>limit</label>|
202 , textfield( -name=> 'limit', -default => $limit, -size => 4 )
203
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