11 |
use base qw/SQLSession::Action Jifty::Action/; |
use base qw/SQLSession::Action Jifty::Action/; |
12 |
|
|
13 |
use SQLSession::Model::DatabaseCollection; |
use SQLSession::Model::DatabaseCollection; |
14 |
|
use DBI; |
15 |
|
use Time::HiRes qw/time/; |
16 |
|
|
17 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
18 |
|
|
19 |
use Jifty::Param::Schema; |
use Jifty::Param::Schema; |
21 |
|
|
22 |
param sql_query => |
param sql_query => |
23 |
label is 'SQL', |
label is 'SQL', |
24 |
|
render as 'textarea', |
25 |
|
ajax validates, |
26 |
|
ajax canonicalizes, |
27 |
is mandatory; |
is mandatory; |
28 |
|
|
29 |
param database => |
param database => |
35 |
$dbs->unlimit; |
$dbs->unlimit; |
36 |
[{ |
[{ |
37 |
display_from => 'name', |
display_from => 'name', |
38 |
value_from => 'name', |
value_from => 'id', |
39 |
collection => $dbs, |
collection => $dbs, |
40 |
}]; |
}]; |
41 |
}; |
}; |
42 |
|
|
43 |
}; |
}; |
44 |
|
|
45 |
|
sub sticky_on_success { 1 } |
46 |
|
sub sticky_on_failure { 1 } |
47 |
|
|
48 |
|
=head2 validate_sql_query |
49 |
|
|
50 |
|
Can't be empty! |
51 |
|
|
52 |
|
=cut |
53 |
|
|
54 |
|
sub validate_sql_query { |
55 |
|
my $self = shift; |
56 |
|
my $value = shift; |
57 |
|
|
58 |
|
if ( $value =~ m/^\s+;*\s*$/s ) { |
59 |
|
return $self->validation_error( sql_query => 'You need to type in SQL query' ); |
60 |
|
} else { |
61 |
|
return $self->validation_ok('sql_query'); |
62 |
|
} |
63 |
|
} |
64 |
|
|
65 |
|
use SQL::ReservedWords; |
66 |
|
|
67 |
|
sub canonicalize_sql_query { |
68 |
|
my $self = shift; |
69 |
|
my $sql = shift; |
70 |
|
|
71 |
|
my @words = SQL::ReservedWords->words; |
72 |
|
|
73 |
|
my $w = join('|', @words); |
74 |
|
|
75 |
|
$w .= '|limit|offset'; # fixup |
76 |
|
|
77 |
|
warn "original SQL:\t$sql"; |
78 |
|
|
79 |
|
$sql =~ s/([\b^'])($w)\1/uc($2)/egis; |
80 |
|
|
81 |
|
warn "canonicalize SQL:\t$sql"; |
82 |
|
|
83 |
|
return $sql; |
84 |
|
} |
85 |
|
|
86 |
=head2 take_action |
=head2 take_action |
87 |
|
|
88 |
|
Execute SQL query on database |
89 |
|
|
90 |
=cut |
=cut |
91 |
|
|
92 |
sub take_action { |
sub take_action { |
93 |
my $self = shift; |
my $self = shift; |
94 |
|
|
95 |
# Custom action code |
# Custom action code |
|
|
|
|
$self->report_success if not $self->result->failure; |
|
|
|
|
|
return 1; |
|
|
} |
|
96 |
|
|
97 |
=head2 report_success |
my $database = $self->argument_value('database') || $self->form_value('database'); |
98 |
|
warn "database: $database\n"; |
99 |
|
|
100 |
=cut |
my $db = SQLSession::Model::Database->new; |
101 |
|
$db->load_by_cols( id => $database ) || |
102 |
|
$self->result->error("Can't find database $database"); |
103 |
|
|
104 |
sub report_success { |
my $dbh = DBI->connect( $db->dsn, $db->login, $db->passwd ) || |
105 |
my $self = shift; |
$self->result->error("Can't connect to " . $db->dsn . " as " . $db->login . "<br/>" . $DBI::errstr) && return 0; |
106 |
# Your success message here |
|
107 |
$self->result->message('Success'); |
$dbh->do("SET client_encoding = UTF8") if ($db->dsn =~ /Pg/); |
108 |
|
|
109 |
|
my $sql = $self->argument_value('sql_query'); |
110 |
|
|
111 |
|
warn "SQL: $sql\n"; |
112 |
|
|
113 |
|
my $t = time(); |
114 |
|
|
115 |
|
my $sth = $dbh->prepare( $sql ) || |
116 |
|
$self->result->error( $dbh->errstr() ) && return 0; |
117 |
|
|
118 |
|
$sth->execute() || |
119 |
|
$self->result->error( $sth->errstr() ) && return 0; |
120 |
|
|
121 |
|
$self->result->message('Query produced ' . ( |
122 |
|
$sth->rows == 0 ? 'no results' : |
123 |
|
$sth->rows == 1 ? 'single row' : |
124 |
|
sprintf("%d rows in %.2fs", $sth->rows, time() - $t) |
125 |
|
)) if ($sth->rows >= 0); |
126 |
|
|
127 |
|
$self->result->content( sth => $sth ); |
128 |
|
|
129 |
|
return 1; |
130 |
} |
} |
131 |
|
|
132 |
1; |
1; |