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

Annotation of /index.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (hide 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 dpavlin 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 dpavlin 2 use Time::HiRes qw/time/;
11 dpavlin 1
12     our $dsn = 'DBI:Pg:dbname=syslog';
13     our $user = 'dpavlin';
14 dpavlin 17 our $table = 'log';
15 dpavlin 1
16 dpavlin 2 require 'config.pl' if -e 'config.pl';
17    
18 dpavlin 17 $table = param('table') || $table;
19 dpavlin 1 my @columns = param('columns');
20     @columns = ('*') unless @columns;
21     my $limit = param('limit') || 1000;
22     my $offset = param('offset') || 0;
23    
24 dpavlin 2 my @where_parts = param('where_parts');
25    
26 dpavlin 10 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 dpavlin 15 print qq|<tr><td>$c</td><td><a href="#">$n</a></td></tr>|;
42 dpavlin 10 }
43     print qq|</table>|;
44     exit;
45     }
46    
47 dpavlin 1 print header, q|
48    
49     <html>
50     <head>
51 dpavlin 2 <title>SQL Web Session</title>
52 dpavlin 1 <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 dpavlin 7
56 dpavlin 10 <script type="text/javascript" src="sql-editor.js"></script>
57 dpavlin 7
58 dpavlin 1 </head>
59     <body>
60     |;
61    
62 dpavlin 10 my $group_by = param('group_by');
63 dpavlin 1
64     if ( param('where_operator') && length( param('where_value') ) > 0 ) {
65 dpavlin 2 my $where_value = param('where_value');
66     push @where_parts, param('where_column') . ' ' . param('where_operator') . " ?\t$where_value";
67     param('where_value','');
68 dpavlin 1 }
69    
70 dpavlin 3
71 dpavlin 2 my $c = join(',', @columns);
72 dpavlin 1
73 dpavlin 2 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 dpavlin 3
87     $sql .= ' group by ' . $group_by if $group_by;
88 dpavlin 2 $sql .= ' order by ' . param('order_by') if param('order_by');
89     $sql .= ' limit ? offset ?';
90    
91     push @data, ( $limit, $offset );
92    
93 dpavlin 3 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 dpavlin 2 my $t = time();
101    
102 dpavlin 1 my $sth = $dbh->prepare( $sql );
103    
104     $sth->execute( @data );
105    
106 dpavlin 2 $t = time() - $t;
107    
108 dpavlin 3 print $sth->rows, qq| rows in $t s</code>|;
109 dpavlin 2
110 dpavlin 14 #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 dpavlin 3 @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
119 dpavlin 2
120 dpavlin 1 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 dpavlin 12 , qq|<a href="#" onclick="\$('form#sql').toggleClass('visible'); return false;" class=close title="hide sql editor">[x]</a>|
145 dpavlin 6
146 dpavlin 1 , qq|<label for=columns>select</label>|
147 dpavlin 2 , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
148 dpavlin 1
149     , qq|<label for=from>from</label>|
150     , textfield( -name => 'from', -value => $table, -default => 'log' )
151    
152     , qq|<label for=where>where</label>|
153 dpavlin 2 , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] )
154 dpavlin 1 , popup_menu( -name => 'where_column', -values => [ @columns ] ),
155 dpavlin 2 , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ])
156 dpavlin 1 , textfield( -name => 'where_value' )
157 dpavlin 10 , qq|
158     <span>
159     <input type=button name=add_group_by>
160     <input type=button name=close_group_by value="[x]" disabled=1>
161 dpavlin 12 <div id="lookup"></div>
162 dpavlin 10 <span>
163     |
164 dpavlin 1
165 dpavlin 3 , qq|<label for=group_by>group by</label>|
166     , textfield( -name => 'group_by' )
167    
168 dpavlin 1 , qq|<label for=order_by>order by</label>|
169 dpavlin 2 , textfield( -name => 'order_by' )
170 dpavlin 1
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