DBIx/Class/SQLMaker.pm

filter: Statementstmt 90.7% 196 / 216 Branchbran 78.4% 91 / 116 Conditioncond 60.6% 40 / 66 MC/DCmcdc 38.4% 15 / 39 Subroutinesub 93.5% 29 / 31 Podpod 66.6% 2 / 3 totaltotal 79.1% 373 / 471 CC 86 SCAR 26.3
CC
86
Cov
82%
_where_op_multicolumn_in
59.3
select
18.5
_recurse_fields
12.5
CRAP 13.8
« DBIx/Class/ResultSource/RowParser/Util.pm ↑ Summary DBIx/Class/CDBICompat/DestroyWarning.pm »
1 package DBIx::Class::SQLMaker;
2  
3 200 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 200 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 DBIx::Class::SQLMaker - An SQL::Abstract-based SQL maker class
9  
10 =head1 DESCRIPTION
11  
12 This module is a subclass of L<SQL::Abstract> and includes a number of
13 DBIC-specific workarounds, not yet suitable for inclusion into the
14 L<SQL::Abstract> core. It also provides all (and more than) the functionality
15 of L<SQL::Abstract::Limit>, see L<DBIx::Class::SQLMaker::LimitDialects> for
16 more info.
17  
18 Currently the enhancements to L<SQL::Abstract> are:
19  
20 =over
21  
22 =item * Support for C<JOIN> statements (via extended C<table/from> support)
23  
24 =item * Support of functions in C<SELECT> lists
25  
26 =item * C<GROUP BY>/C<HAVING> support (via extensions to the order_by parameter)
27  
28 =item * Support of C<...FOR UPDATE> type of select statement modifiers
29  
30 =back
31  
32 =cut
33  
34 200 use base qw/
35 DBIx::Class::SQLMaker::LimitDialects
36 SQL::Abstract
37 DBIx::Class
38 200 ▶/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 200 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 200 ▶use Sub::Name 'subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 200 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 200 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 200 __PACKAGE__->mk_group_accessors (simple => qw/quote_char name_sep limit_dialect/);
46  
47 # for when I need a normalized l/r pair
48 sub _quote_chars {
49 map
50 5866 ▶ { defined $_ ? $_ : '' }
Branch: 2 branches
BranchTrueFalseTotal
defined $_ ? : 4296 1570 5866
ref $_[0]{'quote_char'} ? : 1911 1022 2933
51 2933 ▶ ( ref $_[0]->{quote_char} ? (@{$_[0]->{quote_char}}) : ( ($_[0]->{quote_char}) x 2 ) )
Subroutine _quote_chars: called CC=3 SCAR=11.0
52 ;
53 }
54  
55 # FIXME when we bring in the storage weaklink, check its schema
56 # weaklink and channel through $schema->throw_exception
57 2 ▶sub throw_exception { DBIx::Class::Exception->throw($_[1]) }
Subroutine throw_exception: called CC=1 SCAR=0.0
Pod undocumented
58  
59 BEGIN {
60 # reinstall the belch()/puke() functions of SQL::Abstract with custom versions
61 # that use DBIx::Class::Carp/DBIx::Class::Exception instead of plain Carp
62 200 ▶ no warnings qw/redefine/;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 *SQL::Abstract::belch = subname 'SQL::Abstract::belch' => sub (@) {
65 0 ▶ my($func) = (caller(1))[3];
Subroutine belch: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
66 0 carp "[$func] Warning: ", @_;
67 200 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 *SQL::Abstract::puke = subname 'SQL::Abstract::puke' => sub (@) {
70 1 ▶ my($func) = (caller(1))[3];
Subroutine __ANON__: called CC=1 SCAR=0.0
71 1 __PACKAGE__->throw_exception("[$func] Fatal: " . join ('', @_));
72 200 };
73 }
74  
75 # the "oh noes offset/top without limit" constant
76 # limited to 31 bits for sanity (and consistency,
77 # since it may be handed to the like of sprintf %u)
78 #
79 # Also *some* builds of SQLite fail the test
80 # some_column BETWEEN ? AND ?: 1, 4294967295
81 # with the proper integer bind attrs
82 #
83 # Implemented as a method, since ::Storage::DBI also
84 # refers to it (i.e. for the case of software_limit or
85 # as the value to abuse with MSSQL ordered subqueries)
86 sub __max_int () { 0x7FFFFFFF };
87  
88 # we ne longer need to check this - DBIC has ways of dealing with it
89 # specifically ::Storage::DBI::_resolve_bindattrs()
90 sub _assert_bindval_matches_bindtype () { 1 };
91  
92 # poor man's de-qualifier
93 sub _quote {
94 125323 ▶ $_[0]->next::method( ( $_[0]{_dequalify_idents} and ! ref $_[1] )
Subroutine _quote: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'_dequalify_idents'} && !ref($_[1]) ? : 1202 124121 125323
Condition66%2/3
$_[0]{"_dequalify_idents"} and not ref $_[1]
!ll&&!rl&&r
12412101202
MC/DC50%1/2
$_[0]{"_dequalify_idents"} and not ref $_[1]
$_[0]{"_dequalify_idents"} not ref $_[1]
Truth table66%2/3
A and not(B)
A= $_[0]{"_dequalify_idents"}B= not ref $_[1]
ABresult
0X0
100
111
95 ? $_[1] =~ / ([^\.]+) $ /x
96 : $_[1]
97 );
98 }
99  
100 sub _where_op_NEST {
101 0 ▶ carp_unique ("-nest in search conditions is deprecated, you most probably wanted:\n"
Subroutine _where_op_NEST: not called CC=1 SCAR=6.9
102 .q|{..., -and => [ \%cond0, \@cond1, \'cond2', \[ 'cond3', [ col => bind ] ], etc. ], ... }|
103 );
104  
105 0 shift->next::method(@_);
106 }
107  
108 # Handle limit-dialect selection
109 sub select {
110 7421 ▶ my ($self, $table, $fields, $where, $rs_attrs, $limit, $offset) = @_;
Subroutine select: called CC=16 SCAR=29.2
Pod documented
111  
112  
113 7421 $fields = $self->_recurse_fields($fields);
114  
115 7420 ▶ if (defined $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset) 128 7292 7420
116 128 ▶ $self->throw_exception('A supplied offset must be a non-negative integer')
Branch: 1 branch
BranchTrueFalseTotal
if $offset =~ /\D/ or $offset < 0 0 128 128
Condition33%1/3
$offset =~ /\D/ or $offset < 0
l!l&&r!l&&!r
00128
MC/DC0%0/2
$offset =~ /\D/ or $offset < 0
$offset =~ /\D/ $offset < 0
Truth table33%1/3
A or B
A= $offset =~ /\D/B= $offset < 0
ABresult
1X1
011
000
117 if ( $offset =~ /\D/ or $offset < 0 );
118 }
119 7420 ▶ $offset ||= 0;
Condition100%2/2
$offset ||= 0
l!l
1287292
MC/DC100%1/1
$offset ||= 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
120  
121 7420 ▶ if (defined $limit) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $limit) { } 1594 5826 7420
elsif ($offset) { } 0 5826 5826
122 1594 ▶ $self->throw_exception('A supplied limit must be a positive integer')
Branch: 1 branch
BranchTrueFalseTotal
if $limit =~ /\D/ or $limit <= 0 0 1594 1594
Condition33%1/3
$limit =~ /\D/ or $limit <= 0
l!l&&r!l&&!r
001594
MC/DC0%0/2
$limit =~ /\D/ or $limit <= 0
$limit =~ /\D/ $limit <= 0
Truth table33%1/3
A or B
A= $limit =~ /\D/B= $limit <= 0
ABresult
1X1
011
000
123 if ( $limit =~ /\D/ or $limit <= 0 );
124 }
125 elsif ($offset) {
126 0 $limit = $self->__max_int;
127 }
128  
129  
130 7420 my ($sql, @bind);
131 7420 ▶ if ($limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) { } 1594 5826 7420
132 # this is legacy code-flow from SQLA::Limit, it is not set in stone
133  
134 1594 ($sql, @bind) = $self->next::method ($table, $fields, $where);
135  
136 1594 my $limiter;
137  
138 1594 ▶ if( $limiter = $self->can ('emulate_limit') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limiter = $self->can('emulate_limit')) { } 1 1593 1594
139 1 carp_unique(
140 'Support for the legacy emulate_limit() mechanism inherited from '
141 . 'SQL::Abstract::Limit has been deprecated, and will be removed when '
142 . 'DBIC transitions to Data::Query. If your code uses this type of '
143 . 'limit specification please file an RT and provide the source of '
144 . 'your emulate_limit() implementation, so an acceptable upgrade-path '
145 . 'can be devised'
146 );
147 }
148 else {
149 1593 ▶ my $dialect = $self->limit_dialect
Branch: 1 branch
BranchTrueFalseTotal
my $dialect = $self->limit_dialect or $self->throw_exception("Unable to generate SQL-limit - no limit dialect specified on $self") 0 1593 1593
150 or $self->throw_exception( "Unable to generate SQL-limit - no limit dialect specified on $self" );
151  
152 1593 ▶ $limiter = $self->can ("_$dialect")
Branch: 1 branch
BranchTrueFalseTotal
$limiter = $self->can("_$dialect") or $self->throw_exception('DBIx::Class::SQLMaker' . " does not implement the requested dialect '${dialect}'") 0 1593 1593
153 or $self->throw_exception(__PACKAGE__ . " does not implement the requested dialect '$dialect'");
154 }
155  
156 $sql = $self->$limiter (
157 $sql,
158 1594 ▶ { %{$rs_attrs||{}}, _selector_sql => $fields },
Branch: 1 branch
BranchTrueFalseTotal
$rs_attrs or {} 0 1594 1594
159 $limit,
160 $offset
161 );
162 }
163 else {
164 5826 ($sql, @bind) = $self->next::method ($table, $fields, $where, $rs_attrs);
165 }
166  
167 7419 push @{$self->{where_bind}}, @bind;
168  
169 # this *must* be called, otherwise extra binds will remain in the sql-maker
170 7419 my @all_bind = $self->_assemble_binds;
171  
172 $sql .= $self->_lock_select ($rs_attrs->{for})
173 7419 ▶ if $rs_attrs->{for};
Branch: 1 branch
BranchTrueFalseTotal
if $rs_attrs->{"for"} 3 7416 7419
174  
175 7419 ▶ return wantarray ? ($sql, @all_bind) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7419 0 7419
176 }
177  
178 sub _assemble_binds {
179 7419 ▶ my $self = shift;
Subroutine _assemble_binds: called CC=2 SCAR=6.9
180 7419 ▶ return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/pre_select select from where group having order limit/);
Branch: 1 branch
BranchTrueFalseTotal
delete $$self{"${_}_bind"} or [] 45256 14096 59352
181 }
182  
183 200 my $for_syntax = {
184 update => 'FOR UPDATE',
185 shared => 'FOR SHARE',
186 };
187 sub _lock_select {
188 2 ▶ my ($self, $type) = @_;
Subroutine _lock_select: called CC=3 SCAR=11.2
189  
190 2 my $sql;
191 2 ▶ if (ref($type) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq 'SCALAR') { } 1 1 2
192 1 $sql = "FOR $$type";
193 }
194 else {
195 1 ▶ $sql = $for_syntax->{$type} || $self->throw_exception( "Unknown SELECT .. FOR type '$type' requested" );
Condition33%1/3
$for_syntax->{$type} || $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
l!l&&r!l&&!r
100
MC/DC0%0/2
$for_syntax->{$type} || $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
$for_syntax->{$type} $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
Truth table33%1/3
A || B
A= $for_syntax->{$type}B= $self->throw_exception("Unknown SELECT .. FOR type '${type}' requested")
ABresult
1X1
011
000
196 }
197  
198 2 return " $sql";
199 }
200  
201 # Handle default inserts
202 sub insert {
203 # optimized due to hotttnesss
204 # my ($self, $table, $data, $options) = @_;
205  
206 # SQLA will emit INSERT INTO $table ( ) VALUES ( )
207 # which is sadly understood only by MySQL. Change default behavior here,
208 # until SQLA2 comes with proper dialect support
209 8927 ▶ if (! $_[2] or (ref $_[2] eq 'HASH' and !keys %{$_[2]} ) ) {
Subroutine insert: called CC=6 SCAR=19.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[2] or ref $_[2] eq "HASH" and not keys %{$_[2];}) 2 8925 8927
Condition66%4/6
not $_[2] or ref $_[2] eq "HASH" and not keys %{$_[2];}
l!l&&r!l&&!r
028925
ref $_[2] eq "HASH" and not keys %{$_[2];}
!ll&&!rl&&r
089252
MC/DC33%1/3
not $_[2] or ref $_[2] eq "HASH" and not keys %{$_[2];}
not $_[2] ref $_[2] eq "HASH" not keys %{$_[2];}
Truth table50%2/4
not(A) or B and not(C)
A= not $_[2]B= ref $_[2] eq "HASH"C= not keys %{$_[2];}
ABCresult
1XX1
0111
00X0
0100
210 2 my @bind;
211 2 my $sql = sprintf(
212 'INSERT INTO %s DEFAULT VALUES', $_[0]->_quote($_[1])
213 );
214  
215 2 ▶ if ( ($_[3]||{})->{returning} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (+($_[3] || {})->{"returning"}) 0 2 2
Condition50%1/2
$_[3] || {}
l!l
02
MC/DC0%0/1
$_[3] || {}
$_[3]
Truth table50%1/2
A
A= $_[3]
Aresult
11
00
216 0 my $s;
217 0 ($s, @bind) = $_[0]->_insert_returning ($_[3]);
218 0 $sql .= $s;
219 }
220  
221 2 return ($sql, @bind);
222 }
223  
224 8925 next::method(@_);
225 }
226  
227 sub _recurse_fields {
228 49979 ▶ my ($self, $fields) = @_;
Subroutine _recurse_fields: called CC=12 SCAR=25.3
229 49979 my $ref = ref $fields;
230 49979 ▶ return $self->_quote($fields) unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 40836 9143 49979
231 9143 ▶ return $$fields if $ref eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "SCALAR" 84 9059 9143
232  
233 9059 ▶ if ($ref eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 7840 1219 9059
elsif ($ref eq 'HASH') { } 822 397 1219
elsif ($ref eq 'REF' and ref $$fields eq 'ARRAY') { } 397 0 397
234 7840 return join(', ', map { $self->_recurse_fields($_) } @$fields);
235 }
236 elsif ($ref eq 'HASH') {
237 822 my %hash = %$fields; # shallow copy
238  
239 822 my $as = delete $hash{-as}; # if supplied
240  
241 822 my ($func, $args, @toomany) = %hash;
242  
243 # there should be only one pair
244 822 ▶ if (@toomany) {
Branch: 1 branch
BranchTrueFalseTotal
if (@toomany) 0 822 822
245 0 $self->throw_exception( "Malformed select argument - too many keys in hash: " . join (',', keys %$fields ) );
246 }
247  
248 822 ▶ if (lc ($func) eq 'distinct' && ref $args eq 'ARRAY' && @$args > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $func eq "distinct" and ref $args eq "ARRAY" and @$args > 1) 1 821 822
Condition83%5/6
lc $func eq "distinct" and ref $args eq "ARRAY" and @$args > 1
!ll&&!rl&&r
82101
lc $func eq "distinct" and ref $args eq "ARRAY"
!ll&&!rl&&r
81921
MC/DC66%2/3
lc $func eq "distinct" and ref $args eq "ARRAY" and @$args > 1
lc $func eq "distinct" ref $args eq "ARRAY" @$args > 1
Truth table75%3/4
A and B and C
A= lc $func eq "distinct"B= ref $args eq "ARRAY"C= @$args > 1
ABCresult
0XX0
10X0
1100
1111
249 1 $self->throw_exception (
250 'The select => { distinct => ... } syntax is not supported for multiple columns.'
251 .' Instead please use { group_by => [ qw/' . (join ' ', @$args) . '/ ] }'
252 .' or { select => [ qw/' . (join ' ', @$args) . '/ ], distinct => 1 }'
253 );
254 }
255  
256 821 ▶ my $select = sprintf ('%s( %s )%s',
Branch: 1 branch
BranchTrueFalseTotal
$as ? : 69 752 821
257 $self->_sqlcase($func),
258 $self->_recurse_fields($args),
259 $as
260 ? sprintf (' %s %s', $self->_sqlcase('as'), $self->_quote ($as) )
261 : ''
262 );
263  
264 821 return $select;
265 }
266 # Is the second check absolutely necessary?
267 ▶ elsif ( $ref eq 'REF' and ref($$fields) eq 'ARRAY' ) {
Condition33%1/3
$ref eq "REF" and ref $$fields eq "ARRAY"
!ll&&!rl&&r
00397
MC/DC0%0/2
$ref eq "REF" and ref $$fields eq "ARRAY"
$ref eq "REF" ref $$fields eq "ARRAY"
Truth table33%1/3
A and B
A= $ref eq "REF"B= ref $$fields eq "ARRAY"
ABresult
0X0
100
111
268 397 push @{$self->{select_bind}}, @{$$fields}[1..$#$$fields];
269 397 return $$fields->[0];
270 }
271 else {
272 0 $self->throw_exception( $ref . qq{ unexpected in _recurse_fields()} );
273 }
274 }
275  
276  
277 # this used to be a part of _order_by but is broken out for clarity.
278 # What we have been doing forever is hijacking the $order arg of
279 # SQLA::select to pass in arbitrary pieces of data (first the group_by,
280 # then pretty much the entire resultset attr-hash, as more and more
281 # things in the SQLA space need to have more info about the $rs they
282 # create SQL for. The alternative would be to keep expanding the
283 # signature of _select with more and more positional parameters, which
284 # is just gross. All hail SQLA2!
285 sub _parse_rs_attrs {
286 9494 ▶ my ($self, $arg) = @_;
Subroutine _parse_rs_attrs: called CC=7 SCAR=19.5
287  
288 9494 my $sql = '';
289  
290 9494 ▶ if ($arg->{group_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"group_by"}) 415 9079 9494
291 # horrible horrible, waiting for refactor
292 415 local $self->{select_bind};
293 415 ▶ if (my $g = $self->_recurse_fields($arg->{group_by}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = $self->_recurse_fields($arg->{"group_by"})) 415 0 415
294 415 $sql .= $self->_sqlcase(' group by ') . $g;
295 415 ▶ push @{$self->{group_bind} ||= []}, @{$self->{select_bind}||[]};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"select_bind"} or [] 305 110 415
Condition50%1/2
$self->{"group_bind"} ||= []
l!l
0415
MC/DC0%0/1
$self->{"group_bind"} ||= []
$self->{"group_bind"}
Truth table50%1/2
A
A= $self->{"group_bind"}
Aresult
11
00
296 }
297 }
298  
299 9494 ▶ if (defined $arg->{having}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg->{"having"}) 99 9395 9494
300 99 my ($frag, @bind) = $self->_recurse_where($arg->{having});
301 99 push(@{$self->{having_bind}}, @bind);
302 99 $sql .= $self->_sqlcase(' having ') . $frag;
303 }
304  
305 9494 ▶ if (defined $arg->{order_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg->{"order_by"}) 3732 5762 9494
306 3732 $sql .= $self->_order_by ($arg->{order_by});
307 }
308  
309 9493 return $sql;
310 }
311  
312 sub _order_by {
313 9712 ▶ my ($self, $arg) = @_;
Subroutine _order_by: called CC=3 SCAR=11.0
314  
315 # check that we are not called in legacy mode (order_by as 4th argument)
316 9712 ▶ if (ref $arg eq 'HASH' and not grep { $_ =~ /^-(?:desc|asc)/i } keys %$arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'HASH' and not grep({$_ =~ /^-(?:desc|asc)/i;} keys %$arg)) { } 5820 3892 9712
Condition100%3/3
ref $arg eq "HASH" and not grep({$_ =~ /^-(?:desc|asc)/i;} keys %$arg)
!ll&&!rl&&r
389115820
MC/DC100%2/2
ref $arg eq "HASH" and not grep({$_ =~ /^-(?:desc|asc)/i;} keys %$arg)
ref $arg eq "HASH" not grep({$_ =~ /^-(?:desc|asc)/i;} keys %$arg)
Truth table100%3/3
A and not(B)
A= ref $arg eq "HASH"B= not grep({$_ =~ /^-(?:desc|asc)/i;} keys %$arg)
ABresult
0X0
100
111
317 5820 return $self->_parse_rs_attrs ($arg);
318 }
319 else {
320 3892 my ($sql, @bind) = $self->next::method($arg);
321 3891 push @{$self->{order_bind}}, @bind;
322 3891 return $sql;
323 }
324 }
325  
326 sub _split_order_chunk {
327 1432 ▶ my ($self, $chunk) = @_;
Subroutine _split_order_chunk: called CC=3 SCAR=11.0
328  
329 # strip off sort modifiers, but always succeed, so $1 gets reset
330 1432 $chunk =~ s/ (?: \s+ (ASC|DESC) )? \s* $//ix;
331  
332 return (
333 1432 ▶ $chunk,
Branch: 1 branch
BranchTrueFalseTotal
$1 && uc $1 eq 'DESC' ? : 596 836 1432
Condition100%3/3
$1 and uc $1 eq "DESC"
!ll&&!rl&&r
623213596
MC/DC100%2/2
$1 and uc $1 eq "DESC"
$1 uc $1 eq "DESC"
Truth table100%3/3
A and B
A= $1B= uc $1 eq "DESC"
ABresult
0X0
100
111
334 ( $1 and uc($1) eq 'DESC' ) ? 1 : 0,
335 );
336 }
337  
338 sub _table {
339 # optimized due to hotttnesss
340 # my ($self, $from) = @_;
341 7420 ▶ if (my $ref = ref $_[1] ) {
Subroutine _table: called CC=6 SCAR=18.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $_[1]) 7416 4 7420
342 7416 ▶ if ($ref eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 7413 3 7416
elsif ($ref eq 'HASH') { } 0 3 3
elsif ($ref eq 'REF' and ref ${$_[1];} eq 'ARRAY') { } 1 4 5
343 7413 return $_[0]->_recurse_from(@{$_[1]});
344 }
345 elsif ($ref eq 'HASH') {
346 0 return $_[0]->_recurse_from($_[1]);
347 }
348 1 ▶ elsif ($ref eq 'REF' && ref ${$_[1]} eq 'ARRAY') {
Condition66%2/3
$ref eq "REF" and ref ${$_[1];} eq "ARRAY"
!ll&&!rl&&r
201
MC/DC50%1/2
$ref eq "REF" and ref ${$_[1];} eq "ARRAY"
$ref eq "REF" ref ${$_[1];} eq "ARRAY"
Truth table66%2/3
A and B
A= $ref eq "REF"B= ref ${$_[1];} eq "ARRAY"
ABresult
0X0
100
111
349 1 my ($sql, @bind) = @{ ${$_[1]} };
350 1 push @{$_[0]->{from_bind}}, @bind;
351 1 return $sql
352 }
353 }
354 6 return $_[0]->next::method ($_[1]);
355 }
356  
357 sub _generate_join_clause {
358 3063 ▶ my ($self, $join_type) = @_;
Subroutine _generate_join_clause: called CC=3 SCAR=11.0
359  
360 $join_type = $self->{_default_jointype}
361 3063 ▶ if ! defined $join_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $join_type 1453 1610 3063
362  
363 3063 ▶ return sprintf ('%s JOIN ',
Branch: 1 branch
BranchTrueFalseTotal
$join_type ? : 1616 1447 3063
364 $join_type ? $self->_sqlcase($join_type) : ''
365 );
366 }
367  
368 sub _recurse_from {
369 8453 ▶ my $self = shift;
Subroutine _recurse_from: called CC=1 SCAR=0.0
370 8453 return join (' ', $self->_gen_from_blocks(@_) );
371 }
372  
373 sub _gen_from_blocks {
374 8459 ▶ my ($self, $from, @joins) = @_;
Subroutine _gen_from_blocks: called CC=6 SCAR=17.9
375  
376 8459 my @fchunks = $self->_from_chunk_to_sql($from);
377  
378 8459 for (@joins) {
379 3065 my ($to, $on) = @$_;
380  
381 # check whether a join type exists
382 3065 ▶ my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to;
Branch: 1 branch
BranchTrueFalseTotal
ref $to eq 'ARRAY' ? : 2 3063 3065
383 3065 my $join_type;
384 3065 ▶ if (ref($to_jt) eq 'HASH' and defined($to_jt->{-join_type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $to_jt eq "HASH" and defined $to_jt->{"-join_type"}) 1612 1453 3065
Condition66%2/3
ref $to_jt eq "HASH" and defined $to_jt->{"-join_type"}
!ll&&!rl&&r
014531612
MC/DC50%1/2
ref $to_jt eq "HASH" and defined $to_jt->{"-join_type"}
ref $to_jt eq "HASH" defined $to_jt->{"-join_type"}
Truth table66%2/3
A and B
A= ref $to_jt eq "HASH"B= defined $to_jt->{"-join_type"}
ABresult
0X0
100
111
385 1612 $join_type = $to_jt->{-join_type};
386 1612 $join_type =~ s/^\s+ | \s+$//xg;
387 }
388  
389 3065 my @j = $self->_generate_join_clause( $join_type );
390  
391 3065 ▶ if (ref $to eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $to eq 'ARRAY') { } 2 3063 3065
392 2 push(@j, '(', $self->_recurse_from(@$to), ')');
393 }
394 else {
395 3063 push(@j, $self->_from_chunk_to_sql($to));
396 }
397  
398 3065 my ($sql, @bind) = $self->_join_condition($on);
399 3065 push(@j, ' ON ', $sql);
400 3065 push @{$self->{from_bind}}, @bind;
401  
402 3065 push @fchunks, join '', @j;
403 }
404  
405 8459 return @fchunks;
406 }
407  
408 sub _from_chunk_to_sql {
409 23044 ▶ my ($self, $fromspec) = @_;
Subroutine _from_chunk_to_sql: called CC=7 SCAR=19.7
410  
411 23044 return join (' ', do {
412 23044 ▶ if (! ref $fromspec) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $fromspec) { } 8452 14592 23044
elsif (ref $fromspec eq 'SCALAR') { } 2847 11745 14592
elsif (ref $fromspec eq 'REF' and ref $$fromspec eq 'ARRAY') { } 223 11522 11745
elsif (ref $fromspec eq 'HASH') { } 11522 0 11522
413 8452 $self->_quote($fromspec);
414 }
415 elsif (ref $fromspec eq 'SCALAR') {
416 2847 $$fromspec;
417 }
418 ▶ elsif (ref $fromspec eq 'REF' and ref $$fromspec eq 'ARRAY') {
Condition66%2/3
ref $fromspec eq "REF" and ref $$fromspec eq "ARRAY"
!ll&&!rl&&r
115220223
MC/DC50%1/2
ref $fromspec eq "REF" and ref $$fromspec eq "ARRAY"
ref $fromspec eq "REF" ref $$fromspec eq "ARRAY"
Truth table66%2/3
A and B
A= ref $fromspec eq "REF"B= ref $$fromspec eq "ARRAY"
ABresult
0X0
100
111
419 223 push @{$self->{from_bind}}, @{$$fromspec}[1..$#$$fromspec];
420 223 $$fromspec->[0];
421 }
422 elsif (ref $fromspec eq 'HASH') {
423 my ($as, $table, $toomuch) = ( map
424 11522 { $_ => $fromspec->{$_} }
425 11522 ( grep { $_ !~ /^\-/ } keys %$fromspec )
426 );
427  
428 11522 ▶ $self->throw_exception( "Only one table/as pair expected in from-spec but an exra '$toomuch' key present" )
Branch: 1 branch
BranchTrueFalseTotal
if defined $toomuch 0 11522 11522
429 if defined $toomuch;
430  
431 11522 ($self->_from_chunk_to_sql($table), $self->_quote($as) );
432 }
433 else {
434 0 $self->throw_exception('Unsupported from refkind: ' . ref $fromspec );
435 }
436 });
437 }
438  
439 sub _join_condition {
440 3093 ▶ my ($self, $cond) = @_;
Subroutine _join_condition: called CC=7 SCAR=19.5
441  
442 # Backcompat for the old days when a plain hashref
443 # { 't1.col1' => 't2.col2' } meant ON t1.col1 = t2.col2
444 # Once things settle we should start warning here so that
445 # folks unroll their hacks
446 3093 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cond eq 'HASH' and keys %$cond == 1 and (keys %$cond)[0] =~ /\./ and not ref((values %$cond)[0])) { } 23 3070 3093
elsif (ref $cond eq 'ARRAY') { } 14 3056 3070
Condition88%8/9
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /\./ and not ref((values %$cond)[0])
!ll&&!rl&&r
83298723
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /\./
!ll&&!rl&&r
8303010
ref $cond eq "HASH" and keys %$cond == 1
!ll&&!rl&&r
14693010
MC/DC75%3/4
ref $cond eq "HASH" and keys %$cond == 1 and (keys %$cond)[0] =~ /\./ and not ref((values %$cond)[0])
ref $cond eq "HASH" keys %$cond == 1 (keys %$cond)[0] =~ /\./ not ref((values %$cond)[0])
Truth table80%4/5
A and B and C and not(D)
A= ref $cond eq "HASH"B= keys %$cond == 1C= (keys %$cond)[0] =~ /\./D= not ref((values %$cond)[0])
ABCDresult
0XXX0
10XX0
110X0
11100
11111
447 ref $cond eq 'HASH'
448 and
449 keys %$cond == 1
450 and
451 (keys %$cond)[0] =~ /\./
452 and
453 ! ref ( (values %$cond)[0] )
454 ) {
455 23 $cond = { keys %$cond => { -ident => values %$cond } }
456 }
457 elsif ( ref $cond eq 'ARRAY' ) {
458 # do our own ORing so that the hashref-shim above is invoked
459 14 my @parts;
460 my @binds;
461 14 foreach my $c (@$cond) {
462 28 my ($sql, @bind) = $self->_join_condition($c);
463 28 push @binds, @bind;
464 28 push @parts, $sql;
465 }
466 14 return join(' OR ', @parts), @binds;
467 }
468  
469 3079 return $self->_recurse_where($cond);
470 }
471  
472 # This is hideously ugly, but SQLA does not understand multicol IN expressions
473 # FIXME TEMPORARY - DQ should have native syntax for this
474 # moved here to raise API questions
475 #
476 # !!! EXPERIMENTAL API !!! WILL CHANGE !!!
477 sub _where_op_multicolumn_in {
478 1 ▶ my ($self, $lhs, $rhs) = @_;
Subroutine _where_op_multicolumn_in: called CC=14 SCAR=40.8
479  
480 1 ▶ if (! ref $lhs or ref $lhs eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $lhs or ref $lhs eq 'ARRAY') { } 1 0 1
elsif (ref $lhs eq 'SCALAR') { } 0 0 0
elsif (ref $lhs eq 'REF' and ref $$lhs eq 'ARRAY') { } 0 0 0
Condition33%1/3
not ref $lhs or ref $lhs eq "ARRAY"
l!l&&r!l&&!r
010
MC/DC0%0/2
not ref $lhs or ref $lhs eq "ARRAY"
not ref $lhs ref $lhs eq "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $lhsB= ref $lhs eq "ARRAY"
ABresult
1X1
011
000
481 1 my (@sql, @bind);
482 1 ▶ for (ref $lhs ? @$lhs : $lhs) {
Branch: 1 branch
BranchTrueFalseTotal
ref $lhs ? : 1 0 1
483 4 ▶ if (! ref $_) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $_) { } 4 0 4
elsif (ref $_ eq 'SCALAR') { } 0 0 0
elsif (ref $_ eq 'REF' and ref $$_ eq 'ARRAY') { } 0 0 0
484 4 push @sql, $self->_quote($_);
485 }
486 elsif (ref $_ eq 'SCALAR') {
487 0 push @sql, $$_;
488 }
489 ▶ elsif (ref $_ eq 'REF' and ref $$_ eq 'ARRAY') {
Condition0%0/3
ref $_ eq "REF" and ref $$_ eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ eq "REF" and ref $$_ eq "ARRAY"
ref $_ eq "REF" ref $$_ eq "ARRAY"
Truth table0%0/3
A and B
A= ref $_ eq "REF"B= ref $$_ eq "ARRAY"
ABresult
0X0
100
111
490 0 my ($s, @b) = @$$_;
491 0 push @sql, $s;
492 0 push @bind, @b;
493 }
494 else {
495 0 $self->throw_exception("ARRAY of @{[ ref $_ ]}es unsupported for multicolumn IN lhs...");
496 }
497 }
498 1 $lhs = \[ join(', ', @sql), @bind];
499 }
500 elsif (ref $lhs eq 'SCALAR') {
501 0 $lhs = \[ $$lhs ];
502 }
503 ▶ elsif (ref $lhs eq 'REF' and ref $$lhs eq 'ARRAY' ) {
Condition0%0/3
ref $lhs eq "REF" and ref $$lhs eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $lhs eq "REF" and ref $$lhs eq "ARRAY"
ref $lhs eq "REF" ref $$lhs eq "ARRAY"
Truth table0%0/3
A and B
A= ref $lhs eq "REF"B= ref $$lhs eq "ARRAY"
ABresult
0X0
100
111
504 # noop
505 }
506 else {
507 0 $self->throw_exception( ref($lhs) . "es unsupported for multicolumn IN lhs...");
508 }
509  
510 # is this proper...?
511 1 $rhs = \[ $self->_recurse_where($rhs) ];
512  
513 1 for ($lhs, $rhs) {
514 2 ▶ $$_->[0] = "( $$_->[0] )"
Branch: 1 branch
BranchTrueFalseTotal
unless $$_->[0] =~ /^ \s* \( .* \) \s* $/sx 1 1 2
515 unless $$_->[0] =~ /^ \s* \( .* \) \s* $/xs;
516 }
517  
518 1 \[ join( ' IN ', shift @$$lhs, shift @$$rhs ), @$$lhs, @$$rhs ];
519 }
520  
521 200 1;
522  
523 =head1 AUTHORS
524  
525 See L<DBIx::Class/CONTRIBUTORS>.
526  
527 =head1 LICENSE
528  
529 You may distribute this code under the same terms as Perl itself.
530  
531 =cut
« DBIx/Class/ResultSource/RowParser/Util.pm ↑ Summary DBIx/Class/CDBICompat/DestroyWarning.pm »