blib/lib/Mojolicious/Plugin/Marky/DbTableSet.pm

filter: Statementstmt 25.7% 72 / 280 Branchbran 2.9% 1 / 34 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 42.1% 16 / 38 Podpod 100% 1 / 1 totaltotal 24.7% 91 / 368 SCAR 60.3
CC
29
Cov
23%
_do_query
56.0
_set_options
42.0
_save_new_bookmark
30.0
CRAP 414.3
« blib/lib/Marky/DbTable.pm ↑ Summary blib/lib/Marky/Bookmarker.pm »
1 package Mojolicious::Plugin::Marky::DbTableSet;
2 2 $Mojolicious::Plugin::Marky::DbTableSet::VERSION = '0.034';
3 #ABSTRACT: Mojolicious::Plugin::Marky::DbTableSet - querying one database table
4  
5 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Marky::DbTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Marky::Bookmarker;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Search::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Sort::Naturally;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Text::NeatTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use HTML::TagCloud;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 sub register {
21 1 ▶ my ( $self, $app, $conf ) = @_;
Subroutine register: called CC=1 SCAR=2.3
Pod documented
22  
23 1 $self->_init($app,$conf);
24  
25 $app->helper( 'marky_do_query' => sub {
26 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 my %args = @_;
28  
29 0 return $self->_do_query($c);
30 1 } );
31  
32 $app->helper( 'marky_table_list' => sub {
33 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 my %args = @_;
35  
36 0 return $self->_make_table_list($c);
37 1 } );
38  
39 $app->helper( 'marky_table_array' => sub {
40 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
41 1 my %args = @_;
42  
43 1 my @tables = sort keys %{$self->{dbtables}};
44 1 return \@tables;
45 1 } );
46 $app->helper( 'marky_total_records' => sub {
47 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 my %args = @_;
49  
50 0 return $self->_total_records($c,%args);
51 1 } );
52  
53 $app->helper( 'marky_db_related_list' => sub {
54 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 my %args = @_;
56  
57 0 return $self->_make_db_related_list($c,%args);
58 1 } );
59  
60 $app->helper( 'marky_taglist' => sub {
61 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 0 my %args = @_;
63  
64 0 return $self->_taglist($c,%args);
65 1 } );
66  
67 $app->helper( 'marky_tagcloud' => sub {
68 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 my %args = @_;
70  
71 0 return $self->_tagcloud($c,%args);
72 1 } );
73  
74 $app->helper( 'marky_set_options' => sub {
75 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 my %args = @_;
77  
78 0 return $self->_set_options($c,%args);
79 1 } );
80 $app->helper( 'marky_settings' => sub {
81 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 my %args = @_;
83  
84 0 return $self->_settings($c,%args);
85 1 } );
86 $app->helper( 'marky_add_bookmark_form' => sub {
87 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 my %args = @_;
89  
90 0 return $self->_add_bookmark_form($c,%args);
91 1 } );
92 $app->helper( 'marky_add_bookmark_bookmarklet' => sub {
93 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 0 my %args = @_;
95  
96 0 return $self->_add_bookmark_bookmarklet($c,%args);
97 1 } );
98 $app->helper( 'marky_save_new_bookmark' => sub {
99 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 my %args = @_;
101  
102 0 return $self->_save_new_bookmark($c,%args);
103 1 } );
104 }
105  
106  
107 sub _init {
108 1 ▶ my $self = shift;
Subroutine _init: called CC=4 SCAR=14.6
109 1 my $app = shift;
110 1 my $conf = shift;
111  
112 1 $self->{route_prefix} = $app->config->{route_prefix};
113 1 $self->{dbtables} = {};
114 1 $self->{edit_tables} = {};
115 1 foreach my $t (sort keys %{$app->config->{tables}})
116 {
117 1 $self->{dbtables}->{$t} = Marky::DbTable->new(%{$app->config->{tables}->{$t}});
118 1 ▶ if (exists $app->config->{tables}->{$t}->{editing}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $app->config->{"tables"}{$t}{"editing"} and ref $app->config->{"tables"}{$t}{"editing"} eq "HASH") 0 1 1
Condition33%1/3
exists $app->config->{"tables"}{$t}{"editing"} and ref $app->config->{"tables"}{$t}{"editing"} eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
exists $app->config->{"tables"}{$t}{"editing"} and ref $app->config->{"tables"}{$t}{"editing"} eq "HASH"
exists $app->config->{"tables"}{$t}{"editing"} ref $app->config->{"tables"}{$t}{"editing"} eq "HASH"
Truth table33%1/3
A and B
A= exists $app->config->{"tables"}{$t}{"editing"}B= ref $app->config->{"tables"}{$t}{"editing"} eq "HASH"
ABresult
0X0
100
111
119 and ref $app->config->{tables}->{$t}->{editing} eq 'HASH')
120 {
121 0 $self->{edit_tables}->{$t} = Marky::Bookmarker->new(%{$app->config->{tables}->{$t}->{editing}});
122 }
123 }
124 1 return $self;
125 } # _init
126  
127  
128 sub _do_query {
129 0 ▶ my $self = shift;
Subroutine _do_query: not called CC=7 SCAR=40.3
130 0 my $c = shift;
131 0 my $app = $c->app;
132  
133 0 my $db = $c->param('db');
134 0 ▶ if (!exists $self->{dbtables}->{$db})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"dbtables"}{$db}) 0 0 0
135 {
136 0 $c->render(template => 'apperror',
137 errormsg=>"<p>No such db: $db</p>");
138 0 return undef;
139 }
140  
141 0 my $tags = $c->param('tags');
142 0 my $q = $c->param('q');
143 0 my $p = $c->param('p');
144 0 my $where = $c->param('where');
145  
146 0 my $n = $c->session('n');
147 0 my $sort_by = $c->session("${db}_sort_by");
148 0 my $sort_by2 = $c->session("${db}_sort_by2");
149 0 my $sort_by3 = $c->session("${db}_sort_by3");
150  
151 0 my $delterm = $c->param('delterm');
152 0 ▶ if ($delterm && $q)
Branch: 1 branch
BranchTrueFalseTotal
if ($delterm and $q) 0 0 0
Condition0%0/3
$delterm and $q
!ll&&!rl&&r
000
MC/DC0%0/2
$delterm and $q
$delterm $q
Truth table0%0/3
A and B
A= $deltermB= $q
ABresult
0X0
100
111
153 {
154 0 $q =~ s/\b[ +]?$delterm\b//;
155 0 $q =~ s/[ +]$//;
156 0 $q =~ s/^[ +]//;
157 0 $c->param('q'=>$q);
158 0 $c->param(delterm=>undef);
159 }
160 0 my $deltag = $c->param('deltag');
161 0 ▶ if ($deltag && $tags)
Branch: 1 branch
BranchTrueFalseTotal
if ($deltag and $tags) 0 0 0
Condition0%0/3
$deltag and $tags
!ll&&!rl&&r
000
MC/DC0%0/2
$deltag and $tags
$deltag $tags
Truth table0%0/3
A and B
A= $deltagB= $tags
ABresult
0X0
100
111
162 {
163 0 $tags =~ s/\b[ +]?$deltag\b//;
164 0 $tags =~ s/[ +]$//;
165 0 $tags =~ s/^[ +]//;
166 0 $c->param('tags'=>$tags);
167 0 $c->param(deltag=>undef);
168 }
169 0 my $opt_url = $c->url_for("/db/$db/opt");
170 0 my $location = $c->url_for("/db/$db");
171 my $res = $self->{dbtables}->{$db}->query(location=>$location,
172 opt_url=>$opt_url,
173 db=>$db,
174 q=>$q,
175 tags=>$tags,
176 where=>$where,
177 n=>$n,
178 p=>$p,
179 sort_by=>$sort_by,
180 sort_by2=>$sort_by2,
181 sort_by3=>$sort_by3,
182 show_sql=>$app->config->{tables}->{$db}->{show_sql},
183 0 );
184 0 ▶ if (!defined $res)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
185 {
186 $c->render(template => 'apperror',
187 0 errormsg=>$self->{dbtables}->{$db}->what_error());
188 0 return undef;
189 }
190  
191 0 $c->content('footer',$res->{pagination});
192 0 $c->content_for('footer',$res->{searchform});
193 0 $c->stash('query_taglist', $res->{query_tags});
194  
195 0 $c->stash('results' => $res->{results});
196 0 $c->render(template => 'results');
197 } # _do_query
198  
199  
200 sub _total_records {
201 0 ▶ my $self = shift;
Subroutine _total_records: not called CC=2 SCAR=17.9
202 0 my $c = shift;
203  
204 0 my $db = $c->param('db');
205  
206 0 my $total = $self->{dbtables}->{$db}->total_records();
207 0 ▶ if (!defined $total)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $total) 0 0 0
208 {
209 $c->render(template => 'apperror',
210 0 errormsg=>$self->{dbtables}->{$db}->what_error());
211 0 return undef;
212 }
213 0 return $total;
214 } # _total_records
215  
216  
217 sub _make_db_related_list {
218 0 ▶ my $self = shift;
Subroutine _make_db_related_list: not called CC=2 SCAR=17.9
219 0 my $c = shift;
220  
221 0 my $db = $c->param('db');
222 0 my $db_url = $c->url_for("/db/$db");
223 0 my @out = ();
224 0 push @out, "<div class='dblist'><ul>";
225 0 push @out, "<li><a href='${db_url}'>$db</a></li>";
226 0 foreach my $t (qw(taglist tagcloud))
227 {
228 0 push @out, "<li><a href='${db_url}/$t'>$db $t</a></li>";
229 }
230 0 push @out, "</ul></div>";
231 0 my $out = join("\n", @out);
232 0 return $out;
233 } # _make_db_related_list
234  
235  
236 sub _make_table_list {
237 0 ▶ my $self = shift;
Subroutine _make_table_list: not called CC=2 SCAR=17.9
238 0 my $c = shift;
239  
240 0 my @out = ();
241 0 push @out, "<div class='dblist'><ul>";
242 0 foreach my $t (sort keys %{$self->{dbtables}})
243 {
244 0 my $url = $c->url_for("/db/$t");
245 0 push @out, "<li><a href='$url'>$t</a></li>";
246 }
247 0 push @out, "</ul></div>";
248 0 my $out = join("\n", @out);
249 0 return $out;
250 } # _make_table_list
251  
252  
253 sub _taglist {
254 0 ▶ my $self = shift;
Subroutine _taglist: not called CC=2 SCAR=17.9
255 0 my $c = shift;
256  
257 0 my $db = $c->param('db');
258 0 my $opt_url = $c->url_for("/db/$db/opt");
259 0 my $location = $c->url_for("/db/$db");
260 0 my $res = $self->{dbtables}->{$db}->taglist(location=>$location,
261 opt_url=>$opt_url,
262 db=>$db,
263 n=>0,
264 );
265 0 ▶ if (!defined $res)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
266 {
267 $c->render(template => 'apperror',
268 0 errormsg=>$self->{dbtables}->{$db}->what_error());
269 0 return undef;
270 }
271 0 return $res->{results};
272 } # _taglist
273  
274  
275 sub _tagcloud {
276 0 ▶ my $self = shift;
Subroutine _tagcloud: not called CC=2 SCAR=17.9
277 0 my $c = shift;
278  
279 0 my $db = $c->param('db');
280 0 my $opt_url = $c->url_for("/db/$db/opt");
281 0 my $location = $c->url_for("/db/$db");
282 0 my $res = $self->{dbtables}->{$db}->tagcloud(location=>$location,
283 opt_url=>$opt_url,
284 db=>$db,
285 n=>0,
286 );
287 0 ▶ if (!defined $res)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
288 {
289 $c->render(template => 'apperror',
290 0 errormsg=>$self->{dbtables}->{$db}->what_error());
291 0 return undef;
292 }
293 0 return $res->{results};
294 } # _tagcloud
295  
296  
297 sub _set_options {
298 0 ▶ my $self = shift;
Subroutine _set_options: not called CC=6 SCAR=37.4
299 0 my $c = shift;
300 0 my %args = @_;
301  
302 # Set options for things like n
303 0 my @db = (sort keys %{$self->{dbtables}});
304  
305 0 my @fields = (qw(n));
306 0 foreach my $db (@db)
307 {
308 0 push @fields, "${db}_sort_by";
309 0 push @fields, "${db}_sort_by2";
310 0 push @fields, "${db}_sort_by3";
311 }
312 0 my %fields_set = ();
313 0 foreach my $field (@fields)
314 {
315 0 my $val = $c->param($field);
316 0 ▶ if ($val)
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 0 0 0
317 {
318 0 $c->session->{$field} = $val;
319 0 $fields_set{$field} = 1;
320 }
321 else
322 {
323 0 ▶ if ($field =~ /(\w+)_sort_by./)
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /(\w+)_sort_by./u) 0 0 0
324 {
325 # We want to delete later sort-by values
326 # if they are blank and the first one was set,
327 # because we want to be able to sort by just
328 # one or two fields if we want.
329 0 my $db = $1;
330 0 ▶ if ($fields_set{"${db}_sort_by"})
Branch: 1 branch
BranchTrueFalseTotal
if ($fields_set{"${db}_sort_by"}) 0 0 0
331 {
332 0 delete $c->session->{$field};
333 }
334 }
335 }
336 }
337 0 my $referrer = $c->req->headers->referrer;
338 } # _set_options
339  
340  
341 sub _settings {
342 0 ▶ my $self = shift;
Subroutine _settings: not called CC=2 SCAR=17.9
343 0 my $c = shift;
344 0 my %args = @_;
345  
346 0 my $db = $c->param('db');
347  
348 0 my @fields = (qw(n));
349 0 push @fields, "${db}_sort_by";
350 0 push @fields, "${db}_sort_by2";
351 0 push @fields, "${db}_sort_by3";
352
353 0 my @out = ();
354 0 foreach my $field (@fields)
355 {
356 0 my $val = $c->session->{$field};
357 0 push @out, "<p><b>$field:</b> $val</p>";
358 }
359 0 my $referrer = $c->req->headers->referrer;
360 0 print STDERR "_settings referrer=$referrer\n";
361 0 push @out, "<p>Back: <a href='$referrer'>$referrer</a></p>";
362  
363 0 return join("\n", @out);
364 } # _settings
365  
366  
367 sub _add_bookmark_form {
368 0 ▶ my $self = shift;
Subroutine _add_bookmark_form: not called CC=4 SCAR=30.0
369 0 my $c = shift;
370 0 my %args = @_;
371  
372 0 my $db = $c->param('db');
373 0 ▶ if (!exists $self->{edit_tables}->{$db})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"edit_tables"}{$db}) 0 0 0
374 {
375 0 $c->render(template => 'apperror',
376 errormsg=>"<p>No such editable db: $db</p>");
377 0 return;
378 }
379  
380 0 my %data = ();
381 0 my @fields = $self->{edit_tables}->{$db}->fields();
382 0 foreach my $fn (@fields)
383 {
384 0 my $val = $c->param($fn);
385 0 ▶ if (defined $val)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
386 {
387 0 $data{$fn} = $val;
388 }
389 }
390 0 my $add_url = $c->url_for("/db/$db/add");
391 0 return $self->{edit_tables}->{$db}->bookmark_form(
392 action=>$add_url,
393 %data,
394 );
395 } # _add_bookmark_form
396  
397  
398 sub _add_bookmark_bookmarklet {
399 0 ▶ my $self = shift;
Subroutine _add_bookmark_bookmarklet: not called CC=2 SCAR=17.9
400 0 my $c = shift;
401 0 my %args = @_;
402  
403 0 my $db = $c->param('db');
404 0 ▶ if (!exists $self->{edit_tables}->{$db})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"edit_tables"}{$db}) 0 0 0
405 {
406 0 $c->render(template => 'apperror',
407 errormsg=>"<p>No such editable db: $db</p>");
408 0 return;
409 }
410  
411 0 my $add_url = $c->url_for("/db/$db/add")->to_abs;
412 0 return $self->{edit_tables}->{$db}->bookmarklet(
413 %args,
414 action=>$add_url,
415 );
416 } # _add_bookmark_bookmarklet
417  
418  
419 sub _save_new_bookmark {
420 0 ▶ my $self = shift;
Subroutine _save_new_bookmark: not called CC=5 SCAR=34.0
421 0 my $c = shift;
422 0 my %args = @_;
423  
424 0 my $db = $c->param('db');
425 0 ▶ if (!exists $self->{edit_tables}->{$db})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"edit_tables"}{$db}) 0 0 0
426 {
427 0 $c->render(template => 'apperror',
428 errormsg=>"<p>No such editable db: $db</p>");
429 0 return;
430 }
431 0 my @status = ();
432 0 push @status, "<div class='status'>";
433 0 my %data = ();
434 0 my @fields = $self->{edit_tables}->{$db}->fields();
435 0 foreach my $fn (@fields)
436 {
437 0 my $val = $c->param($fn);
438 0 ▶ if (defined $val)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
439 {
440 0 $data{$fn} = $val;
441 0 push @status, "<p><span class='field'>$fn</span>: $val</p>";
442 }
443 }
444 0 push @status, "</div>";
445 0 $c->content('results', join("\n", @status));
446 0 ▶ if (!$self->{edit_tables}->{$db}->save_new_bookmark(data=>\%data))
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'edit_tables'}{$db}->save_new_bookmark('data', \%data)) { } 0 0 0
447 {
448 0 $c->content_for('results',"<p class='error'>Bookmark-save failed.</p>");
449 }
450 else
451 {
452 0 $c->content_for('results',"<p>Bookmark saved.</p>");
453 }
454 } # _save_new_bookmark
455  
456 2 1; # End of Mojolicious::Plugin::Marky::DbTableSet
457  
458 __END__
« blib/lib/Marky/DbTable.pm ↑ Summary blib/lib/Marky/Bookmarker.pm »