/[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 35 - (show annotations)
Wed Jan 17 17:24:32 2007 UTC (17 years, 4 months ago) by dpavlin
File size: 2471 byte(s)
after simple patch to HTTP::Server::Simple ajax validate and canonicalize
work, added limit and offset as upprecased keywords.

--- HTTP-Server-Simple-0.26/lib/HTTP/Server/Simple.pm	2006-11-26 01:15:39.000000000 +0100
+++ Simple.pm	2007-01-17 18:22:46.000000000 +0100
@@ -305,7 +305,7 @@
         $proto ||= "HTTP/0.9";
 
         my ( $file, $query_string )
-            = ( $request_uri =~ /([^?]*)(?:\?(.*))?/ );    # split at ?
+            = ( $request_uri =~ /([^?]*)(?:\?(.*))?/s );    # split at ?
 
         $self->setup(
             method       => $method,

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

  ViewVC Help
Powered by ViewVC 1.1.26