14 |
|
|
15 |
require 'config.pl' if -e 'config.pl'; |
require 'config.pl' if -e 'config.pl'; |
16 |
|
|
17 |
|
my $table = param('table') || 'log'; |
18 |
my @columns = param('columns'); |
my @columns = param('columns'); |
19 |
@columns = ('*') unless @columns; |
@columns = ('*') unless @columns; |
|
my $table = param('table') || 'log'; |
|
20 |
my $limit = param('limit') || 1000; |
my $limit = param('limit') || 1000; |
21 |
my $offset = param('offset') || 0; |
my $offset = param('offset') || 0; |
22 |
|
|
23 |
my @where_parts = param('where_parts'); |
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| |
print header, q| |
47 |
|
|
48 |
<html> |
<html> |
51 |
<link rel="stylesheet" type="text/css" href="style.css"> |
<link rel="stylesheet" type="text/css" href="style.css"> |
52 |
<!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js --> |
<!-- http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js --> |
53 |
<script type="text/javascript" src="jquery-1.3.2.min.js"></script> |
<script type="text/javascript" src="jquery-1.3.2.min.js"></script> |
|
<script type="text/javascript"> |
|
|
|
|
|
$(document).ready( function() { |
|
|
|
|
|
function click_on_cell(e) { |
|
|
|
|
|
var tag = e.originalTarget.tagName; |
|
|
|
|
|
var col_nr = e.originalTarget.cellIndex; |
|
|
|
|
|
var column = $('table#results th:nth-child(' + ( col_nr + 1 ) + ')').text(); |
|
|
var where_operator = '='; |
|
|
var where_value = window.getSelection().getRangeAt(0).cloneContents().textContent; |
|
|
if ( where_value.length == 0 ) |
|
|
where_value = e.originalTarget.textContent; |
|
|
else |
|
|
where_value = '%' + where_value + '%'; |
|
|
|
|
|
console.debug('click on ', this, e, |
|
|
e.originalTarget, |
|
|
column, where_operator, where_value |
|
|
); |
|
|
|
|
|
if ( tag == 'TH' ) { |
|
|
console.info('header', column); |
|
|
$('form#sql input[name=order_by]').attr('value', where_value + ' desc'); |
|
|
} else if ( tag = 'TD' ) { |
|
|
console.info('column', column, where_operator, where_value); |
|
|
$('form#sql input[name=where_value]').attr('value', where_value); |
|
|
$('form#sql select[name=where_column]').attr('options').selectedIndex = col_nr; |
|
|
$('form#sql input[name=add_group_by]').attr('value', column).css('display','block'); |
|
|
} else { |
|
|
console.error('unknown click on ', tag, e); |
|
|
} |
|
|
|
|
|
$('form#sql').addClass('visible'); |
|
|
}; |
|
54 |
|
|
55 |
$('table#results').bind('mouseup', click_on_cell); |
<!-- insert firebug lite because we use console.* all over --> |
56 |
|
<!-- http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js --> |
57 |
$('#status').bind('click', function() { |
<script type="text/javascript" src="firebug-lite-compressed.js"></script> |
58 |
$('form#sql').toggleClass('visible'); |
<script type="text/javascript"> |
59 |
}); |
firebug.env.height = 100; |
60 |
|
</script> |
61 |
|
|
62 |
console.info('ready'); |
<script type="text/javascript" src="sql-editor.js"></script> |
|
}); |
|
63 |
|
|
|
</script> |
|
64 |
</head> |
</head> |
65 |
<body> |
<body> |
66 |
|; |
|; |
67 |
|
|
68 |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
my $group_by = param('group_by'); |
|
|
|
|
|
|
|
my $group_by = param('add_group_by'); |
|
|
|
|
|
if ( $group_by ) { |
|
|
@columns = ( "count($group_by)", $group_by ); |
|
|
my $g; |
|
|
foreach my $c ( @where_parts, $group_by ) { |
|
|
$c =~ s/\s.+$//; |
|
|
$g->{$c}++ if length($c) > 0; |
|
|
} |
|
|
$group_by = join( ',', keys %$g ); |
|
|
print "# $group_by g = ",dump( $g ); |
|
|
param('where_value',''); |
|
|
param('group_by', $group_by); |
|
|
param('columns', [ @columns ], [ @columns ]); |
|
|
param('order_by', $columns[0] . ' desc'); |
|
|
} else { |
|
|
$group_by = param('group_by'); |
|
|
} |
|
|
|
|
69 |
|
|
70 |
if ( param('where_operator') && length( param('where_value') ) > 0 ) { |
if ( param('where_operator') && length( param('where_value') ) > 0 ) { |
71 |
my $where_value = param('where_value'); |
my $where_value = param('where_value'); |
140 |
qq|</table>| |
qq|</table>| |
141 |
, start_form( -id => 'sql' ) |
, start_form( -id => 'sql' ) |
142 |
|
|
143 |
|
, qq|<a href="#" onclick="\$('form#sql').toggleClass('visible'); return false;" class=close>close</a>| |
144 |
|
|
145 |
, qq|<label for=columns>select</label>| |
, qq|<label for=columns>select</label>| |
146 |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
147 |
|
|
153 |
, popup_menu( -name => 'where_column', -values => [ @columns ] ), |
, popup_menu( -name => 'where_column', -values => [ @columns ] ), |
154 |
, popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ]) |
, popup_menu( -name => 'where_operator', -values => [ 'not like', 'like', '!=', '=' ]) |
155 |
, textfield( -name => 'where_value' ) |
, textfield( -name => 'where_value' ) |
156 |
|
, qq| |
157 |
|
<span> |
158 |
|
<input type=button name=add_group_by> |
159 |
|
<input type=button name=close_group_by value="[x]" disabled=1> |
160 |
|
<div id="lookup">...loading...</div> |
161 |
|
<span> |
162 |
|
| |
163 |
|
|
164 |
, qq|<label for=group_by>group by</label>| |
, qq|<label for=group_by>group by</label>| |
165 |
, textfield( -name => 'group_by' ) |
, textfield( -name => 'group_by' ) |
|
, submit( -name => 'add_group_by' ) |
|
166 |
|
|
167 |
, qq|<label for=order_by>order by</label>| |
, qq|<label for=order_by>order by</label>| |
168 |
, textfield( -name => 'order_by' ) |
, textfield( -name => 'order_by' ) |