/[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 15 - (show annotations)
Wed Apr 15 18:42:41 2009 UTC (10 years, 4 months ago) by dpavlin
File size: 4375 byte(s)
make group by values clickable to refine where value

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26