DBomb/Query.pm

filter: Statementstmt 42.2% 106 / 251 Branchbran 10.0% 6 / 60 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 55.5% 25 / 45 Podpod 0.0% 0 / 30 totaltotal 35.0% 140 / 400 CC 34 SCAR 30.4
CC
34
Cov
36%
sql
90.0
clone
72.0
prepare
12.0
CRAP 21.0
« DBomb/Query/Expr.pm ↑ Summary DBomb/Value/Column.pm »
1 package DBomb::Query;
2  
3 =head1 NAME
4  
5 DBomb::Query - A query abstraction.
6  
7 =head1 SYNOPSIS
8  
9 =cut
10  
11 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use DBomb::Conf;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use DBomb::Query::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use DBomb::Query::Expr;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use DBomb::Query::Join;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use DBomb::Query::LeftJoin;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use DBomb::Query::RightJoin;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use DBomb::Query::Limit;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use DBomb::Query::OrderBy;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use DBomb::Query::GroupBy;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 12 ▶use DBomb::Util qw(ctx_0);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 ## The CVS Revision. See DBomb.pm for the DBomb release version.
26 12 our $VERSION = '$Revision: 1.22 $';
27  
28 use Class::MethodMaker
29 12 ▶ 'new_with_init' => 'new',
Subroutine BEGIN: called CC=1 SCAR=0.0
30 'get_set' => [qw(column_names where_obj having_obj orderbys groupbys
31 limit_obj table_sources dbh sth)],
32 'boolean' => [qw(_sql_small_result)]
33 12 ;
34  
35 ## Lexically scoped unique placeholder value. Used by Expr.
36 12 my $place_holder = '?';
37 0 ▶sub PlaceHolder { \$place_holder };
Subroutine PlaceHolder: not called CC=1 SCAR=6.9
Pod undocumented
38  
39 ## new Query()
40 ## new Query(column_names)
41 ## new Query($dbh,column_names)
42 sub init
43 12 ▶{
Subroutine init: called CC=3 SCAR=11.1
Pod undocumented
44 12 my $self = shift;
45 12 $self->table_sources([]);
46 12 $self->column_names([]);
47 12 $self->orderbys([]);
48 12 $self->groupbys([]);
49 12 $self->where_obj(new DBomb::Query::Expr());
50 12 $self->having_obj(new DBomb::Query::Expr());
51  
52 ## Check for a dbh
53 12 ▶ $self->dbh(shift) if UNIVERSAL::isa($_[0],'DBI::db');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "DBI::db") 0 12 12
54  
55 12 ▶ $self->select(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 12 0 12
56 12 return $self;
57 }
58  
59 sub select
60 12 ▶{
Subroutine select: called CC=2 SCAR=7.4
Pod undocumented
61 12 my $self = shift;
62 0 ▶ push @{$self->column_names}, (ref($_[0]) ? @{$_[0]}: @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 12 12
63 12 return $self;
64 }
65  
66 ## mysql-specific extension
67 sub sql_small_result
68 0 ▶{
Subroutine sql_small_result: not called CC=1 SCAR=6.9
Pod undocumented
69 0 my $self = shift;
70 0 $self->_sql_small_result(1);
71 0 return $self;
72 }
73  
74 ## from($tables..)
75 sub from
76 6 ▶{
Subroutine from: called CC=3 SCAR=11.4
Pod undocumented
77 6 my $self = shift;
78 6 ▶ assert(@_ && defined($_[0]), 'valid parameters');
Condition33%1/3
@_ && defined $_[0]
!ll&&!rl&&r
006
MC/DC0%0/2
@_ && defined $_[0]
@_ defined $_[0]
Truth table33%1/3
A && B
A= @_B= defined $_[0]
ABresult
0X0
100
111
79 6 ▶ push @{$self->table_sources}, map { new DBomb::Query::Text($_)} map { ref($_)? @$_ : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 6 6
80 6 return $self;
81 }
82  
83 ## join($right)
84 ## join($left, $right)
85 sub join
86 5 ▶{
Subroutine join: called CC=1 SCAR=0.0
Pod undocumented
87 5 my $self = shift;
88 5 $self->_mk_join('DBomb::Query::Join',@_);
89 5 return $self;
90 }
91  
92 ## right_join($right)
93 ## right_join($left, $right)
94 sub right_join
95 1 ▶{
Subroutine right_join: called CC=1 SCAR=0.0
Pod undocumented
96 1 my $self = shift;
97 1 $self->_mk_join('DBomb::Query::RightJoin',@_);
98 1 return $self;
99 }
100  
101 ## left_join($right)
102 ## left_join($left, $right)
103 sub left_join
104 1 ▶{
Subroutine left_join: called CC=1 SCAR=0.0
Pod undocumented
105 1 my $self = shift;
106 1 $self->_mk_join('DBomb::Query::LeftJoin',@_);
107 1 return $self;
108 }
109  
110 ## _mk_join($joinclass,$right)
111 ## _mk_join($joinclass,$left, $right)
112 sub _mk_join
113 7 ▶{
Subroutine _mk_join: called CC=4 SCAR=14.3
114 7 my $self = shift;
115 7 my $joinclass = shift;
116 7 my($left,$right) = @_;
117 7 my $table_sources = $self->table_sources;
118 7 ▶ if(@_ == 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 2 5 7
119 2 ▶ assert(@$table_sources > 0
Condition33%2/6
@$table_sources > 0 && (UNIVERSAL::isa(ref $$table_sources[$table_sources->$#*], 'DBomb::Query::Text') || UNIVERSAL::isa(ref $$table_sources[$table_sources->$#*], 'DBomb::Query::Join'))
!ll&&!rl&&r
002
UNIVERSAL::isa(ref $$table_sources[$table_sources->$#*], "DBomb::Query::Text") or UNIVERSAL::isa(ref $$table_sources[$table_sources->$#*], "DBomb::Query::Join")
l!l&&r!l&&!r
200
MC/DC0%0/3
@$table_sources > 0 && (UNIVERSAL::isa(ref $$table_sources[$table_sources->$#*], 'DBomb::Query::Text') || UNIVERSAL::isa(ref $$table_sources[$table_sources->$#*], 'DBomb::Query::Join'))
@$table_sources > 0 UNIVERSAL::isa(ref $$table_sources[$table_sources->$#*], "DBomb::Query::Text") UNIVERSAL::isa(ref $$table_sources[$table_sources->$#*], "DBomb::Query::Join")
Truth table25%1/4
A && B or C
A= @$table_sources > 0B= UNIVERSAL::isa(ref $$table_sources[$table_sources->$#*], "DBomb::Query::Text")C= UNIVERSAL::isa(ref $$table_sources[$table_sources->$#*], "DBomb::Query::Join")
ABCresult
0XX0
1000
11X1
1011
120 && ( UNIVERSAL::isa(ref($table_sources->[$#$table_sources]),'DBomb::Query::Text')
121 ||UNIVERSAL::isa(ref($table_sources->[$#$table_sources]),'DBomb::Query::Join')
122 ), 'single argument join requires left table (or join)');
123 2 $right = $left;
124 2 $left = pop @$table_sources;
125 }
126 7 push @$table_sources, $joinclass->new($left,$right);
127 7 return $self;
128 }
129  
130 sub on
131 1 ▶{
Subroutine on: called CC=1 SCAR=0.0
Pod undocumented
132 1 my $self = shift;
133 1 my $table_sources = $self->table_sources;
134 1 assert(@$table_sources, 'ON requires a JOIN');
135 1 assert(UNIVERSAL::isa($table_sources->[$#$table_sources],'DBomb::Query::Join'), 'ON requires a JOIN');
136  
137 1 $table_sources->[$#$table_sources]->on(new DBomb::Query::Expr(@_));
138 1 return $self;
139 }
140  
141 sub using
142 0 ▶{
Subroutine using: not called CC=1 SCAR=6.9
Pod undocumented
143 0 my $self = shift;
144 0 my $table_sources = $self->table_sources;
145 0 assert(scalar(@_), 'USING requires a list of column names');
146 0 assert(UNIVERSAL::isa($table_sources->[$#$table_sources],'DBomb::Query::Join'), 'USING requires a JOIN');
147 0 $table_sources->[$#$table_sources]->using(@_);
148 0 return $self;
149 }
150  
151 ## where(EXPR, @bind_values)
152 sub where
153 0 ▶{
Subroutine where: not called CC=1 SCAR=6.9
Pod undocumented
154 0 my $self = shift;
155 0 $self->where_obj->append(new DBomb::Query::Expr(@_));
156 0 return $self;
157 }
158  
159 ## and (EXPR, @bind_values)
160 sub and
161 0 ▶{
Subroutine and: not called CC=1 SCAR=6.9
Pod undocumented
162 0 my $self = shift;
163 0 $self->where_obj->and(@_);
164 0 $self
165 }
166  
167 ## or (EXPR, @bind_values)
168 sub or
169 0 ▶{
Subroutine or: not called CC=1 SCAR=6.9
Pod undocumented
170 0 my $self = shift;
171 0 $self->where_obj->or(@_);
172 0 $self
173 }
174  
175 sub group_by
176 0 ▶{
Subroutine group_by: not called CC=1 SCAR=6.9
Pod undocumented
177 0 my $self = shift;
178 0 my $o = new DBomb::Query::GroupBy(@_);
179 0 push @{$self->groupbys}, $o;
180 0 return $self;
181 }
182  
183 sub having
184 0 ▶{
Subroutine having: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my $self = shift;
186 0 my $o = new DBomb::Query::Expr(@_);
187 0 $self->having_obj->append($o);
188 0 return $self;
189 }
190  
191 sub order_by
192 4 ▶{
Subroutine order_by: called CC=1 SCAR=0.0
Pod undocumented
193 4 my $self = shift;
194 4 my $o = new DBomb::Query::OrderBy(@_);
195 4 push @{$self->orderbys}, $o;
196 4 return $self;
197 }
198  
199 sub asc
200 0 ▶{
Subroutine asc: not called CC=1 SCAR=6.9
Pod undocumented
201 0 my $self = shift;
202 0 assert(@_ == 0, "asc takes no arguments");
203 0 my $orderbys = $self->orderbys;
204 0 assert(@$orderbys > 0, "asc requires previous call to order_by");
205 0 $orderbys->[$#$orderbys]->asc;
206 0 return $self;
207 }
208  
209 sub desc
210 2 ▶{
Subroutine desc: called CC=1 SCAR=0.0
Pod undocumented
211 2 my $self = shift;
212 2 assert(@_ == 0, "desc takes no arguments");
213 2 my $orderbys = $self->orderbys;
214 2 assert(@$orderbys > 0, "asc requires previous call to order_by");
215 2 $orderbys->[$#$orderbys]->desc;
216 2 return $self;
217 }
218  
219 sub limit
220 3 ▶{
Subroutine limit: called CC=1 SCAR=0.0
Pod undocumented
221 3 my $self = shift;
222 3 my $o = new DBomb::Query::Limit(@_);
223 3 $self->limit_obj($o);
224 3 return $self;
225 }
226  
227 sub sql
228 0 ▶{
Subroutine sql: not called CC=9 SCAR=45.0
Pod undocumented
229 0 my ($self, $dbh) = @_;
230 0 ▶ $self->dbh($dbh) if defined $dbh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbh 0 0 0
231 0 $dbh = $self->dbh();
232 0 assert(defined($dbh), 'DBomb::Query::sql method requires a dbh');
233  
234 0 my $sql = "SELECT ";
235  
236 0 ▶ if ($self->_sql_small_result){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_sql_small_result) 0 0 0
237 0 $sql .= "SQL_SMALL_RESULT ";
238 }
239  
240 0 $sql .= CORE::join ',', @{$self->column_names};
241  
242 0 ▶ $sql .= " FROM " . CORE::join ', ', map { $_->sql($dbh) } @{$self->table_sources} if @{$self->table_sources};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->table_sources;} 0 0 0
243  
244 0 my $where_sql = $self->where_obj->sql($dbh);
245 0 ▶ $sql .= " WHERE $where_sql " if length $where_sql;
Branch: 1 branch
BranchTrueFalseTotal
if length $where_sql 0 0 0
246  
247 0 my $groupby_sql = CORE::join ', ', map { $_->sql($dbh) } @{$self->groupbys};
248 0 ▶ $sql .= " GROUP BY " . $groupby_sql if length $groupby_sql;
Branch: 1 branch
BranchTrueFalseTotal
if length $groupby_sql 0 0 0
249  
250 0 my $having_sql = $self->having_obj->sql($dbh);
251 0 ▶ $sql .= " HAVING $having_sql " if length $having_sql;
Branch: 1 branch
BranchTrueFalseTotal
if length $having_sql 0 0 0
252  
253 0 my $orderby_sql = CORE::join ', ', map { $_->sql($dbh) } @{$self->orderbys};
254 0 ▶ $sql .= " ORDER BY " . $orderby_sql if length $orderby_sql;
Branch: 1 branch
BranchTrueFalseTotal
if length $orderby_sql 0 0 0
255  
256 0 ▶ $sql .= $self->limit_obj->sql($dbh) if defined $self->limit_obj;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->limit_obj 0 0 0
257  
258 0 return ctx_0($sql,@{$self->bind_values});
259 }
260  
261 sub bind_values
262 0 ▶{
Subroutine bind_values: not called CC=1 SCAR=6.9
Pod undocumented
263 0 my $self = shift;
264 0 my $bv = [];
265  
266 0 push @$bv, map{ @{$_->bind_values} } @{$self->table_sources};
267 0 push @$bv, @{$self->where_obj->bind_values};
268 0 push @$bv, @{$self->having_obj->bind_values};
269 0 return $bv;
270 }
271  
272 ## prepare()
273 ## prepare($dbh)
274 sub prepare
275 0 ▶{
Subroutine prepare: not called CC=3 SCAR=24.8
Pod undocumented
276 0 my ($self,$dbh) = @_;
277  
278 0 ▶ $self->dbh($dbh) if defined $dbh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbh 0 0 0
279 0 assert(defined($self->dbh), 'prepare requires a dbh');
280  
281 0 ▶ if ($DBomb::Conf::prepare_cached){
Branch: 1 branch
BranchTrueFalseTotal
if ($DBomb::Conf::prepare_cached) { } 0 0 0
282 0 $self->sth($self->dbh->prepare_cached(scalar $self->sql));
283 }
284 else{
285 0 $self->sth($self->dbh->prepare(scalar $self->sql));
286 }
287 0 return $self;
288 }
289  
290 ## execute(@bind_values)
291 ## execute($dbh,@bind_values)
292 sub execute
293 0 ▶{
Subroutine execute: not called CC=3 SCAR=24.8
Pod undocumented
294 0 my ($self, @bind_values) = @_;
295  
296 0 ▶ $self->dbh(shift @bind_values) if UNIVERSAL::isa($bind_values[0],'DBI::db');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($bind_values[0], "DBI::db") 0 0 0
297 0 assert(defined($self->dbh), 'execute requires a dbh');
298  
299 0 ▶ $self->prepare unless $self->sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sth 0 0 0
300 0 assert($self->sth, 'execute requires a valid sth. Did you forget to call prepare()?');
301  
302 0 $self->sth->execute((@{$self->bind_values},@bind_values));
303 0 return $self;
304 }
305  
306 ## fetchrow_arrayref()
307 sub fetchrow_arrayref
308 0 ▶{
Subroutine fetchrow_arrayref: not called CC=1 SCAR=6.9
Pod undocumented
309 0 my $self = shift;
310 0 assert(defined($self->sth), 'fetchrow_arrayref requires an sth');
311  
312 0 return $self->sth->fetchrow_arrayref;
313 }
314  
315 ## fetchall_arrayref()
316 sub fetchall_arrayref
317 0 ▶{
Subroutine fetchall_arrayref: not called CC=2 SCAR=17.9
Pod undocumented
318 0 my $self = shift;
319 0 my $a = [];
320 0 ▶ while(my $row = $self->fetchrow_arrayref){
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->fetchrow_arrayref) 0 0 0
321  
322 ## DBI reuses array refs, so create new ones each time
323 0 push @$a, [@$row];
324 }
325 0 return $a;
326 }
327  
328 ## fetchcol_arrayref()
329 sub fetchcol_arrayref
330 0 ▶{
Subroutine fetchcol_arrayref: not called CC=2 SCAR=17.9
Pod undocumented
331 0 my $self = shift;
332 0 my $a = [];
333 0 ▶ while(my $row = $self->fetchrow_arrayref){
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->fetchrow_arrayref) 0 0 0
334  
335 0 push @$a, $row->[0];
336 }
337 0 return $a;
338 }
339  
340 ## selectall_arrayref(@bind_values)
341 ## selectall_arrayref($dbh, @bind_values)
342 sub selectall_arrayref
343 0 ▶{
Subroutine selectall_arrayref: not called CC=3 SCAR=24.8
Pod undocumented
344 0 my ($self, @bind_values) = @_;
345  
346 0 ▶ $self->dbh(shift @bind_values) if UNIVERSAL::isa($bind_values[0],'DBI::db');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($bind_values[0], "DBI::db") 0 0 0
347 0 assert(defined($self->dbh), 'execute requires a dbh');
348  
349 0 ▶ $self->prepare($self->dbh) unless $self->sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sth 0 0 0
350 0 $self->execute(@bind_values);
351 0 return $self->fetchall_arrayref;
352 }
353  
354 ## selectcol_arrayref(@bind_values)
355 ## selectcol_arrayref($dbh, @bind_values)
356 sub selectcol_arrayref
357 0 ▶{
Subroutine selectcol_arrayref: not called CC=3 SCAR=24.8
Pod undocumented
358 0 my ($self, @bind_values) = @_;
359  
360 0 ▶ $self->dbh(shift @bind_values) if UNIVERSAL::isa($bind_values[0],'DBI::db');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($bind_values[0], "DBI::db") 0 0 0
361 0 assert(defined($self->dbh), 'execute requires a dbh');
362  
363 0 ▶ $self->prepare($self->dbh) unless $self->sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sth 0 0 0
364 0 $self->execute(@bind_values);
365 0 return $self->fetchcol_arrayref;
366 }
367  
368 ## finish()
369 sub finish
370 0 ▶{
Subroutine finish: not called CC=1 SCAR=6.9
Pod undocumented
371 0 my $self = shift;
372 0 assert(defined($self->sth), 'finish() requires an sth');
373 0 $self->sth->finish;
374 }
375  
376 ## returns a deep copy.
377 ## @note The database handle will be shared by the clone, and
378 ## the internal statement handle will set to undef in the clone.
379 ## clone ()
380 sub clone
381 0 ▶{
Subroutine clone: not called CC=8 SCAR=42.8
Pod undocumented
382 0 my $self = shift;
383  
384 0 assert(UNIVERSAL::isa($self,__PACKAGE__));
385 0 assert(@_ == 0);
386  
387 0 my $clone = __PACKAGE__->new();
388  
389 ## copy simple lists
390 0 ▶ push @{$clone->column_names}, @{$self->column_names} if @{$self->column_names};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->column_names;} 0 0 0
391 0 ▶ push @{$clone->table_sources}, @{$self->table_sources} if @{$self->table_sources};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->table_sources;} 0 0 0
392  
393 ## clone object lists
394 0 ▶ push @{$clone->orderbys}, map{$_->clone} @{$self->orderbys} if @{$self->orderbys};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->orderbys;} 0 0 0
395 0 ▶ push @{$clone->groupbys}, map{$_->clone} @{$self->groupbys} if @{$self->groupbys};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->groupbys;} 0 0 0
396  
397 ## clone objects
398 0 ▶ $clone->where_obj ($self->where_obj->clone) if $self->where_obj;
Branch: 1 branch
BranchTrueFalseTotal
if $self->where_obj 0 0 0
399 0 ▶ $clone->having_obj ($self->having_obj) if $self->having_obj;
Branch: 1 branch
BranchTrueFalseTotal
if $self->having_obj 0 0 0
400 0 ▶ $clone->limit_obj ($self->limit_obj->clone) if $self->limit_obj;
Branch: 1 branch
BranchTrueFalseTotal
if $self->limit_obj 0 0 0
401  
402 ## share the dbh, and undef the sth
403 0 $clone->dbh($self->dbh);
404 0 $clone->sth(undef);
405  
406 0 return $clone;
407 }
408  
409 12 1;
410 __END__
« DBomb/Query/Expr.pm ↑ Summary DBomb/Value/Column.pm »