blib/lib/DBIx/DBO/Query.pm

filter: Statementstmt 91.9% 478 / 520 Branchbran 69.2% 162 / 234 Conditioncond 63.6% 84 / 132 MC/DCmcdc 28.3% 21 / 74 Subroutinesub 98.4% 64 / 65 Podpod 94.7% 36 / 38 totaltotal 79.4% 845 / 1063 SCAR 60.2
CC
190
Cov
82%
sql
105.0
_add_where
31.2
col_arrayref
27.6
CRAP 410.7
« Test/DBO.pm ↑ Summary blib/lib/DBIx/DBO/DBD.pm »
1 package DBIx::DBO::Query;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Devel::Peek 'SvREFCNT';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use overload '**' => \&column, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 BEGIN {
11 12 ▶ if ($] < 5.008_009) {
Subroutine BEGIN: called CC=2 SCAR=8.4
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.008009) { } 0 12 12
12 0 require XSLoader;
13 0 XSLoader::load(__PACKAGE__, $DBIx::DBO::VERSION);
14 } else {
15 12 require Hash::Util;
16 12 *_hv_store = \&Hash::Util::hv_store;
17 }
18 }
19  
20 9 ▶sub _table_class { $_[0]{DBO}->_table_class }
Subroutine _table_class: called CC=1 SCAR=0.0
21 10 ▶sub _row_class { $_[0]{DBO}->_row_class }
Subroutine _row_class: called CC=1 SCAR=0.0
22  
23 12 *_isa = \&DBIx::DBO::DBD::_isa;
24  
25 =head1 NAME
26  
27 DBIx::DBO::Query - An OO interface to SQL queries and results. Encapsulates an entire query in an object.
28  
29 =head1 SYNOPSIS
30  
31 # Create a Query object by JOINing 2 tables
32 my $query = $dbo->query('my_table', 'my_other_table');
33
34 # Get the Table objects from the query
35 my($table1, $table2) = $query->tables;
36
37 # Add a JOIN ON clause
38 $query->join_on($table1 ** 'login', '=', $table2 ** 'username');
39
40 # Find our ancestors, and order by age (oldest first)
41 $query->where('name', '=', 'Adam');
42 $query->where('name', '=', 'Eve');
43 $query->order_by({ COL => 'age', ORDER => 'DESC' });
44
45 # New Query using a LEFT JOIN
46 ($query, $table1) = $dbo->query('my_table');
47 $table2 = $query->join_table('another_table', 'LEFT');
48 $query->join_on($table1 ** 'parent_id', '=', $table2 ** 'child_id');
49
50 # Find those not aged between 20 and 30.
51 $query->where($table1 ** 'age', '<', 20, FORCE => 'OR'); # Force OR so that we get: (age < 20 OR age > 30)
52 $query->where($table1 ** 'age', '>', 30, FORCE => 'OR'); # instead of the default: (age < 20 AND age > 30)
53  
54 =head1 DESCRIPTION
55  
56 A C<Query> object represents rows from a database (from one or more tables). This module makes it easy, not only to fetch and use the data in the returned rows, but also to modify the query to return a different result set.
57  
58 =head1 METHODS
59  
60 =head3 C<new>
61  
62 DBIx::DBO::Query->new($dbo, $table1, ...);
63 # or
64 $dbo->query($table1, ...);
65  
66 Create a new C<Query> object from the tables specified.
67 In scalar context, just the C<Query> object will be returned.
68 In list context, the C<Query> object and L<Table|DBIx::DBO::Table> objects will be returned for each table specified.
69 Tables can be specified with the same arguments as L<DBIx::DBO::Table/new> or another Query can be used as a subquery.
70  
71 my($query, $table1, $table2) = DBIx::DBO::Query->new($dbo, 'customers', ['history', 'transactions']);
72  
73 You can also pass in a Query instead of a Table to use that query as a subquery.
74  
75 my $subquery = DBIx::DBO::Query->new($dbo, 'history.transactions');
76 my $query = DBIx::DBO::Query->new($dbo, 'customers', $subquery);
77 # SELECT * FROM customers, (SELECT * FROM history.transactions) t1;
78  
79 =cut
80  
81 sub new {
82 13 ▶ my $proto = shift;
Subroutine new: called CC=3 SCAR=11.8
Pod documented
83 13 ▶ eval { $_[0]->isa('DBIx::DBO') } or croak 'Invalid DBO Object';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $_[0]->isa("DBIx::DBO") } } or croak("Invalid DBO Object") 0 13 13
84 13 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0130
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
85 13 $class->_init(@_);
86 }
87  
88 sub _init {
89 13 ▶ my $class = shift;
Subroutine _init: called CC=4 SCAR=13.9
90 13 my $me = { DBO => shift, sql => undef, Columns => [] };
91 13 ▶ croak 'No table specified in new Query' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 13 13
92 13 bless $me, $class;
93  
94 13 for my $table (@_) {
95 14 $me->join_table($table);
96 }
97 12 $me->reset;
98 12 ▶ return wantarray ? ($me, $me->tables) : $me;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 7 12
99 }
100  
101 sub _build_data {
102 320 ▶ $_[0]->{build_data};
Subroutine _build_data: called CC=1 SCAR=0.0
103 }
104  
105 =head3 C<reset>
106  
107 $query->reset;
108  
109 Reset the query, start over with a clean slate.
110 Resets the columns to return, removes all the WHERE, DISTINCT, HAVING, LIMIT, GROUP BY & ORDER BY clauses.
111  
112 B<NB>: This will not remove the JOINs or JOIN ON clauses.
113  
114 =cut
115  
116 sub reset {
117 14 ▶ my $me = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
118 14 $me->finish;
119 14 $me->unwhere;
120 14 $me->distinct(0);
121 14 $me->show;
122 14 $me->group_by;
123 14 $me->order_by;
124 14 $me->unhaving;
125 14 $me->limit;
126 }
127  
128 =head3 C<tables>
129  
130 Return a list of L<Table|DBIx::DBO::Table> or Query objects that appear in the C<FROM> clause for this query.
131  
132 =cut
133  
134 sub tables {
135 225 ▶ @{$_[0]->{Tables}};
Subroutine tables: called CC=1 SCAR=0.0
Pod documented
136 }
137  
138 sub _table_idx {
139 26 ▶ my($me, $tbl) = @_;
Subroutine _table_idx: called CC=3 SCAR=11.0
140 26 for my $i (0 .. $#{$me->{Tables}}) {
141 27 ▶ return $i if $tbl == $me->{Tables}[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $tbl == $me->{"Tables"}[$i] 14 13 27
142 }
143 12 return undef;
144 }
145  
146 sub _table_alias {
147 119 ▶ my($me, $tbl) = @_;
Subroutine _table_alias: called CC=6 SCAR=17.9
148  
149 # This means it's checking for an aliased column in this Query
150 119 ▶ return undef if $me == $tbl;
Branch: 1 branch
BranchTrueFalseTotal
if $me == $tbl 7 112 119
151  
152 # Don't use aliases, when there's only 1 table unless its a subquery
153 112 ▶ return undef if $me->tables == 1 and _isa($tbl, 'DBIx::DBO::Table');
Branch: 1 branch
BranchTrueFalseTotal
if $me->tables == 1 and _isa($tbl, "DBIx::DBO::Table") 81 31 112
Condition66%2/3
$me->tables == 1 and _isa($tbl, "DBIx::DBO::Table")
!ll&&!rl&&r
31081
MC/DC50%1/2
$me->tables == 1 and _isa($tbl, "DBIx::DBO::Table")
$me->tables == 1 _isa($tbl, "DBIx::DBO::Table")
Truth table66%2/3
A and B
A= $me->tables == 1B= _isa($tbl, "DBIx::DBO::Table")
ABresult
0X0
100
111
154  
155 31 ▶ my $_from_alias = $me->{build_data}{_from_alias} ||= {};
Condition100%2/2
$me->{"build_data"}{"_from_alias"} ||= {}
l!l
274
MC/DC100%1/1
$me->{"build_data"}{"_from_alias"} ||= {}
$me->{"build_data"}{"_from_alias"}
Truth table100%2/2
A
A= $me->{"build_data"}{"_from_alias"}
Aresult
11
00
156 31 ▶ return $_from_alias->{$tbl} ||= 't'.scalar(keys %$_from_alias);
Condition100%2/2
$_from_alias->{$tbl} ||= 't' . scalar(keys %$_from_alias)
l!l
238
MC/DC100%1/1
$_from_alias->{$tbl} ||= 't' . scalar(keys %$_from_alias)
$_from_alias->{$tbl}
Truth table100%2/2
A
A= $_from_alias->{$tbl}
Aresult
11
00
157 }
158  
159 sub _from {
160 0 ▶ my($me, $parent_build_data) = @_;
Subroutine _from: not called CC=1 SCAR=6.9
161 0 $parent_build_data->{_subqueries}{$me} = $me->sql;
162 local(
163 $me->{build_data}{_from_alias},
164 $me->{build_data}{from},
165 $me->{build_data}{show},
166 $me->{build_data}{where},
167 $me->{build_data}{orderby},
168 $me->{build_data}{groupby},
169 $me->{build_data}{having}
170 0 ) = ($parent_build_data->{_from_alias});
171 0 return '('.$me->{DBO}{dbd_class}->_build_sql_select($me).')';
172 }
173  
174 =head3 C<columns>
175  
176 Return a list of column names that will be returned by L<fetch>.
177  
178 =cut
179  
180 sub columns {
181 18 ▶ my($me) = @_;
Subroutine columns: called CC=4 SCAR=13.9
Pod documented
182  
183 6 @{$me->{Columns}} = do {
184 6 ▶ if (@{$me->{build_data}{Showing}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$me->{'build_data'}{'Showing'};}) { } 1 5 6
185 map {
186 2 ▶ _isa($_, 'DBIx::DBO::Table', 'DBIx::DBO::Query') ? ($_->columns) : $me->_build_col_val_name(@$_)
Branch: 1 branch
BranchTrueFalseTotal
_isa($_, 'DBIx::DBO::Table', 'DBIx::DBO::Query') ? : 0 2 2
187 1 } @{$me->{build_data}{Showing}};
188 } else {
189 5 map { $_->columns } @{$me->{Tables}};
190 }
191 18 ▶ } unless @{$me->{Columns}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$me->{"Columns"};} 6 12 18
192  
193 18 @{$me->{Columns}};
194 }
195  
196 sub _build_col_val_name {
197 17 ▶ my($me, $fld, $func, $opt) = @_;
Subroutine _build_col_val_name: called CC=7 SCAR=23.6
198 17 ▶ return $opt->{AS} if exists $opt->{AS};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"AS"} 8 9 17
199  
200 my @ary = map {
201 9 ▶ if (not ref $_) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $_) { } 0 8 8
elsif (_isa($_, 'DBIx::DBO::Column')) { } 8 0 8
elsif (ref $_ eq 'SCALAR') { } 0 0 0
elsif (_isa($_, 'DBIx::DBO::Query')) { } 0 0 0
202 0 $me->rdbh->quote($_);
203 } elsif (_isa($_, 'DBIx::DBO::Column')) {
204 8 $_->[1];
205 } elsif (ref $_ eq 'SCALAR') {
206 0 $$_;
207 } elsif (_isa($_, 'DBIx::DBO::Query')) {
208 0 $_->_from($me->{build_data});
209 }
210 } @$fld;
211 9 ▶ return $ary[0] unless defined $func;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $func 8 1 9
212 1 $func =~ s/$DBIx::DBO::DBD::placeholder/shift @ary/ego;
213 1 return $func;
214 }
215  
216 =head3 C<column>
217  
218 $query->column($alias_or_column_name);
219 $query ** $column_name;
220  
221 Returns a reference to a column for use with other methods.
222 The C<**> method is a shortcut for the C<column> method.
223  
224 =cut
225  
226 sub column {
227 3 ▶ my($me, $col) = @_;
Subroutine column: called CC=11 SCAR=27.4
Pod documented
228 3 my @show;
229 3 ▶ @show = @{$me->{build_data}{Showing}} or @show = @{$me->{Tables}};
Branch: 1 branch
BranchTrueFalseTotal
@show = @{$me->{"build_data"}{"Showing"};} or @show = @{$me->{"Tables"};} 0 3 3
230 3 for my $fld (@show) {
231 return $me->{Column}{$col} ||= bless [$me, $col], 'DBIx::DBO::Column'
232 if (_isa($fld, 'DBIx::DBO::Table') and exists $fld->{Column_Idx}{$col})
233 0 ▶ or (_isa($fld, 'DBIx::DBO::Query') and eval { $fld->column($col) })
Condition75%6/8
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
!ll&&!rl&&r
132
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"}
!ll&&!rl&&r
105
$me->{"Column"}{$col} ||= bless([$me, $col], "DBIx::DBO::Column")
l!l
02
MC/DC0%0/3
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
ref $fld eq "ARRAY" exists $fld->[2]{"AS"} $col eq $fld->[2]{"AS"}
MC/DC0%0/1
$me->{"Column"}{$col} ||= bless([$me, $col], "DBIx::DBO::Column")
$me->{"Column"}{$col}
Truth table0%0/4
A and B and C
A= ref $fld eq "ARRAY"B= exists $fld->[2]{"AS"}C= $col eq $fld->[2]{"AS"}
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $me->{"Column"}{$col}
Aresult
11
00
234 6 ▶ or (ref($fld) eq 'ARRAY' and exists $fld->[2]{AS} and $col eq $fld->[2]{AS});
Branch: 1 branch
BranchTrueFalseTotal
if _isa($fld, "DBIx::DBO::Table") and exists $fld->{"Column_Idx"}{$col} or _isa($fld, "DBIx::DBO::Query") and eval { do { $fld->column($col) } } or ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"} 2 4 6
Condition50%6/12
_isa($fld, "DBIx::DBO::Table") and exists $fld->{"Column_Idx"}{$col} or _isa($fld, "DBIx::DBO::Query") and eval { do { $fld->column($col) } } or ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
l!l&&r!l&&!r
033
_isa($fld, "DBIx::DBO::Table") and exists $fld->{"Column_Idx"}{$col} or _isa($fld, "DBIx::DBO::Query") and eval { do { $fld->column($col) } }
l!l&&r!l&&!r
006
_isa($fld, "DBIx::DBO::Table") and exists $fld->{"Column_Idx"}{$col}
!ll&&!rl&&r
510
_isa($fld, "DBIx::DBO::Query") and eval { do { $fld->column($col) } }
!ll&&!rl&&r
600
MC/DC0%0/5
_isa($fld, "DBIx::DBO::Table") and exists $fld->{"Column_Idx"}{$col} or _isa($fld, "DBIx::DBO::Query") and eval { do { $fld->column($col) } } or ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
_isa($fld, "DBIx::DBO::Table") exists $fld->{"Column_Idx"}{$col} _isa($fld, "DBIx::DBO::Query") eval { do { $fld->column($col) } } ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
Truth table9%1/11
A and B or C and D or E
A= _isa($fld, "DBIx::DBO::Table")B= exists $fld->{"Column_Idx"}{$col}C= _isa($fld, "DBIx::DBO::Query")D= eval { do { $fld->column($col) } }E= ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
235 }
236 1 croak 'No such column: '.$me->{DBO}{dbd_class}->_qi($me, $col);
237 }
238  
239 sub _inner_col {
240 50 ▶ my($me, $col, $_check_aliases) = @_;
Subroutine _inner_col: called CC=9 SCAR=24.0
241 50 ▶ $_check_aliases = $me->{DBO}{dbd_class}->_alias_preference($me, 'column') unless defined $_check_aliases;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_check_aliases 0 50 50
242 50 my $column;
243 50 ▶ return $column if $_check_aliases == 1 and $column = $me->_check_alias($col);
Branch: 1 branch
BranchTrueFalseTotal
if $_check_aliases == 1 and $column = $me->_check_alias($col) 6 44 50
Condition100%3/3
$_check_aliases == 1 and $column = $me->_check_alias($col)
!ll&&!rl&&r
17276
MC/DC100%2/2
$_check_aliases == 1 and $column = $me->_check_alias($col)
$_check_aliases == 1 $column = $me->_check_alias($col)
Truth table100%3/3
A and B
A= $_check_aliases == 1B= $column = $me->_check_alias($col)
ABresult
0X0
100
111
244 44 for my $tbl ($me->tables) {
245 44 ▶ return $tbl->column($col) if exists $tbl->{Column_Idx}{$col};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tbl->{"Column_Idx"}{$col} 43 1 44
246 }
247 1 ▶ return $column if $_check_aliases == 2 and $column = $me->_check_alias($col);
Branch: 1 branch
BranchTrueFalseTotal
if $_check_aliases == 2 and $column = $me->_check_alias($col) 1 0 1
Condition33%1/3
$_check_aliases == 2 and $column = $me->_check_alias($col)
!ll&&!rl&&r
001
MC/DC0%0/2
$_check_aliases == 2 and $column = $me->_check_alias($col)
$_check_aliases == 2 $column = $me->_check_alias($col)
Truth table33%1/3
A and B
A= $_check_aliases == 2B= $column = $me->_check_alias($col)
ABresult
0X0
100
111
248 0 ▶ croak 'No such column'.($_check_aliases ? '/alias' : '').': '.$me->{DBO}{dbd_class}->_qi($me, $col);
Branch: 1 branch
BranchTrueFalseTotal
$_check_aliases ? : 0 0 0
249 }
250  
251 sub _check_alias {
252 34 ▶ my($me, $col) = @_;
Subroutine _check_alias: called CC=6 SCAR=17.9
253 34 for my $fld (@{$me->{build_data}{Showing}}) {
254 return $me->{Column}{$col} ||= bless [$me, $col], 'DBIx::DBO::Column'
255 30 ▶ if ref($fld) eq 'ARRAY' and exists $fld->[2]{AS} and $col eq $fld->[2]{AS};
Branch: 1 branch
BranchTrueFalseTotal
if ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"} 7 23 30
Condition87%7/8
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
!ll&&!rl&&r
1947
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"}
!ll&&!rl&&r
01911
$me->{"Column"}{$col} ||= bless([$me, $col], "DBIx::DBO::Column")
l!l
61
MC/DC66%2/3
ref $fld eq "ARRAY" and exists $fld->[2]{"AS"} and $col eq $fld->[2]{"AS"}
ref $fld eq "ARRAY" exists $fld->[2]{"AS"} $col eq $fld->[2]{"AS"}
MC/DC100%1/1
$me->{"Column"}{$col} ||= bless([$me, $col], "DBIx::DBO::Column")
$me->{"Column"}{$col}
Truth table75%3/4
A and B and C
A= ref $fld eq "ARRAY"B= exists $fld->[2]{"AS"}C= $col eq $fld->[2]{"AS"}
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $me->{"Column"}{$col}
Aresult
11
00
256 }
257 }
258  
259 =head3 C<show>
260  
261 $query->show(@columns);
262 $query->show($table1, { COL => $table2 ** 'name', AS => 'name2' });
263 $query->show($table1 ** 'id', { FUNC => 'UCASE(?)', COL => 'name', AS => 'alias' }, ...
264  
265 List which columns to return when we L<fetch>.
266 If called without arguments all columns will be shown, C<SELECT * ...>.
267 If you use a Table object, all the columns from that table will be shown, C<SELECT table.* ...>
268 You can also add a subquery by passing that Query as the value with an alias, Eg.
269  
270 $query->show({ VAL => $subquery, AS => 'sq' }, ...);
271 # SELECT ($subquery_sql) AS sq ...
272  
273 =cut
274  
275 # TODO: Keep track of all aliases in use and die if a used alias is removed
276 sub show {
277 26 ▶ my $me = shift;
Subroutine show: called CC=4 SCAR=13.9
Pod documented
278 26 undef $me->{sql};
279 26 undef $me->{build_data}{from};
280 26 undef $me->{build_data}{show};
281 26 undef @{$me->{build_data}{Showing}};
282 26 undef @{$me->{Columns}};
283 26 for my $fld (@_) {
284 21 ▶ if (_isa($fld, 'DBIx::DBO::Table', 'DBIx::DBO::Query')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isa($fld, "DBIx::DBO::Table", "DBIx::DBO::Query")) 6 15 21
285 6 ▶ croak 'Invalid table to show' unless defined $me->_table_idx($fld);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $me->_table_idx($fld) 1 5 6
286 5 push @{$me->{build_data}{Showing}}, $fld;
287 5 push @{$me->{Columns}}, $fld->columns;
288 5 next;
289 }
290 # If the $fld is just a scalar use it as a column name not a value
291 15 my @col = $me->{DBO}{dbd_class}->_parse_col_val($me, $fld, Aliases => 0);
292 15 push @{$me->{build_data}{Showing}}, \@col;
293 15 push @{$me->{Columns}}, $me->_build_col_val_name(@col);
294 }
295 }
296  
297 =head3 C<distinct>
298  
299 $query->distinct(1);
300  
301 Takes a boolean argument to add or remove the DISTINCT clause for the returned rows.
302  
303 =cut
304  
305 sub distinct {
306 16 ▶ my $me = shift;
Subroutine distinct: called CC=3 SCAR=11.0
Pod documented
307 16 undef $me->{sql};
308 16 undef $me->{build_data}{show};
309 16 my $distinct = $me->{build_data}{Show_Distinct};
310 16 ▶ $me->{build_data}{Show_Distinct} = shift() ? 1 : undef if @_;
Branch: 2 branches
BranchTrueFalseTotal
if @_ 16 0 16
shift() ? : 1 15 16
311 }
312  
313 =head3 C<join_table>
314  
315 $query->join_table($table, $join_type);
316  
317 Join a table onto the query, creating a L<Table|DBIx::DBO::Table> object if needed.
318 This will perform a comma (", ") join unless $join_type is specified.
319  
320 Tables can be specified with the same arguments as L<DBIx::DBO::Table/new> or another Query can be used as a subquery.
321  
322 Valid join types are any accepted by the DB. Eg: C<'JOIN'>, C<'LEFT'>, C<'RIGHT'>, C<undef> (for comma join), C<'INNER'>, C<'OUTER'>, ...
323  
324 Returns the Table or Query object added.
325  
326 =cut
327  
328 sub join_table {
329 18 ▶ my($me, $tbl, $type) = @_;
Subroutine join_table: called CC=8 SCAR=20.9
Pod documented
330 18 ▶ if (_isa($tbl, 'DBIx::DBO::Table')) {
Branch: 2 branches
BranchTrueFalseTotal
if (_isa($tbl, 'DBIx::DBO::Table')) { } 9 9 18
elsif (_isa($tbl, 'DBIx::DBO::Query')) { } 0 9 9
331 9 ▶ croak 'This table is already in this query' if defined $me->_table_idx($tbl);
Branch: 1 branch
BranchTrueFalseTotal
if defined $me->_table_idx($tbl) 1 8 9
332 8 ▶ croak 'This table is from a different DBO connection' if $me->{DBO} != $tbl->{DBO};
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"DBO"} != $tbl->{"DBO"} 1 7 8
333 } elsif (_isa($tbl, 'DBIx::DBO::Query')) {
334 # Subquery
335 0 ▶ croak 'This table is from a different DBO connection' if $me->{DBO} != $tbl->{DBO};
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"DBO"} != $tbl->{"DBO"} 0 0 0
336 } else {
337 9 $tbl = $me->_table_class->new($me->{DBO}, $tbl);
338 }
339 16 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 3 13 16
340 3 $type =~ s/^\s*/ /;
341 3 $type =~ s/\s*$/ /;
342 3 $type = uc $type;
343 3 ▶ $type .= 'JOIN ' if $type !~ /\bJOIN\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /\bJOIN\b/ 1 2 3
344 } else {
345 13 $type = ', ';
346 }
347 16 push @{$me->{Tables}}, $tbl;
348 16 push @{$me->{build_data}{Join}}, $type;
349 16 push @{$me->{build_data}{Join_On}}, undef;
350 16 push @{$me->{Join_Bracket_Refs}}, [];
351 16 push @{$me->{Join_Brackets}}, [];
352 16 undef $me->{sql};
353 16 undef $me->{build_data}{from};
354 16 undef $me->{build_data}{show};
355 16 undef @{$me->{Columns}};
356 16 return $tbl;
357 }
358  
359 =head3 C<join_on>
360  
361 $query->join_on($table_object, $expression1, $operator, $expression2);
362 $query->join_on($table2, $table1 ** 'id', '=', $table2 ** 'id');
363  
364 Join tables on a specific WHERE clause. The first argument is the table object being joined onto.
365 Then a JOIN ON condition follows, which uses the same arguments as L</where>.
366  
367 =cut
368  
369 sub join_on {
370 7 ▶ my $me = shift;
Subroutine join_on: called CC=5 SCAR=16.1
Pod documented
371 7 my $t2 = shift;
372 7 ▶ my $i = $me->_table_idx($t2) or croak 'Invalid table object to join onto';
Branch: 1 branch
BranchTrueFalseTotal
my $i = $me->_table_idx($t2) or croak("Invalid table object to join onto") 1 6 7
373  
374 6 my($col1, $col1_func, $col1_opt) = $me->{DBO}{dbd_class}->_parse_col_val($me, shift);
375 6 my $op = shift;
376 6 my($col2, $col2_func, $col2_opt) = $me->{DBO}{dbd_class}->_parse_col_val($me, shift);
377  
378 # Validate the fields
379 6 $me->_validate_where_fields(@$col1, @$col2);
380  
381 # Force a new search
382 6 undef $me->{sql};
383 6 undef $me->{build_data}{from};
384  
385 # Find the current Join_On reference
386 6 ▶ my $ref = $me->{build_data}{Join_On}[$i] ||= [];
Condition100%2/2
$me->{"build_data"}{"Join_On"}[$i] ||= []
l!l
24
MC/DC100%1/1
$me->{"build_data"}{"Join_On"}[$i] ||= []
$me->{"build_data"}{"Join_On"}[$i]
Truth table100%2/2
A
A= $me->{"build_data"}{"Join_On"}[$i]
Aresult
11
00
387 6 $ref = $ref->[$_] for (@{$me->{Join_Bracket_Refs}[$i]});
388  
389 6 ▶ $me->{build_data}{Join}[$i] = ' JOIN ' if $me->{build_data}{Join}[$i] eq ', ';
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"build_data"}{"Join"}[$i] eq ", " 1 5 6
390 6 $me->_add_where($ref, $op, $col1, $col1_func, $col1_opt, $col2, $col2_func, $col2_opt, @_);
391 }
392  
393 =head3 C<open_join_on_bracket>, C<close_join_on_bracket>
394  
395 $query->open_join_on_bracket($table, 'OR');
396 $query->join_on(...
397 $query->close_join_on_bracket($table);
398  
399 Equivalent to L<open_bracket|/open_bracket__close_bracket>, but for the JOIN ON clause.
400 The first argument is the table being joined onto.
401  
402 =cut
403  
404 sub open_join_on_bracket {
405 3 ▶ my $me = shift;
Subroutine open_join_on_bracket: called CC=4 SCAR=13.9
Pod documented
406 3 ▶ my $tbl = shift or croak 'Invalid table object for join on clause';
Branch: 1 branch
BranchTrueFalseTotal
my $tbl = shift() or croak("Invalid table object for join on clause") 1 2 3
407 2 ▶ my $i = $me->_table_idx($tbl) or croak 'No such table object in the join';
Branch: 1 branch
BranchTrueFalseTotal
my $i = $me->_table_idx($tbl) or croak("No such table object in the join") 1 1 2
408 1 ▶ $me->_open_bracket($me->{Join_Brackets}[$i], $me->{Join_Bracket_Refs}[$i], $me->{build_data}{Join_On}[$i] ||= [], @_);
Condition50%1/2
$me->{"build_data"}{"Join_On"}[$i] ||= []
l!l
10
MC/DC0%0/1
$me->{"build_data"}{"Join_On"}[$i] ||= []
$me->{"build_data"}{"Join_On"}[$i]
Truth table50%1/2
A
A= $me->{"build_data"}{"Join_On"}[$i]
Aresult
11
00
409 }
410  
411 sub close_join_on_bracket {
412 3 ▶ my $me = shift;
Subroutine close_join_on_bracket: called CC=3 SCAR=11.0
Pod documented
413 3 ▶ my $tbl = shift or croak 'Invalid table object for join on clause';
Branch: 1 branch
BranchTrueFalseTotal
my $tbl = shift() or croak("Invalid table object for join on clause") 1 2 3
414 2 ▶ my $i = $me->_table_idx($tbl) or croak 'No such table object in the join';
Branch: 1 branch
BranchTrueFalseTotal
my $i = $me->_table_idx($tbl) or croak("No such table object in the join") 1 1 2
415 1 $me->_close_bracket($me->{Join_Brackets}[$i], $me->{Join_Bracket_Refs}[$i]);
416 }
417  
418 =head3 C<where>
419  
420 Restrict the query with the condition specified (WHERE clause).
421  
422 $query->where($expression1, $operator, $expression2);
423  
424 C<$operator> is one of: C<'=', '<E<gt>', '<', 'E<gt>', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', 'BETWEEN', 'NOT BETWEEN', ...>
425  
426 C<$expression>s can be any of the following:
427  
428 =over 4
429  
430 =item *
431  
432 A scalar value: C<123> or C<'hello'> (or for C<$expression1> a column name: C<'id'>)
433  
434 $query->where('name', '<>', 'John');
435  
436 =item *
437  
438 A scalar reference: C<\"22 * 3"> (These are passed unquoted in the SQL statement!)
439  
440 $query->where(\'CONCAT(id, name)', '=', \'"22John"');
441  
442 =item *
443  
444 An array reference: C<[1, 3, 5]> (Used with C<IN> and C<BETWEEN> etc)
445  
446 $query->where('id', 'NOT IN', [21, 22, 25, 39]);
447  
448 =item *
449  
450 A Column object: C<$table ** 'id'> or C<$table-E<gt>column('id')>
451  
452 $query->where($table1 ** 'id', '=', $table2 ** 'id');
453  
454 =item *
455  
456 A Query object, to be used as a subquery.
457  
458 $query->where('id', '>', $subquery);
459  
460 =item *
461  
462 A hash reference: see L</Complex_expressions>
463  
464 =back
465  
466 Multiple C<where> expressions are combined I<cleverly> using the preferred aggregator C<'AND'> (unless L<open_bracket|/open_bracket__close_bracket> was used to change this). So that when you add where expressions to the query, they will be C<AND>ed together. However some expressions that refer to the same column will automatically be C<OR>ed instead where this makes sense, currently: C<'='>, C<'IS NULL'>, C<'E<lt>=E<gt>'>, C<'IN'> and C<'BETWEEN'>. Similarly, when the preferred aggregator is C<'OR'> the following operators will be C<AND>ed together: C<'!='>, C<'IS NOT NULL'>, C<'E<lt>E<gt>'>, C<'NOT IN'> and C<'NOT BETWEEN'>.
467  
468 $query->where('id', '=', 5);
469 $query->where('name', '=', 'Bob');
470 $query->where('id', '=', 7);
471 $query->where(...
472 # Produces: WHERE ("id" = 5 OR "id" = 7) AND "name" = 'Bob' AND ...
473  
474 =cut
475  
476 sub where {
477 20 ▶ my $me = shift;
Subroutine where: called CC=3 SCAR=11.0
Pod documented
478  
479 # If the $fld is just a scalar use it as a column name not a value
480 20 my($fld, $fld_func, $fld_opt) = $me->{DBO}{dbd_class}->_parse_col_val($me, shift);
481 20 my $op = shift;
482 20 my($val, $val_func, $val_opt) = $me->{DBO}{dbd_class}->_parse_val($me, shift, Check => 'Auto');
483  
484 # Validate the fields
485 19 $me->_validate_where_fields(@$fld, @$val);
486  
487 # Force a new search
488 19 undef $me->{sql};
489 19 undef $me->{build_data}{where};
490  
491 # Find the current Where_Data reference
492 19 ▶ my $ref = $me->{build_data}{Where_Data} ||= [];
Condition100%2/2
$me->{"build_data"}{"Where_Data"} ||= []
l!l
172
MC/DC100%1/1
$me->{"build_data"}{"Where_Data"} ||= []
$me->{"build_data"}{"Where_Data"}
Truth table100%2/2
A
A= $me->{"build_data"}{"Where_Data"}
Aresult
11
00
493 19 $ref = $ref->[$_] for (@{$me->{Where_Bracket_Refs}});
494  
495 19 $me->_add_where($ref, $op, $fld, $fld_func, $fld_opt, $val, $val_func, $val_opt, @_);
496 }
497  
498 =head3 C<unwhere>
499  
500 $query->unwhere();
501 $query->unwhere($column);
502  
503 Removes all previously added L</where> restrictions for a column.
504 If no column is provided, the I<whole> WHERE clause is removed.
505  
506 =cut
507  
508 sub unwhere {
509 16 ▶ my $me = shift;
Subroutine unwhere: called CC=1 SCAR=0.0
Pod documented
510 16 $me->_del_where('Where', @_);
511 }
512  
513 sub _validate_where_fields {
514 28 ▶ my $me = shift;
Subroutine _validate_where_fields: called CC=6 SCAR=18.5
515 28 for my $f (@_) {
516 58 ▶ if (_isa($f, 'DBIx::DBO::Column')) {
Branch: 2 branches
BranchTrueFalseTotal
if (_isa($f, 'DBIx::DBO::Column')) { } 32 26 58
elsif (my $type = ref $f) { } 1 25 26
517 32 $me->{DBO}{dbd_class}->_valid_col($me, $f);
518 } elsif (my $type = ref $f) {
519 1 ▶ croak 'Invalid value type: '.$type if $type ne 'SCALAR' and not _isa($f, 'DBIx::DBO::Query');
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "SCALAR" and not _isa($f, "DBIx::DBO::Query") 0 1 1
Condition33%1/3
$type ne "SCALAR" and not _isa($f, "DBIx::DBO::Query")
!ll&&!rl&&r
100
MC/DC0%0/2
$type ne "SCALAR" and not _isa($f, "DBIx::DBO::Query")
$type ne "SCALAR" not _isa($f, "DBIx::DBO::Query")
Truth table33%1/3
A and not(B)
A= $type ne "SCALAR"B= not _isa($f, "DBIx::DBO::Query")
ABresult
0X0
100
111
520 }
521 }
522 }
523  
524 sub _del_where {
525 32 ▶ my $me = shift;
Subroutine _del_where: called CC=6 SCAR=17.9
526 32 my $clause = shift;
527  
528 32 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4 28 32
529 4 require Data::Dumper;
530 4 my($fld, $fld_func, $fld_opt) = $me->{DBO}{dbd_class}->_parse_col_val($me, shift);
531 # TODO: Validate the fields?
532  
533 4 ▶ return unless exists $me->{build_data}{$clause.'_Data'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $me->{"build_data"}{$clause . '_Data'} 0 4 4
534 # Find the current Where_Data reference
535 4 my $ref = $me->{build_data}{$clause.'_Data'};
536 4 $ref = $ref->[$_] for (@{$me->{$clause.'_Bracket_Refs'}});
537  
538 4 local $Data::Dumper::Indent = 0;
539 4 local $Data::Dumper::Maxdepth = 2;
540 my @match = grep {
541 4 Data::Dumper::Dumper($fld, $fld_func, $fld_opt) eq Data::Dumper::Dumper(@{$ref->[$_]}[1,2,3])
542 } 0 .. $#$ref;
543  
544 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 3 4
545 1 my $op = shift;
546 1 my($val, $val_func, $val_opt) = $me->{DBO}{dbd_class}->_parse_val($me, shift, Check => 'Auto');
547  
548 @match = grep {
549 1 Data::Dumper::Dumper($op, $val, $val_func, $val_opt) eq Data::Dumper::Dumper(@{$ref->[$_]}[0,4,5,6])
550 } @match;
551 }
552 4 splice @$ref, $_, 1 for reverse @match;
553 } else {
554 28 delete $me->{build_data}{$clause.'_Data'};
555 28 $me->{$clause.'_Bracket_Refs'} = [];
556 28 $me->{$clause.'_Brackets'} = [];
557 }
558 # This forces a new search
559 32 undef $me->{sql};
560 32 undef $me->{build_data}{lc $clause};
561 }
562  
563 ##
564 # This will add an arrayref to the $ref given.
565 # The arrayref will contain 8 values:
566 # $op, $fld, $fld_func, $fld_opt, $val, $val_func, $val_opt, $force
567 # $op is the operator (those supported differ by DBD)
568 # $fld_func is undef or a scalar of the form '? AND ?' or 'POSITION(? IN ?)'
569 # $fld is an arrayref of columns/values for use with $fld_func
570 # $val_func is similar to $fld_func
571 # $val is an arrayref of values for use with $val_func
572 # $force is one of undef / 'AND' / 'OR' which if defined, overrides the default aggregator
573 ##
574 sub _add_where {
575 28 ▶ my $me = shift;
Subroutine _add_where: called CC=27 SCAR=34.4
576 28 my($ref, $op, $fld, $fld_func, $fld_opt, $val, $val_func, $val_opt, %opt) = @_;
577  
578 croak 'Invalid option, FORCE must be AND or OR'
579 28 ▶ if defined $opt{FORCE} and $opt{FORCE} ne 'AND' and $opt{FORCE} ne 'OR';
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"FORCE"} and $opt{"FORCE"} ne "AND" and $opt{"FORCE"} ne "OR" 0 28 28
Condition66%4/6
defined $opt{"FORCE"} and $opt{"FORCE"} ne "AND" and $opt{"FORCE"} ne "OR"
!ll&&!rl&&r
2620
defined $opt{"FORCE"} and $opt{"FORCE"} ne "AND"
!ll&&!rl&&r
2602
MC/DC0%0/3
defined $opt{"FORCE"} and $opt{"FORCE"} ne "AND" and $opt{"FORCE"} ne "OR"
defined $opt{"FORCE"} $opt{"FORCE"} ne "AND" $opt{"FORCE"} ne "OR"
Truth table50%2/4
A and B and C
A= defined $opt{"FORCE"}B= $opt{"FORCE"} ne "AND"C= $opt{"FORCE"} ne "OR"
ABCresult
0XX0
10X0
1100
1111
580  
581 # Deal with NULL values
582 28 ▶ $op = '<>' if $op eq '!='; # Use the valid SQL op
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "!=" 2 26 28
583 28 ▶ if (@$val == 1 and !defined $val->[0] and !defined $val_func) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$val == 1 and not defined $val->[0] and not defined $val_func) 2 26 28
Condition83%5/6
@$val == 1 and not defined $val->[0] and not defined $val_func
!ll&&!rl&&r
2602
@$val == 1 and not defined $val->[0]
!ll&&!rl&&r
11152
MC/DC66%2/3
@$val == 1 and not defined $val->[0] and not defined $val_func
@$val == 1 not defined $val->[0] not defined $val_func
Truth table75%3/4
A and not(B) and not(C)
A= @$val == 1B= not defined $val->[0]C= not defined $val_func
ABCresult
0XX0
10X0
1100
1111
584 2 ▶ if ($op eq '=') { $op = 'IS'; $val_func = 'NULL'; delete $val->[0]; }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '=') { } 1 1 2
elsif ($op eq '<>') { } 1 0 1
585 1 elsif ($op eq '<>') { $op = 'IS NOT'; $val_func = 'NULL'; delete $val->[0]; }
586 }
587  
588 # Deal with array values: BETWEEN & IN
589 28 ▶ unless (defined $val_func) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val_func) 19 9 28
590 19 ▶ if ($op eq 'BETWEEN' or $op eq 'NOT BETWEEN') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'BETWEEN' or $op eq 'NOT BETWEEN') { } 3 16 19
elsif ($op eq 'IN' or $op eq 'NOT IN') { } 3 13 16
elsif (@$val != 1) { } 1 12 13
Condition100%3/3
$op eq "BETWEEN" or $op eq "NOT BETWEEN"
l!l&&r!l&&!r
2116
MC/DC100%2/2
$op eq "BETWEEN" or $op eq "NOT BETWEEN"
$op eq "BETWEEN" $op eq "NOT BETWEEN"
Truth table100%3/3
A or B
A= $op eq "BETWEEN"B= $op eq "NOT BETWEEN"
ABresult
1X1
011
000
591 3 ▶ croak 'Invalid value argument, BETWEEN requires 2 values'
Branch: 1 branch
BranchTrueFalseTotal
if ref $val ne "ARRAY" or @$val != 2 1 2 3
Condition66%2/3
ref $val ne "ARRAY" or @$val != 2
l!l&&r!l&&!r
012
MC/DC50%1/2
ref $val ne "ARRAY" or @$val != 2
ref $val ne "ARRAY" @$val != 2
Truth table66%2/3
A or B
A= ref $val ne "ARRAY"B= @$val != 2
ABresult
1X1
011
000
592 if ref $val ne 'ARRAY' or @$val != 2;
593 2 $val_func = $me->{DBO}{dbd_class}->PLACEHOLDER.' AND '.$me->{DBO}{dbd_class}->PLACEHOLDER;
594 ▶ } elsif ($op eq 'IN' or $op eq 'NOT IN') {
Condition100%3/3
$op eq "IN" or $op eq "NOT IN"
l!l&&r!l&&!r
1213
MC/DC100%2/2
$op eq "IN" or $op eq "NOT IN"
$op eq "IN" $op eq "NOT IN"
Truth table100%3/3
A or B
A= $op eq "IN"B= $op eq "NOT IN"
ABresult
1X1
011
000
595 3 ▶ if (ref $val eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 3 0 3
596 3 ▶ croak 'Invalid value argument, IN requires at least 1 value' if @$val == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$val == 0 0 3 3
597 } else {
598 0 $val = [ $val ];
599 }
600 # Add to previous 'IN' and 'NOT IN' Where expressions
601 3 my $op_ag = $me->{DBO}{dbd_class}->_op_ag($op);
602 3 ▶ unless ($opt{FORCE} and $opt{FORCE} ne $op_ag) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"FORCE"} and $opt{"FORCE"} ne $op_ag) 3 0 3
Condition33%1/3
$opt{"FORCE"} and $opt{"FORCE"} ne $op_ag
!ll&&!rl&&r
300
MC/DC0%0/2
$opt{"FORCE"} and $opt{"FORCE"} ne $op_ag
$opt{"FORCE"} $opt{"FORCE"} ne $op_ag
Truth table33%1/3
A and B
A= $opt{"FORCE"}B= $opt{"FORCE"} ne $op_ag
ABresult
0X0
100
111
603 3 for my $lim (grep $$_[0] eq $op, @$ref) {
604 # $fld and $$lim[1] are always ARRAY refs
605 1 ▶ next if "@{$$lim[1]}" ne "@$fld";
Branch: 1 branch
BranchTrueFalseTotal
if join($", @{$lim->[1];}) ne join($", @$fld) 0 1 1
606 1 ▶ last if $$lim[7] and $$lim[7] ne $op_ag;
Branch: 1 branch
BranchTrueFalseTotal
if $lim->[7] and $lim->[7] ne $op_ag 0 1 1
Condition33%1/3
$lim->[7] and $lim->[7] ne $op_ag
!ll&&!rl&&r
100
MC/DC0%0/2
$lim->[7] and $lim->[7] ne $op_ag
$lim->[7] $lim->[7] ne $op_ag
Truth table33%1/3
A and B
A= $lim->[7]B= $lim->[7] ne $op_ag
ABresult
0X0
100
111
607 1 ▶ last if $$lim[5] ne '('.join(',', ($me->{DBO}{dbd_class}->PLACEHOLDER) x @{$$lim[4]}).')';
Branch: 1 branch
BranchTrueFalseTotal
if $lim->[5] ne '(' . join(",", ($me->{"DBO"}{"dbd_class"}->PLACEHOLDER) x @{$lim->[4];}) . ')' 0 1 1
608 1 push @{$$lim[4]}, @$val;
609 1 $$lim[5] = '('.join(',', ($me->{DBO}{dbd_class}->PLACEHOLDER) x @{$$lim[4]}).')';
610 1 return;
611 }
612 }
613 2 $val_func = '('.join(',', ($me->{DBO}{dbd_class}->PLACEHOLDER) x @$val).')';
614 } elsif (@$val != 1) {
615 # Check that there is only 1 placeholder
616 1 croak 'Wrong number of fields/values, called with '.@$val.' while needing 1';
617 }
618 }
619  
620 25 push @{$ref}, [ $op, $fld, $fld_func, $fld_opt, $val, $val_func, $val_opt, $opt{FORCE} ];
621 }
622  
623 =head3 C<open_bracket>, C<close_bracket>
624  
625 $query->open_bracket('OR');
626 $query->where( ...
627 $query->where( ...
628 $query->close_bracket;
629  
630 Used to group C<where> expressions together in parenthesis using either C<'AND'> or C<'OR'> as the preferred aggregator.
631 All the C<where> calls made between C<open_bracket> and C<close_bracket> will be inside the parenthesis.
632  
633 Without any parenthesis C<'AND'> is the preferred aggregator.
634  
635 =cut
636  
637 sub open_bracket {
638 2 ▶ my $me = shift;
Subroutine open_bracket: called CC=2 SCAR=7.2
Pod documented
639 2 ▶ $me->_open_bracket($me->{Where_Brackets}, $me->{Where_Bracket_Refs}, $me->{build_data}{Where_Data} ||= [], @_);
Condition50%1/2
$me->{"build_data"}{"Where_Data"} ||= []
l!l
20
MC/DC0%0/1
$me->{"build_data"}{"Where_Data"} ||= []
$me->{"build_data"}{"Where_Data"}
Truth table50%1/2
A
A= $me->{"build_data"}{"Where_Data"}
Aresult
11
00
640 }
641  
642 sub _open_bracket {
643 3 ▶ my($me, $brackets, $bracket_refs, $ref, $ag) = @_;
Subroutine _open_bracket: called CC=6 SCAR=18.7
644 3 ▶ croak 'Invalid argument MUST be AND or OR' if !$ag or $ag !~ /^(AND|OR)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ag and $ag =~ /^(AND|OR)$/ 0 3 3
Condition33%1/3
$ag and $ag =~ /^(AND|OR)$/
!ll&&!rl&&r
003
MC/DC0%0/2
$ag and $ag =~ /^(AND|OR)$/
$ag $ag =~ /^(AND|OR)$/
Truth table33%1/3
A and B
A= $agB= $ag =~ /^(AND|OR)$/
ABresult
0X0
100
111
645 3 ▶ my $last = @$brackets ? $brackets->[-1] : 'AND';
Branch: 1 branch
BranchTrueFalseTotal
@$brackets ? : 1 2 3
646 3 ▶ if ($ag ne $last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ag ne $last) 3 0 3
647 # Find the current data reference
648 3 $ref = $ref->[$_] for @$bracket_refs;
649  
650 3 push @$ref, [];
651 3 push @$bracket_refs, $#$ref;
652 }
653 3 push @$brackets, $ag;
654 }
655  
656 sub close_bracket {
657 2 ▶ my $me = shift;
Subroutine close_bracket: called CC=1 SCAR=0.0
Pod documented
658 2 $me->_close_bracket($me->{Where_Brackets}, $me->{Where_Bracket_Refs});
659 }
660  
661 sub _close_bracket {
662 3 ▶ my($me, $brackets, $bracket_refs) = @_;
Subroutine _close_bracket: called CC=4 SCAR=14.0
663 3 ▶ my $ag = pop @{$brackets} or croak "Can't close bracket with no open bracket!";
Branch: 1 branch
BranchTrueFalseTotal
my $ag = pop @{$brackets;} or croak("Can't close bracket with no open bracket!") 0 3 3
664 3 ▶ my $last = @$brackets ? $brackets->[-1] : 'AND';
Branch: 1 branch
BranchTrueFalseTotal
@$brackets ? : 1 2 3
665 3 ▶ pop @$bracket_refs if $last ne $ag;
Branch: 1 branch
BranchTrueFalseTotal
if $last ne $ag 3 0 3
666 3 return $ag;
667 }
668  
669 =head3 C<group_by>
670  
671 $query->group_by('column', ...);
672 $query->group_by($table ** 'column', ...);
673 $query->group_by({ COL => $table ** 'column', ORDER => 'DESC' }, ...);
674  
675 Group the results by the column(s) listed. This will replace the GROUP BY clause.
676 To remove the GROUP BY clause simply call C<group_by> without any columns.
677  
678 =cut
679  
680 sub group_by {
681 17 ▶ my $me = shift;
Subroutine group_by: called CC=2 SCAR=6.9
Pod documented
682 17 undef $me->{sql};
683 17 undef $me->{build_data}{group};
684 17 undef @{$me->{build_data}{GroupBy}};
685 17 for my $col (@_) {
686 3 my @group = $me->{DBO}{dbd_class}->_parse_col_val($me, $col);
687 3 push @{$me->{build_data}{GroupBy}}, \@group;
688 }
689 }
690  
691 =head3 C<having>
692  
693 Restrict the query with the condition specified (HAVING clause). This takes the same arguments as L</where>.
694  
695 $query->having($expression1, $operator, $expression2);
696  
697 =cut
698  
699 sub having {
700 3 ▶ my $me = shift;
Subroutine having: called CC=3 SCAR=11.0
Pod documented
701  
702 # If the $fld is just a scalar use it as a column name not a value
703 3 my($fld, $fld_func, $fld_opt) = $me->{DBO}{dbd_class}->_parse_col_val($me, shift);
704 3 my $op = shift;
705 3 my($val, $val_func, $val_opt) = $me->{DBO}{dbd_class}->_parse_val($me, shift, Check => 'Auto');
706  
707 # Validate the fields
708 3 $me->_validate_where_fields(@$fld, @$val);
709  
710 # Force a new search
711 3 undef $me->{sql};
712 3 undef $me->{build_data}{having};
713  
714 # Find the current Having_Data reference
715 3 ▶ my $ref = $me->{build_data}{Having_Data} ||= [];
Condition100%2/2
$me->{"build_data"}{"Having_Data"} ||= []
l!l
21
MC/DC100%1/1
$me->{"build_data"}{"Having_Data"} ||= []
$me->{"build_data"}{"Having_Data"}
Truth table100%2/2
A
A= $me->{"build_data"}{"Having_Data"}
Aresult
11
00
716 3 $ref = $ref->[$_] for (@{$me->{Having_Bracket_Refs}});
717  
718 3 $me->_add_where($ref, $op, $fld, $fld_func, $fld_opt, $val, $val_func, $val_opt, @_);
719 }
720  
721 =head3 C<unhaving>
722  
723 $query->unhaving();
724 $query->unhaving($column);
725  
726 Removes all previously added L</having> restrictions for a column.
727 If no column is provided, the I<whole> HAVING clause is removed.
728  
729 =cut
730  
731 sub unhaving {
732 16 ▶ my $me = shift;
Subroutine unhaving: called CC=1 SCAR=0.0
Pod documented
733 16 $me->_del_where('Having', @_);
734 }
735  
736 =head3 C<order_by>
737  
738 $query->order_by('column', ...);
739 $query->order_by($table ** 'column', ...);
740 $query->order_by({ COL => $table ** 'column', ORDER => 'DESC' }, ...);
741  
742 Order the results by the column(s) listed. This will replace the ORDER BY clause.
743 To remove the ORDER BY clause simply call C<order_by> without any columns.
744  
745 =cut
746  
747 sub order_by {
748 24 ▶ my $me = shift;
Subroutine order_by: called CC=2 SCAR=6.9
Pod documented
749 24 undef $me->{sql};
750 24 undef $me->{build_data}{order};
751 24 undef @{$me->{build_data}{OrderBy}};
752 24 for my $col (@_) {
753 9 my @order = $me->{DBO}{dbd_class}->_parse_col_val($me, $col);
754 9 push @{$me->{build_data}{OrderBy}}, \@order;
755 }
756 }
757  
758 =head3 C<limit>
759  
760 $query->limit;
761 $query->limit($rows);
762 $query->limit($rows, $offset);
763  
764 Limit the maximum number of rows returned to C<$rows>, optionally skipping the first C<$offset> rows.
765 When called without arguments or if C<$rows> is undefined, the limit is removed.
766  
767 NB. Oracle does not support pagging prior to version 12c, so this has been implemented in software,
768 , but if an offset is given, an extra column "_DBO_ROWNUM_" is added to the Query to achieve this.
769 TODO: Implement the new "FIRST n / NEXT n" clause if connected to a 12c database.
770  
771 =cut
772  
773 sub limit {
774 18 ▶ my($me, $rows, $offset) = @_;
Subroutine limit: called CC=4 SCAR=13.9
Pod documented
775 18 undef $me->{sql};
776 18 undef $me->{build_data}{limit};
777 18 ▶ return undef $me->{build_data}{LimitOffset} unless defined $rows;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rows 14 4 18
778 4 ▶ /^\d+$/ or croak "Invalid argument '$_' in limit" for grep defined, $rows, $offset;
Branch: 1 branch
BranchTrueFalseTotal
/^\d+$/ or croak("Invalid argument '${_}' in limit") 0 6 6
779 4 @{$me->{build_data}{LimitOffset}} = ($rows, $offset);
780 }
781  
782 =head3 C<arrayref>
783  
784 $query->arrayref;
785 $query->arrayref(\%attr);
786  
787 Run the query using L<DBI-E<gt>selectall_arrayref|DBI/"selectall_arrayref"> which returns the result as an arrayref.
788 You can specify a slice by including a 'Slice' or 'Columns' attribute in C<%attr> - See L<DBI-E<gt>selectall_arrayref|DBI/"selectall_arrayref">.
789  
790 =cut
791  
792 sub arrayref {
793 3 ▶ my($me, $attr) = @_;
Subroutine arrayref: called CC=1 SCAR=0.0
Pod documented
794 $me->{DBO}{dbd_class}->_selectall_arrayref($me, $me->sql, $attr,
795 3 $me->{DBO}{dbd_class}->_bind_params_select($me));
796 }
797  
798 =head3 C<hashref>
799  
800 $query->hashref($key_field);
801 $query->hashref($key_field, \%attr);
802  
803 Run the query using L<DBI-E<gt>selectall_hashref|DBI/"selectall_hashref"> which returns the result as an hashref.
804 C<$key_field> defines which column, or columns, are used as keys in the returned hash.
805  
806 =cut
807  
808 sub hashref {
809 1 ▶ my($me, $key, $attr) = @_;
Subroutine hashref: called CC=1 SCAR=0.0
Pod documented
810 $me->{DBO}{dbd_class}->_selectall_hashref($me, $me->sql, $key, $attr,
811 1 $me->{DBO}{dbd_class}->_bind_params_select($me));
812 }
813  
814 =head3 C<col_arrayref>
815  
816 $query->col_arrayref;
817 $query->col_arrayref(\%attr);
818  
819 Run the query using L<DBI-E<gt>selectcol_arrayref|DBI/"selectcol_arrayref"> which returns the result as an arrayref of the values of each row in one array. By default it pushes all the columns requested by the L</show> method onto the result array (this differs from the C<DBI>). Or to specify which columns to include in the result use the 'Columns' attribute in C<%attr> - see L<DBI-E<gt>selectcol_arrayref|DBI/"selectcol_arrayref">.
820  
821 =cut
822  
823 sub col_arrayref {
824 3 ▶ my($me, $attr) = @_;
Subroutine col_arrayref: called CC=9 SCAR=33.2
Pod documented
825 3 my($sql, @bind) = ($me->sql, $me->{DBO}{dbd_class}->_bind_params_select($me));
826 3 $me->{DBO}{dbd_class}->_sql($me, $sql, @bind);
827 3 ▶ my $sth = $me->rdbh->prepare($sql, $attr) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $me->rdbh->prepare($sql, $attr) or return 0 3 3
828 3 ▶ unless (defined $attr->{Columns}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attr->{"Columns"}) 1 2 3
829 # Some drivers don't provide $sth->{NUM_OF_FIELDS} until after execute is called
830 1 ▶ if ($sth->{NUM_OF_FIELDS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{'NUM_OF_FIELDS'}) { } 1 0 1
831 1 $attr->{Columns} = [1 .. $sth->{NUM_OF_FIELDS}];
832 } else {
833 0 ▶ $sth->execute(@bind) or return;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@bind) or return 0 0 0
834 0 my @col;
835 0 ▶ if (my $max = $attr->{MaxRows}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $max = $attr->{'MaxRows'}) { } 0 0 0
836 0 ▶ push @col, @$_ while 0 < $max-- and $_ = $sth->fetch;
Branch: 1 branch
BranchTrueFalseTotal
0 < $max-- and $_ = $sth->fetch and do { push @col, @$_ } 0 0 0
Condition0%0/3
0 < $max-- and $_ = $sth->fetch
!ll&&!rl&&r
000
MC/DC0%0/2
0 < $max-- and $_ = $sth->fetch
0 < $max-- $_ = $sth->fetch
Truth table0%0/3
A and B
A= 0 < $max--B= $_ = $sth->fetch
ABresult
0X0
100
111
837 } else {
838 0 ▶ push @col, @$_ while $_ = $sth->fetch;
Branch: 1 branch
BranchTrueFalseTotal
$_ = $sth->fetch and do { push @col, @$_ } 0 0 0
839 }
840 0 return \@col;
841 }
842 }
843 3 return $me->rdbh->selectcol_arrayref($sth, $attr, @bind);
844 }
845  
846 =head3 C<fetch>
847  
848 my $row = $query->fetch;
849  
850 Fetch the next row from the query. This will run/rerun the query if needed.
851  
852 Returns a L<Row|DBIx::DBO::Row> object or undefined if there are no more rows.
853  
854 =cut
855  
856 sub fetch {
857 30 ▶ my $me = $_[0];
Subroutine fetch: called CC=9 SCAR=22.8
Pod documented
858 # Prepare and/or execute the query if needed
859 30 ▶ $me->_sth and ($me->{Active} or $me->run)
Branch: 1 branch
BranchTrueFalseTotal
$me->_sth and $me->{"Active"} || $me->run or croak($me->rdbh->errstr) 0 30 30
Condition66%4/6
$me->_sth and $me->{"Active"} || $me->run
!ll&&!rl&&r
0426
$me->{"Active"} or $me->run
l!l&&r!l&&!r
20100
MC/DC0%0/3
$me->_sth and $me->{"Active"} || $me->run
$me->_sth $me->{"Active"} $me->run
Truth table25%1/4
A and B or C
A= $me->_sthB= $me->{"Active"}C= $me->run
ABCresult
0XX0
1000
11X1
1011
860 or croak $me->rdbh->errstr;
861 # Detach the old row if there is still another reference to it
862 30 ▶ if (defined $me->{Row} and SvREFCNT(${$me->{Row}}) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"Row"} and &SvREFCNT(\${$$me{"Row"};}) > 1) 0 30 30
Condition66%2/3
defined $me->{"Row"} and &SvREFCNT(\${$$me{"Row"};}) > 1
!ll&&!rl&&r
7230
MC/DC0%0/2
defined $me->{"Row"} and &SvREFCNT(\${$$me{"Row"};}) > 1
defined $me->{"Row"} &SvREFCNT(\${$$me{"Row"};}) > 1
Truth table66%2/3
A and B
A= defined $me->{"Row"}B= &SvREFCNT(\${$$me{"Row"};}) > 1
ABresult
0X0
100
111
863 0 $me->{Row}->_detach;
864 }
865  
866 30 my $row = $me->row;
867 30 ▶ if (exists $me->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $me->{'cache'}) { } 4 26 30
868 4 ▶ if ($me->{cache}{idx} < @{$me->{cache}{data}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"cache"}{"idx"} < @{$me->{"cache"}{"data"};}) 4 0 4
869 4 @{$me->{cache}{array}}[0..$#{$me->{cache}{array}}] = @{$me->{cache}{data}[$me->{cache}{idx}++]};
870 4 $$row->{array} = $me->{cache}{array};
871 4 $$row->{hash} = $me->{hash};
872 4 return $row;
873 }
874 0 undef $$row->{array};
875 0 $me->{cache}{idx} = 0;
876 } else {
877 # Fetch and store the data then return the Row on success and undef on failure or no more rows
878 26 ▶ if ($$row->{array} = $me->{sth}->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$row->{"array"} = $me->{"sth"}->fetch) 22 4 26
879 22 $$row->{hash} = $me->{hash};
880 22 return $row;
881 }
882 4 $me->{Active} = 0;
883 }
884 4 $$row->{hash} = {};
885 4 return;
886 }
887  
888 =head3 C<row>
889  
890 my $row = $query->row;
891  
892 Returns the L<Row|DBIx::DBO::Row> object for the current row from the query or an empty L<Row|DBIx::DBO::Row> object if there is no current row.
893  
894 =cut
895  
896 sub row {
897 37 ▶ my $me = $_[0];
Subroutine row: called CC=2 SCAR=7.0
Pod documented
898 37 $me->sql; # Build the SQL and detach the Row if needed
899 37 ▶ $me->{Row} ||= $me->_row_class->new($me->{DBO}, $me);
Condition66%2/3
$me->{"Row"} ||= $me->_row_class->new($me->{"DBO"}, $me)
l!l&&r!l&&!r
26110
MC/DC0%0/2
$me->{"Row"} ||= $me->_row_class->new($me->{"DBO"}, $me)
$me->{"Row"} $me->_row_class->new($me->{"DBO"}, $me)
Truth table66%2/3
A ||= B
A= $me->{"Row"}B= $me->_row_class->new($me->{"DBO"}, $me)
ABresult
1X1
011
000
900 }
901  
902 =head3 C<run>
903  
904 $query->run;
905  
906 Run/rerun the query.
907 This is called automatically before fetching the first row.
908  
909 =cut
910  
911 sub run {
912 16 ▶ my $me = shift;
Subroutine run: called CC=4 SCAR=13.9
Pod documented
913 16 $me->sql; # Build the SQL and detach the Row if needed
914 16 ▶ if (defined $me->{Row}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"Row"}) 12 4 16
915 12 undef ${$me->{Row}}->{array};
916 12 ${$me->{Row}}->{hash} = {};
917 }
918  
919 16 ▶ my $rv = $me->_execute or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rv = $me->_execute or return undef 0 16 16
920 16 $me->{Active} = 1;
921 16 $me->_bind_cols_to_hash;
922 16 ▶ if ($me->config('CacheQuery')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->config('CacheQuery')) { } 1 15 16
923 1 $me->{cache}{data} = $me->{sth}->fetchall_arrayref;
924 1 $me->{cache}{idx} = 0;
925 } else {
926 15 delete $me->{cache};
927 }
928 16 return $rv;
929 }
930  
931 sub _execute {
932 16 ▶ my $me = shift;
Subroutine _execute: called CC=2 SCAR=7.0
933 16 $me->{DBO}{dbd_class}->_sql($me, $me->sql, $me->{DBO}{dbd_class}->_bind_params_select($me));
934 16 ▶ $me->_sth or return;
Branch: 1 branch
BranchTrueFalseTotal
$me->_sth or return 0 16 16
935 16 $me->{sth}->execute($me->{DBO}{dbd_class}->_bind_params_select($me));
936 }
937  
938 sub _bind_cols_to_hash {
939 16 ▶ my $me = shift;
Subroutine _bind_cols_to_hash: called CC=7 SCAR=19.5
940 16 ▶ unless ($me->{hash}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($me->{"hash"}) 14 2 16
941 # Bind only to the first column of the same name
942 14 @{$me->{Columns}} = @{$me->{sth}{NAME}};
943 14 ▶ if ($me->config('CacheQuery')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->config('CacheQuery')) { } 1 13 14
944 1 @{$me->{cache}{array}} = (undef) x @{$me->{Columns}};
945 1 $me->{hash} = \my %hash;
946 1 my $i = 0;
947 1 for (@{$me->{Columns}}) {
948 3 ▶ _hv_store(%hash, $_, $me->{cache}{array}[$i]) unless exists $hash{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash{$_} 3 0 3
949 3 $i++;
950 }
951 } else {
952 13 my $i;
953 13 for (@{$me->{Columns}}) {
954 33 $i++;
955 33 ▶ $me->{sth}->bind_col($i, \$me->{hash}{$_}) unless exists $me->{hash}{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $me->{"hash"}{$_} 27 6 33
956 }
957 }
958 }
959 }
960  
961 =head3 C<rows>
962  
963 my $row_count = $query->rows;
964  
965 Count the number of rows returned.
966 Returns undefined if the number is unknown.
967 This uses the DBI C<rows> method which is unreliable in some situations (See L<DBI-E<gt>rows|DBI/"rows">).
968  
969 =cut
970  
971 sub rows {
972 1 ▶ my $me = shift;
Subroutine rows: called CC=2 SCAR=7.0
Pod documented
973 1 $me->sql; # Ensure the Row_Count is cleared if needed
974 1 ▶ $me->{DBO}{dbd_class}->_rows($me) unless defined $me->{Row_Count};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $me->{"Row_Count"} 1 0 1
975 1 $me->{Row_Count};
976 }
977  
978 =head3 C<count_rows>
979  
980 my $row_count = $query->count_rows;
981  
982 Count the number of rows that would be returned.
983 Returns undefined if there is an error.
984  
985 =cut
986  
987 sub count_rows {
988 2 ▶ my $me = shift;
Subroutine count_rows: called CC=2 SCAR=6.9
Pod documented
989 2 local $me->{Config}{CalcFoundRows} = 0;
990 2 my $old_sb = delete $me->{build_data}{Show_Bind};
991 2 $me->{build_data}{show} = '1';
992  
993 2 my $sql = 'SELECT COUNT(*) FROM ('.$me->{DBO}{dbd_class}->_build_sql_select($me).') t';
994 my($count) = $me->{DBO}{dbd_class}->_selectrow_array($me, $sql, undef,
995 2 $me->{DBO}{dbd_class}->_bind_params_select($me));
996  
997 2 ▶ $me->{build_data}{Show_Bind} = $old_sb if $old_sb;
Branch: 1 branch
BranchTrueFalseTotal
if $old_sb 2 0 2
998 2 undef $me->{build_data}{show};
999 2 return $count;
1000 }
1001  
1002 =head3 C<found_rows>
1003  
1004 $query->config(CalcFoundRows => 1); # Only applicable to MySQL
1005 my $total_rows = $query->found_rows;
1006  
1007 Return the number of rows that would have been returned if there was no limit clause. Before runnning the query the C<CalcFoundRows> config option can be enabled for improved performance on supported databases.
1008  
1009 Returns undefined if there is an error or is unable to determine the number of found rows.
1010  
1011 =cut
1012  
1013 sub found_rows {
1014 1 ▶ my $me = shift;
Subroutine found_rows: called CC=2 SCAR=7.1
Pod documented
1015 1 ▶ $me->{DBO}{dbd_class}->_calc_found_rows($me) unless defined $me->{Found_Rows};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $me->{"Found_Rows"} 1 0 1
1016 1 $me->{Found_Rows};
1017 }
1018  
1019 =head3 C<sql>
1020  
1021 my $sql = $query->sql;
1022  
1023 Returns the SQL statement string.
1024  
1025 =cut
1026  
1027 sub _search_where_chunk {
1028 map {
1029 166 ▶ ref $_->[0] eq 'ARRAY' ? _search_where_chunk(@$_) : ($_->[1], $_->[4])
Subroutine _search_where_chunk: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_->[0] eq 'ARRAY' ? : 18 139 157
1030 } @_
1031 }
1032  
1033 12 our @_RECURSIVE_SQ;
1034 sub sql {
1035 135 ▶ my $me = shift;
Subroutine sql: called CC=24 SCAR=46.5
Pod documented
1036 # Check for changes to subqueries and recursion
1037 135 ▶ croak 'Recursive subquery found' if grep $me eq $_, @_RECURSIVE_SQ;
Branch: 1 branch
BranchTrueFalseTotal
if grep(($me eq $_), @DBIx::DBO::Query::_RECURSIVE_SQ) 0 135 135
1038 135 local @_RECURSIVE_SQ = (@_RECURSIVE_SQ, $me);
1039 135 for my $fld (@{$me->{build_data}{Showing}}) {
1040 149 ▶ if (ref $fld eq 'ARRAY' and @{$fld->[0]} == 1 and _isa($fld->[0][0], 'DBIx::DBO::Query')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fld eq "ARRAY" and @{$fld->[0];} == 1 and _isa($fld->[0][0], "DBIx::DBO::Query")) 0 149 149
Condition83%5/6
ref $fld eq "ARRAY" and @{$fld->[0];} == 1 and _isa($fld->[0][0], "DBIx::DBO::Query")
!ll&&!rl&&r
54950
ref $fld eq "ARRAY" and @{$fld->[0];} == 1
!ll&&!rl&&r
312395
MC/DC0%0/3
ref $fld eq "ARRAY" and @{$fld->[0];} == 1 and _isa($fld->[0][0], "DBIx::DBO::Query")
ref $fld eq "ARRAY" @{$fld->[0];} == 1 _isa($fld->[0][0], "DBIx::DBO::Query")
Truth table75%3/4
A and B and C
A= ref $fld eq "ARRAY"B= @{$fld->[0];} == 1C= _isa($fld->[0][0], "DBIx::DBO::Query")
ABCresult
0XX0
10X0
1100
1111
1041 0 my $sq = $fld->[0][0];
1042 0 ▶ if ($sq->sql ne ($me->{build_data}{_subqueries}{$sq} ||= '')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sq->sql ne ($me->{"build_data"}{"_subqueries"}{$sq} ||= "")) 0 0 0
Condition0%0/2
$me->{"build_data"}{"_subqueries"}{$sq} ||= ""
l!l
00
MC/DC0%0/1
$me->{"build_data"}{"_subqueries"}{$sq} ||= ""
$me->{"build_data"}{"_subqueries"}{$sq}
Truth table0%0/2
A
A= $me->{"build_data"}{"_subqueries"}{$sq}
Aresult
11
00
1043 0 undef $me->{sql};
1044 0 undef $me->{build_data}{show};
1045 }
1046 }
1047 }
1048 135 for my $sq (@{$me->{Tables}}) {
1049 149 ▶ if (_isa($sq, 'DBIx::DBO::Query')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_isa($sq, "DBIx::DBO::Query")) 0 149 149
1050 0 ▶ if ($sq->sql ne ($me->{build_data}{_subqueries}{$sq} ||= '')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sq->sql ne ($me->{"build_data"}{"_subqueries"}{$sq} ||= "")) 0 0 0
Condition0%0/2
$me->{"build_data"}{"_subqueries"}{$sq} ||= ""
l!l
00
MC/DC0%0/1
$me->{"build_data"}{"_subqueries"}{$sq} ||= ""
$me->{"build_data"}{"_subqueries"}{$sq}
Truth table0%0/2
A
A= $me->{"build_data"}{"_subqueries"}{$sq}
Aresult
11
00
1051 0 undef $me->{sql};
1052 0 undef $me->{build_data}{from};
1053 }
1054 }
1055 }
1056 135 ▶ for my $w (map { $_ ? _search_where_chunk(@$_) : () } @{$me->{build_data}{Join_On}}) {
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 13 136 149
1057 50 ▶ if (@$w == 1 and _isa($w->[0], 'DBIx::DBO::Query')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$w == 1 and _isa($w->[0], "DBIx::DBO::Query")) 0 50 50
Condition66%2/3
@$w == 1 and _isa($w->[0], "DBIx::DBO::Query")
!ll&&!rl&&r
24260
MC/DC0%0/2
@$w == 1 and _isa($w->[0], "DBIx::DBO::Query")
@$w == 1 _isa($w->[0], "DBIx::DBO::Query")
Truth table66%2/3
A and B
A= @$w == 1B= _isa($w->[0], "DBIx::DBO::Query")
ABresult
0X0
100
111
1058 0 my $sq = $w->[0];
1059 0 ▶ if ($sq->sql ne ($me->{build_data}{_subqueries}{$sq} ||= '')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sq->sql ne ($me->{"build_data"}{"_subqueries"}{$sq} ||= "")) 0 0 0
Condition0%0/2
$me->{"build_data"}{"_subqueries"}{$sq} ||= ""
l!l
00
MC/DC0%0/1
$me->{"build_data"}{"_subqueries"}{$sq} ||= ""
$me->{"build_data"}{"_subqueries"}{$sq}
Truth table0%0/2
A
A= $me->{"build_data"}{"_subqueries"}{$sq}
Aresult
11
00
1060 0 undef $me->{sql};
1061 0 undef $me->{build_data}{from};
1062 }
1063 }
1064 }
1065 135 for my $w (_search_where_chunk(@{$me->{build_data}{Where_Data}})) {
1066 228 ▶ if (@$w == 1 and _isa($w->[0], 'DBIx::DBO::Query')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$w == 1 and _isa($w->[0], "DBIx::DBO::Query")) 0 228 228
Condition66%2/3
@$w == 1 and _isa($w->[0], "DBIx::DBO::Query")
!ll&&!rl&&r
731550
MC/DC0%0/2
@$w == 1 and _isa($w->[0], "DBIx::DBO::Query")
@$w == 1 _isa($w->[0], "DBIx::DBO::Query")
Truth table66%2/3
A and B
A= @$w == 1B= _isa($w->[0], "DBIx::DBO::Query")
ABresult
0X0
100
111
1067 0 my $sq = $w->[0];
1068 0 ▶ if ($sq->sql ne ($me->{build_data}{_subqueries}{$sq} ||= '')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sq->sql ne ($me->{"build_data"}{"_subqueries"}{$sq} ||= "")) 0 0 0
Condition0%0/2
$me->{"build_data"}{"_subqueries"}{$sq} ||= ""
l!l
00
MC/DC0%0/1
$me->{"build_data"}{"_subqueries"}{$sq} ||= ""
$me->{"build_data"}{"_subqueries"}{$sq}
Truth table0%0/2
A
A= $me->{"build_data"}{"_subqueries"}{$sq}
Aresult
11
00
1069 0 undef $me->{sql};
1070 0 undef $me->{build_data}{where};
1071 }
1072 }
1073 }
1074 135 ▶ $me->{sql} || $me->_build_sql;
Branch: 1 branch
BranchTrueFalseTotal
$me->{"sql"} or $me->_build_sql 31 104 135
1075 }
1076  
1077 sub _build_sql {
1078 31 ▶ my $me = shift;
Subroutine _build_sql: called CC=3 SCAR=11.0
1079 31 undef $me->{sth};
1080 31 undef $me->{hash};
1081 31 undef $me->{Row_Count};
1082 31 undef $me->{Found_Rows};
1083 31 delete $me->{cache};
1084 31 $me->{Active} = 0;
1085 31 ▶ if (defined $me->{Row}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"Row"}) 19 12 31
1086 19 ▶ if (SvREFCNT(${$me->{Row}}) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (&SvREFCNT(\${$$me{'Row'};}) > 1) { } 0 19 19
1087 0 $me->{Row}->_detach;
1088 } else {
1089 19 undef ${$me->{Row}}->{array};
1090 19 undef %{$me->{Row}};
1091  
1092 19 $me->{sql} = $me->{DBO}{dbd_class}->_build_sql_select($me, $me->{build_data});
1093 19 $me->{Row}{from} = $me->{DBO}{dbd_class}->_build_from($me, $me->{build_data});
1094 19 $me->{Row}->_copy_build_data;
1095 19 return $me->{sql};
1096 }
1097 }
1098 12 undef @{$me->{Columns}};
1099  
1100 12 $me->{sql} = $me->{DBO}{dbd_class}->_build_sql_select($me);
1101 }
1102  
1103 # Get the DBI statement handle for the query.
1104 # It may not have been executed yet.
1105 sub _sth {
1106 50 ▶ my $me = shift;
Subroutine _sth: called CC=2 SCAR=7.0
1107 # Ensure the sql is rebuilt if needed
1108 50 my $sql = $me->sql;
1109 50 ▶ $me->{sth} ||= $me->rdbh->prepare($sql);
Condition66%2/3
$me->{"sth"} ||= $me->rdbh->prepare($sql)
l!l&&r!l&&!r
33170
MC/DC0%0/2
$me->{"sth"} ||= $me->rdbh->prepare($sql)
$me->{"sth"} $me->rdbh->prepare($sql)
Truth table66%2/3
A ||= B
A= $me->{"sth"}B= $me->rdbh->prepare($sql)
ABresult
1X1
011
000
1110 }
1111  
1112 =head3 C<update>
1113  
1114 $query->update(department => 'Tech');
1115 $query->update(salary => { FUNC => '? * 1.10', COL => 'salary' }); # 10% raise
1116  
1117 Updates every row in the query with the new values specified.
1118 Returns the number of rows updated or C<'0E0'> for no rows to ensure the value is true,
1119 and returns false if there was an error.
1120  
1121 =cut
1122  
1123 sub update {
1124 2 ▶ my $me = shift;
Subroutine update: called CC=1 SCAR=0.0
Pod documented
1125 2 my @update = $me->{DBO}{dbd_class}->_parse_set($me, @_);
1126 2 my $sql = $me->{DBO}{dbd_class}->_build_sql_update($me, @update);
1127 2 $me->{DBO}{dbd_class}->_do($me, $sql, undef, $me->{DBO}{dbd_class}->_bind_params_update($me));
1128 }
1129  
1130 =head3 C<finish>
1131  
1132 $query->finish;
1133  
1134 Calls L<DBI-E<gt>finish|DBI/"finish"> on the statement handle, if it's active.
1135 Restarts cached queries from the first row (if created using the C<CacheQuery> config).
1136 This ensures that the next call to L</fetch> will return the first row from the query.
1137  
1138 =cut
1139  
1140 sub finish {
1141 18 ▶ my $me = shift;
Subroutine finish: called CC=6 SCAR=18.2
Pod documented
1142 18 ▶ if (defined $me->{Row}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"Row"}) 6 12 18
1143 6 ▶ if (SvREFCNT(${$me->{Row}}) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (&SvREFCNT(\${$$me{'Row'};}) > 1) { } 0 6 6
1144 0 $me->{Row}->_detach;
1145 } else {
1146 6 undef ${$me->{Row}}{array};
1147 6 ${$me->{Row}}{hash} = {};
1148 }
1149 }
1150 18 ▶ if (exists $me->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $me->{'cache'}) { } 0 18 18
1151 0 $me->{cache}{idx} = 0;
1152 } else {
1153 18 ▶ $me->{sth}->finish if $me->{sth} and $me->{sth}{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"sth"} and $me->{"sth"}{"Active"} 3 15 18
Condition100%3/3
$me->{"sth"} and $me->{"sth"}{"Active"}
!ll&&!rl&&r
1323
MC/DC100%2/2
$me->{"sth"} and $me->{"sth"}{"Active"}
$me->{"sth"} $me->{"sth"}{"Active"}
Truth table100%3/3
A and B
A= $me->{"sth"}B= $me->{"sth"}{"Active"}
ABresult
0X0
100
111
1154 18 $me->{Active} = 0;
1155 }
1156 }
1157  
1158 =head2 Common Methods
1159  
1160 These methods are accessible from all DBIx::DBO* objects.
1161  
1162 =head3 C<dbo>
1163  
1164 The C<DBO> object.
1165  
1166 =head3 C<dbh>
1167  
1168 The I<read-write> C<DBI> handle.
1169  
1170 =head3 C<rdbh>
1171  
1172 The I<read-only> C<DBI> handle, or if there is no I<read-only> connection, the I<read-write> C<DBI> handle.
1173  
1174 =cut
1175  
1176 5 ▶sub dbo { $_[0]{DBO} }
Subroutine dbo: called CC=1 SCAR=0.0
Pod documented
1177 2 ▶sub dbh { $_[0]{DBO}->dbh }
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
1178 140 ▶sub rdbh { $_[0]{DBO}->rdbh }
Subroutine rdbh: called CC=1 SCAR=0.0
Pod documented
1179  
1180 =head3 C<config>
1181  
1182 $query_setting = $query->config($option);
1183 $query->config($option => $query_setting);
1184  
1185 Get or set this C<Query> object's config settings. When setting an option, the previous value is returned. When getting an option's value, if the value is undefined, the L<DBIx::DBO|DBIx::DBO>'s value is returned.
1186  
1187 See: L<DBIx::DBO/Available_config_options>.
1188  
1189 =cut
1190  
1191 sub config {
1192 224 ▶ my $me = shift;
Subroutine config: called CC=4 SCAR=13.9
Pod documented
1193 224 my $opt = shift;
1194 224 ▶ return $me->{DBO}{dbd_class}->_set_config($me->{Config} ||= {}, $opt, shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 29 195 224
Condition50%1/2
$me->{"Config"} ||= {}
l!l
290
MC/DC0%0/1
$me->{"Config"} ||= {}
$me->{"Config"}
Truth table50%1/2
A
A= $me->{"Config"}
Aresult
11
00
1195 195 ▶ $me->{DBO}{dbd_class}->_get_config($opt, $me->{Config} ||= {}, $me->{DBO}{Config}, \%DBIx::DBO::Config);
Condition100%2/2
$me->{"Config"} ||= {}
l!l
1869
MC/DC100%1/1
$me->{"Config"} ||= {}
$me->{"Config"}
Truth table100%2/2
A
A= $me->{"Config"}
Aresult
11
00
1196 }
1197  
1198 sub STORABLE_freeze {
1199 5 ▶ my($me, $cloning) = @_;
Subroutine STORABLE_freeze: called CC=5 SCAR=16.4
Pod undocumented
1200 5 ▶ return unless defined $me->{sth};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $me->{"sth"} 3 2 5
1201  
1202 2 local $me->{sth};
1203 2 local $me->{Row};
1204 2 ▶ local $me->{hash} unless exists $me->{cache};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $me->{"cache"} 0 2 2
1205 2 ▶ local $me->{Active} = 0 unless exists $me->{cache};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $me->{"cache"} 0 2 2
1206 2 ▶ local $me->{cache}{idx} = 0 if exists $me->{cache};
Branch: 1 branch
BranchTrueFalseTotal
if exists $me->{"cache"} 2 0 2
1207 2 return Storable::nfreeze($me);
1208 }
1209  
1210 sub STORABLE_thaw {
1211 2 ▶ my($me, $cloning, @frozen) = @_;
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
1212 2 %$me = %{ Storable::thaw(@frozen) };
1213 }
1214  
1215 sub DESTROY {
1216 15 ▶ undef %{$_[0]};
Subroutine DESTROY: called CC=1 SCAR=0.0
1217 }
1218  
1219 12 1;
1220  
1221 __END__
« Test/DBO.pm ↑ Summary blib/lib/DBIx/DBO/DBD.pm »