File Coverage

blib/lib/Mojolicious/Plugin/SQLiteViewerLite/Base/Sqliteviewerlite.pm
Criterion Covered Total %
statement 95 95 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 10 10 100.0
pod 0 8 0.0
total 109 118 92.3


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::SQLiteViewerLite::Base::Sqliteviewerlite;
2 2     2   908 use Mojo::Base 'Mojolicious::Controller';
  2         4  
  2         13  
3 2     2   1136 use Data::Page;
  2         7811  
  2         16  
4              
5             sub default {
6 8     8 0 74749 my $self = shift;
7            
8 8         37 my $plugin = $self->stash->{plugin};
9 8         91 my $command = $plugin->command;
10            
11 8         121 my $database = $command->show_databases;
12 8         39 my $current_database = $command->current_database;
13              
14 8         323 $self->render(
15             databases => $database,
16             current_database => $current_database,
17             );
18             }
19              
20             sub tables {
21 6     6 0 142150 my $self = shift;;
22            
23 6         26 my $plugin = $self->stash->{plugin};
24 6         75 my $command = $plugin->command;
25              
26 6         78 my $params = $command->params($self);
27 6         45 my $rule = [
28             database => {default => ''} => [
29             'safety_name'
30             ]
31             ];
32 6         45 my $vresult = $plugin->validator->validate($params, $rule);
33 6         459 my $database = $vresult->data->{database};
34 6         77 my $tables = $command->show_tables($database);
35            
36 6         45 return $self->render(
37             database => $database,
38             tables => $tables
39             );
40             }
41              
42             sub table {
43 13     13 0 152454 my $self = shift;;
44            
45 13         81 my $plugin = $self->stash->{plugin};
46 13         155 my $command = $plugin->command;
47              
48             # Validation
49 13         134 my $params = $command->params($self);
50 13         101 my $rule = [
51             database => {default => ''} => [
52             'safety_name'
53             ],
54             table => {default => ''} => [
55             'safety_name'
56             ]
57             ];
58 13         73 my $vresult = $plugin->validator->validate($params, $rule);
59 13         845 my $database = $vresult->data->{database};
60 13         243 my $table = $vresult->data->{table};
61            
62 13         137 my $table_def = $command->show_create_table($database, $table);
63              
64 13         92 return $self->render(
65             database => $database,
66             table => $table,
67             table_def => $table_def,
68             );
69             }
70              
71             sub showcreatetables {
72 4     4 0 45153 my $self = shift;;
73            
74 4         18 my $plugin = $self->stash->{plugin};
75 4         45 my $command = $plugin->command;
76              
77             # Validation
78 4         41 my $params = $command->params($self);
79 4         25 my $rule = [
80             database => {default => ''} => [
81             'safety_name'
82             ]
83             ];
84 4         27 my $vresult = $plugin->validator->validate($params, $rule);
85 4         289 my $database = $vresult->data->{database};
86 4         46 my $tables = $command->show_tables($database);
87            
88             # Get create tables
89 4         15 my $create_tables = {};
90 4         14 for my $table (@$tables) {
91 12         50 $create_tables->{$table} = $plugin->command->show_create_table($database, $table);
92             }
93            
94             $self->render(
95 4         28 database => $database,
96             create_tables => $create_tables
97             );
98             }
99              
100             sub showselecttables {
101 4     4 0 48542 my $self = shift;;
102            
103 4         19 my $plugin = $self->stash->{plugin};
104 4         47 my $command = $plugin->command;
105              
106             # Validation
107 4         63 my $params = $command->params($self);
108 4         24 my $rule = [
109             database => {default => ''} => [
110             'safety_name'
111             ]
112             ];
113 4         21 my $vresult = $plugin->validator->validate($params, $rule);
114 4         283 my $database = $vresult->data->{database};
115 4         44 my $tables = $command->show_tables($database);
116            
117 4         28 $self->render(
118             database => $database,
119             tables => $tables
120             );
121             }
122              
123             sub showprimarykeys {
124 5     5 0 89442 my $self = shift;;
125            
126 5         20 my $plugin = $self->stash->{plugin};
127 5         56 my $command = $plugin->command;
128              
129             # Validation
130 5         78 my $params = $command->params($self);
131 5         34 my $rule = [
132             database => {default => ''} => [
133             'safety_name'
134             ],
135             ];
136 5         47 my $vresult = $plugin->validator->validate($params, $rule);
137 5         397 my $database = $vresult->data->{database};
138            
139             # Get primary keys
140 5         76 my $primary_keys = $command->show_primary_keys($database);
141            
142 5         39 $self->render(
143             database => $database,
144             primary_keys => $primary_keys
145             );
146             }
147              
148             sub shownullallowedcolumns {
149 5     5 0 79863 my $self = shift;;
150            
151 5         37 my $plugin = $self->stash->{plugin};
152 5         62 my $command = $plugin->command;
153              
154             # Validation
155 5         54 my $params = $command->params($self);
156 5         31 my $rule = [
157             database => {default => ''} => [
158             'safety_name'
159             ],
160             ];
161 5         30 my $vresult = $plugin->validator->validate($params, $rule);
162 5         370 my $database = $vresult->data->{database};
163            
164             # Get null allowed columns
165 5         77 my $null_allowed_columns = $command->show_null_allowed_columns($database);
166            
167 5         32 $self->render(
168             database => $database,
169             null_allowed_columns => $null_allowed_columns
170             );
171             }
172              
173             sub select {
174 11     11 0 2904483 my $self = shift;;
175            
176 11         59 my $plugin = $self->stash->{plugin};
177 11         136 my $command = $plugin->command;
178              
179             # Validation
180 11         153 my $params = $command->params($self);
181 11         149 my $rule = [
182             database => {default => ''} => [
183             'safety_name'
184             ],
185             table => {default => ''} => [
186             'safety_name'
187             ],
188             page => {default => 1} => [
189             'uint'
190             ],
191             condition_column => [
192             'safety_name'
193             ],
194             condition_value => [
195             'not_blank'
196             ]
197             ];
198 11         97 my $vresult = $plugin->validator->validate($params, $rule);
199 11         2464 my $database = $vresult->data->{database};
200 11         247 my $table = $vresult->data->{table};
201            
202             # Where
203 11         223 my $column = $vresult->data->{condition_column};
204 11         223 my $value = $vresult->data->{condition_value};
205            
206 11         80 my $where;
207 11 100 66     69 if (defined $column && defined $value) {
208 1         7 $where = $plugin->dbi->where;
209 1         92 $where->clause(":${column}{like}");
210 1         26 $where->param({$column => $value});
211             }
212            
213             # Limit
214 11         194 my $page = $vresult->data->{page};
215 11         88 my $count = 100;
216 11         39 my $offset = ($page - 1) * $count;
217            
218             # Get null allowed columns
219 11         72 my $result = $plugin->dbi->select(
220             table => "$database.$table",
221             where => $where,
222             append => "limit $offset, $count"
223             );
224 9         8976 my $header = $result->header;
225 9         348 my $rows = $result->fetch_all;
226 9         6339 my $sql = $plugin->dbi->last_sql;
227            
228             # Pager
229 9         328 my $total = $plugin->dbi->select(
230             'count(*)',
231             table => "$database.$table",
232             where => $where
233             )->value;
234 9         7577 my $pager = Data::Page->new($total, $count, $page);
235            
236 9         727 $self->render(
237             database => $database,
238             table => $table,
239             header => $header,
240             rows => $rows,
241             sql => $sql,
242             pager => $pager
243             );
244             }
245              
246             1;