/[SQLSession]/trunk/lib/SQLSession/Action/DoSQL.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/SQLSession/Action/DoSQL.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 84 - (show annotations)
Tue Jan 30 14:08:47 2007 UTC (17 years, 3 months ago) by dpavlin
File size: 2672 byte(s)
use on_database parameter for database id throughout all code,
implement save query using tangents and continuations
1 use strict;
2 use warnings;
3
4 =head1 NAME
5
6 SQLSession::Action::DoSQL
7
8 =cut
9
10 package SQLSession::Action::DoSQL;
11 use base qw/SQLSession::Action Jifty::Action/;
12
13 use SQLSession::Model::DatabaseCollection;
14 use DBI;
15 use Time::HiRes qw/time/;
16
17 use Data::Dump qw/dump/;
18
19 use Jifty::Param::Schema;
20 use Jifty::Action schema {
21
22 param sql_query =>
23 label is 'SQL',
24 render as 'textarea',
25 ajax validates,
26 ajax canonicalizes,
27 is mandatory;
28
29 param on_database =>
30 label is 'Database',
31 render as 'Select',
32 # available are qw( test foo bar );
33 available are defer {
34 my $dbs = SQLSession::Model::DatabaseCollection->new;
35 $dbs->unlimit;
36 [{
37 display_from => 'name',
38 value_from => 'id',
39 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 sub canonicalize_sql_query {
66 my $self = shift;
67 my $sql = shift;
68
69 warn "<<<< original SQL:\t$sql";
70
71 sub sql_uc {
72 my ($prefix,$sql,$suff) = @_;
73 return uc($sql).$suff if (! $prefix || $prefix eq '');
74 return "\n" . uc($sql) . $suff;
75 }
76
77 $sql =~ s/(\s*)(select|from|inner\s+join|order\s+by|where|group\s+by|having|limit|offset)(\s+)/sql_uc($1,$2,$3)/egis;
78
79 warn ">>>> canonicalize SQL:\n$sql";
80
81 return $sql;
82 }
83
84 =head2 take_action
85
86 Execute SQL query on database
87
88 =cut
89
90 sub take_action {
91 my $self = shift;
92
93 # Custom action code
94
95 my $on_database = $self->argument_value('on_database') || $self->form_value('on_database');
96 warn "on_database: $on_database\n";
97
98 my $db = SQLSession::Model::Database->new;
99 $db->load_by_cols( id => $on_database ) ||
100 $self->result->error("Can't find database $on_database");
101
102 my $dbh = DBI->connect( $db->dsn, $db->login, $db->passwd ) ||
103 $self->result->error("Can't connect to " . $db->dsn . " as " . $db->login . "<br/>" . $DBI::errstr) && return 0;
104
105 $dbh->do("SET client_encoding = UTF8") if ($db->dsn =~ /Pg/);
106
107 my $sql = $self->argument_value('sql_query');
108
109 warn "SQL: $sql\n";
110
111 my $t = time();
112
113 my $sth = $dbh->prepare( $sql ) ||
114 $self->result->error( $dbh->errstr() ) && return 0;
115
116 $sth->execute() ||
117 $self->result->error( $sth->errstr() ) && return 0;
118
119 $self->result->message('Query produced ' . (
120 $sth->rows == 0 ? 'no results' :
121 $sth->rows == 1 ? 'single row' :
122 sprintf("%d rows in %.2fs", $sth->rows, time() - $t)
123 )) if ($sth->rows >= 0);
124
125 $self->result->content( sth => $sth );
126
127 return 1;
128 }
129
130 1;
131

  ViewVC Help
Powered by ViewVC 1.1.26