/[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 18 - (show annotations)
Sat Apr 18 12:13:40 2009 UTC (14 years, 11 months ago) by dpavlin
File size: 4414 byte(s)
make $limit also configurable in config.pl

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26