1 |
<%args> |
<%args> |
|
$page => 1 |
|
2 |
$sth => undef |
$sth => undef |
3 |
|
|
4 |
|
$sql_query => undef |
5 |
|
$name => undef |
6 |
|
$database => undef |
7 |
|
|
8 |
|
$max_rows => 100 |
9 |
|
|
10 |
</%args> |
</%args> |
11 |
<%init> |
<%init> |
12 |
|
|
13 |
my $action = Jifty->web->new_action( |
my $execute_sql = Jifty->web->new_action( |
14 |
class => 'DoSQL', |
class => 'DoSQL', |
15 |
moniker => 'execute_sql', |
moniker => 'execute_sql', |
16 |
); |
); |
18 |
my $result = Jifty->web->response->result('execute_sql'); |
my $result = Jifty->web->response->result('execute_sql'); |
19 |
$sth = $result->content('sth') if $result and !defined $sth; |
$sth = $result->content('sth') if $result and !defined $sth; |
20 |
|
|
21 |
|
my $edit = Jifty->web->current_user->role('edit'); |
22 |
|
warn $edit ? 'edit mode for SQL' : 'student mode'; |
23 |
|
|
24 |
</%init> |
</%init> |
25 |
|
|
26 |
<&| /_elements/wrapper, title => "Try out SQL query" &> |
<&| /_elements/wrapper, title => "Try out SQL query" &> |
27 |
<% Jifty->web->form->start() %> |
<% Jifty->web->form->start() %> |
28 |
<% $action->form_field('sql_query') %> |
<% $execute_sql->form_field('sql_query', |
29 |
<% $action->form_field('database') %> |
default_value => $sql_query, |
30 |
<% Jifty->web->form->submit( label => 'Try it!' ) %> |
cols => 80, |
31 |
<% Jifty->web->form->end() %> |
rows => 12, |
32 |
</&> |
) %> |
33 |
|
% if ( $edit ) { |
34 |
|
<% $execute_sql->form_field('database', default_value => $database ) %> |
35 |
|
% } else { |
36 |
|
|
37 |
|
<!-- hidden database --> |
38 |
|
<% $execute_sql->hidden('database', $database ) %> |
39 |
|
|
40 |
|
% } |
41 |
|
<div class="submit_button"> |
42 |
|
<% $execute_sql->button( label => 'Try it!' ) %> |
43 |
|
|
44 |
|
|
45 |
% if ( defined($sth) ) { |
% if ( defined($sth) ) { |
46 |
|
|
47 |
|
% if ( $edit ) { |
48 |
|
|
49 |
|
% warn "save SQL: ", $execute_sql->argument_value('sql_query'); |
50 |
|
|
51 |
|
<% Jifty->web->link( |
52 |
|
label => 'Save', |
53 |
|
onclick => { |
54 |
|
region => 'save_query', |
55 |
|
replace_with => '/save', |
56 |
|
args => { |
57 |
|
sql_query => $execute_sql->argument_value('sql_query'), |
58 |
|
database => $execute_sql->argument_value('database'), |
59 |
|
}, |
60 |
|
refresh_self => 1, |
61 |
|
toggle => 1, |
62 |
|
}, |
63 |
|
as_button => 1, |
64 |
|
) %> |
65 |
|
|
66 |
|
</div><!-- submit_button edit --> |
67 |
|
|
68 |
|
<% Jifty->web->region( |
69 |
|
name => 'save_query', |
70 |
|
) %> |
71 |
|
|
72 |
|
% } else { |
73 |
|
|
74 |
|
</div><!-- submit_button no edit --> |
75 |
|
|
76 |
|
% } |
77 |
|
|
78 |
|
<% Jifty->web->form->end() %> |
79 |
|
|
80 |
<table cellpadding="3" cellspacing="0" border="1"> |
<table cellpadding="3" cellspacing="0" border="1"> |
81 |
|
|
82 |
<tr> |
<tr> |
85 |
% } |
% } |
86 |
</tr> |
</tr> |
87 |
|
|
88 |
|
% my $rows = 0; |
89 |
% while ( my @row = $sth->fetchrow_array() ) { |
% while ( my @row = $sth->fetchrow_array() ) { |
90 |
|
% last if (++$rows > $max_rows); |
91 |
<tr> |
<tr> |
92 |
% foreach my $v ( @row ) { |
% foreach my $v ( @row ) { |
93 |
<td><% $v %></td> |
<td><% defined($v) ? $v : 'NULL' %></td> |
94 |
% } |
% } |
95 |
</tr> |
</tr> |
96 |
% } |
% } |
97 |
|
|
98 |
</table> |
</table> |
99 |
|
|
100 |
|
% if ($sth->rows > $max_rows) { |
101 |
|
<div class="note"> |
102 |
|
Query produced more than <% $max_rows %> rows, try adding <tt>LIMIT</tt> and |
103 |
|
<tt>OFFSET</tt> to see another part of results. |
104 |
|
</div> |
105 |
|
% } |
106 |
|
|
107 |
|
% } else { |
108 |
|
|
109 |
|
</div><!-- submit_button no query --> |
110 |
|
<% Jifty->web->form->end() %> |
111 |
|
|
112 |
% } |
% } |
113 |
|
|
114 |
|
<% Jifty->web->return( label => "Go back" ) %> |
115 |
|
|
116 |
|
</&> |
117 |
|
|