Mojolicious/Plugin/Marky/DbTableSet.pm

filter: Statementstmt 30.0% 70 / 233 Branchbran 3.5% 1 / 28 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 47.0% 16 / 34 Podpod 100% 1 / 1 totaltotal 28.6% 89 / 311 CC 23 SCAR 25.9
CC
23
Cov
27%
_do_query
56.0
_save_new_bookmark
30.0
_add_bookmark_form
20.0
CRAP 13.3
« Marky/Bookmarker.pm ↑ Summary Marky/Controller/Db.pm »
1 package Mojolicious::Plugin::Marky::DbTableSet;
2 2 $Mojolicious::Plugin::Marky::DbTableSet::VERSION = '0.0604';
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 1 ▶sub register {
Subroutine register: called CC=1 SCAR=2.1
Pod documented
21 1 my ( $self, $app, $conf ) = @_;
22  
23 1 $self->_init($app,$conf);
24  
25 0 ▶ $app->helper( 'marky_do_query' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 my $c = shift;
27 0 my %args = @_;
28  
29 0 return $self->_do_query($c);
30 1 } );
31  
32 0 ▶ $app->helper( 'marky_table_list' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 my $c = shift;
34 0 my %args = @_;
35  
36 0 return $self->_make_table_list($c);
37 1 } );
38  
39 1 ▶ $app->helper( 'marky_table_array' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 my $c = shift;
41 1 my %args = @_;
42  
43 1 my @tables = sort keys %{$self->{dbtables}};
44 1 return \@tables;
45 1 } );
46 0 ▶ $app->helper( 'marky_total_records' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 my $c = shift;
48 0 my %args = @_;
49  
50 0 return $self->_total_records($c,%args);
51 1 } );
52  
53 0 ▶ $app->helper( 'marky_db_related_list' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 my $c = shift;
55 0 my %args = @_;
56  
57 0 return $self->_make_db_related_list($c,%args);
58 1 } );
59  
60 0 ▶ $app->helper( 'marky_taglist' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 my $c = shift;
62 0 my %args = @_;
63  
64 0 return $self->_taglist($c,%args);
65 1 } );
66  
67 0 ▶ $app->helper( 'marky_tagcloud' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 my $c = shift;
69 0 my %args = @_;
70  
71 0 return $self->_tagcloud($c,%args);
72 1 } );
73  
74 0 ▶ $app->helper( 'marky_add_bookmark_form' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 my $c = shift;
76 0 my %args = @_;
77  
78 0 return $self->_add_bookmark_form($c,%args);
79 1 } );
80 0 ▶ $app->helper( 'marky_add_bookmark_bookmarklet' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 my $c = shift;
82 0 my %args = @_;
83  
84 0 return $self->_add_bookmark_bookmarklet($c,%args);
85 1 } );
86 0 ▶ $app->helper( 'marky_save_new_bookmark' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 my $c = shift;
88 0 my %args = @_;
89  
90 0 return $self->_save_new_bookmark($c,%args);
91 1 } );
92 }
93  
94  
95 1 ▶sub _init {
Subroutine _init: called CC=4 SCAR=14.6
96 1 my $self = shift;
97 1 my $app = shift;
98 1 my $conf = shift;
99  
100 1 $self->{route_prefix} = $app->config->{route_prefix};
101 1 $self->{dbtables} = {};
102 1 $self->{edit_tables} = {};
103 1 foreach my $t (sort keys %{$app->config->{tables}})
104 {
105 1 $self->{dbtables}->{$t} = Marky::DbTable->new(%{$app->config->{tables}->{$t}});
106 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
107 and ref $app->config->{tables}->{$t}->{editing} eq 'HASH')
108 {
109 0 $self->{edit_tables}->{$t} = Marky::Bookmarker->new(%{$app->config->{tables}->{$t}->{editing}});
110 }
111 }
112 1 return $self;
113 } # _init
114  
115  
116 0 ▶sub _do_query {
Subroutine _do_query: not called CC=7 SCAR=40.3
117 0 my $self = shift;
118 0 my $c = shift;
119 0 my $app = $c->app;
120  
121 0 my $db = $c->param('db');
122 0 ▶ if (!exists $self->{dbtables}->{$db})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"dbtables"}{$db}) 0 0 0
123 {
124 0 $c->render(template => 'apperror',
125 errormsg=>"<p>No such db: $db</p>");
126 0 return undef;
127 }
128  
129 0 my $tags = $c->param('tags');
130 0 my $q = $c->param('q');
131 0 my $p = $c->param('p');
132 0 my $where = $c->param('where');
133 0 my $n = $c->param('n');
134 0 my $sort_by = $c->param("sort_by");
135 0 my $sort_by2 = $c->param("sort_by2");
136 0 my $sort_by3 = $c->param("sort_by3");
137 0 my $sort_by4 = $c->param("sort_by4");
138  
139 0 my $delterm = $c->param('delterm');
140 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
141 {
142 0 $q =~ s/\b[ +]?$delterm\b//;
143 0 $q =~ s/[ +]$//;
144 0 $q =~ s/^[ +]//;
145 0 $c->param('q'=>$q);
146 0 $c->param(delterm=>undef);
147 }
148 0 my $deltag = $c->param('deltag');
149 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
150 {
151 0 $tags =~ s/\b[ +]?$deltag\b//;
152 0 $tags =~ s/[ +]$//;
153 0 $tags =~ s/^[ +]//;
154 0 $c->param('tags'=>$tags);
155 0 $c->param(deltag=>undef);
156 }
157 0 my $location = $c->url_for("/db/$db");
158 my $res = $self->{dbtables}->{$db}->query(location=>$location,
159 db=>$db,
160 q=>$q,
161 tags=>$tags,
162 where=>$where,
163 n=>$n,
164 p=>$p,
165 sort_by=>$sort_by,
166 sort_by2=>$sort_by2,
167 sort_by3=>$sort_by3,
168 sort_by4=>$sort_by4,
169 show_sql=>$app->config->{tables}->{$db}->{show_sql},
170 0 );
171 0 ▶ if (!defined $res)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
172 {
173 $c->render(template => 'apperror',
174 0 errormsg=>$self->{dbtables}->{$db}->what_error());
175 0 return undef;
176 }
177  
178 0 $c->content('footer',$res->{pagination});
179 0 $c->content_for('footer',$res->{searchform});
180 0 $c->stash('query_taglist', $res->{query_tags});
181  
182 0 $c->stash('results' => $res->{results});
183 0 $c->render(template => 'results');
184 } # _do_query
185  
186  
187 0 ▶sub _total_records {
Subroutine _total_records: not called CC=2 SCAR=17.9
188 0 my $self = shift;
189 0 my $c = shift;
190  
191 0 my $db = $c->param('db');
192  
193 0 my $total = $self->{dbtables}->{$db}->total_records();
194 0 ▶ if (!defined $total)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $total) 0 0 0
195 {
196 $c->render(template => 'apperror',
197 0 errormsg=>$self->{dbtables}->{$db}->what_error());
198 0 return undef;
199 }
200 0 return $total;
201 } # _total_records
202  
203  
204 0 ▶sub _make_db_related_list {
Subroutine _make_db_related_list: not called CC=2 SCAR=17.9
205 0 my $self = shift;
206 0 my $c = shift;
207  
208 0 my $db = $c->param('db');
209 0 my $db_url = $c->url_for("/db/$db");
210 0 my @out = ();
211 0 push @out, "<div class='dblist'><ul>";
212 0 push @out, "<li><a href='${db_url}'>$db</a></li>";
213 0 foreach my $t (qw(taglist tagcloud))
214 {
215 0 push @out, "<li><a href='${db_url}/$t'>$db $t</a></li>";
216 }
217 0 push @out, "</ul></div>";
218 0 my $out = join("\n", @out);
219 0 return $out;
220 } # _make_db_related_list
221  
222  
223 0 ▶sub _make_table_list {
Subroutine _make_table_list: not called CC=2 SCAR=17.9
224 0 my $self = shift;
225 0 my $c = shift;
226  
227 0 my @out = ();
228 0 push @out, "<div class='dblist'><ul>";
229 0 foreach my $t (sort keys %{$self->{dbtables}})
230 {
231 0 my $url = $c->url_for("/db/$t");
232 0 push @out, "<li><a href='$url'>$t</a></li>";
233 }
234 0 push @out, "</ul></div>";
235 0 my $out = join("\n", @out);
236 0 return $out;
237 } # _make_table_list
238  
239  
240 0 ▶sub _taglist {
Subroutine _taglist: not called CC=2 SCAR=17.9
241 0 my $self = shift;
242 0 my $c = shift;
243  
244 0 my $db = $c->param('db');
245 0 my $location = $c->url_for("/db/$db");
246 0 my $res = $self->{dbtables}->{$db}->taglist(location=>$location,
247 db=>$db,
248 n=>0,
249 );
250 0 ▶ if (!defined $res)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
251 {
252 $c->render(template => 'apperror',
253 0 errormsg=>$self->{dbtables}->{$db}->what_error());
254 0 return undef;
255 }
256 0 return $res->{results};
257 } # _taglist
258  
259  
260 0 ▶sub _tagcloud {
Subroutine _tagcloud: not called CC=2 SCAR=17.9
261 0 my $self = shift;
262 0 my $c = shift;
263  
264 0 my $db = $c->param('db');
265 0 my $location = $c->url_for("/db/$db");
266 0 my $res = $self->{dbtables}->{$db}->tagcloud(location=>$location,
267 db=>$db,
268 n=>0,
269 );
270 0 ▶ if (!defined $res)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
271 {
272 $c->render(template => 'apperror',
273 0 errormsg=>$self->{dbtables}->{$db}->what_error());
274 0 return undef;
275 }
276 0 return $res->{results};
277 } # _tagcloud
278  
279  
280 0 ▶sub _add_bookmark_form {
Subroutine _add_bookmark_form: not called CC=4 SCAR=30.0
281 0 my $self = shift;
282 0 my $c = shift;
283 0 my %args = @_;
284  
285 0 my $db = $c->param('db');
286 0 ▶ if (!exists $self->{edit_tables}->{$db})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"edit_tables"}{$db}) 0 0 0
287 {
288 0 $c->render(template => 'apperror',
289 errormsg=>"<p>No such editable db: $db</p>");
290 0 return;
291 }
292  
293 0 my %data = ();
294 0 my @fields = $self->{edit_tables}->{$db}->fields();
295 0 foreach my $fn (@fields)
296 {
297 0 my $val = $c->param($fn);
298 0 ▶ if (defined $val)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
299 {
300 0 $data{$fn} = $val;
301 }
302 }
303 0 my $add_url = $c->url_for("/db/$db/add");
304 0 return $self->{edit_tables}->{$db}->bookmark_form(
305 action=>$add_url,
306 %data,
307 );
308 } # _add_bookmark_form
309  
310  
311 0 ▶sub _add_bookmark_bookmarklet {
Subroutine _add_bookmark_bookmarklet: not called CC=2 SCAR=17.9
312 0 my $self = shift;
313 0 my $c = shift;
314 0 my %args = @_;
315  
316 0 my $db = $c->param('db');
317 0 ▶ if (!exists $self->{edit_tables}->{$db})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"edit_tables"}{$db}) 0 0 0
318 {
319 0 $c->render(template => 'apperror',
320 errormsg=>"<p>No such editable db: $db</p>");
321 0 return;
322 }
323  
324 0 my $add_url = $c->url_for("/db/$db/add")->to_abs;
325 0 return $self->{edit_tables}->{$db}->bookmarklet(
326 %args,
327 action=>$add_url,
328 );
329 } # _add_bookmark_bookmarklet
330  
331  
332 0 ▶sub _save_new_bookmark {
Subroutine _save_new_bookmark: not called CC=5 SCAR=34.0
333 0 my $self = shift;
334 0 my $c = shift;
335 0 my %args = @_;
336  
337 0 my $db = $c->param('db');
338 0 ▶ if (!exists $self->{edit_tables}->{$db})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"edit_tables"}{$db}) 0 0 0
339 {
340 0 $c->render(template => 'apperror',
341 errormsg=>"<p>No such editable db: $db</p>");
342 0 return;
343 }
344 0 my @status = ();
345 0 push @status, "<div class='status'>";
346 0 my %data = ();
347 0 my @fields = $self->{edit_tables}->{$db}->fields();
348 0 foreach my $fn (@fields)
349 {
350 0 my $val = $c->param($fn);
351 0 ▶ if (defined $val)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
352 {
353 0 $data{$fn} = $val;
354 0 push @status, "<p><span class='field'>$fn</span>: $val</p>";
355 }
356 }
357 0 push @status, "</div>";
358 0 $c->content('results', join("\n", @status));
359 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
360 {
361 0 $c->content_for('results',"<p class='error'>Bookmark-save failed.</p>");
362 }
363 else
364 {
365 0 $c->content_for('results',"<p>Bookmark saved.</p>");
366 }
367 } # _save_new_bookmark
368  
369 2 1; # End of Mojolicious::Plugin::Marky::DbTableSet
370  
371 __END__
« Marky/Bookmarker.pm ↑ Summary Marky/Controller/Db.pm »