30 |
<link rel="stylesheet" type="text/css" href="style.css"> |
<link rel="stylesheet" type="text/css" href="style.css"> |
31 |
<!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js --> |
<!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js --> |
32 |
<script type="text/javascript" src="jquery-1.3.2.min.js"></script> |
<script type="text/javascript" src="jquery-1.3.2.min.js"></script> |
33 |
|
|
34 |
|
<!-- insert firebug lite because we use console.* all over --> |
35 |
|
<script type="text/javascript" src="http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js"></script> |
36 |
|
<script type="text/javascript"> |
37 |
|
firebug.env.height = 100; |
38 |
|
</script> |
39 |
|
|
40 |
<script type="text/javascript"> |
<script type="text/javascript"> |
41 |
|
|
42 |
$(document).ready( function() { |
$(document).ready( function() { |
62 |
|
|
63 |
if ( tag == 'TH' ) { |
if ( tag == 'TH' ) { |
64 |
console.info('header', column); |
console.info('header', column); |
65 |
$('form#sql input[name=order_by]').attr('value', where_value); |
$('form#sql input[name=order_by]').attr('value', where_value + ' desc'); |
66 |
} else if ( tag = 'TD' ) { |
} else if ( tag = 'TD' ) { |
67 |
console.info('column', column, where_operator, where_value); |
console.info('column', column, where_operator, where_value); |
68 |
$('form#sql input[name=where_value]').attr('value', where_value); |
$('form#sql input[name=where_value]').attr('value', where_value); |
69 |
$('form#sql select[name=where_column]').attr('options').selectedIndex = col_nr; |
$('form#sql select[name=where_column]').attr('options').selectedIndex = col_nr; |
70 |
|
$('form#sql input[name=add_group_by]').attr('value', column).css('display','block'); |
71 |
} else { |
} else { |
72 |
console.error('unknown click on ', tag, e); |
console.error('unknown click on ', tag, e); |
73 |
} |
} |
92 |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
93 |
|
|
94 |
|
|
95 |
|
my $group_by = param('add_group_by'); |
96 |
|
|
97 |
|
if ( $group_by ) { |
98 |
|
@columns = ( "count($group_by)", $group_by ); |
99 |
|
my $g; |
100 |
|
foreach my $c ( @where_parts, $group_by ) { |
101 |
|
$c =~ s/\s.+$//; |
102 |
|
$g->{$c}++ if length($c) > 0; |
103 |
|
} |
104 |
|
$group_by = join( ',', keys %$g ); |
105 |
|
print "# $group_by g = ",dump( $g ); |
106 |
|
param('where_value',''); |
107 |
|
param('group_by', $group_by); |
108 |
|
param('columns', [ @columns ], [ @columns ]); |
109 |
|
param('order_by', $columns[0] . ' desc'); |
110 |
|
} else { |
111 |
|
$group_by = param('group_by'); |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
if ( param('where_operator') && length( param('where_value') ) > 0 ) { |
if ( param('where_operator') && length( param('where_value') ) > 0 ) { |
116 |
my $where_value = param('where_value'); |
my $where_value = param('where_value'); |
117 |
push @where_parts, param('where_column') . ' ' . param('where_operator') . " ?\t$where_value"; |
push @where_parts, param('where_column') . ' ' . param('where_operator') . " ?\t$where_value"; |
118 |
param('where_value',''); |
param('where_value',''); |
119 |
} |
} |
120 |
|
|
121 |
|
|
122 |
my $c = join(',', @columns); |
my $c = join(',', @columns); |
123 |
|
|
124 |
my $sql = "select $c from $table"; |
my $sql = "select $c from $table"; |
134 |
$sql .= ' where ' . join(' and ', @w); |
$sql .= ' where ' . join(' and ', @w); |
135 |
} |
} |
136 |
|
|
137 |
|
|
138 |
|
$sql .= ' group by ' . $group_by if $group_by; |
139 |
$sql .= ' order by ' . param('order_by') if param('order_by'); |
$sql .= ' order by ' . param('order_by') if param('order_by'); |
140 |
$sql .= ' limit ? offset ?'; |
$sql .= ' limit ? offset ?'; |
141 |
|
|
142 |
push @data, ( $limit, $offset ); |
push @data, ( $limit, $offset ); |
143 |
|
|
144 |
|
my $sql_html = $sql; |
145 |
|
{ |
146 |
|
my @d = @data; |
147 |
|
$sql_html =~ s{\?}{dump( shift @d )}ge; |
148 |
|
} |
149 |
|
print qq|<code id="status">$sql_html<br>\n\r\n\r|; |
150 |
|
|
151 |
my $t = time(); |
my $t = time(); |
152 |
|
|
153 |
my $sth = $dbh->prepare( $sql ); |
my $sth = $dbh->prepare( $sql ); |
156 |
|
|
157 |
$t = time() - $t; |
$t = time() - $t; |
158 |
|
|
159 |
my $sql_html = $sql; |
print $sth->rows, qq| rows in $t s</code>|; |
|
$sql_html =~ s{\?}{dump( shift @data )}ge; |
|
|
|
|
|
print qq|<code id="status">$sql_html<br>|, $sth->rows, qq| rows in $t s</code>|; |
|
160 |
|
|
161 |
|
|
162 |
@columns = @{ $sth->{NAME} }; |
@columns = @{ $sth->{NAME} } if $#columns == 0 && $columns[0] eq '*'; |
163 |
|
|
164 |
print qq|<table id="results">|; |
print qq|<table id="results">|; |
165 |
|
|
185 |
qq|</table>| |
qq|</table>| |
186 |
, start_form( -id => 'sql' ) |
, start_form( -id => 'sql' ) |
187 |
|
|
188 |
|
, qq|<a href="#" onclick="\$('form#sql').toggleClass('visible'); return false;" class=close>close</a>| |
189 |
|
|
190 |
, qq|<label for=columns>select</label>| |
, qq|<label for=columns>select</label>| |
191 |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
192 |
|
|
199 |
, popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ]) |
, popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ]) |
200 |
, textfield( -name => 'where_value' ) |
, textfield( -name => 'where_value' ) |
201 |
|
|
202 |
|
, qq|<label for=group_by>group by</label>| |
203 |
|
, textfield( -name => 'group_by' ) |
204 |
|
, submit( -name => 'add_group_by' ) |
205 |
|
|
206 |
, qq|<label for=order_by>order by</label>| |
, qq|<label for=order_by>order by</label>| |
207 |
, textfield( -name => 'order_by' ) |
, textfield( -name => 'order_by' ) |
208 |
|
|