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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26