blib/lib/DBIx/DBO/Query.pm

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