blib/lib/App/dbitemplater.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package App::dbitemplater;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Template ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use File::Slurp qw(write_file);
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use DBI ();
9  
10 =head1 NAME
11  
12 App::dbitemplater - A utility for running a SQL query via DBI and using the output in a template.
13  
14 =head1 VERSION
15  
16 Version 0.0.1
17  
18 =cut
19  
20 our $VERSION = '0.0.1';
21  
22 =head1 SYNOPSIS
23  
24 use App::dbitemplater;
25 use YAML::XS;
26 use File::Slurp;
27  
28 $config=Load(read_file('/usr/local/etc/dbitemplater.yaml'));
29  
30 my $dbitemplater = App::dbitemplater->new($config);
31  
32 $dbitemplater->process;
33  
34 =head1 METHODS
35  
36 =head2 new
37  
38 Initiates the object.
39  
40 A hash reference is required. The contents should be the config.
41 See the docs for dbitemplater for the config.
42  
43 =cut
44  
45 sub new {
46 my %args;
47 if ( defined( $_[1] ) ) {
48 %args = %{ $_[1] };
49 }
50  
51 my @possible_args = (
52 'ds', 'user', 'pass', 'output', 'query', 'header',
53 'row', 'footer', 'POST_CHOMP', 'PRE_CHOMP', 'TRIM', 'START_TAG',
54 'END_TAG'
55 );
56 my $required_args = {
57 'ds' => 1,
58 'header' => 1,
59 'row' => 1,
60 'footer' => 1,
61 'query' => 1
62 };
63  
64 my $self = {
65 'ds' => undef,
66 'user' => undef,
67 'pass' => undef,
68 'output' => undef,
69 'header' => undef,
70 'row' => undef,
71 'footer' => undef,
72 'POST_CHOMP' => undef,
73 'PRE_CHOMP' => undef,
74 'TRIM' => undef,
75 'START_TAG' => undef,
76 'END_TAG' => undef,
77 'config' => \%args,
78 };
79 bless $self;
80  
81 # ensure that all required args are present and that any arts that are present have a
82 # ref of ""
83 foreach my $item (@possible_args) {
84 if ( defined( $required_args->{$item} ) && !defined( $args{$item} ) ) {
85 die( '$args{"' . $item . '"} is not defined and is required' );
86 }
87 if ( defined( $args{$item} ) && ref( $args{$item} ) ne '' ) {
88 die( '$args{"' . $item . '"} is defined and ref is "' . ref( $args{$item} ) . '" and not ""' );
89 }
90 if ( defined( $args{$item} ) ) {
91 $self->{$item} = $args{$item};
92 }
93 if ( $item eq 'header' || $item eq 'row' || $item eq 'footer' ) {
94 # if a slash is found, assume it is a full path
95 if ( $args{$item} =~ /[\\\/]/ ) {
96 if ( !-f $args{$item} ) {
97 die( '$args{"' . $item . '"}, "' . $args{$item} . '", is not a file or does not exist' );
98 }
99 } else {
100 my $full_path = '/usr/local/etc/dbitemplater/templates/' . $item . '/' . $args{$item};
101 if ( !-f $full_path ) {
102 die( '$args{"'
103 . $item . '"}, "'
104 . $args{$item} . '","'
105 . $full_path
106 . '", is not a file or does not exist' );
107 }
108 $self->{$item} = $full_path;
109 } ## end else [ if ( $args{$item} =~ /[\\\/]/ ) ]
110 } ## end if ( $item eq 'header' || $item eq 'row' ||...)
111 } ## end foreach my $item (@possible_args)
112  
113 return $self;
114 } ## end sub new
115  
116 =head2 process
117  
118 Connects, run the query, and processes the templates.
119  
120 =cut
121  
122 sub process {
123 my $self = $_[0];
124  
125 # initiate TT
126 my $tt = Template->new(
127 {
128 'INCLUDE_PATH' => '/usr/local/etc/dbitemplater/templates',
129 'EVAL_PERL' => 1,
130 'POST_CHOMP' => $self->{'POST_CHOMP'},
131 'PRE_CHOMP' => $self->{'PRE_CHOMP'},
132 'TRIM' => $self->{'TRIM'},
133 'START_TAG' => $self->{'START_TAG'},
134 'END_TAG' => $self->{'END_TAG'},
135 'ABSOLUTE' => 1,
136 }
137 );
138  
139 # stores the results of the template
140 my $results = '';
141  
142 # will be passed to the template for vars
143 my $to_pass_to_template = { 'config' => $self->{config}, };
144 # process the headertemplate
145 my $output = '';
146 eval {
147 $tt->process( $self->{'header'}, $to_pass_to_template, \$output )
148 || die $tt->error(), "\n";
149 $results = $results . $output;
150 };
151 if ($@) {
152 warn( 'Error processing header template, "' . $self->{'header'} . '"... ' . $@ );
153 }
154  
155 # connect to it
156 my $dbh;
157 eval { $dbh = DBI->connect( $self->{'ds'}, $self->{'user'}, $self->{'pass'} ) or die $DBI::errstr; };
158 if ($@) {
159 die( 'DBI connect failed... ' . $@ );
160 }
161  
162 # prepare the statement
163 my $sth;
164 eval { $sth = $dbh->prepare( $self->{'query'} ) or die $DBI::errstr; };
165 if ($@) {
166 die( 'statement prepare failed... ' . $@ );
167 }
168  
169 eval { $sth->execute or die $DBI::errstr; };
170 if ($@) {
171 die( 'statement execute failed... ' . $@ );
172 }
173  
174 # fetch each row and process it
175 while ( my $row_hash_ref = $sth->fetchrow_hashref ) {
176 $to_pass_to_template->{'row'} = $row_hash_ref;
177 # process the template for each row
178 my $output = '';
179 eval {
180 $tt->process( $self->{'row'}, $to_pass_to_template, \$output )
181 || die $tt->error(), "\n";
182 $results = $results . $output;
183 };
184 if ($@) {
185 warn( 'Error processing header template, "' . $self->{'row'} . '"... ' . $@ );
186 }
187 } ## end while ( my $row_hash_ref = $sth->fetchrow_hashref)
188  
189 # remove the row variable from what is passed to the template
190 delete( $to_pass_to_template->{'row'} );
191  
192 # process the footer template
193 $output = '';
194 eval {
195 $tt->process( $self->{'footer'}, $to_pass_to_template, \$output )
196 || die $tt->error(), "\n";
197 $results = $results . $output;
198 };
199 if ($@) {
200 warn( 'Error processing footer template, "' . $self->{'header'} . '"... ' . $@ );
201 }
202  
203 # handle the output
204 if ( !defined( $self->{output} ) ) {
205 print $results;
206 } else {
207 eval { write_file( $self->{'output'}, { atomic => 1 }, $results ); };
208 if ($@) {
209 die( 'Failed to write results out to "' . $self->{'output'} . '"... ' . $@ );
210 }
211 }
212  
213 return 1;
214 } ## end sub process
215  
216 =head1 AUTHOR
217  
218 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
219  
220 =head1 BUGS
221  
222 Please report any bugs or feature requests to C<bug-app-dbitemplater at rt.cpan.org>, or through
223 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-dbitemplater>. I will be notified, and then you'll
224 automatically be notified of progress on your bug as I make changes.
225  
226  
227  
228  
229 =head1 SUPPORT
230  
231 You can find documentation for this module with the perldoc command.
232  
233 perldoc App::dbitemplater
234  
235  
236 You can also look for information at:
237  
238 =over 4
239  
240 =item * RT: CPAN's request tracker (report bugs here)
241  
242 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=App-dbitemplater>
243  
244 =item * CPAN Ratings
245  
246 L<https://cpanratings.perl.org/d/App-dbitemplater>
247  
248 =item * Search CPAN
249  
250 L<https://metacpan.org/release/App-dbitemplater>
251  
252 =back
253  
254  
255 =head1 ACKNOWLEDGEMENTS
256  
257  
258 =head1 LICENSE AND COPYRIGHT
259  
260 This software is Copyright (c) 2025 by Zane C. Bowers-Hadley.
261  
262 This is free software, licensed under:
263  
264 The GNU General Public License, Version 3, June 2007
265  
266  
267 =cut
268  
269 1; # End of App::dbitemplater
↑ Summary