/[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 12 - (hide annotations)
Tue Apr 14 21:25:00 2009 UTC (10 years, 2 months ago) by dpavlin
File size: 4445 byte(s)
tweaks to design and changed highlights

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    
15 dpavlin 2 require 'config.pl' if -e 'config.pl';
16    
17 dpavlin 10 my $table = param('table') || 'log';
18 dpavlin 1 my @columns = param('columns');
19     @columns = ('*') unless @columns;
20     my $limit = param('limit') || 1000;
21     my $offset = param('offset') || 0;
22    
23 dpavlin 2 my @where_parts = param('where_parts');
24    
25 dpavlin 10 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 dpavlin 1 print header, q|
47    
48     <html>
49     <head>
50 dpavlin 2 <title>SQL Web Session</title>
51 dpavlin 1 <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 dpavlin 7
55     <!-- insert firebug lite because we use console.* all over -->
56 dpavlin 10 <!-- http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js -->
57     <script type="text/javascript" src="firebug-lite-compressed.js"></script>
58 dpavlin 1 <script type="text/javascript">
59 dpavlin 7 firebug.env.height = 100;
60     </script>
61 dpavlin 1
62 dpavlin 10 <script type="text/javascript" src="sql-editor.js"></script>
63 dpavlin 7
64 dpavlin 1 </head>
65     <body>
66     |;
67    
68 dpavlin 10 my $group_by = param('group_by');
69 dpavlin 1
70     if ( param('where_operator') && length( param('where_value') ) > 0 ) {
71 dpavlin 2 my $where_value = param('where_value');
72     push @where_parts, param('where_column') . ' ' . param('where_operator') . " ?\t$where_value";
73     param('where_value','');
74 dpavlin 1 }
75    
76 dpavlin 3
77 dpavlin 2 my $c = join(',', @columns);
78 dpavlin 1
79 dpavlin 2 my $sql = "select $c from $table";
80     my @data;
81    
82     if ( @where_parts ) {
83     my @w;
84     foreach ( @where_parts ) {
85     my ( $w,$v ) = split(/\?\t/,$_,2);
86     push @w, "$w ?";
87     push @data, $v;
88     }
89     $sql .= ' where ' . join(' and ', @w);
90     }
91    
92 dpavlin 3
93     $sql .= ' group by ' . $group_by if $group_by;
94 dpavlin 2 $sql .= ' order by ' . param('order_by') if param('order_by');
95     $sql .= ' limit ? offset ?';
96    
97     push @data, ( $limit, $offset );
98    
99 dpavlin 3 my $sql_html = $sql;
100     {
101     my @d = @data;
102     $sql_html =~ s{\?}{dump( shift @d )}ge;
103     }
104     print qq|<code id="status">$sql_html<br>\n\r\n\r|;
105    
106 dpavlin 2 my $t = time();
107    
108 dpavlin 1 my $sth = $dbh->prepare( $sql );
109    
110     $sth->execute( @data );
111    
112 dpavlin 2 $t = time() - $t;
113    
114 dpavlin 3 print $sth->rows, qq| rows in $t s</code>|;
115 dpavlin 2
116    
117 dpavlin 3 @columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*';
118 dpavlin 2
119 dpavlin 1 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 dpavlin 12 , qq|<a href="#" onclick="\$('form#sql').toggleClass('visible'); return false;" class=close title="hide sql editor">[x]</a>|
144 dpavlin 6
145 dpavlin 1 , qq|<label for=columns>select</label>|
146 dpavlin 2 , checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] )
147 dpavlin 1
148     , qq|<label for=from>from</label>|
149     , textfield( -name => 'from', -value => $table, -default => 'log' )
150    
151     , qq|<label for=where>where</label>|
152 dpavlin 2 , checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] )
153 dpavlin 1 , popup_menu( -name => 'where_column', -values => [ @columns ] ),
154 dpavlin 2 , popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ])
155 dpavlin 1 , textfield( -name => 'where_value' )
156 dpavlin 10 , qq|
157     <span>
158     <input type=button name=add_group_by>
159     <input type=button name=close_group_by value="[x]" disabled=1>
160 dpavlin 12 <div id="lookup"></div>
161 dpavlin 10 <span>
162     |
163 dpavlin 1
164 dpavlin 3 , qq|<label for=group_by>group by</label>|
165     , textfield( -name => 'group_by' )
166    
167 dpavlin 1 , qq|<label for=order_by>order by</label>|
168 dpavlin 2 , textfield( -name => 'order_by' )
169 dpavlin 1
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