1 |
<%args> |
2 |
$sth => undef |
3 |
|
4 |
$sql_query => undef |
5 |
$name => undef |
6 |
$database => undef |
7 |
|
8 |
$max_rows => 100 |
9 |
|
10 |
</%args> |
11 |
<%init> |
12 |
|
13 |
my $execute_sql = Jifty->web->new_action( |
14 |
class => 'DoSQL', |
15 |
moniker => 'execute_sql', |
16 |
); |
17 |
|
18 |
my $result = Jifty->web->response->result('execute_sql'); |
19 |
$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> |
25 |
|
26 |
<&| /_elements/wrapper, title => "Try out SQL query" &> |
27 |
<% Jifty->web->form->start() %> |
28 |
<% $execute_sql->form_field('sql_query', |
29 |
default_value => $sql_query, |
30 |
cols => 80, |
31 |
rows => 12, |
32 |
) %> |
33 |
|
34 |
<div class="database-schema"> |
35 |
<%perl> |
36 |
my $db = SQLSession::Model::Database->new(); |
37 |
$db->load_by_cols( id => $database ); |
38 |
my $name = $db->dsn; |
39 |
$name =~ s/^.*dbname=//; |
40 |
$m->out("<a href=\"/static/spy/$name/index.html\" target=\"_spy\">$name shema</a>"); |
41 |
</%perl> |
42 |
</div> |
43 |
|
44 |
% if ( $edit ) { |
45 |
<% $execute_sql->form_field('database', default_value => $database ) %> |
46 |
% } else { |
47 |
|
48 |
<!-- hidden database --> |
49 |
<% $execute_sql->hidden('database', $database ) %> |
50 |
<% $execute_sql->form_value('database', default_value => $database ) %> |
51 |
|
52 |
% } |
53 |
|
54 |
<div class="submit_button"> |
55 |
<% $execute_sql->button( label => 'Try it!' ) %> |
56 |
|
57 |
|
58 |
% if ( defined($sth) ) { |
59 |
|
60 |
% if ( $edit ) { |
61 |
|
62 |
% warn "save SQL: ", $execute_sql->argument_value('sql_query'); |
63 |
|
64 |
<% Jifty->web->link( |
65 |
label => 'Save', |
66 |
onclick => { |
67 |
region => 'save_query', |
68 |
replace_with => '/save', |
69 |
args => { |
70 |
sql_query => $execute_sql->argument_value('sql_query'), |
71 |
database => $execute_sql->argument_value('database'), |
72 |
}, |
73 |
refresh_self => 1, |
74 |
toggle => 1, |
75 |
}, |
76 |
as_button => 1, |
77 |
) %> |
78 |
|
79 |
</div><!-- submit_button edit --> |
80 |
|
81 |
<% Jifty->web->region( |
82 |
name => 'save_query', |
83 |
) %> |
84 |
|
85 |
% } else { |
86 |
|
87 |
</div><!-- submit_button no edit --> |
88 |
|
89 |
% } |
90 |
|
91 |
<% Jifty->web->form->end() %> |
92 |
|
93 |
<table cellpadding="3" cellspacing="0" border="1"> |
94 |
|
95 |
<tr> |
96 |
% foreach my $col ( @{ $sth->{'NAME'} } ) { |
97 |
<th><% $col %></th> |
98 |
% } |
99 |
</tr> |
100 |
|
101 |
% my $rows = 0; |
102 |
% while ( my @row = $sth->fetchrow_array() ) { |
103 |
% last if (++$rows > $max_rows); |
104 |
<tr> |
105 |
% foreach my $v ( @row ) { |
106 |
<td><% defined($v) ? $v : 'NULL' %></td> |
107 |
% } |
108 |
</tr> |
109 |
% } |
110 |
|
111 |
</table> |
112 |
|
113 |
% if ($sth->rows > $max_rows) { |
114 |
<div class="note"> |
115 |
Query produced more than <% $max_rows %> rows, try adding <tt>LIMIT</tt> and |
116 |
<tt>OFFSET</tt> to see another part of results. |
117 |
</div> |
118 |
% } |
119 |
|
120 |
% } else { |
121 |
|
122 |
</div><!-- submit_button no query --> |
123 |
<% Jifty->web->form->end() %> |
124 |
|
125 |
% } |
126 |
|
127 |
<% Jifty->web->return( label => "Go back" ) %> |
128 |
|
129 |
</&> |
130 |
|