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