Basic.pm

filter: Statementstmt 14.6% 27 / 184 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 40.0% 8 / 20 Podpod 25.0% 3 / 12 totaltotal 15.2% 38 / 249 CC 25 SCAR 32.6
CC
25
Cov
13%
setup_db_interface
72.0
build_commands_output
42.0
update_options
30.0
CRAP 26.0
↑ Summary Basic/Five.pm »
1 package CGI::Application::Demo::Basic;
2  
3 # Documentation:
4 # POD-style documentation is at the end. Extract it with pod2html.*.
5 #
6 # Note:
7 # o tab = 4 spaces || die
8 #
9 # Author:
10 # Ron Savage <ron@savage.net.au>
11 # http://savage.net.au/index.html
12  
13 1 ▶use base 'CGI::Application';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 require 5.005_62;
18  
19 1 ▶use CGI::Application::Demo::Basic::Util::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use CGI::Application::Plugin::LogDispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use CGI::Application::Plugin::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use CGI::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Class::DBI::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 our $VERSION = '1.06';
26  
27 # -----------------------------------------------
28  
29 sub build_basic_pane
30 0 ▶{
Subroutine build_basic_pane: not called CC=3 SCAR=24.8
Pod undocumented
31 0 my($self, $submit) = @_;
32 0 my($content) = $self -> load_tmpl('basic.tmpl');
33 0 ▶ my($count) = $self -> session -> param('count') || 0;
Condition0%0/2
$self->session->param('count') || 0
l!l
00
MC/DC0%0/1
$self->session->param('count') || 0
$self->session->param('count')
Truth table0%0/2
A
A= $self->session->param('count')
Aresult
11
00
34  
35 0 $count++;
36  
37 0 $self -> session -> param(count => $count);
38  
39 0 my(@tr_loop);
40  
41 0 for my $table (sort keys %{$self -> param('cgi_app_demo_tables')})
42 {
43 0 my($class) = ${$self -> param('cgi_app_demo_tables')}{$table};
44 0 my(@column) = $self -> get_columns($class);
45 0 my(@column_name) = sort @{$column[2]};
46  
47 0 push @tr_loop,
48 {
49 th => 'Table',
50 td => $table,
51 },
52 {
53 th => 'Class',
54 td => $class,
55 },
56 {
57 th => 'Columns',
58 td => join(', ', @column_name),
59 },
60 }
61  
62 0 $content -> param(count => "sub build_basic_pane has run $count time(s)");
63 0 $content -> param(tr_loop => \@tr_loop);
64 0 $content -> param(commands => $self -> build_commands_output
65 ([
66 'Refresh',
67 ]) );
68 0 $content -> param(notes => $self -> build_notes_output
69 ([
70 'Hint: Click Refresh (below)',
71 "Previous command: $submit",
72 ]) );
73  
74 0 return $content -> output;
75  
76 } # End of build_basic_pane.
77  
78 # -----------------------------------------------
79  
80 sub build_commands_output
81 0 ▶{
Subroutine build_commands_output: not called CC=6 SCAR=37.4
Pod undocumented
82 0 my($self, $command) = @_;
83 0 my($content) = $self -> load_tmpl('commands.tmpl');
84 0 my(@loop) = ();
85 0 my($max_column_count) = $self -> param('columns_of_commands_option');
86 0 my($row_count) = int( (@$command + $max_column_count - 1) / $max_column_count);
87 0 my($command_index) = - 1;
88  
89 0 my($row, $col);
90  
91 0 for $row (1 .. $row_count)
92 {
93 0 my(@td_loop);
94  
95 0 for $col (1 .. $max_column_count)
96 {
97 0 $command_index++;
98  
99 0 ▶ next if ($command_index > $#$command);
Branch: 1 branch
BranchTrueFalseTotal
if $command_index > $command->$#* 0 0 0
100  
101 0 ▶ if (ref($$command[$command_index]) eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $command->[$command_index] eq 'ARRAY') { } 0 0 0
102 {
103 0 push @td_loop, {td => $$command[$command_index][0], onClick => $$command[$command_index][1]};
104 }
105 else
106 {
107 0 push @td_loop, {td => $$command[$command_index]};
108 }
109 }
110  
111 0 push @loop, {col_loop => \@td_loop};
112 }
113  
114 0 ▶ $content -> param(commands => $#$command == 0 ? 'Command' : 'Commands');
Branch: 1 branch
BranchTrueFalseTotal
$command->$#* == 0 ? : 0 0 0
115 0 $content -> param(row_loop => \@loop);
116  
117 0 return $content -> output;
118  
119 } # End of build_commands_output.
120  
121 # -----------------------------------------------
122  
123 sub build_notes_output
124 0 ▶{
Subroutine build_notes_output: not called CC=2 SCAR=17.9
Pod undocumented
125 0 my($self, $note) = @_;
126 0 my($content) = $self -> load_tmpl('notes.tmpl');
127 0 my(@loop) = ();
128  
129 0 push @loop, {td => $_} for (@$note);
130  
131 0 $content -> param(note_loop => \@loop);
132  
133 0 return $content -> output;
134  
135 } # End of build_notes_output.
136  
137 # -----------------------------------------------
138  
139 sub build_options_pane
140 0 ▶{
Subroutine build_options_pane: not called CC=2 SCAR=17.9
Pod undocumented
141 0 my($self, $submit) = @_;
142 0 my($content) = $self -> load_tmpl('options.tmpl');
143 0 my(@key) = sort keys %{${$self -> param('key')}{'option'} };
144  
145 0 my(@loop, $minimum, $maximum, $s);
146  
147 0 for (@key)
148 {
149 0 $minimum = ${$self -> param('key')}{'option'}{$_}{'minimum'};
150 0 $maximum = ${$self -> param('key')}{'option'}{$_}{'maximum'};
151 0 ($s = $_) =~ s/_option$//;
152 0 $s =~ tr/_/ /;
153 0 $s = "Number of $s ($minimum .. $maximum)";
154  
155 0 push @loop,
156 {
157 option => $s,
158 name => $_,
159 value => $self -> session -> param($_),
160 };
161 }
162  
163 0 $content -> param(commands => $self -> build_commands_output
164 ([
165 ['Update options', q|onClick = "set('update_options')"|],
166 ]) );
167 0 $content -> param(notes => $self -> build_notes_output
168 ([
169 'DSN: ' . $self -> param('dsn'),
170 "Previous command: $submit",
171 ]) );
172 0 $content -> param(tr_loop => \@loop);
173  
174 0 return $content -> output;
175  
176 } # End of build_options_pane.
177  
178 # -----------------------------------------------
179  
180 sub cgiapp_get_query
181 0 ▶{
Subroutine cgiapp_get_query: not called CC=1 SCAR=6.9
Pod documented
182 0 my($self) = @_;
183  
184 0 return CGI::Simple -> new;
185  
186 } # End of cgiapp_get_query.
187  
188 # -----------------------------------------------
189  
190 sub cgiapp_init
191 0 ▶{
Subroutine cgiapp_init: not called CC=4 SCAR=30.0
Pod documented
192 0 my($self) = @_;
193 0 my($config) = CGI::Application::Demo::Basic::Util::Config -> new('basic.conf') -> config;
194  
195 # All this stuff is here so that we can call
196 # CGI::Application::Plugin::LogDispatch's log_config, if at all,
197 # in cgiapp_init (as suggested by its docs) rather than in setup.
198  
199 0 $self -> param(config => $config);
200 0 $self -> param(css_url => $$config{'css_url'});
201 0 $self -> param(dsn => $$config{'dsn'});
202 0 $self -> param(title => $$config{'dsn'});
203 0 $self -> param(tmpl_path => $$config{'tmpl_path'});
204  
205 # Set up the classes for each table, via the magic of Class::DBI.
206 # I have used a constraint because this is a demo, and I've only
207 # created one module for Class::DBI to chew on:
208 # CGI::Application::Demo::Basic::Faculty.
209  
210 my($loader) = Class::DBI::Loader -> new
211 (
212 constraint => '^faculty$',
213 dsn => $$config{'dsn'},
214 user => $$config{'username'},
215 password => $$config{'password'},
216 0 options => $$config{'dsn_attribute'},
217 namespace => '',
218 relationships => 1,
219 );
220  
221 0 $self -> setup_db_interface($loader);
222 0 $self -> param(dbh => ${$self -> param('cgi_app_demo_classes')}[0] -> db_Main);
223  
224 # Set up interface to logger.
225  
226 0 $self -> log_config
227 (
228 LOG_DISPATCH_MODULES =>
229 [{
230 dbh => $self -> param('dbh'),
231 min_level => 'info',
232 module => 'CGI::Application::Demo::Basic::Util::LogDispatchDBI',
233 name => __PACKAGE__,
234 },
235 ]
236 );
237  
238 # Set up interface to CGI::Session.
239  
240 $self -> session_config
241 (
242 CGI_SESSION_OPTIONS => [$$config{'session_driver'}, $self -> query, {Handle => $self -> param('dbh')}],
243 0 DEFAULT_EXPIRY => $$config{'session_timeout'},
244 SEND_COOKIE => 0,
245 );
246  
247 # Recover options from session, if possible.
248 # If not, initialize them.
249 # This hash holds details of the set of options.
250  
251 0 $self -> param(key =>
252 {
253 option =>
254 {
255 columns_of_commands_option =>
256 {
257 default => 3,
258 maximum => 20,
259 minimum => 1,
260 size => 2,
261 type => 'integer',
262 },
263 records_per_page_option =>
264 {
265 default => 100,
266 maximum => 1000,
267 minimum => 1,
268 size => 4,
269 type => 'integer',
270 },
271 },
272 });
273  
274 0 my(@key) = keys %{${$self -> param('key')}{'option'} };
275  
276 0 $self -> param($_ => $self -> session -> param($_) ) for @key;
277  
278 # Pick any option to see if they've all be initialized.
279  
280 0 ▶ if (! $self -> param('records_per_page_option') )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->param("records_per_page_option")) 0 0 0
281 {
282 0 my($value);
283  
284 0 for (@key)
285 {
286 0 $value = ${$self -> param('key')}{'option'}{$_}{'default'};
287  
288 0 $self -> param($_ => $value);
289 0 $self -> session -> param($_ => $value);
290 }
291 }
292  
293 } # End of cgiapp_init.
294  
295 # --------------------------------------------------
296 # Note: This code retrieves the config in order to access 'dsn'.
297 # This illustrates a different method of accessing config data
298 # than, say, sub setup. The latter uses the fact that some data
299 # (tmpl_path) has been copied out of the config into an app param.
300 # This copying took place near the start of sub cgiapp_init.
301 # In the same way (as the latter technique) sub start uses
302 # css_url, which was also copied in sub cgiapp_init.
303  
304 sub db_vendor
305 0 ▶{
Subroutine db_vendor: not called CC=1 SCAR=6.9
Pod undocumented
306 0 my($self) = @_;
307 0 my($config) = $self -> param('config');
308 0 my($vendor) = $$config{'dsn'} =~ /[^:]+:([^:]+):/;
309  
310 0 return uc $vendor;
311  
312 } # End of db_vendor.
313  
314 # -----------------------------------------------
315 # Given a class we return an array of 3 elements:
316 # 0: An array ref of primary column names
317 # 1: An array ref of all other column names
318 # 2: An array ref of all column names
319 # The names are in the order returned by the class, which is best because
320 # the database designer probably set up the table with the columns in a
321 # specific order, and the names of the primary key columns are in a
322 # specific order anyway. And the caller can sort the [1] if desired.
323  
324 sub get_columns
325 0 ▶{
Subroutine get_columns: not called CC=1 SCAR=6.9
Pod undocumented
326 0 my($self, $class) = @_;
327 0 my(@column) = $class -> columns;
328 0 my(@primary_column) = $class -> primary_columns;
329  
330 0 my(%primary_column);
331  
332 0 @primary_column{@primary_column} = (1) x @primary_column;
333 0 my(@other_column) = grep{! $primary_column{$_} } @column;
334  
335 0 return ([@primary_column], [@other_column], [@column]);
336  
337 } # End of get_columns.
338  
339 # -----------------------------------------------
340  
341 sub setup
342 0 ▶{
Subroutine setup: not called CC=1 SCAR=6.9
Pod documented
343 0 my($self) = @_;
344  
345 0 $self -> run_modes(start => \&start, update_options => \&update_options);
346 0 $self -> tmpl_path($self -> param('tmpl_path') );
347  
348 } # End of setup.
349  
350 # -----------------------------------------------
351  
352 sub setup_db_interface
353 0 ▶{
Subroutine setup_db_interface: not called CC=8 SCAR=42.8
Pod undocumented
354 0 my($self, $parameter ) = @_;
355 0 my($classes) = [];
356  
357 0 ▶ if (ref($parameter) eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $parameter eq 'ARRAY') { } 0 0 0
elsif (ref($parameter) =~ /^Class::DBI::Loader/) { } 0 0 0
358 {
359 0 for my $cdbi_class (@$parameter)
360 {
361 # Check to see if it's loaded already.
362  
363 0 ▶ if (! $cdbi_class::)
Branch: 1 branch
BranchTrueFalseTotal
unless ($main::cdbi_class::) 0 0 0
364 {
365 0 my($file) = $cdbi_class;
366 0 $file =~ s|::|/|g;
367  
368 eval
369 0 {
370 0 require "$file.pm";
371  
372 0 $cdbi_class -> import;
373 };
374  
375 0 ▶ die "CGI::Application::Demo::Basic::setup_db_interface: Couldn't require class: $cdbi_class: $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
376 }
377  
378 0 push @$classes, $cdbi_class;
379 }
380 }
381 elsif (ref($parameter) =~ /^Class::DBI::Loader/)
382 {
383 0 push @$classes, $_ for $parameter -> classes;
384 }
385 else
386 {
387 0 my($ref) = ref($parameter);
388  
389 0 die "CGI::Application::Demo::Basic::setup_db_interface: Invalid parameter\nParameter must either be an array reference of Class::DBI classes or a Class::DBI::Loader object\nYou gave me a $ref object.";
390 }
391  
392 0 $self -> param(cgi_app_demo_classes => $classes);
393  
394 0 my($tables) = {};
395  
396 0 for my $cdbi_class (@{$self -> param('cgi_app_demo_classes')})
397 {
398 0 my($table) = $cdbi_class -> table;
399 0 $$tables{$table} = $cdbi_class;
400 }
401  
402 0 $self -> param(cgi_app_demo_tables => $tables);
403  
404 } # End of setup_db_interface.
405  
406 # -----------------------------------------------
407  
408 sub start
409 0 ▶{
Subroutine start: not called CC=2 SCAR=17.9
Pod undocumented
410 0 my($self) = shift;
411 0 my($config) = $self -> param('config');
412 0 ▶ my($submit) = $self -> query -> param('submit') || '';
Condition0%0/2
$self->query->param('submit') || ''
l!l
00
MC/DC0%0/1
$self->query->param('submit') || ''
$self->query->param('submit')
Truth table0%0/2
A
A= $self->query->param('submit')
Aresult
11
00
413 0 my($template) = $self -> load_tmpl($$config{'tmpl_name'});
414 0 my($content) = $self -> build_basic_pane($submit) . $self -> build_options_pane($submit);
415  
416 0 $template -> param(content => $content);
417 0 $template -> param(css_url => $self -> param('css_url') );
418 0 $template -> param(rm => $self -> query -> param('rm') );
419 0 $template -> param(sid => $self -> session -> id);
420 0 $template -> param(title => $self -> param('title') );
421 0 $template -> param(url => $self -> query -> url . $self -> query -> path_info);
422  
423 0 return $template -> output;
424  
425 } # End of start.
426  
427 # -----------------------------------------------
428  
429 sub update_options
430 0 ▶{
Subroutine update_options: not called CC=5 SCAR=34.0
Pod undocumented
431 0 my($self) = @_;
432 0 my(@key) = keys %{${$self -> param('key')}{'option'} };
433  
434 0 $self -> log -> info('Called update_options');
435  
436 0 my($value, $default, $minimum, $maximum);
437  
438 0 for (@key)
439 {
440 0 $default = ${$self -> param('key')}{'option'}{$_}{'default'};
441 0 $minimum = ${$self -> param('key')}{'option'}{$_}{'minimum'};
442 0 $maximum = ${$self -> param('key')}{'option'}{$_}{'maximum'};
443 0 $value = $self -> query -> param($_);
444 0 ▶ $value = $default if (! defined($value) || ($value < $minimum) || ($value > $maximum) );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value < $minimum or $value > $maximum 0 0 0
Condition0%0/6
not defined $value or $value < $minimum or $value > $maximum
l!l&&r!l&&!r
000
not defined $value or $value < $minimum
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $value or $value < $minimum or $value > $maximum
not defined $value $value < $minimum $value > $maximum
Truth table0%0/4
not(A) or B or C
A= not defined $valueB= $value < $minimumC= $value > $maximum
ABCresult
1XX1
01X1
0011
0000
445  
446 0 $self -> param($_ => $value);
447 0 $self -> session -> param($_ => $value);
448 }
449  
450 0 return $self -> start;
451  
452 } # End of update_options.
453  
454 # -----------------------------------------------
455  
456 1 1;
457  
458 __END__
↑ Summary Basic/Five.pm »