blib/lib/DBIx/QuickORM/SQLBuilder/SQLAbstract.pm

filter: Statementstmt 80.3% 200 / 249 Branchbran 78.4% 80 / 102 Conditioncond 68.6% 59 / 86 MC/DCmcdc 51.0% 24 / 47 Subroutinesub 86.1% 31 / 36 Podpod 100% 12 / 12 totaltotal 76.3% 406 / 532 CC 128 SCAR 18.7
CC
128
Cov
78%
select
13.7
select
13.7
combine_where
11.4
CRAP 6.5
« blib/lib/DBIx/QuickORM/DSL.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/MySQL.pm »
1 package DBIx::QuickORM::SQLBuilder::SQLAbstract;
2 307 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 307 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 307 our $VERSION = '0.000032';
6  
7 307 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 307 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 307 ▶use SQL::Abstract 2.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 307 ▶use DBIx::QuickORM::Value();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 307 ▶use DBIx::QuickORM::WhereBuilder::SQLAbstract();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 307 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
15 +sqla
16 307 };
17  
18 307 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 307 with 'DBIx::QuickORM::Role::SQLBuilder';
20  
21 =pod
22  
23 =encoding UTF-8
24  
25 =head1 NAME
26  
27 DBIx::QuickORM::SQLBuilder::SQLAbstract - The default SQL builder.
28  
29 =head1 DESCRIPTION
30  
31 The builder handles use unless the ORM is given another. It fulfils
32 L<DBIx::QuickORM::Role::SQLBuilder> with L<SQL::Abstract> 2.x for the
33 C<where>, C<having>, and order, and writes everything else itself.
34  
35 The C<WHERE> is built by the request's C<where_builders>, the first whose
36 C<can_build_where> is true (see L<DBIx::QuickORM::Role::WhereBuilder>). When
37 no builder can, it croaks. Without C<where_builders> it is built as below, by
38 L<DBIx::QuickORM::WhereBuilder::SQLAbstract>.
39  
40 A C<where> uses L<SQL::Abstract>'s syntax with its C<columns> bind type.
41 Binds inside literal SQL are C<[$name, $value]> pairs, as in
42 C<< \["age > ?", [age => 40]] >>. The name decides how the value is bound
43 when it is a field of the table. A name with a dot is read as qualified
44 (C<table.field>).
45  
46 C<having> uses the C<where> syntax, and C<order_by> L<SQL::Abstract>'s order
47 syntax.
48  
49 C<limit> and C<offset> must be non-negative integers and are written into the
50 SQL. An C<offset> without a C<limit> gets the largest limit every supported
51 database accepts.
52  
53 Value markers (L<DBIx::QuickORM::Value>) may be values anywhere in a C<where>.
54 An C<sql> marker becomes L<SQL::Abstract> literal SQL with its binds,
55 C<< \[$sql, @binds] >>. The others become binds of the field they are under,
56 resolved when the SQL runs.
57  
58 As with any L<SQL::Abstract> literal, an C<sql> marker directly under a field
59 supplies its own operator: C<< {hits => sql('= 2 + ?', 5)} >>, or
60 C<< {hits => {'=' => sql('2 + ?', 5)}} >>. Markers inside L<SQL::Abstract>'s
61 own literal SQL are left to its binds.
62  
63 =head1 SYNOPSIS
64  
65 my $orm = DBIx::QuickORM->new(..., sql_builder => 'DBIx::QuickORM::SQLBuilder::SQLAbstract');
66  
67 my ($sql, $binds) = $orm->sql_builder->select(
68 $source,
69 $dialect,
70 where => {name => 'bob'},
71 limit => 10,
72 );
73  
74 =head1 PUBLIC METHODS
75  
76 =over 4
77  
78 =item $builder = DBIx::QuickORM::SQLBuilder::SQLAbstract->new
79  
80 A new builder. It takes no arguments.
81  
82 =item ($sql, \@binds) = $builder->select($source, $dialect, %request)
83  
84 =item ($sql, \@binds) = $builder->count($source, $dialect, where => $where)
85  
86 =item ($sql, \@binds) = $builder->update($source, $dialect, \%values, where => $where)
87  
88 =item ($sql, \@binds) = $builder->delete($source, $dialect, where => $where)
89  
90 =item $where = $builder->combine_where(op => $op, left => $left, right => $right)
91  
92 As L<DBIx::QuickORM::Role::SQLBuilder> describes. Each croaks on a request
93 key it does not know.
94  
95 A C<select> that adds nothing to a query source returns the query as it is.
96  
97 C<update> croaks without a hashref of at least one value. An C<sql> marker as
98 a value is written into the SQL.
99  
100 C<combine_where> returns C<< {-and => [$left, $right]} >> or C<< {-or =>
101 [$left, $right]} >>, and croaks on any other C<op>. A plain-string side is
102 raw SQL, as a C<where>.
103  
104 A side that is wholly literal SQL, a plain string, an C<sql> marker, or
105 C<< \[...] >>, is wrapped in parentheses, so an C<OR> in it stays grouped.
106 Literal SQL you nest inside C<-and> or C<-or> yourself is not touched.
107  
108 A side that is undef, an empty string, or an empty hash or array matches
109 every row: under C<and> the other side is returned as it is, and under C<or>
110 the result is C<{}>.
111  
112 =back
113  
114 =cut
115  
116 307 my %SELECT = map { $_ => 1 } qw/fields where where_builders order_by limit offset group_by having/;
117 307 my %WHERE = map { $_ => 1 } qw/where where_builders/;
118 307 my %COMBINE = map { $_ => 1 } qw/op left right/;
119  
120 # Agent Note: the largest signed 64-bit integer, a limit PostgreSQL, SQLite, DuckDB, and the MySQL family all accept.
121 307 my $NO_LIMIT = '9223372036854775807';
122  
123 309 ▶sub select {
Subroutine select: called CC=7 SCAR=26.2
Pod documented
124 0 ▶ my $self = shift;
Subroutine select: not called CC=7 SCAR=26.2
Pod documented
125 0 my ($source, $dialect, %request) = @_;
126 0 $self->_check(select => \%SELECT, \%request);
127  
128 0 my @given = grep { defined $request{$_} } keys %request;
129 0 ▶ if (!@given and my @query = $source->query_sql) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @given and my(@query) = $source->query_sql) 8 300 308
if (not @given and my(@query) = $source->query_sql) 0 0 0
Condition50%3/6
not @given and my(@query) = $source->query_sql
!ll&&!rl&&r
29918
not @given and my(@query) = $source->query_sql
!ll&&!rl&&r
000
MC/DC100%2/2
not @given and my(@query) = $source->query_sql
not @given my(@query) = $source->query_sql
MC/DC0%0/2
not @given and my(@query) = $source->query_sql
not @given my(@query) = $source->query_sql
Truth table100%3/3
not(A) and B
A= not @givenB= my(@query) = $source->query_sql
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not @givenB= my(@query) = $source->query_sql
ABresult
0X0
100
111
130 0 return @query;
131 }
132  
133 0 my ($fields, $field_binds) = $self->_fields($dialect, $request{fields});
134 0 my $computed = $self->_having_computed($dialect, @request{qw/fields having/});
135 0 my ($sql, $binds) = $self->_from("SELECT $fields FROM", $source, $dialect, (map { ($_ => $request{$_}) } qw/where where_builders group_by having order_by/), computed => $computed);
136 0 unshift @$binds => @$field_binds;
137  
138 0 my ($limit, $offset) = map { $self->_count_arg($_, $request{$_}) } qw/limit offset/;
139 0 ▶ $limit //= $NO_LIMIT if defined $offset;
Branch: 2 branches
BranchTrueFalseTotal
if defined $offset 6 290 296
if defined $offset 0 0 0
Condition33%2/6
$limit //= $NO_LIMIT
l!l&&r!l&&!r
330
$limit //= $NO_LIMIT
l!l&&r!l&&!r
000
MC/DC0%0/2
$limit //= $NO_LIMIT
$limit $NO_LIMIT
MC/DC0%0/2
$limit //= $NO_LIMIT
$limit $NO_LIMIT
Truth table66%2/3
A //= B
A= $limitB= $NO_LIMIT
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $limitB= $NO_LIMIT
ABresult
1X1
011
000
140 0 ▶ $sql .= " LIMIT $limit" if defined $limit;
Branch: 2 branches
BranchTrueFalseTotal
if defined $limit 35 261 296
if defined $limit 0 0 0
141 0 ▶ $sql .= " OFFSET $offset" if defined $offset;
Branch: 2 branches
BranchTrueFalseTotal
if defined $offset 6 290 296
if defined $offset 0 0 0
142  
143 0 return ($sql, $binds);
144 }
145  
146 18 ▶sub count {
Subroutine count: called CC=1 SCAR=1.2
Pod documented
147 0 ▶ my $self = shift;
Subroutine count: not called CC=1 SCAR=1.2
Pod documented
148 0 my ($source, $dialect, %request) = @_;
149 0 $self->_check(count => \%WHERE, \%request);
150 0 return $self->_from('SELECT COUNT(*) AS count FROM', $source, $dialect, %request);
151 }
152  
153 13 ▶sub update {
Subroutine update: called CC=4 SCAR=18.5
Pod documented
154 0 ▶ my $self = shift;
Subroutine update: not called CC=4 SCAR=18.5
Pod documented
155 0 my ($source, $dialect, $values, %request) = @_;
156 0 $self->_check(update => \%WHERE, \%request);
157 0 ▶ croak "update() needs a hashref of at least one value" unless ref($values) eq 'HASH' && %$values;
Branch: 2 branches
BranchTrueFalseTotal
unless ref $values eq "HASH" and %$values 1 12 13
unless ref $values eq "HASH" and %$values 0 0 0
Condition33%2/6
ref $values eq "HASH" and %$values
!ll&&!rl&&r
0112
ref $values eq "HASH" and %$values
!ll&&!rl&&r
000
MC/DC50%1/2
ref $values eq "HASH" and %$values
ref $values eq "HASH" %$values
MC/DC0%0/2
ref $values eq "HASH" and %$values
ref $values eq "HASH" %$values
Truth table66%2/3
A and B
A= ref $values eq "HASH"B= %$values
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $values eq "HASH"B= %$values
ABresult
0X0
100
111
158  
159 0 my (@set, @binds);
160 0 for my $field (sort keys %$values) {
161 0 my ($sql, @value_binds) = $self->_value($field, $values->{$field});
162 0 push @set => $dialect->quote_identifier($field) . " = $sql";
163 0 push @binds => @value_binds;
164 }
165  
166 0 my ($from, $from_binds) = $source->from_sql($dialect);
167 0 my ($where, $where_binds) = $self->_where_clause($source, $dialect, @request{qw/where where_builders/});
168 0 return ("UPDATE $from SET " . join(', ' => @set) . $where, [@$from_binds, @binds, @$where_binds]);
169 }
170  
171 14 ▶sub delete {
Subroutine delete: called CC=1 SCAR=1.2
Pod documented
172 0 ▶ my $self = shift;
Subroutine delete: not called CC=1 SCAR=1.2
Pod documented
173 0 my ($source, $dialect, %request) = @_;
174 0 $self->_check(delete => \%WHERE, \%request);
175 0 return $self->_from('DELETE FROM', $source, $dialect, %request);
176 }
177  
178 42 ▶sub combine_where {
Subroutine combine_where: called CC=8 SCAR=24.3
Pod documented
179 0 ▶ my $self = shift;
Subroutine combine_where: not called CC=8 SCAR=24.3
Pod documented
180 0 my %params = @_;
181 0 $self->_check(combine_where => \%COMBINE, \%params);
182  
183 0 my ($op, $left, $right) = @params{qw/op left right/};
184 0 ▶ croak "combine_where() takes op 'and' or 'or'" unless defined($op) && $op =~ m/^(?:and|or)\z/;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $op and $op =~ /^(?:and|or)\z/ 1 40 41
unless defined $op and $op =~ /^(?:and|or)\z/ 0 0 0
Condition33%2/6
defined $op and $op =~ /^(?:and|or)\z/
!ll&&!rl&&r
0140
defined $op and $op =~ /^(?:and|or)\z/
!ll&&!rl&&r
000
MC/DC50%1/2
defined $op and $op =~ /^(?:and|or)\z/
defined $op $op =~ /^(?:and|or)\z/
MC/DC0%0/2
defined $op and $op =~ /^(?:and|or)\z/
defined $op $op =~ /^(?:and|or)\z/
Truth table66%2/3
A and B
A= defined $opB= $op =~ /^(?:and|or)\z/
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $opB= $op =~ /^(?:and|or)\z/
ABresult
0X0
100
111
185  
186 0 ▶ my $empty = sub { my $r = ref($_[0]); !defined($_[0]) || ($r eq 'HASH' && !%{$_[0]}) || ($r eq 'ARRAY' && !@{$_[0]}) || ($r eq '' && $_[0] eq '') };
Subroutine __ANON__: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
not defined $_[0] or $r eq "HASH" and not %{$_[0];} or $r eq "ARRAY" and not @{$_[0];} or $r eq "" and $_[0] eq "" 3 102 105
Condition94%17/18
!defined($_[0]) || $r eq 'HASH' && !%{$_[0];} || $r eq 'ARRAY' && !@{$_[0];} || $r eq '' && $_[0] eq ''
l!l&&r!l&&!r
3399
not defined $_[0] or $r eq "HASH" and not %{$_[0];} or $r eq "ARRAY" and not @{$_[0];}
l!l&&r!l&&!r
21102
not defined $_[0] or $r eq "HASH" and not %{$_[0];}
l!l&&r!l&&!r
11103
$r eq "HASH" and not %{$_[0];}
!ll&&!rl&&r
30731
$r eq "ARRAY" and not @{$_[0];}
!ll&&!rl&&r
10201
$r eq "" and $_[0] eq ""
!ll&&!rl&&r
81183
MC/DC85%6/7
!defined($_[0]) || $r eq 'HASH' && !%{$_[0];} || $r eq 'ARRAY' && !@{$_[0];} || $r eq '' && $_[0] eq ''
not defined $_[0] $r eq "HASH" not %{$_[0];} $r eq "ARRAY" not @{$_[0];} $r eq "" $_[0] eq ""
Truth table43%7/16
not(A) or B and not(C) or D and not(E) || F and G
A= not defined $_[0]B= $r eq "HASH"C= not %{$_[0];}D= $r eq "ARRAY"E= not @{$_[0];}F= $r eq ""G= $_[0] eq ""
ABCDEFGresult
1XXXXXX1
011XXXX1
00X11XX1
01011XX1
00X0X111
00X10111
0100X111
01010111
00X0X0X0
00X0X100
00X100X0
00X10100
0100X0X0
0100X100
010100X0
01010100
187 0 ▶ return {} if $op eq 'or' && ($empty->($left) || $empty->($right));
Branch: 2 branches
BranchTrueFalseTotal
if $op eq "or" and &$empty($left) || &$empty($right) 2 38 40
if $op eq "or" and &$empty($left) || &$empty($right) 0 0 0
Condition50%6/12
$op eq "or" and &$empty($left) || &$empty($right)
!ll&&!rl&&r
24142
&$empty($left) or &$empty($right)
l!l&&r!l&&!r
1114
$op eq "or" and &$empty($left) || &$empty($right)
!ll&&!rl&&r
000
&$empty($left) or &$empty($right)
l!l&&r!l&&!r
000
MC/DC100%3/3
$op eq "or" and &$empty($left) || &$empty($right)
$op eq "or" &$empty($left) &$empty($right)
MC/DC0%0/3
$op eq "or" and &$empty($left) || &$empty($right)
$op eq "or" &$empty($left) &$empty($right)
Truth table100%4/4
A and B or C
A= $op eq "or"B= &$empty($left)C= &$empty($right)
ABCresult
0XX0
1000
11X1
1011
Truth table0%0/4
A and B or C
A= $op eq "or"B= &$empty($left)C= &$empty($right)
ABCresult
0XX0
1000
11X1
1011
188 0 ▶ return $right if $empty->($left);
Branch: 2 branches
BranchTrueFalseTotal
if &$empty($left) 2 36 38
if &$empty($left) 0 0 0
189 0 ▶ return $left if $empty->($right);
Branch: 2 branches
BranchTrueFalseTotal
if &$empty($right) 2 34 36
if &$empty($right) 0 0 0
190 0 return {"-$op" => [map { $self->_group($_) } $left, $right]};
191 }
192  
193 =pod
194  
195 =over 4
196  
197 =item $sqla = $builder->sqla($dialect)
198  
199 The L<SQL::Abstract> for the dialect's quote character, made on first use.
200  
201 =back
202  
203 =cut
204  
205 718 ▶sub sqla {
Subroutine sqla: called CC=2 SCAR=7.0
Pod documented
206 718 my $self = shift;
207 718 my ($dialect) = @_;
208 718 my $q = $dialect->quote_char;
209 718 ▶ return $self->{+SQLA}->{$q} //= SQL::Abstract->new(quote_char => $q, name_sep => '.', bindtype => 'columns');
Condition66%2/3
$self->{"sqla"}{$q} //= "SQL::Abstract"->new("quote_char", $q, "name_sep", ".", "bindtype", "columns")
l!l&&r!l&&!r
656620
MC/DC0%0/2
$self->{"sqla"}{$q} //= "SQL::Abstract"->new("quote_char", $q, "name_sep", ".", "bindtype", "columns")
$self->{"sqla"}{$q} "SQL::Abstract"->new("quote_char", $q, "name_sep", ".", "bindtype", "columns")
Truth table66%2/3
A //= B
A= $self->{"sqla"}{$q}B= "SQL::Abstract"->new("quote_char", $q, "name_sep", ".", "bindtype", "columns")
ABresult
1X1
011
000
210 }
211  
212 =pod
213  
214 =over 4
215  
216 =item $where = $builder->convert_where($where)
217  
218 A copy of C<$where> with every value marker, at any depth, in the form
219 L<SQL::Abstract> takes.
220  
221 A marker holding undef becomes a plain undef, so it gives C<IS NULL> (or
222 C<IS NOT NULL> under C<!=>) as a plain undef does.
223  
224 =back
225  
226 =cut
227  
228 1550 ▶sub convert_where {
Subroutine convert_where: called CC=9 SCAR=22.0
Pod documented
229 1550 my $self = shift;
230 1550 my ($where) = @_;
231 1550 ▶ my $type = ref($where) or return $where;
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $where or return $where 570 980 1550
232  
233 570 ▶ if (blessed($where)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($where)) 56 514 570
234 56 ▶ return $where unless $where->isa('DBIx::QuickORM::Value');
Branch: 1 branch
BranchTrueFalseTotal
unless $where->isa("DBIx::QuickORM::Value") 0 56 56
235 56 ▶ return undef unless $where->kind eq 'sql' || defined $where->value; # so SQL::Abstract writes IS NULL
Branch: 1 branch
BranchTrueFalseTotal
unless $where->kind eq "sql" or defined $where->value 8 48 56
Condition100%3/3
$where->kind eq "sql" or defined $where->value
l!l&&r!l&&!r
29198
MC/DC100%2/2
$where->kind eq "sql" or defined $where->value
$where->kind eq "sql" defined $where->value
Truth table100%3/3
A or B
A= $where->kind eq "sql"B= defined $where->value
ABresult
1X1
011
000
236 48 ▶ return {-value => $where} unless $where->kind eq 'sql';
Branch: 1 branch
BranchTrueFalseTotal
unless $where->kind eq "sql" 19 29 48
237 29 return \[$where->value, map { [undef, $_] } @{$where->binds}];
238 }
239  
240 514 ▶ return {map { ($_ => $self->convert_where($where->{$_})) } keys %$where} if $type eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" 463 51 514
241 51 ▶ return [map { $self->convert_where($_) } @$where] if $type eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ARRAY" 33 18 51
242 18 return $where;
243 }
244  
245 =head1 PRIVATE METHODS
246  
247 =over 4
248  
249 =item $builder->_check($method, \%known, \%request)
250  
251 Croaks naming every request key not in C<%known>.
252  
253 =back
254  
255 =cut
256  
257 396 ▶sub _check {
Subroutine _check: called CC=2 SCAR=6.9
258 396 my $self = shift;
259 396 my ($method, $known, $request) = @_;
260 396 my @unknown = sort grep { !$known->{$_} } keys %$request;
261 396 ▶ croak "Unknown request key(s) for $method(): " . join(', ' => @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 3 393 396
262 }
263  
264 =pod
265  
266 =over 4
267  
268 =item ($sql, \@binds) = $builder->_from($prefix, $source, $dialect, where => $where, where_builders => \@chain, group_by => \@fields, having => $having, order_by => $order, computed => \%computed)
269  
270 C<$prefix>, the source's C<FROM> part, then the C<WHERE>, C<GROUP BY>,
271 C<HAVING>, and C<ORDER BY> for the clauses given. With C<computed> (from
272 C<_having_computed>), the C<HAVING> writes those names as their SQL.
273  
274 =back
275  
276 =cut
277  
278 331 ▶sub _from {
Subroutine _from: called CC=3 SCAR=11.0
279 331 my $self = shift;
280 331 my ($prefix, $source, $dialect, %clauses) = @_;
281  
282 331 my $sqla = $self->sqla($dialect);
283 331 my ($from, $from_binds) = $source->from_sql($dialect);
284 331 my ($where, $where_binds) = $self->_where_clause($source, $dialect, @clauses{qw/where where_builders/});
285 329 my $sql = "$prefix $from$where";
286  
287 329 ▶ $sql .= ' GROUP BY ' . join(', ' => map { $self->_name($dialect, $_) } @{$clauses{group_by}}) if $clauses{group_by};
Branch: 1 branch
BranchTrueFalseTotal
if $clauses{"group_by"} 41 288 329
288  
289 329 ▶ my $having_sqla = $clauses{computed} ? $self->_computed_sqla($dialect, $clauses{computed}) : $sqla;
Branch: 1 branch
BranchTrueFalseTotal
$clauses{'computed'} ? : 15 314 329
290 329 my ($having, @having_binds) = $having_sqla->where($self->convert_where($clauses{having}));
291 329 $having =~ s/^ WHERE / HAVING /;
292 329 my ($order, @order_binds) = $sqla->where(undef, $clauses{order_by});
293  
294 329 return ("$sql$having$order", [@$from_binds, @$where_binds, @having_binds, @order_binds]);
295 }
296  
297 =pod
298  
299 =over 4
300  
301 =item ($sql, \@binds) = $builder->_where_clause($source, $dialect, $where, \@chain)
302  
303 The C<WHERE> clause, with a leading space, or an empty string, from the first
304 where builder in C<@chain> that can build it, or
305 L<DBIx::QuickORM::WhereBuilder::SQLAbstract> without a chain. Croaks when
306 none can. An undef or empty-string C<where> asks no builder.
307  
308 =back
309  
310 =cut
311  
312 307 my @DEFAULT_CHAIN = ('DBIx::QuickORM::WhereBuilder::SQLAbstract');
313  
314 343 ▶sub _where_clause {
Subroutine _where_clause: called CC=9 SCAR=22.0
315 343 my $self = shift;
316 343 my ($source, $dialect, $where, $chain) = @_;
317 343 ▶ return ('', []) unless defined($where) && (ref($where) || length($where));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $where and ref $where || length $where 119 224 343
Condition100%6/6
defined $where and ref $where || length $where
!ll&&!rl&&r
1172224
ref $where or length $where
l!l&&r!l&&!r
22042
MC/DC100%3/3
defined $where and ref $where || length $where
defined $where ref $where length $where
Truth table100%4/4
A and B or C
A= defined $whereB= ref $whereC= length $where
ABCresult
0XX0
1000
11X1
1011
318  
319 224 ▶ for my $builder (@{$chain // \@DEFAULT_CHAIN}) {
Condition100%2/2
$chain // \@DEFAULT_CHAIN
l!l
20024
MC/DC100%1/1
$chain // \@DEFAULT_CHAIN
$chain
Truth table100%2/2
A
A= $chain
Aresult
11
00
320 466 ▶ next unless $builder->can_build_where($where);
Branch: 1 branch
BranchTrueFalseTotal
unless $builder->can_build_where($where) 244 222 466
321 222 my ($sql, $binds) = $builder->build_where($where, $source, $dialect);
322 222 ▶ return (length($sql) ? " WHERE $sql" : '', $binds);
Branch: 1 branch
BranchTrueFalseTotal
length $sql ? : 220 2 222
323 }
324  
325 2 ▶ croak "No where builder can build this where: " . (ref($where) || $where);
Condition66%2/3
ref $where || $where
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $where || $where
ref $where $where
Truth table66%2/3
A || B
A= ref $whereB= $where
ABresult
1X1
011
000
326 }
327  
328 =pod
329  
330 =over 4
331  
332 =item ($sql, \@binds) = $builder->_fields($dialect, $fields)
333  
334 The select list: C<*> for undef, or the names of an arrayref quoted as
335 C<_name> does. For a hashref, each expression (a field name, an C<sql()>
336 marker, or C<FUNC(field)> from C<[$func, $field]>, the field C<*> left
337 unquoted) C<AS> its quoted name, sorted by name.
338  
339 =back
340  
341 =cut
342  
343 300 ▶sub _fields {
Subroutine _fields: called CC=4 SCAR=13.9
344 300 my $self = shift;
345 300 my ($dialect, $fields) = @_;
346  
347 300 ▶ return ('*', []) unless $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields 30 270 300
348 270 ▶ return (join(', ' => map { $self->_name($dialect, $_) } @$fields), []) if ref($fields) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $fields eq "ARRAY" 202 68 270
349  
350 68 my (@sql, @binds);
351 68 for my $name (sort keys %$fields) {
352 206 my ($sql, @expr_binds) = $self->_expr($dialect, $fields->{$name});
353 206 push @sql => "$sql AS " . $dialect->quote_identifier($name);
354 206 push @binds => @expr_binds;
355 }
356  
357 68 return (join(', ' => @sql), \@binds);
358 }
359  
360 =pod
361  
362 =over 4
363  
364 =item ($sql, @binds) = $builder->_expr($dialect, $expr)
365  
366 One computed column's SQL: a field name, an C<sql()> marker, or
367 C<FUNC(field)> from C<[$func, $field]>, the field C<*> left unquoted.
368  
369 =back
370  
371 =cut
372  
373 236 ▶sub _expr {
Subroutine _expr: called CC=4 SCAR=13.9
374 236 my $self = shift;
375 236 my ($dialect, $expr) = @_;
376 236 ▶ return $self->_value(undef, $expr) if $self->_is_sql($expr);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_sql($expr) 34 202 236
377 202 ▶ return ("$expr->[0](" . ($expr->[1] eq '*' ? '*' : $self->_name($dialect, $expr->[1])) . ')') if ref($expr) eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if ref $expr eq "ARRAY" 39 163 202
$expr->[1] eq '*' ? : 17 22 39
378 163 return ($self->_name($dialect, $expr));
379 }
380  
381 =pod
382  
383 =over 4
384  
385 =item \%computed = $builder->_having_computed($dialect, $fields, $having)
386  
387 Each computed column's SQL and binds, C<< {$name => [$sql, @binds]} >>, an
388 C<sql()> column's in parentheses, for a C<having> to name. Undef when there
389 is no C<having>, no computed columns, or the dialect's C<having_uses_alias>
390 is true.
391  
392 =item $sqla = $builder->_computed_sqla($dialect, \%computed)
393  
394 An L<SQL::Abstract> like C<sqla>'s that writes each name in C<%computed> as
395 that column's SQL.
396  
397 =back
398  
399 =cut
400  
401 300 ▶sub _having_computed {
Subroutine _having_computed: called CC=7 SCAR=19.5
402 300 my $self = shift;
403 300 my ($dialect, $fields, $having) = @_;
404 300 ▶ return undef unless defined($having) && ref($fields) eq 'HASH' && %$fields;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $having and ref $fields eq "HASH" and %$fields 284 16 300
Condition83%5/6
defined $having and ref $fields eq "HASH" and %$fields
!ll&&!rl&&r
284016
defined $having and ref $fields eq "HASH"
!ll&&!rl&&r
283116
MC/DC66%2/3
defined $having and ref $fields eq "HASH" and %$fields
defined $having ref $fields eq "HASH" %$fields
Truth table75%3/4
A and B and C
A= defined $havingB= ref $fields eq "HASH"C= %$fields
ABCresult
0XX0
10X0
1100
1111
405 16 ▶ return undef if $dialect->having_uses_alias;
Branch: 1 branch
BranchTrueFalseTotal
if $dialect->having_uses_alias 1 15 16
406 15 my %computed;
407 15 for my $name (keys %$fields) {
408 30 my ($sql, @binds) = $self->_expr($dialect, $fields->{$name});
409 30 ▶ $sql = "($sql)" if $self->_is_sql($fields->{$name}); # sql() may hold a low-precedence operator
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_sql($fields->{$name}) 10 20 30
410 30 $computed{$name} = [$sql, @binds];
411 }
412 15 return \%computed;
413 }
414  
415 15 ▶sub _computed_sqla {
Subroutine _computed_sqla: called CC=1 SCAR=0.0
416 15 my $self = shift;
417 15 my ($dialect, $computed) = @_;
418  
419 15 my $sqla = SQL::Abstract->new(quote_char => $dialect->quote_char, name_sep => '.', bindtype => 'columns');
420 $sqla->wrap_expander(
421 15 ▶ ident => sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
422 15 my ($orig) = @_;
423 11 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
424 11 my ($sqla, $type, $ident) = @_;
425 11 ▶ my $name = ref($ident) ? join('.' => @$ident) : $ident;
Branch: 1 branch
BranchTrueFalseTotal
ref $ident ? : 0 11 11
426 11 ▶ if (my $expr = $computed->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expr = $computed->{$name}) 10 1 11
427 10 return {-literal => [@$expr]}; # the SQL, then its binds, already [undef, $value] pairs
428 }
429 1 ▶ return $orig ? $sqla->$orig($type, $ident) : {-ident => ref($ident) ? $ident : [split m/\./, $ident, -1]};
Branch: 2 branches
BranchTrueFalseTotal
$orig ? : 0 1 1
ref $ident ? : 0 1 1
430 15 };
431 },
432 15 );
433  
434 15 return $sqla;
435 }
436  
437 =pod
438  
439 =over 4
440  
441 =item $sql = $builder->_name($dialect, $name)
442  
443 A field name quoted through the dialect, a dot separating a qualifier.
444  
445 =back
446  
447 =cut
448  
449 776 ▶sub _name {
Subroutine _name: called CC=1 SCAR=0.0
450 776 my $self = shift;
451 776 my ($dialect, $name) = @_;
452 776 return join '.' => map { $dialect->quote_identifier($_) } split m/\./, $name, -1;
453 }
454  
455 =pod
456  
457 =over 4
458  
459 =item $n = $builder->_count_arg($name, $value)
460  
461 The value, or undef for undef. Croaks unless it is a non-negative integer.
462  
463 =back
464  
465 =cut
466  
467 595 ▶sub _count_arg {
Subroutine _count_arg: called CC=3 SCAR=11.0
468 595 my $self = shift;
469 595 my ($name, $value) = @_;
470 595 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 555 40 595
471 40 ▶ croak "'$name' must be a non-negative integer, got '$value'" unless $value =~ m/^\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\d+\z/ 2 38 40
472 38 return $value;
473 }
474  
475 =pod
476  
477 =over 4
478  
479 =item ($sql, @binds) = $builder->_value($field, $value)
480  
481 C<?> and the bind for one value, or an C<sql> marker's SQL and binds.
482  
483 =back
484  
485 =cut
486  
487 48 ▶sub _value {
Subroutine _value: called CC=2 SCAR=6.9
488 48 my $self = shift;
489 48 my ($field, $value) = @_;
490 48 ▶ return ('?', [$field => $value]) unless $self->_is_sql($value);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_sql($value) 12 36 48
491 36 return ($value->value, map { [undef, $_] } @{$value->binds});
492 }
493  
494 =pod
495  
496 =over 4
497  
498 =item $bool = $builder->_is_sql($value)
499  
500 True for an C<sql> marker.
501  
502 =back
503  
504 =cut
505  
506 370 ▶sub _is_sql { blessed($_[1]) && $_[1]->isa('DBIx::QuickORM::Value') && $_[1]->kind eq 'sql' ? 1 : 0 }
Subroutine _is_sql: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[1]) && $_[1]->isa('DBIx::QuickORM::Value') && $_[1]->kind eq 'sql' ? : 84 286 370
Condition83%5/6
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value") and $_[1]->kind eq "sql"
!ll&&!rl&&r
285184
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value")
!ll&&!rl&&r
285085
MC/DC66%2/3
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value") and $_[1]->kind eq "sql"
builtin::blessed($_[1]) $_[1]->isa("DBIx::QuickORM::Value") $_[1]->kind eq "sql"
Truth table75%3/4
A and B and C
A= builtin::blessed($_[1])B= $_[1]->isa("DBIx::QuickORM::Value")C= $_[1]->kind eq "sql"
ABCresult
0XX0
10X0
1100
1111
507  
508 =pod
509  
510 =over 4
511  
512 =item $side = $builder->_group($side)
513  
514 A side of C<combine_where> in parentheses when the whole side is literal SQL:
515 a plain string, an C<sql> marker, or C<< \[$sql, @binds] >>. Any other side
516 is returned as it is. Nothing inside a side is touched.
517  
518 =back
519  
520 =cut
521  
522 68 ▶sub _group {
Subroutine _group: called CC=5 SCAR=16.1
523 68 my $self = shift;
524 68 my ($side) = @_;
525 68 ▶ return \["( $side )"] unless ref($side);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $side 12 56 68
526 56 ▶ return DBIx::QuickORM::Value::sql("( " . $side->value . " )", @{$side->binds}) if $self->_is_sql($side);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_sql($side) 4 52 56
527 52 ▶ return $side unless ref($side) eq 'REF' && ref($$side) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $side eq "REF" and ref $$side eq "ARRAY" 50 2 52
Condition66%2/3
ref $side eq "REF" and ref $$side eq "ARRAY"
!ll&&!rl&&r
5002
MC/DC50%1/2
ref $side eq "REF" and ref $$side eq "ARRAY"
ref $side eq "REF" ref $$side eq "ARRAY"
Truth table66%2/3
A and B
A= ref $side eq "REF"B= ref $$side eq "ARRAY"
ABresult
0X0
100
111
528 2 my ($sql, @binds) = @$$side;
529 2 return \["( $sql )", @binds];
530 }
531  
532 307 1;
533  
534 __END__
« blib/lib/DBIx/QuickORM/DSL.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect/MySQL.pm »