AutoCRUD/Controller/Table.pm

filter: Statementstmt 62.7% 145 / 231 Branchbran 41.6% 25 / 60 Conditioncond 33.3% 9 / 27 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 80.7% 21 / 26 Podpod 30.0% 3 / 10 totaltotal 55.1% 204 / 370 CC 50 SCAR 23.2
CC
50
Cov
56%
clone
30.0
_display_update_form
20.0
count_where
20.0
CRAP 10.2
« AutoCRUD/DataSource.pm ↑ Summary AutoCRUD/Context.pm »
1 package App::AutoCRUD::Controller::Table;
2
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 extends 'App::AutoCRUD::Controller';
9 1 ▶use SQL::Abstract::More 1.27;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use List::MoreUtils qw/mesh firstval/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 1 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 #----------------------------------------------------------------------
17 # entry point to the controller
18 #----------------------------------------------------------------------
19 15 ▶sub serve {
Subroutine serve: called CC=3 SCAR=11.2
Pod documented
20 15 my ($self) = @_;
21
22 15 my $context = $self->context;
23
24 # extract from path : table name and method to dispatch to
25 15 ▶ my ($table, $meth_name) = $context->extract_path_segments(2)
Branch: 1 branch
BranchTrueFalseTotal
() = $context->extract_path_segments(2) or die "URL too short, missing table and method name" 15 0 15
26 or die "URL too short, missing table and method name";
27 15 ▶ my $method = $self->can($meth_name)
Branch: 1 branch
BranchTrueFalseTotal
my $method = $self->can($meth_name) or die "no such method: $meth_name" 15 0 15
28 or die "no such method: $meth_name";
29
30 # set default template and title
31 15 $context->set_template("table/$meth_name.tt");
32 15 $context->set_title($context->title . "-" . $table);
33
34 # dispatch to method
35 15 return $self->$method($table);
36 }
37
38
39 #----------------------------------------------------------------------
40 # published methods
41 #----------------------------------------------------------------------
42
43 9 ▶sub descr {
Subroutine descr: called CC=1 SCAR=0.0
Pod documented
44 9 my ($self, $table) = @_;
45
46 9 my $datasource = $self->datasource;
47 9 my $descr = $datasource->config(tables => $table => 'descr');
48
49 # datastructure describing this table
50 9 return {table => $table,
51 colgroups => $datasource->colgroups($table),
52 primary_key => [$datasource->primary_key($table)],
53 descr => $descr};
54
55 }
56
57
58 2 ▶sub list {
Subroutine list: called CC=5 SCAR=16.3
Pod documented
59 2 my ($self, $table) = @_;
60
61 2 my $context = $self->context;
62 2 my $req_data = $context->req_data;
63 2 my $datasource = $context->datasource;
64
65 # the "message" arg is sent once from inserts/updates/deletes; not to
66 # be repeated in links to other queries
67 2 my $message = delete $req_data->{-message};
68
69 # dashed args are set apart
70 2 my %where_args = %$req_data; # need a clone because of deletes below
71 2 my %dashed_args = $context->view->default_dashed_args($context);
72 2 foreach my $arg (grep {/^-/} keys %where_args) {
73 0 $dashed_args{$arg} = delete $where_args{$arg};
74 }
75
76 # some dashed args are treated here (not sent to the SQL request)
77 2 my $with_count = delete $dashed_args{-with_count};
78 2 my $template = delete $dashed_args{-template};
79 2 ▶ $context->set_template($template) if $template;
Branch: 1 branch
BranchTrueFalseTotal
if $template 0 2 2
80
81 # select from database
82 2 ▶ my $criteria = $datasource->query_parser->parse(\%where_args) || {};
Condition50%1/2
$datasource->query_parser->parse(\%where_args) || {}
l!l
20
MC/DC0%0/1
$datasource->query_parser->parse(\%where_args) || {}
$datasource->query_parser->parse(\%where_args)
Truth table50%1/2
A
A= $datasource->query_parser->parse(\%where_args)
Aresult
11
00
83 2 my $statement = $datasource->schema->db_table($table)->select(
84 -where => $criteria,
85 %dashed_args,
86 -result_as => 'statement',
87 );
88 2 my $rows = $statement->select();
89
90 # recuperate SQL for logging / informational purposes
91 2 my ($sql, @bind) = $statement->sql;
92 2 my $show_sql = join " / ", $sql, @bind;
93 2 $self->logger({level => 'debug', message => $show_sql});
94
95 # assemble results
96 2 my $data = $self->descr($table);
97 2 $data->{rows} = $rows;
98 2 $data->{message} = $message;
99 2 $data->{criteria} = $show_sql;
100 2 ▶ if ($with_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_count) 0 2 2
101 0 $data->{row_count} = $statement->row_count;
102 0 $data->{page_count} = $statement->page_count;
103 }
104
105 # links to prev/next pages
106 $self->_add_links_to_other_pages($data, $req_data,
107 $dashed_args{-page_index},
108 2 $dashed_args{-page_size});
109
110 # link to update/delete forms
111 $data->{where_args} = $self->_query_string(
112 2 map { ("where.$_" => $where_args{$_}) } keys %where_args,
113 );
114
115 2 return $data;
116 }
117
118
119 2 ▶sub _add_links_to_other_pages {
Subroutine _add_links_to_other_pages: called CC=5 SCAR=17.3
120 2 my ($self, $data, $req_data, $page_index, $page_size) = @_;
121
122 2 ▶ return unless defined $page_index && defined $page_size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page_index and defined $page_size 0 2 2
Condition33%1/3
defined $page_index and defined $page_size
!ll&&!rl&&r
002
MC/DC0%0/2
defined $page_index and defined $page_size
defined $page_index defined $page_size
Truth table33%1/3
A and B
A= defined $page_indexB= defined $page_size
ABresult
0X0
100
111
123
124 2 $data->{page_index} = $page_index;
125 2 $data->{offset} = ($page_index - 1) * $page_size + 1;
126 2 $data->{similar_query} = $self->_query_string(%$req_data,
127 -page_index => 1);
128 $data->{next_page} = $self->_query_string(%$req_data,
129 -page_index => $page_index+1)
130 2 ▶ unless @{$data->{rows}} < $page_size;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$data->{"rows"};} < $page_size 0 2 2
131 2 ▶ $data->{prev_page} = $self->_query_string(%$req_data,
Branch: 1 branch
BranchTrueFalseTotal
unless $page_index <= 1 0 2 2
132 -page_index => $page_index-1)
133 unless $page_index <= 1;
134 }
135
136
137
138 5 ▶sub id {
Subroutine id: called CC=1 SCAR=0.0
Pod undocumented
139 5 my ($self, $table) = @_;
140
141 5 my $data = $self->descr($table);
142
143 5 my $pk = $data->{primary_key};
144 5 my @vals = $self->context->extract_path_segments(scalar(@$pk));
145 5 my %criteria = mesh @$pk, @vals;
146
147 # get row from database
148 5 my $row = $self->datasource->schema->db_table($table)->fetch(@vals);
149
150 # assemble results
151 5 $data->{row} = $row;
152 5 $data->{pk_val} = join "/", @vals;
153
154 # links
155 5 my %where_pk = map { ("where_pk.$_" => $criteria{$_}) } keys %criteria;
156 5 $data->{where_pk} = $self->_query_string(%where_pk);
157
158 5 return $data;
159 }
160
161
162 2 ▶sub search {
Subroutine search: called CC=6 SCAR=18.1
Pod undocumented
163 2 my ($self, $table) = @_;
164
165 2 my $context = $self->context;
166 2 my $req_data = $context->req_data;
167
168 2 ▶ if ($context->req->method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->req->method eq 'POST') { } 1 1 2
169 1 ▶ my $output = delete $req_data->{-output} || "";
Condition50%1/2
delete $req_data->{'-output'} || ''
l!l
01
MC/DC0%0/1
delete $req_data->{'-output'} || ''
delete $req_data->{'-output'}
Truth table50%1/2
A
A= delete $req_data->{'-output'}
Aresult
11
00
170 1 ▶ my $cols = [keys %{delete $req_data->{col} || {}}];
Condition50%1/2
delete $req_data->{'col'} || {}
l!l
01
MC/DC0%0/1
delete $req_data->{'col'} || {}
delete $req_data->{'col'}
Truth table50%1/2
A
A= delete $req_data->{'col'}
Aresult
11
00
171 1 $req_data->{-columns} = join ",", @$cols;
172 1 $self->redirect("list$output?" . $self->_query_string(%$req_data));
173 }
174 else {
175 # display the search form
176 1 ▶ my @cols = split /,/, (delete $req_data->{-columns} || "");
Condition50%1/2
delete $req_data->{'-columns'} || ''
l!l
01
MC/DC0%0/1
delete $req_data->{'-columns'} || ''
delete $req_data->{'-columns'}
Truth table50%1/2
A
A= delete $req_data->{'-columns'}
Aresult
11
00
177 1 $req_data->{"col.$_"} = 1 foreach @cols;
178 1 my $data = $self->descr($table);
179 1 $data->{init_form} = $self->_encode_json($req_data);
180 1 return $data;
181 }
182 }
183
184
185 3 ▶sub update {
Subroutine update: called CC=2 SCAR=7.4
Pod undocumented
186 3 my ($self, $table) = @_;
187
188 3 $self->_check_canmodify;
189
190 3 ▶ if ($self->context->req->method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->context->req->method eq 'POST') { } 3 0 3
191 3 $self->_do_update_data($table);
192 }
193 else {
194 0 $self->_display_update_form($table);
195 }
196 }
197
198 8 ▶sub _check_canmodify {
Subroutine _check_canmodify: called CC=2 SCAR=8.1
199 8 my ($self) = @_;
200
201 8 ▶ if ($self->context->app->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->context->app->readonly) 0 8 8
202 0 die 'readonly mode';
203 }
204 }
205
206 3 ▶sub _do_update_data {
Subroutine _do_update_data: called CC=10 SCAR=23.5
207 3 my ($self, $table) = @_;
208
209 3 $self->_check_canmodify;
210
211 3 my $context = $self->context;
212 3 my $req_data = $context->req_data;
213 3 my $datasource = $context->datasource;
214
215 # columns to update
216 3 ▶ my $to_set = $req_data->{set} || {};
Condition50%1/2
$req_data->{'set'} || {}
l!l
30
MC/DC0%0/1
$req_data->{'set'} || {}
$req_data->{'set'}
Truth table50%1/2
A
A= $req_data->{'set'}
Aresult
11
00
217 3 foreach my $key (keys %$to_set) {
218 3 my $val = $to_set->{$key};
219 3 ▶ delete $to_set->{$key} if ! length $val;
Branch: 1 branch
BranchTrueFalseTotal
unless length $val 0 3 3
220 3 ▶ $to_set->{$key} = undef if $val eq 'Null';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "Null" 0 3 3
221 }
222 3 ▶ keys %$to_set or die "nothing to update";
Branch: 1 branch
BranchTrueFalseTotal
keys %$to_set or die "nothing to update" 3 0 3
223
224 # build filtering criteria
225 3 ▶ my $where = $req_data->{where} or die "update without any '-where' clause";
Branch: 1 branch
BranchTrueFalseTotal
my $where = $req_data->{"where"} or die "update without any '-where' clause" 2 1 3
226 2 my $criteria = $datasource->query_parser->parse($where);
227 2 ▶ $criteria and keys %$criteria or die "update without any '-where' criteria";
Branch: 1 branch
BranchTrueFalseTotal
$criteria and keys %$criteria or die "update without any '-where' criteria" 1 1 2
Condition66%2/3
$criteria and keys %$criteria
!ll&&!rl&&r
011
MC/DC50%1/2
$criteria and keys %$criteria
$criteria keys %$criteria
Truth table66%2/3
A and B
A= $criteriaB= keys %$criteria
ABresult
0X0
100
111
228
229 # perform the update
230 1 my $db_table = $datasource->schema->db_table($table);
231 1 my $n_updates = $db_table->update(-set => $to_set, -where => $criteria);
232
233 # redirect to a list to display the results
234 1 ▶ my $message = ($n_updates == 1) ? "1 record was updated"
Branch: 1 branch
BranchTrueFalseTotal
$n_updates == 1 ? : 1 0 1
235 : "$n_updates records were updated";
236 # TODO: $message could repeat the $to_set pairs
237 1 my $query_string = $self->_query_string(%$where, -message => $message);
238 1 $self->redirect("list?$query_string");
239 }
240
241 0 ▶sub _display_update_form {
Subroutine _display_update_form: not called CC=4 SCAR=30.0
242 0 my ($self, $table) = @_;
243
244 0 $self->_check_canmodify;
245
246 0 my $context = $self->context;
247 0 my $req_data = $context->req_data;
248 0 my $datasource = $context->datasource;
249 0 my $data = $self->descr($table);
250
251 0 ▶ if (my $where_pk = delete $req_data->{where_pk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where_pk = delete $req_data->{'where_pk'}) { } 0 0 0
252 # we got the primary key of one single record
253 0 $data->{where_pk} = $where_pk;
254 0 $req_data->{where} = $where_pk;
255
256 # fetch current values so that we can display them on page
257 0 my $criteria = $datasource->query_parser->parse($where_pk);
258 0 my $db_table = $datasource->schema->db_table($table);
259 0 $req_data->{curr} = $db_table->select(-where => $criteria,
260 -result_as => 'firstrow');
261 }
262 else {
263 # we got criteria that may touch several records
264 0 $self->_mark_multicols_keys($data);
265 }
266
267 # fields that should not be updatable
268 0 ▶ if (my $noupd = delete $req_data->{_noupd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $noupd = delete $req_data->{"_noupd"}) 0 0 0
269 0 $data->{noupd}{$_} = 1 foreach split qr[/], $noupd;
270 }
271
272 # initial values for the form
273 0 $data->{init_form} = $self->_encode_json($req_data);
274
275 0 return $data;
276 }
277
278
279 2 ▶sub delete {
Subroutine delete: called CC=7 SCAR=28.3
Pod undocumented
280 2 my ($self, $table) = @_;
281
282 2 $self->_check_canmodify;
283
284 2 my $context = $self->context;
285 2 my $req_data = $context->req_data;
286 2 my $datasource = $context->datasource;
287
288 2 ▶ if ($context->req->method eq 'POST') { # POST => delete in database
Branch: 1 branch
BranchTrueFalseTotal
if ($context->req->method eq 'POST') { } 2 0 2
289 # build filtering criteria
290 2 ▶ my $where = $req_data->{where} or die "delete without any '-where' clause";
Branch: 1 branch
BranchTrueFalseTotal
my $where = $req_data->{"where"} or die "delete without any '-where' clause" 1 1 2
291 1 my $criteria = $datasource->query_parser->parse($where);
292 1 ▶ $criteria and keys %$criteria or die "delete without any '-where' criteria";
Branch: 1 branch
BranchTrueFalseTotal
$criteria and keys %$criteria or die "delete without any '-where' criteria" 0 1 1
Condition33%1/3
$criteria and keys %$criteria
!ll&&!rl&&r
010
MC/DC0%0/2
$criteria and keys %$criteria
$criteria keys %$criteria
Truth table33%1/3
A and B
A= $criteriaB= keys %$criteria
ABresult
0X0
100
111
293
294 # perform the delete
295 0 my $db_table = $datasource->schema->db_table($table);
296 0 my $n_deletes = $db_table->delete(-where => $criteria);
297
298 # redirect to a list to display the results
299 0 ▶ my $message = ($n_deletes == 1) ? "1 record was deleted"
Branch: 1 branch
BranchTrueFalseTotal
$n_deletes == 1 ? : 0 0 0
300 : "$n_deletes records were deleted";
301 0 my $query_string = $self->_query_string(%$where, -message => $message);
302 0 $self->redirect("list?$query_string");
303 }
304 else { # GET => display the delete form
305 # display the delete form
306 0 my $data = $self->descr($table);
307 0 ▶ if (my $where_pk = delete $req_data->{where_pk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where_pk = delete $req_data->{'where_pk'}) { } 0 0 0
308 # we got the primary key of one single record
309 0 $data->{where_pk} = $where_pk;
310 0 $req_data->{where} = $where_pk;
311 }
312 else {
313 # we got criteria that may touch several records
314 0 $self->_mark_multicols_keys($data);
315 }
316
317 # initial values for the form
318 0 $data->{init_form} = $self->_encode_json($req_data);
319
320 0 return $data;
321 }
322 }
323
324
325
326
327 0 ▶sub clone {
Subroutine clone: not called CC=5 SCAR=34.0
Pod undocumented
328 0 my ($self, $table) = @_;
329
330 0 $self->_check_canmodify;
331
332 0 my $context = $self->context;
333 0 ▶ $context->req->method eq 'GET'
Branch: 1 branch
BranchTrueFalseTotal
$context->req->method eq "GET" or die "the /clone URL only accepts GET requests" 0 0 0
334 or die "the /clone URL only accepts GET requests";
335
336 # get primary key
337 0 my $data = $self->descr($table);
338 0 my $pk = $data->{primary_key};
339 0 my %is_pk = map {$_ => 1} @$pk;
340 0 my @vals = $context->extract_path_segments(scalar(@$pk));
341
342 # get row from database
343 0 my $row = $self->datasource->schema->db_table($table)->fetch(@vals);
344
345 # populate req_data before calling insert()
346 0 my $req_data = $context->req_data;
347 0 foreach my $col (keys %$row) {
348 0 my $val = $row->{$col};
349 0 ▶ $req_data->{$col} = $val if $val and !$is_pk{$col};
Branch: 1 branch
BranchTrueFalseTotal
if $val and not $is_pk{$col} 0 0 0
Condition0%0/3
$val and not $is_pk{$col}
!ll&&!rl&&r
000
MC/DC0%0/2
$val and not $is_pk{$col}
$val not $is_pk{$col}
Truth table0%0/3
A and not(B)
A= $valB= not $is_pk{$col}
ABresult
0X0
100
111
350 }
351
352 # cheat with path (simulating a call to insert())
353 0 my $path = $context->path;
354 0 $path =~ s/clone$/insert/;
355 0 $context->set_path($path);
356 0 $context->set_template('table/insert.tt');
357
358 # forward to insert()
359 0 $self->insert($table);
360 }
361
362
363 0 ▶sub insert {
Subroutine insert: not called CC=2 SCAR=17.9
Pod undocumented
364 0 my ($self, $table) = @_;
365
366 0 $self->_check_canmodify;
367
368 0 my $context = $self->context;
369 0 my $req_data = $context->req_data;
370 0 my $datasource = $context->datasource;
371
372 0 ▶ if ($context->req->method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->req->method eq 'POST') { } 0 0 0
373 # perform the insert
374 0 my $db_table = $datasource->schema->db_table($table);
375 0 my @pk = $db_table->insert($req_data);
376
377 # redirect to a list to display the results
378 0 my $message = "1 record was inserted";
379 0 my $query_string = $self->_query_string(-message => $message);
380 0 $self->redirect(join("/", "id", @pk) . "?$query_string");
381 }
382 else {
383 # display the insert form
384 0 my $data = $self->descr($table);
385 0 $data->{init_form} = $self->_encode_json($req_data);
386
387 0 return $data;
388 }
389 }
390
391
392
393 0 ▶sub count_where { # used in Ajax mode by update and delete forms
Subroutine count_where: not called CC=4 SCAR=30.0
Pod undocumented
394 0 my ($self, $table) = @_;
395
396 0 my $context = $self->context;
397 0 my $req_data = $context->req_data;
398 0 my $datasource = $context->datasource;
399
400 0 my $n_records = -1;
401
402 0 ▶ if (my $where = $req_data->{where}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where = $req_data->{"where"}) 0 0 0
403 0 my $criteria = $datasource->query_parser->parse($where);
404 0 ▶ if ($criteria and keys %$criteria) {
Branch: 1 branch
BranchTrueFalseTotal
if ($criteria and keys %$criteria) 0 0 0
Condition0%0/3
$criteria and keys %$criteria
!ll&&!rl&&r
000
MC/DC0%0/2
$criteria and keys %$criteria
$criteria keys %$criteria
Truth table0%0/3
A and B
A= $criteriaB= keys %$criteria
ABresult
0X0
100
111
405 0 my $db_table = $datasource->schema->db_table($table);
406 0 my $result = $db_table->select(
407 -columns => 'COUNT(*)',
408 -where => $criteria,
409 -result_as => 'flat_arrayref',
410 );
411 0 $n_records = $result->[0];
412 }
413 }
414
415 0 return {n_records => $n_records};
416 }
417
418
419
420
421 #----------------------------------------------------------------------
422 # auxiliary methods
423 #----------------------------------------------------------------------
424
425
426 11 ▶sub _query_string {
Subroutine _query_string: called CC=4 SCAR=13.9
427 11 my ($self, %params) = @_;
428 11 my @fragments;
429 KEY:
430 11 foreach my $key (sort keys %params) {
431 12 my $val = $params{$key};
432 12 ▶ length $val or next KEY;
Branch: 1 branch
BranchTrueFalseTotal
length $val or next KEY 11 1 12
433
434 # cheap URI escape (for chars '=', '&', ';' and '+')
435 11 s/=/%3D/g, s/&/%26/g, s/;/%3B/g, s/\+/%2B/g for $key, $val;
436
437 11 push @fragments, "$key=$val";
438 }
439
440 11 return join "&", @fragments;
441 }
442
443
444 1 ▶sub _encode_json {
Subroutine _encode_json: called CC=1 SCAR=0.0
445 1 my ($self, $data) = @_;
446
447 # utf8-encoding is done in the view, so here we turn it off
448 1 my $json_maker = JSON::MaybeXS->new(allow_blessed => 1,
449 convert_blessed => 1,
450 utf8 => 0);
451 1 return $json_maker->encode($data);
452 }
453
454
455 0 ▶sub _mark_multicols_keys {
Subroutine _mark_multicols_keys: not called CC=4 SCAR=30.0
456 0 my ($self, $data) = @_;
457
458 0 ▶ if (my $sep = $self->datasource->schema->sql_abstract->multicols_sep) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sep = $self->datasource->schema->sql_abstract->multicols_sep) 0 0 0
459 # in case of multi-columns keys, the form needs to add special fields
460 # and to ignore regular fields for those columns
461 0 ▶ my $where = $self->context->req_data->{where} || {};
Condition0%0/2
$self->context->req_data->{'where'} || {}
l!l
00
MC/DC0%0/1
$self->context->req_data->{'where'} || {}
$self->context->req_data->{'where'}
Truth table0%0/2
A
A= $self->context->req_data->{'where'}
Aresult
11
00
462 0 my @multi_cols_keys = grep m[$sep], keys %$where;
463 0 $data->{multi_cols_keys} = \@multi_cols_keys;
464 0 $data->{ignore_col}{$_} = 1 foreach map {split m[$sep]} @multi_cols_keys;
465 }
466 }
467
468
469 1 1;
470
471 __END__
« AutoCRUD/DataSource.pm ↑ Summary AutoCRUD/Context.pm »