blib/lib/Template/Provider/DBI.pm

filter: Statementstmt 77.1% 54 / 70 Branchbran 38.2% 13 / 34 Conditioncond 30.3% 10 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 87.5% 7 / 8 Podpod 50.0% 1 / 2 totaltotal 50.9% 85 / 167 CC 32 SCAR 30.2
CC
32
Cov
56%
convert_timestamp
42.0
_init
28.5
fetch
7.5
CRAP 20.5
↑ Summary
1 package Template::Provider::DBI;
2  
3 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use DateTime::Format::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base 'Template::Provider';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.03';
8  
9 sub _init
10 {
11 # check dbi specific params from args, then call super
12 # modified timestamps?
13 # precompiled?
14 1 ▶ my ($self, $args) = @_;
Subroutine _init: called CC=16 SCAR=33.5
15  
16 1 $self->{$_} = $args->{$_} for keys %$args;
17  
18 1 ▶ if($self->{DBI_DBH} && $self->{DBI_DSN})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DBI_DBH"} and $self->{"DBI_DSN"}) 0 1 1
Condition33%1/3
$self->{"DBI_DBH"} and $self->{"DBI_DSN"}
!ll&&!rl&&r
010
MC/DC0%0/2
$self->{"DBI_DBH"} and $self->{"DBI_DSN"}
$self->{"DBI_DBH"} $self->{"DBI_DSN"}
Truth table33%1/3
A and B
A= $self->{"DBI_DBH"}B= $self->{"DBI_DSN"}
ABresult
0X0
100
111
19 {
20 0 return $self->error("DBI:Can't use DBI_DBH and DBI_DSN at the same time");
21 }
22  
23 1 ▶ if($self->{DBI_DSN})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DBI_DSN"}) 0 1 1
24 {
25 0 ▶ $self->{DBI_DBH} = DBI->connect($self->{DBI_DSN}, $self->{DBI_USER}, $self->{DBI_PASSWD}) or return $self->error("DBI: Failed to connect to $self->{DBI_DSN}, $self->{DBI_USER}, $self->{DBI_PASSWD}, $DBI::errstr");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DBI_DBH"} = "DBI"->connect($self->{"DBI_DSN"}, $self->{"DBI_USER"}, $self->{"DBI_PASSWD"}) or return $self->error(qq[DBI: Failed to connect to $self->{"DBI_DSN"}, $self->{"DBI_USER"}, $self->{"DBI_PASSWD"}, $DBI::errstr]) 0 0 0
26 }
27  
28 1 ▶ $self->{DBI_TABLE} ||= 'templates';
Condition50%1/2
$self->{"DBI_TABLE"} ||= "templates"
l!l
01
MC/DC0%0/1
$self->{"DBI_TABLE"} ||= "templates"
$self->{"DBI_TABLE"}
Truth table50%1/2
A
A= $self->{"DBI_TABLE"}
Aresult
11
00
29 1 ▶ $self->{DBI_MODIFIEDFIELD} ||= 'modified';
Condition50%1/2
$self->{"DBI_MODIFIEDFIELD"} ||= "modified"
l!l
01
MC/DC0%0/1
$self->{"DBI_MODIFIEDFIELD"} ||= "modified"
$self->{"DBI_MODIFIEDFIELD"}
Truth table50%1/2
A
A= $self->{"DBI_MODIFIEDFIELD"}
Aresult
11
00
30 1 ▶ $self->{DBI_TMPLFIELD} ||= 'template';
Condition50%1/2
$self->{"DBI_TMPLFIELD"} ||= "template"
l!l
01
MC/DC0%0/1
$self->{"DBI_TMPLFIELD"} ||= "template"
$self->{"DBI_TMPLFIELD"}
Truth table50%1/2
A
A= $self->{"DBI_TMPLFIELD"}
Aresult
11
00
31 1 ▶ $self->{DBI_FILEFIELD} ||= 'filename';
Condition50%1/2
$self->{"DBI_FILEFIELD"} ||= "filename"
l!l
01
MC/DC0%0/1
$self->{"DBI_FILEFIELD"} ||= "filename"
$self->{"DBI_FILEFIELD"}
Truth table50%1/2
A
A= $self->{"DBI_FILEFIELD"}
Aresult
11
00
32 1 ▶ $self->{DBI_CURRENTTIME} ||= 'current_timestamp';
Condition50%1/2
$self->{"DBI_CURRENTTIME"} ||= "current_timestamp"
l!l
01
MC/DC0%0/1
$self->{"DBI_CURRENTTIME"} ||= "current_timestamp"
$self->{"DBI_CURRENTTIME"}
Truth table50%1/2
A
A= $self->{"DBI_CURRENTTIME"}
Aresult
11
00
33 # $self->{DBI_QUERY} ||= "SELECT $self->{DBI_TMPLFIELD} FROM $self->{DBI_TABLE} WHERE $self->{DBI_FILEFIELD} = ?";
34
35 1 eval {
36 1 ▶ $self->{DBI_DT} ||= DateTime::Format::DBI->new($self->{DBI_DBH}); };
Condition33%1/3
$self->{"DBI_DT"} ||= "DateTime::Format::DBI"->new($self->{"DBI_DBH"})
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"DBI_DT"} ||= "DateTime::Format::DBI"->new($self->{"DBI_DBH"})
$self->{"DBI_DT"} "DateTime::Format::DBI"->new($self->{"DBI_DBH"})
Truth table33%1/3
A ||= B
A= $self->{"DBI_DT"}B= "DateTime::Format::DBI"->new($self->{"DBI_DBH"})
ABresult
1X1
011
000
37 1 ▶ if($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
38 {
39 1 warn "DateTime::Format:: for $self->{DBI_DBH}->{Driver}->{Name} not found, no caching supported";
40 1 $self->{DBI_DT} = undef;
41 1 $self->{DBI_MODIFIEDFIELD} = '';
42 }
43  
44 1 my $modified = '';
45 1 ▶ $modified = ", $self->{DBI_MODIFIEDFIELD}" if($self->{DBI_MODIFIEDFIELD});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DBI_MODIFIEDFIELD"} 0 1 1
46 1 ▶ $self->{DBI_QUERY} ||= "SELECT $self->{DBI_TMPLFIELD} $modified FROM $self->{DBI_TABLE} WHERE $self->{DBI_FILEFIELD} = ?";
Condition50%1/2
$self->{"DBI_QUERY"} ||= qq[SELECT $self->{"DBI_TMPLFIELD"} $modified FROM $self->{"DBI_TABLE"} WHERE $self->{"DBI_FILEFIELD"} = ?]
l!l
01
MC/DC0%0/1
$self->{"DBI_QUERY"} ||= qq[SELECT $self->{"DBI_TMPLFIELD"} $modified FROM $self->{"DBI_TABLE"} WHERE $self->{"DBI_FILEFIELD"} = ?]
$self->{"DBI_QUERY"}
Truth table50%1/2
A
A= $self->{"DBI_QUERY"}
Aresult
11
00
47 1 ▶ $self->{DBI_STH} = $self->{DBI_DBH}->prepare_cached($self->{DBI_QUERY}) or
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DBI_STH"} = $self->{"DBI_DBH"}->prepare_cached($self->{"DBI_QUERY"}) or return $self->error("DBI:Failed to prepare query: ${DBI}:errstr") 0 1 1
48 return $self->error("DBI:Failed to prepare query: $DBI:errstr");
49 1 return $self->SUPER::_init();
50 }
51  
52 sub fetch
53 {
54 # called to fetch template by name
55 # return (undef. status_declined) on missing (and on error & tolerant)
56 # return ($error, status_error) on error
57  
58 1 ▶ my ($self, $name) = @_;
Subroutine fetch: called CC=6 SCAR=20.2
Pod documented
59 1 my ($data, $error);
60 # we dont do refs or handles:
61 1 ▶ return (undef, Template::Constants::STATUS_DECLINED) if(ref($name));
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 0 1 1
62  
63 # Check if caching is allowed / file has been cached
64 # my $compiled = $self->_compiled_filename($name);
65 1 ▶ if(defined $self->{ SIZE } && $self->{ LOOKUP }->{ $name })
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'SIZE'} and $self->{'LOOKUP'}{$name}) { } 0 1 1
Condition33%1/3
defined $self->{"SIZE"} and $self->{"LOOKUP"}{$name}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $self->{"SIZE"} and $self->{"LOOKUP"}{$name}
defined $self->{"SIZE"} $self->{"LOOKUP"}{$name}
Truth table33%1/3
A and B
A= defined $self->{"SIZE"}B= $self->{"LOOKUP"}{$name}
ABresult
0X0
100
111
66 {
67 0 ($data, $error) = _fetch($name);
68 }
69 else
70 {
71 1 ($data, $error) = $self->_load($name);
72 1 ▶ ($data, $error) = $self->_compile($data) unless($error);
Branch: 1 branch
BranchTrueFalseTotal
unless $error 1 0 1
73 1 ▶ $data = $self->_store($name, $data) unless($error);
Branch: 1 branch
BranchTrueFalseTotal
unless $error 1 0 1
74 }
75
76 1 return ($data, $error);
77
78 }
79  
80 sub _load
81 {
82 1 ▶ my ($self, $name) = @_;
Subroutine _load: called CC=5 SCAR=17.1
83  
84 1 my $data = {};
85 # fetch template from db
86 1 $self->{DBI_STH}->execute($name);
87 1 my ($templ, $modified) = $self->{DBI_STH}->fetchrow_array();
88 1 ▶ return (undef, Template::Constants::STATUS_DECLINED)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $templ 0 1 1
89 if(!defined $templ);
90 1 ▶ if($modified && exists $self->{DBI_DT})
Branch: 1 branch
BranchTrueFalseTotal
if ($modified and exists $self->{'DBI_DT'}) { } 0 1 1
Condition33%1/3
$modified and exists $self->{"DBI_DT"}
!ll&&!rl&&r
100
MC/DC0%0/2
$modified and exists $self->{"DBI_DT"}
$modified exists $self->{"DBI_DT"}
Truth table33%1/3
A and B
A= $modifiedB= exists $self->{"DBI_DT"}
ABresult
0X0
100
111
91 {
92 # No "modified" field used, as we have no DT::Format::X
93 0 $data->{time} = $self->convert_timestamp($modified);
94 }
95 else
96 {
97 1 $data->{time} = time();
98 }
99 1 $data->{load} = time();
100 1 $data->{name} = $name;
101 1 $data->{text} = $templ;
102  
103 1 ▶ my $err = $DBI::errstr if(!$templ);
Branch: 1 branch
BranchTrueFalseTotal
unless $templ 0 1 1
104  
105 1 return ($data, $err);
106 }
107  
108 ## _store uses stat on the filename, bah
109 ## patch to call _mtime($name) ?
110  
111 sub _modified
112 {
113 1 ▶ my ($self, $name) = @_;
Subroutine _modified: called CC=3 SCAR=17.3
114  
115 1 ▶ if(!defined $self->{DBI_DT})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"DBI_DT"}) 1 0 1
116 {
117 1 return time();
118 }
119  
120 0 my $sth = $self->{DBI_DBH}->prepare_cached(<<SQL);
121 SELECT $self->{DBI_MODIFIEDFIELD}
122 FROM $self->{DBI_TABLE}
123 WHERE $self->{DBI_FILEFIELD} = ?
124 SQL
125  
126 0 $sth->execute($name);
127 0 my ($result) = $sth->fetchrow_array();
128  
129 0 ▶ return $self->convert_timestamp($result) || $result;
Condition0%0/3
$self->convert_timestamp($result) || $result
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->convert_timestamp($result) || $result
$self->convert_timestamp($result) $result
Truth table0%0/3
A || B
A= $self->convert_timestamp($result)B= $result
ABresult
1X1
011
000
130  
131 }
132  
133 sub convert_timestamp
134 {
135 0 ▶ my ($self, $timestamp) = @_;
Subroutine convert_timestamp: not called CC=6 SCAR=37.4
Pod undocumented
136  
137 0 ▶ return time() if(!$timestamp);
Branch: 1 branch
BranchTrueFalseTotal
unless $timestamp 0 0 0
138 0 ▶ if($self->{DBI_DT} && $self->{DBI_DT}->can('parse_timestamp'))
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'DBI_DT'} and $self->{'DBI_DT'}->can('parse_timestamp')) { } 0 0 0
elsif ($self->{'DBI_DT'} and $self->{'DBI_DT'}->can('format_datetime')) { } 0 0 0
Condition0%0/3
$self->{"DBI_DT"} and $self->{"DBI_DT"}->can("parse_timestamp")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"DBI_DT"} and $self->{"DBI_DT"}->can("parse_timestamp")
$self->{"DBI_DT"} $self->{"DBI_DT"}->can("parse_timestamp")
Truth table0%0/3
A and B
A= $self->{"DBI_DT"}B= $self->{"DBI_DT"}->can("parse_timestamp")
ABresult
0X0
100
111
139 {
140 0 my $dt = $self->{DBI_DT}->parse_timestamp($timestamp);
141 0 return $dt->epoch;
142 }
143 ▶ elsif($self->{DBI_DT} && $self->{DBI_DT}->can('format_datetime'))
Condition0%0/3
$self->{"DBI_DT"} and $self->{"DBI_DT"}->can("format_datetime")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"DBI_DT"} and $self->{"DBI_DT"}->can("format_datetime")
$self->{"DBI_DT"} $self->{"DBI_DT"}->can("format_datetime")
Truth table0%0/3
A and B
A= $self->{"DBI_DT"}B= $self->{"DBI_DT"}->can("format_datetime")
ABresult
0X0
100
111
144 {
145 0 my $dt = $self->{DBI_DT}->parse_datetime($timestamp);
146 0 return $dt->epoch;
147 }
148 0 return 0;
149 }
150  
151  
152 1 1;
153  
154 __END__
155  
156 =head1 NAME
157  
158 Template::Provider::DBI - A class to allow retrieval of templates from a DB
159  
160 =head1 VERSION
161  
162 This documentation refers to version 0.01.
163  
164 =head1 SYNOPSIS
165  
166 use Template::Provider::DBI;
167 use DBI;
168  
169 my $dbh = DBI->connect('dbi:SQLite:./mydatabase.db');
170 my $dbi = Template::Provider::DBI->new({ DBI_DBH => $dbh });
171  
172 my $tt = Template->new({ LOAD_TEMPLATES => [ $dbi ] });
173 $tt->process('mytemplate.tt', \%vars);
174  
175 =head1 DESCRIPTION
176  
177 This class adds a provider to Template Toolkit to retrieve templates from a
178 database of your choice. Using the LOAD_TEMPLATES option to L<Template>,
179 multiple providers can be created and used. The DBI provider searches for the
180 given template name, and returns DECLINED when it can't find it, to allow
181 other providers to be checked.
182  
183 =head2 Caching
184  
185 Caching is supported if L<DateTime::Format::DBI> supports your database. The
186 DateTime formatter/parser is used to convert timestamps out of the database
187 into epoch times for Template Toolkit. Caching is done through the usual
188 Template Provider method of storing the compiled template in a file.
189  
190 =head2 Usage
191  
192 To use this module, create an instance of it (see L<new> below), and pass it
193 to Template Toolkit's LOAD_TEMPLATES option. If you want to use other template
194 providers as well (even the default file template), then you need to also
195 create instances of them, and pass them to LOAD_TEMPLATES in the order you
196 would like them to be checked.
197  
198 =head1 SUBROUTINES/METHODS
199  
200 =head2 new (constructor)
201  
202 Parameters:
203 \%options
204  
205 Many options are supported, most have defaults:
206  
207 =over 4
208  
209 =item DBI_DBH
210  
211 A DBI database handle object, as returned by C<< DBI->connect >>. This
212 argument is optional if you are providing a DBI_DSN argument.
213  
214 =item DBI_DSN
215  
216 A database source name, to be passed to C<< DBI->connect >>. This will be used
217 to make and store a local database handle. It is optional if a DBI_DBH is
218 provided, if both are provided, an error is thrown.
219  
220 =item DBI_TABLE
221  
222 The name of the database table containing your templates. This will default to
223 'templates' if not provided.
224  
225 =item DBI_TMPLFIELD
226  
227 The name of the table field containing the actual template data. This will
228 default to 'template' if not provided.
229  
230 =item DBI_FILEFIELD
231  
232 The name of the table field containing the template filename. This will
233 default to 'filename'.
234  
235 =item DBI_MODIFIEDFIELD
236  
237 The name of the table field containing the timestamp or datetime of when the
238 template data was last modified. Defaults to 'modified' if not provided.
239  
240 =item DBI_DT
241  
242 If L<DateTime::Format::DBI> does not support your database, then you can try
243 getting around it by providing an object in the DBI_DT option that has a
244 "parse_timestamp" method, which will be passed the contents of your
245 DBI_MODIFIEDFIELD.
246  
247 =item DBI_QUERY
248  
249 The query used to retrieve the template data from the database will be create
250 simply as:
251  
252 SELECT $self->{DBI_TMPLFIELD}, $self->{DBI_MODIFIEDFIELD}$modified FROM $self->{DBI_TABLE} WHERE $self->{DBI_FILEFIELD} = ?;
253  
254 If you need a more complex query then provide it here, it should return at
255 least the template data field first, then the modify date field next (or
256 NULL), in that order.
257  
258 =back
259  
260 =head1 DEPENDENCIES
261  
262 Modules used, version dependencies, core yes/no
263  
264 DBI
265  
266 DateTime::Format::DBI
267  
268 =head1 NOTES
269  
270 DateTime::Format::DBI produces DBI warnings when used with SQLite. It calls
271 DBI::_dbtype_names($dbh); .. Not my fault, honest!
272  
273 =head1 BUGS AND LIMITATIONS
274  
275 None known currently, please email the author if you find
276 any.
277  
278 =head1 AUTHOR
279  
280 Jess Robinson <cpan@desert-island.demon.co.uk>
281  
282 =cut
↑ Summary