Basic/Five.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 117 CC 7 SCAR 23.1
CC
7
Cov
0%
setup_db_interface
30.0
start
12.0
cgiapp_get_query
2.0
CRAP 10.1
1 package CGI::Application::Demo::Basic::Five;
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 use base 'CGI::Application';
14 use strict;
15 use warnings;
16  
17 require 5.005_62;
18  
19 use CGI::Application::Demo::Basic::Base;
20 use CGI::Application::Demo::Basic::Faculty;
21 use CGI::Application::Demo::Basic::Util::Config;
22 use CGI::Simple;
23 use Class::DBI::Loader;
24  
25 our $VERSION = '1.06';
26  
27 # -----------------------------------------------
28  
29 sub cgiapp_get_query
30 {
31 my($self) = @_;
32  
33 return CGI::Simple -> new;
34  
35 } # End of cgiapp_get_query.
36  
37 # -----------------------------------------------
38  
39 sub cgiapp_init
40 {
41 my($self) = @_;
42 my($config) = CGI::Application::Demo::Basic::Util::Config -> new('five.conf') -> config;
43  
44 $self -> param(config => $config);
45 $self -> param(tmpl_path => $$config{'tmpl_path'});
46  
47 # Set up the classes for each table, via the magic of Class::DBI.
48 # I have used a constraint because this is a demo, and I've only
49 # created one module for Class::DBI to chew on:
50 # CGI::Application::Demo::Basic::Faculty.
51  
52 my($loader) = Class::DBI::Loader -> new
53 (
54 constraint => '^faculty$',
55 dsn => $$config{'dsn'},
56 user => $$config{'username'},
57 password => $$config{'password'},
58 namespace => '',
59 relationships => 1,
60 );
61  
62 $self -> setup_db_interface($loader);
63 $self -> param(dbh => ${$self -> param('cgi_app_demo_classes')}[0] -> db_Main);
64  
65 } # End of cgiapp_init.
66  
67 # -----------------------------------------------
68  
69 sub setup
70 {
71 my($self) = @_;
72  
73 $self -> run_modes(start => \&start);
74 $self -> tmpl_path($self -> param('tmpl_path') );
75  
76 } # End of setup.
77  
78 # -----------------------------------------------
79  
80 sub setup_db_interface
81 {
82 my($self, $parameter ) = @_;
83 my($classes) = [];
84  
85 if (ref($parameter) eq 'ARRAY')
86 {
87 for my $cdbi_class (@$parameter)
88 {
89 # Check to see if it's loaded already.
90  
91 if (! $cdbi_class::)
92 {
93 my($file) = $cdbi_class;
94 $file =~ s|::|/|g;
95  
96 eval
97 {
98 require "$file.pm";
99  
100 $cdbi_class -> import;
101 };
102  
103 die "CGI::Application::Demo::Basic::setup_db_interface: Couldn't require class: $cdbi_class: $@" if ($@);
104 }
105  
106 push @$classes, $cdbi_class;
107 }
108 }
109 elsif (ref($parameter) =~ /^Class::DBI::Loader/)
110 {
111 push @$classes, $_ for $parameter -> classes;
112 }
113 else
114 {
115 my($ref) = ref($parameter);
116  
117 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.";
118 }
119  
120 $self -> param(cgi_app_demo_classes => $classes);
121  
122 my($tables) = {};
123  
124 for my $cdbi_class (@{$self -> param('cgi_app_demo_classes')})
125 {
126 my($table) = $cdbi_class -> table;
127 $$tables{$table} = $cdbi_class;
128 }
129  
130 $self -> param(cgi_app_demo_tables => $tables);
131  
132 } # End of setup_db_interface.
133  
134 # -----------------------------------------------
135  
136 sub start
137 {
138 my($self) = shift;
139 my($config) = $self -> param('config');
140 my($template) = $self -> load_tmpl($$config{'tmpl_name'});
141 my($db_vendor) = CGI::Application::Demo::Basic::Base -> db_vendor;
142 my(@content) =
143 (
144 'Time: ' . scalar localtime,
145 'URL: ' . $self -> query -> url,
146 'PathInfo: ' . $self -> query -> path_info,
147 "CGI::Simple V $CGI::Simple::VERSION",
148 "Class::DBI::Loader V $Class::DBI::Loader::VERSION",
149 "DBI V $DBI::VERSION",
150 'Template name: ' . $$config{'tmpl_name'},
151 'Template path: ' . $self -> param('tmpl_path'),
152 'DSN: ' . $$config{'dsn'},
153 'Username: ' . $$config{'username'},
154 'dbh: ' . $self -> param('dbh'),
155 "DB vendor: $db_vendor",
156 );
157  
158 # Test Class::DBI::Loader.
159  
160 my $iterator = CGI::Application::Demo::Basic::Faculty -> retrieve_all;
161  
162 while ($_ = $iterator -> next)
163 {
164 push @content, qq|<span class="$$config{'css_class'}">| . $_ -> faculty_id . ': ' . $_ -> faculty_name . '</span>';
165 }
166  
167 $$self{'table'} = 'log';
168 my($sql) = $db_vendor eq 'ORACLE'
169 ? "insert into $$self{'table'} (id, lvl, message, timestamp) values (log_seq.nextval, ?, ?, localtimestamp)"
170 : $db_vendor eq 'SQLITE'
171 ? "insert into $$self{'table'} (lvl, message, timestamp) values (?, ?, 'now')"
172 : "insert into $$self{'table'} (lvl, message, timestamp) values (?, ?, now() )"; # MySQL, Postgres.
173 my($sth) = $self -> param('dbh') -> prepare($sql);
174  
175 $sth -> execute('info', __PACKAGE__ . ": Testing SQL: $sql");
176 $sth -> finish;
177  
178 $template -> param(css_url => $$config{'css_url'});
179 $template -> param(li_loop => [map{ {item => $_} } @content]);
180 $template -> param(title => __PACKAGE__);
181  
182 $self -> param('dbh') -> disconnect;
183  
184 return $template -> output;
185  
186 } # End of start.
187  
188 # -----------------------------------------------
189  
190 1;