blib/lib/DBIx/SimpleGoBetween.pm

filter: Statementstmt 25.8% 16 / 62 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 29.4% 5 / 17 Podpod 100% 9 / 9 totaltotal 27.2% 30 / 110 CC 12 SCAR 37.2
CC
12
Cov
19%
callback
110.0
prep
12.0
new
2.0
CRAP 41.5
↑ Summary
1 package DBIx::SimpleGoBetween;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION = '1.003';
8  
9  
10 =head1 NAME
11  
12 DBIx::SimpleGoBetween - DBI Wrapper interface
13  
14 =head1 SYNOPSIS
15  
16 use DBIx::SimpleGoBetween;
17 use DBI;
18  
19 my $dbh=DBI->connect(blah blah blah);
20  
21 my $db=DBIx::SimpleGoBetween->new(\$dbh);
22  
23  
24 # returns the first column of the first row of your query
25 $count=$db->get_scalar('select count(*) from table_a');
26  
27 # fetches the entire result set as a single list
28 ($max,$sum)=$db->get_list(
29 'select max(column_a) ,sum(column_a) from table_a'
30 );
31  
32 # returns a list reference of list references
33 # each list reference represents a row from the result set
34 $array_ref=$db->get_list_of_lists(
35 'select * from table_a'
36 );
37  
38 # returns a list reference of hash references
39 # each hash reference represents a row from the result set
40 $array_ref=$db->get_list_of_hashes(
41 'select * from table_a'
42 );
43  
44 # does a prepare then executes the query with the list reference
45 # provide by the 2nd argument
46 $db->sql_do('delete from table_a where column_a=?',[$value]);
47  
48 # Callback
49 # code_ref: is called on a per row basis, arguments based on 'type'
50 # type: array,hash,array_ref,hash_ref
51 $sth=$db->callback(
52 'sql',[execute list],[prepare_args],'type',\&code_ref
53 );
54  
55  
56 =head1 DESCRIPTION
57  
58 DBIx::SimpleGoBetween acts as a go between for DBI and any other development interfaces you use. This package has no iterators, no error checking. It simply returns the result sets in one of many data structure formants.
59  
60 Odds are your result does not need to be processed, just placed in a structure in the order it was retrieved, if so then this is the module you are looking for.
61  
62 Example using HTML::Template
63  
64 use DBI;
65 use DBIx::SimpleGoBetween;
66 use HTML::Template;
67  
68 my $dbh=DBH->connect();
69 my $db=DBIx::SimpleGoBetween->new(\$dbh);
70 my $tmpl=HTML::Template->new(filename=>'file.tmpl');
71  
72 $tmpl->param(
73 total_rows=>$db->get_scalar('select count(*) from table_a')
74 ,tmpl_loop=>$db->get_list_of_hashes('select * from table_a')
75 );
76  
77 =head2 EXPORT
78  
79 None
80  
81  
82 =cut
83  
84 1 ▶use constant key_dbh=>0;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 =head2 OO Methods
87  
88 This section documents the OO functions
89  
90 =over 4
91  
92 =item * my $db=DBIx::SimpleGoBetween->new(\$db);
93  
94 This function creates a new instance of DBIx::SimpleGoBetween.
95  
96 =back
97  
98 =cut
99  
100 0 ▶sub new ($) {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
101 0 my ($class,$dbh_ref)=@_;
102 0 bless [$dbh_ref],$class;
103 }
104  
105 =head2 OO interface arguments
106  
107 All OO interfaces in the instance accept the following arguments:
108  
109 -'sql statement'
110 Required argument
111 Must be an "sql statment" or a "prepared statement handle"
112  
113 -[execute list]
114 Optional argument ( manditory if [optional prepare args] is used )
115 Must be an array reference containing the place holder arguments
116 for the $sth->execute command
117  
118 -[optional prepare args]
119 Optional argument ( manditory if you are using $db->callback )
120 Must be an array reference of the arguments passed to
121 $dbh->prepare('sql statement',@{[optional prepare args]})
122  
123 =over 4
124  
125 =item * my $dbh=$db->dbh;
126  
127 Returns the database handle used to create this instance.
128  
129 =cut
130  
131 0 ▶sub dbh () { ${ $_[0]->[key_dbh] } }
Subroutine dbh: not called CC=1 SCAR=6.9
Pod documented
132  
133 =item * my $sth=$db->prep('sql statement',[prepare_args]);
134  
135 Returns a prepared statement handle. If you pass a prepared statement handle, it returns that statement handle.
136  
137 In reality this is just a wrapper for:
138  
139 my $sth=$db->dbh->prepare('sql statement',@$prepare_args);
140  
141 =cut
142  
143 0 ▶sub prep {
Subroutine prep: not called CC=3 SCAR=24.8
Pod documented
144 0 my ($s,$sql,$args)=@_;
145 0 ▶ return $sql if ref($sql);
Branch: 1 branch
BranchTrueFalseTotal
if ref $sql 0 0 0
146 0 ▶ $args=[] unless $args;
Branch: 1 branch
BranchTrueFalseTotal
unless $args 0 0 0
147 0 $s->dbh->prepare($sql,@{$args});
148 }
149  
150 =item * $db->callback('sql statement',[execute list],[optional prepare args],type,code_ref);
151  
152 Although DBIx::Simple offers no iterator interfaces, it does offer a callback interface, that allows you consolidate the following operations: prepare, execute, while.
153  
154 Example:
155  
156 # type eq 'array'
157 $db->callback(
158 'sql statement'
159 ,[execute list]
160 ,[prepare arguments]
161 ,'array'
162 ,sub {
163 print join(',',@_),"\n";
164 }
165 );
166  
167 # type eq 'array_ref'
168 $db->callback(
169 'sql statement'
170 ,[execute list]
171 ,[prepare arguments]
172 ,'array_ref'
173 ,sub {
174 my ($ref)=@_;
175 print join(',',@$ref),"\n";
176 }
177 );
178  
179 # type eq 'hash'
180 $db->callback(
181 'sql statement'
182 ,[execute list]
183 ,[prepare arguments]
184 ,'hash'
185 ,sub {
186 my %hash=@_;
187 while(my ($key,$value)=each %hash) {
188 print $key,',',$value,"\n";
189 }
190 }
191 );
192  
193 # type eq 'hash_ref'
194 $db->callback(
195 'sql statement'
196 ,[execute list]
197 ,[prepare arguments]
198 ,'hash'
199 ,sub {
200 my ($hash)=@_;
201 while(my ($key,$value)=each %$hash) {
202 print $key,',',$value,"\n";
203 }
204 }
205 );
206  
207 =cut
208  
209 0 ▶sub callback {
Subroutine callback: not called CC=10 SCAR=47.0
Pod documented
210 0 my ($s,$sql,$placeholder,$sql_args,$type,$code)=@_;
211 0 my $sth=$s->prep($sql,$sql_args);
212 0 $type=lc($type);
213 0 $sth->execute(@$placeholder);
214 0 ▶ croak 'not a code ref' unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
215 0 ▶ if($type eq 'hash') {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'hash') { } 0 0 0
elsif ($type eq 'hash_ref') { } 0 0 0
elsif ($type eq 'array') { } 0 0 0
elsif ($type eq 'array_ref') { } 0 0 0
216 0 ▶ while(my $row=$sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
217 0 $code->(%$row)
218 }
219 } elsif($type eq 'hash_ref') {
220 0 ▶ while(my $row=$sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
221 0 $code->($row)
222 }
223 } elsif($type eq 'array') {
224 0 ▶ while(my $row=$sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 0 0 0
225 0 $code->(@$row)
226 }
227 } elsif($type eq 'array_ref') {
228 0 ▶ while(my $row=$sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 0 0 0
229 0 $code->($row)
230 }
231 } else {
232 0 croak 'unknown type'
233 }
234 }
235  
236 =item * my @list=$db->get_list('sql statement',[execute list],[optional prepare args]);
237  
238 Returns the entire result set as a single list.
239  
240 The [execute list] and [optional prepare args] are optional arguments.
241  
242 Example:
243  
244 my ($count,$sum)=$db->get_list(
245 'select max(col_a),sum(col_a) from table'
246 );
247  
248 =cut
249  
250 0 ▶sub get_list {
Subroutine get_list: not called CC=1 SCAR=6.9
Pod documented
251 0 my ($s,$sql,$ph,$arg)=@_;
252 0 my @list;
253 0 ▶ $s->callback($sql,$ph,$arg,'array',sub { push @list,@_ });
Subroutine __ANON__: not called CC=1 SCAR=6.9
254 0 @list;
255 }
256  
257 =item * my $value=$db->get_scalar('sql statement',[execute list],[optional prepare args]);
258  
259 Returns the first column of the first row as a single scalar value.
260  
261 This function is intended for those situations where your query contains only one value, or you really only care about the very first value in your result set.
262  
263 The [execute list] and [optional prepare args] are optional arguments.
264  
265 =cut
266  
267 0 ▶sub get_scalar {
Subroutine get_scalar: not called CC=1 SCAR=6.9
Pod documented
268 0 my ($first)=get_list(@_);
269 0 $first
270 }
271  
272 =item * my $ref=$db->get_list_of_lists('sql statement',[execute list],[optional prepare args]);
273  
274 Returns your result set as a list of list references.
275  
276 The [execute list] and [optional prepare args] are optional arguments.
277  
278 Example:
279  
280 if your query contained 2 rows and 2 columns the data structure
281 would look something like this:
282  
283 $ref->[0]->[0] eq 'value of the first column of the first row'
284 $ref->[0]->[1] eq 'value of the second column of the first row'
285  
286 $ref->[1]->[0] eq 'value of the first column of the second row'
287 $ref->[1]->[1] eq 'value of the second column of the second row'
288  
289 =cut
290  
291 0 ▶sub get_list_of_lists {
Subroutine get_list_of_lists: not called CC=1 SCAR=6.9
Pod documented
292 0 my ($s,$sql,$ph,$arg)=@_;
293 0 my $list=[];
294 0 ▶ $s->callback($sql,$ph,$arg,'array_ref',sub { push @$list, [@{$_[0]}] });
Subroutine __ANON__: not called CC=1 SCAR=6.9
295 0 $list
296 }
297  
298 =item * my $ref=$db->get_list_of_hashes('sql statement',[execute list],[optional prepare args]);
299  
300 Returns your result as a list of hash references.
301  
302 The [execute list] and [optional prepare args] are optional arguments.
303  
304 Example:
305  
306 if your query contained 2 rows and 2 columns named:( col_a,col_b)
307 the data structure would look something like this:
308  
309 $ref->[0]->{col_a} eq 'value of the first row col_a'
310 $ref->[0]->{col_b} eq 'value of the first row col_b'
311  
312 $ref->[0]->{col_a} eq 'value of the second row col_a'
313 $ref->[0]->{col_b} eq 'value of the second row col_b'
314  
315 =cut
316  
317 0 ▶sub get_list_of_hashes {
Subroutine get_list_of_hashes: not called CC=1 SCAR=6.9
Pod documented
318 0 my ($s,$sql,$ph,$arg)=@_;
319 0 my $list=[];
320 0 ▶ $s->callback($sql,$ph,$arg,'hash_ref',sub { push @$list, {%{$_[0]}} });
Subroutine __ANON__: not called CC=1 SCAR=6.9
321 0 $list
322 }
323  
324 =item * $db->sql_do('sql statement',[execute list],[optional prepare args]);
325  
326 This is really a wrapper for the following:
327  
328 my $sth=$dbh->prepare('sql statement',( list of sql args if any ));
329 $sth->execute((execute list);
330  
331 The [execute list] and [optional prepare args] are optional arguments.
332  
333 =cut
334  
335 0 ▶sub sql_do {
Subroutine sql_do: not called CC=1 SCAR=6.9
Pod documented
336 0 my ($s,$sql,$ph,$arg)=@_;
337 0 my $sth=$s->prep($sql,$arg);
338 0 $sth->execute(@$ph);
339 }
340  
341 =pod
342  
343 =back
344  
345 =head1 SEE ALSO
346  
347 DBI HTML::Template DBIx::BulkLoader::Mysql DBIx::Simple
348  
349 =head1 Source Forge Porject
350  
351 If you feel this software is useful please donate.
352  
353 L<DBIx Simple Go Between|https://sourceforge.net/projects/dbix-simplegobe/>
354  
355 =head1 AUTHOR
356  
357 Michael Shipper
358  
359 =head1 COPYRIGHT AND LICENSE
360  
361 Copyright (C) 2010 by Michael Shipper
362  
363 This library is free software; you can redistribute it and/or modify
364 it under the same terms as Perl itself, either Perl version 5.8.4 or,
365 at your option, any later version of Perl 5 you may have available.
366  
367  
368 =cut
↑ Summary