/[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 13 - (show annotations)
Wed Apr 15 15:26:38 2009 UTC (10 years, 2 months ago) by dpavlin
File size: 4162 byte(s)
fake firebug console.* methods instead of firebug lite
because it's much faster :-)

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>$n</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 @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
110
111 print qq|<table id="results">|;
112
113 my $counter = 0;
114 sub table_row {
115 my $cell = shift;
116 my $class = $counter++ % 2 == 0 ? ' class=o' : '';
117 return
118 qq|<tr $class><$cell>|
119 . join( qq|</$cell><$cell>|, @_ )
120 . qq|</$cell></tr>|
121 ;
122
123 }
124
125 print table_row( 'th', @columns );
126
127 while ( my @row = $sth->fetchrow_array ) {
128 print table_row( 'td', @row );
129 }
130
131 print
132 qq|</table>|
133 , start_form( -id => 'sql' )
134
135 , qq|<a href="#" onclick="\$('form#sql').toggleClass('visible'); return false;" class=close title="hide sql editor">[x]</a>|
136
137 , qq|<label for=columns>select</label>|
138 , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
139
140 , qq|<label for=from>from</label>|
141 , textfield( -name => 'from', -value => $table, -default => 'log' )
142
143 , qq|<label for=where>where</label>|
144 , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] )
145 , popup_menu( -name => 'where_column', -values => [ @columns ] ),
146 , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ])
147 , textfield( -name => 'where_value' )
148 , qq|
149 <span>
150 <input type=button name=add_group_by>
151 <input type=button name=close_group_by value="[x]" disabled=1>
152 <div id="lookup"></div>
153 <span>
154 |
155
156 , qq|<label for=group_by>group by</label>|
157 , textfield( -name => 'group_by' )
158
159 , qq|<label for=order_by>order by</label>|
160 , textfield( -name => 'order_by' )
161
162 , qq|<label for=limit>limit</label>|
163 , textfield( -name=> 'limit', -default => 1000, -size => 4 )
164
165 , qq|<label for=offset>offset</label>|
166 , textfield( -name=> 'offset', -default => 0, -size => 4 )
167
168 , submit( -name => 'execute', -value => 'execute' )
169
170 , end_form
171
172
173 , qq|</body></html>|
174 ;

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26