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/Source/Table/Temp.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Query.pm »
1 package DBIx::QuickORM::SQLBuilder::SQLAbstract;
2 262 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 262 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 262 our $VERSION = '0.000033';
6  
7 262 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 262 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 262 ▶use SQL::Abstract 2.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 262 ▶use DBIx::QuickORM::Value();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 262 ▶use DBIx::QuickORM::WhereBuilder::SQLAbstract();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 262 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
15 +sqla
16 262 };
17  
18 262 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 262 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<< \["views > ?", [views => 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<< {views => sql('= 2 + ?', 5)} >>, or
60 C<< {views => {'=' => sql('2 + ?', 5)}} >>. Markers inside L<SQL::Abstract>'s
61 own literal SQL are left to its binds.
62  
63 L<DBIx::QuickORM::WhereBuilder::SQLAbstract> shows the C<where> shapes.
64  
65 =head1 SYNOPSIS
66  
67 my $orm = DBIx::QuickORM->new(
68 credentials => \%credentials,
69 sql_builder => 'DBIx::QuickORM::SQLBuilder::SQLAbstract', # the default
70 );
71  
72 my $builder = $orm->sql_builder;
73 my $users = $orm->handle('users')->source;
74 my $dialect = $orm->connection->dialect;
75  
76 my ($sql, $binds) = $builder->select(
77 $users,
78 $dialect,
79 where => {name => 'bob'},
80 limit => 10,
81 );
82 # $sql: SELECT * FROM "users" WHERE ( "name" = ? ) LIMIT 10
83 # $binds: [['name', 'bob']]
84  
85 =head1 PUBLIC METHODS
86  
87 These fulfil L<DBIx::QuickORM::Role::SQLBuilder>, which describes their
88 arguments. Each croaks on a request key it does not know. The examples use
89 C<$builder> and C<$dialect> from the L</SYNOPSIS>.
90  
91 =over 4
92  
93 =item $builder = DBIx::QuickORM::SQLBuilder::SQLAbstract->new
94  
95 A new builder. It takes no arguments.
96  
97 =item ($sql, \@binds) = $builder->select($source, $dialect, %request)
98  
99 A C<SELECT> of the request's fields, conditions, order, and window.
100  
101 my $posts = $orm->handle('posts')->source;
102  
103 my ($sql, $binds) = $builder->select(
104 $posts,
105 $dialect,
106 fields => {
107 user_id => 'user_id',
108 posts => ['count', 'id'],
109 },
110 group_by => ['user_id'],
111 having => {posts => {'>' => 1}},
112 order_by => {-desc => 'posts'},
113 offset => 5,
114 );
115 # $sql: SELECT count("id") AS "posts", "user_id" AS "user_id" FROM "posts"
116 # GROUP BY "user_id" HAVING ( count("id") > ? ) ORDER BY "posts" DESC
117 # LIMIT 9223372036854775807 OFFSET 5
118 # $binds: [['posts', 1]]
119 # the LIMIT: an offset without a limit gets the largest 64-bit integer,
120 # since some databases take an OFFSET only after a LIMIT
121  
122 Computed columns come in name order. A C<having> naming one writes its SQL
123 where the dialect cannot name the alias there.
124  
125 The function name of C<[$func, $field]> is written into the SQL as given. A
126 handle checks it is a word, but a direct call does not, so pass only fixed
127 names, never user input.
128  
129 A C<select> that adds nothing to a query source returns the query as it is.
130  
131 =item ($sql, \@binds) = $builder->count($source, $dialect, where => $where, where_builders => \@builders)
132  
133 =item ($sql, \@binds) = $builder->delete($source, $dialect, where => $where, where_builders => \@builders)
134  
135 As L<DBIx::QuickORM::Role::SQLBuilder> describes.
136  
137 my ($count_sql, $count_binds) = $builder->count($users, $dialect, where => {name => 'bob'});
138 # SELECT COUNT(*) AS count FROM "users" WHERE ( "name" = ? )
139 # [['name', 'bob']]
140  
141 my ($delete_sql, $delete_binds) = $builder->delete($users, $dialect, where => {name => 'bob'});
142 # DELETE FROM "users" WHERE ( "name" = ? )
143 # [['name', 'bob']]
144  
145 =item ($sql, \@binds) = $builder->update($source, $dialect, \%values, where => $where, where_builders => \@builders)
146  
147 An C<UPDATE> setting each field to its value, fields in name order.
148  
149 use DBIx::QuickORM::Value qw/sql/;
150  
151 my ($sql, $binds) = $builder->update(
152 $orm->handle('posts')->source,
153 $dialect,
154 {views => sql('views + ?', 1)},
155 where => {id => 7},
156 );
157 # $sql: UPDATE "posts" SET "views" = views + ? WHERE ( "id" = ? )
158 # $binds: [[undef, 1], ['id', 7]]
159  
160 An C<sql> marker is written into the SQL, its binds tied to no field: an
161 undef field means the value is bound without a field's type. Any other value
162 is a C<?> bound as its field. A C<binary> marker keeps its field name too, and
163 the Connection binds it as binary whatever the field.
164  
165 It croaks without a hashref of at least one value.
166  
167 =item $where = $builder->combine_where(op => $op, left => $left, right => $right)
168  
169 C<< {-and => [$left, $right]} >> or C<< {-or => [$left, $right]} >>.
170  
171 my $both = $builder->combine_where(
172 op => 'and',
173 left => {name => 'Bob'},
174 right => 'id > 10 OR id < 5',
175 );
176 # {-and => [{name => 'Bob'}, \['( id > 10 OR id < 5 )']]}
177  
178 my $any = $builder->combine_where(
179 op => 'or',
180 left => {name => 'Bob'},
181 right => {},
182 );
183 # {}: an empty side matches every row
184  
185 A side that is wholly literal SQL, a plain string, an C<sql> marker, or
186 C<< \[...] >>, is wrapped in parentheses, so an C<OR> in it stays grouped.
187 Literal SQL you nest inside C<-and> or C<-or> yourself is not touched.
188  
189 A side that is undef, an empty string, or an empty hash or array matches
190 every row. Under C<and> the other side is returned as it is, and under C<or>
191 the result is C<{}>.
192  
193 It croaks on an C<op> other than C<'and'> or C<'or'>.
194  
195 =back
196  
197 =cut
198  
199 262 my %SELECT = map { $_ => 1 } qw/fields where where_builders order_by limit offset group_by having/;
200 262 my %WHERE = map { $_ => 1 } qw/where where_builders/;
201 262 my %COMBINE = map { $_ => 1 } qw/op left right/;
202  
203 # Agent Note: the largest signed 64-bit integer, a limit PostgreSQL, SQLite, DuckDB, and the MySQL family all accept.
204 262 my $NO_LIMIT = '9223372036854775807';
205  
206 394 ▶sub select {
Subroutine select: called CC=7 SCAR=26.2
Pod documented
207 0 ▶ my $self = shift;
Subroutine select: not called CC=7 SCAR=26.2
Pod documented
208 0 my ($source, $dialect, %request) = @_;
209 0 $self->_check(select => \%SELECT, \%request);
210  
211 0 my @given = grep { defined $request{$_} } keys %request;
212 0 ▶ if (!@given and my @query = $source->query_sql) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @given and my(@query) = $source->query_sql) 7 386 393
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
38517
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
213 0 return @query;
214 }
215  
216 0 my ($fields, $field_binds) = $self->_fields($dialect, $request{fields});
217 0 my $computed = $self->_having_computed($dialect, @request{qw/fields having/});
218 0 my ($sql, $binds) = $self->_from("SELECT $fields FROM", $source, $dialect, (map { ($_ => $request{$_}) } qw/where where_builders group_by having order_by/), computed => $computed);
219 0 unshift @$binds => @$field_binds;
220  
221 0 my ($limit, $offset) = map { $self->_count_arg($_, $request{$_}) } qw/limit offset/;
222 0 ▶ $limit //= $NO_LIMIT if defined $offset;
Branch: 2 branches
BranchTrueFalseTotal
if defined $offset 6 375 381
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
223 0 ▶ $sql .= " LIMIT $limit" if defined $limit;
Branch: 2 branches
BranchTrueFalseTotal
if defined $limit 46 335 381
if defined $limit 0 0 0
224 0 ▶ $sql .= " OFFSET $offset" if defined $offset;
Branch: 2 branches
BranchTrueFalseTotal
if defined $offset 6 375 381
if defined $offset 0 0 0
225  
226 0 return ($sql, $binds);
227 }
228  
229 23 ▶sub count {
Subroutine count: called CC=1 SCAR=1.2
Pod documented
230 0 ▶ my $self = shift;
Subroutine count: not called CC=1 SCAR=1.2
Pod documented
231 0 my ($source, $dialect, %request) = @_;
232 0 $self->_check(count => \%WHERE, \%request);
233 0 return $self->_from('SELECT COUNT(*) AS count FROM', $source, $dialect, %request);
234 }
235  
236 16 ▶sub update {
Subroutine update: called CC=4 SCAR=18.5
Pod documented
237 0 ▶ my $self = shift;
Subroutine update: not called CC=4 SCAR=18.5
Pod documented
238 0 my ($source, $dialect, $values, %request) = @_;
239 0 $self->_check(update => \%WHERE, \%request);
240 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 15 16
unless ref $values eq "HASH" and %$values 0 0 0
Condition33%2/6
ref $values eq "HASH" and %$values
!ll&&!rl&&r
0115
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
241  
242 0 my (@set, @binds);
243 0 for my $field (sort keys %$values) {
244 0 my ($sql, @value_binds) = $self->_value($field, $values->{$field});
245 0 push @set => $dialect->quote_identifier($field) . " = $sql";
246 0 push @binds => @value_binds;
247 }
248  
249 0 my ($from, $from_binds) = $source->from_sql($dialect);
250 0 my ($where, $where_binds) = $self->_where_clause($source, $dialect, @request{qw/where where_builders/});
251 0 return ("UPDATE $from SET " . join(', ' => @set) . $where, [@$from_binds, @binds, @$where_binds]);
252 }
253  
254 21 ▶sub delete {
Subroutine delete: called CC=1 SCAR=1.2
Pod documented
255 0 ▶ my $self = shift;
Subroutine delete: not called CC=1 SCAR=1.2
Pod documented
256 0 my ($source, $dialect, %request) = @_;
257 0 $self->_check(delete => \%WHERE, \%request);
258 0 return $self->_from('DELETE FROM', $source, $dialect, %request);
259 }
260  
261 42 ▶sub combine_where {
Subroutine combine_where: called CC=8 SCAR=24.3
Pod documented
262 0 ▶ my $self = shift;
Subroutine combine_where: not called CC=8 SCAR=24.3
Pod documented
263 0 my %params = @_;
264 0 $self->_check(combine_where => \%COMBINE, \%params);
265  
266 0 my ($op, $left, $right) = @params{qw/op left right/};
267 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
268  
269 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
270 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
271 0 ▶ return $right if $empty->($left);
Branch: 2 branches
BranchTrueFalseTotal
if &$empty($left) 2 36 38
if &$empty($left) 0 0 0
272 0 ▶ return $left if $empty->($right);
Branch: 2 branches
BranchTrueFalseTotal
if &$empty($right) 2 34 36
if &$empty($right) 0 0 0
273 0 return {"-$op" => [map { $self->_group($_) } $left, $right]};
274 }
275  
276 =pod
277  
278 =over 4
279  
280 =item $sqla = $builder->sqla($dialect)
281  
282 The L<SQL::Abstract> for the dialect's quote character, made on first use.
283  
284 =back
285  
286 =cut
287  
288 831 ▶sub sqla {
Subroutine sqla: called CC=2 SCAR=7.0
Pod documented
289 831 my $self = shift;
290 831 my ($dialect) = @_;
291 831 my $q = $dialect->quote_char;
292 831 ▶ 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
736950
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
293 }
294  
295 =pod
296  
297 =over 4
298  
299 =item $where = $builder->convert_where($where)
300  
301 A copy of C<$where> with every value marker, at any depth, in the form
302 L<SQL::Abstract> takes.
303  
304 use DBIx::QuickORM::Value qw/sql inflated raw/;
305  
306 my $where = $builder->convert_where({
307 name => sql('= lower(?)', 'BOB'),
308 email => inflated(undef),
309 id => raw(5),
310 });
311 # {
312 # name => \['= lower(?)', [undef, 'BOB']],
313 # email => undef,
314 # id => {-value => raw(5)},
315 # }
316  
317 An C<sql> marker becomes literal SQL, its binds tied to no field. Any other
318 marker becomes C<< {-value => $marker} >>, a bind of the field it is under.
319  
320 A marker holding undef becomes a plain undef, so it gives C<IS NULL> (or
321 C<IS NOT NULL> under C<!=>) as a plain undef does.
322  
323 =back
324  
325 =cut
326  
327 1690 ▶sub convert_where {
Subroutine convert_where: called CC=9 SCAR=22.0
Pod documented
328 1690 my $self = shift;
329 1690 my ($where) = @_;
330 1690 ▶ my $type = ref($where) or return $where;
Branch: 1 branch
BranchTrueFalseTotal
my $type = ref $where or return $where 600 1090 1690
331  
332 600 ▶ if (blessed($where)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($where)) 63 537 600
333 63 ▶ return $where unless $where->isa('DBIx::QuickORM::Value');
Branch: 1 branch
BranchTrueFalseTotal
unless $where->isa("DBIx::QuickORM::Value") 0 63 63
334 63 ▶ 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 55 63
Condition100%3/3
$where->kind eq "sql" or defined $where->value
l!l&&r!l&&!r
29268
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
335 55 ▶ return {-value => $where} unless $where->kind eq 'sql';
Branch: 1 branch
BranchTrueFalseTotal
unless $where->kind eq "sql" 26 29 55
336 29 return \[$where->value, map { [undef, $_] } @{$where->binds}];
337 }
338  
339 537 ▶ return {map { ($_ => $self->convert_where($where->{$_})) } keys %$where} if $type eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "HASH" 484 53 537
340 53 ▶ return [map { $self->convert_where($_) } @$where] if $type eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ARRAY" 35 18 53
341 18 return $where;
342 }
343  
344 =head1 PRIVATE METHODS
345  
346 =over 4
347  
348 =item $builder->_check($method, \%known, \%request)
349  
350 Croaks naming every request key not in C<%known>.
351  
352 =back
353  
354 =cut
355  
356 496 ▶sub _check {
Subroutine _check: called CC=2 SCAR=6.9
357 496 my $self = shift;
358 496 my ($method, $known, $request) = @_;
359 496 my @unknown = sort grep { !$known->{$_} } keys %$request;
360 496 ▶ croak "Unknown request key(s) for $method(): " . join(', ' => @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 3 493 496
361 }
362  
363 =pod
364  
365 =over 4
366  
367 =item ($sql, \@binds) = $builder->_from($prefix, $source, $dialect, where => $where, where_builders => \@chain, group_by => \@fields, having => $having, order_by => $order, computed => \%computed)
368  
369 C<$prefix>, the source's C<FROM> part, then the C<WHERE>, C<GROUP BY>,
370 C<HAVING>, and C<ORDER BY> for the clauses given. With C<computed> (from
371 C<_having_computed>), the C<HAVING> writes those names as their SQL.
372  
373 =back
374  
375 =cut
376  
377 429 ▶sub _from {
Subroutine _from: called CC=3 SCAR=11.0
378 429 my $self = shift;
379 429 my ($prefix, $source, $dialect, %clauses) = @_;
380  
381 429 my $sqla = $self->sqla($dialect);
382 429 my ($from, $from_binds) = $source->from_sql($dialect);
383 428 my ($where, $where_binds) = $self->_where_clause($source, $dialect, @clauses{qw/where where_builders/});
384 426 my $sql = "$prefix $from$where";
385  
386 426 ▶ $sql .= ' GROUP BY ' . join(', ' => map { $self->_name($dialect, $_) } @{$clauses{group_by}}) if $clauses{group_by};
Branch: 1 branch
BranchTrueFalseTotal
if $clauses{"group_by"} 42 384 426
387  
388 426 ▶ my $having_sqla = $clauses{computed} ? $self->_computed_sqla($dialect, $clauses{computed}) : $sqla;
Branch: 1 branch
BranchTrueFalseTotal
$clauses{'computed'} ? : 15 411 426
389 426 my ($having, @having_binds) = $having_sqla->where($self->convert_where($clauses{having}));
390 426 $having =~ s/^ WHERE / HAVING /;
391 426 my ($order, @order_binds) = $sqla->where(undef, $clauses{order_by});
392  
393 426 return ("$sql$having$order", [@$from_binds, @$where_binds, @having_binds, @order_binds]);
394 }
395  
396 =pod
397  
398 =over 4
399  
400 =item ($sql, \@binds) = $builder->_where_clause($source, $dialect, $where, \@chain)
401  
402 The C<WHERE> clause, with a leading space, or an empty string, from the first
403 where builder in C<@chain> that can build it, or
404 L<DBIx::QuickORM::WhereBuilder::SQLAbstract> without a chain. Croaks when
405 none can. An undef or empty-string C<where> asks no builder.
406  
407 =back
408  
409 =cut
410  
411 262 my @DEFAULT_CHAIN = ('DBIx::QuickORM::WhereBuilder::SQLAbstract');
412  
413 443 ▶sub _where_clause {
Subroutine _where_clause: called CC=9 SCAR=22.0
414 443 my $self = shift;
415 443 my ($source, $dialect, $where, $chain) = @_;
416 443 ▶ return ('', []) unless defined($where) && (ref($where) || length($where));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $where and ref $where || length $where 162 281 443
Condition100%6/6
defined $where and ref $where || length $where
!ll&&!rl&&r
1602281
ref $where or length $where
l!l&&r!l&&!r
27742
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
417  
418 281 ▶ for my $builder (@{$chain // \@DEFAULT_CHAIN}) {
Condition100%2/2
$chain // \@DEFAULT_CHAIN
l!l
25724
MC/DC100%1/1
$chain // \@DEFAULT_CHAIN
$chain
Truth table100%2/2
A
A= $chain
Aresult
11
00
419 597 ▶ next unless $builder->can_build_where($where);
Branch: 1 branch
BranchTrueFalseTotal
unless $builder->can_build_where($where) 318 279 597
420 279 my ($sql, $binds) = $builder->build_where($where, $source, $dialect);
421 279 ▶ return (length($sql) ? " WHERE $sql" : '', $binds);
Branch: 1 branch
BranchTrueFalseTotal
length $sql ? : 277 2 279
422 }
423  
424 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
425 }
426  
427 =pod
428  
429 =over 4
430  
431 =item ($sql, \@binds) = $builder->_fields($dialect, $fields)
432  
433 The select list: C<*> for undef, or the names of an arrayref quoted as
434 C<_name> does. For a hashref, each expression (a field name, an C<sql()>
435 marker, or C<FUNC(field)> from C<[$func, $field]>, the field C<*> left
436 unquoted) C<AS> its quoted name, sorted by name.
437  
438 =back
439  
440 =cut
441  
442 386 ▶sub _fields {
Subroutine _fields: called CC=4 SCAR=13.9
443 386 my $self = shift;
444 386 my ($dialect, $fields) = @_;
445  
446 386 ▶ return ('*', []) unless $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields 31 355 386
447 355 ▶ return (join(', ' => map { $self->_name($dialect, $_) } @$fields), []) if ref($fields) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $fields eq "ARRAY" 273 82 355
448  
449 82 my (@sql, @binds);
450 82 for my $name (sort keys %$fields) {
451 256 my ($sql, @expr_binds) = $self->_expr($dialect, $fields->{$name});
452 256 push @sql => "$sql AS " . $dialect->quote_identifier($name);
453 256 push @binds => @expr_binds;
454 }
455  
456 82 return (join(', ' => @sql), \@binds);
457 }
458  
459 =pod
460  
461 =over 4
462  
463 =item ($sql, @binds) = $builder->_expr($dialect, $expr)
464  
465 One computed column's SQL: a field name, an C<sql()> marker, or
466 C<FUNC(field)> from C<[$func, $field]>, the field C<*> left unquoted.
467  
468 =back
469  
470 =cut
471  
472 286 ▶sub _expr {
Subroutine _expr: called CC=4 SCAR=13.9
473 286 my $self = shift;
474 286 my ($dialect, $expr) = @_;
475 286 ▶ return $self->_value(undef, $expr) if $self->_is_sql($expr);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_sql($expr) 40 246 286
476 246 ▶ 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 207 246
$expr->[1] eq '*' ? : 17 22 39
477 207 return ($self->_name($dialect, $expr));
478 }
479  
480 =pod
481  
482 =over 4
483  
484 =item \%computed = $builder->_having_computed($dialect, $fields, $having)
485  
486 Each computed column's SQL and binds, C<< {$name => [$sql, @binds]} >>, an
487 C<sql()> column's in parentheses, for a C<having> to name. Undef when there
488 is no C<having>, no computed columns, or the dialect's C<having_uses_alias>
489 is true.
490  
491 =item $sqla = $builder->_computed_sqla($dialect, \%computed)
492  
493 An L<SQL::Abstract> like C<sqla>'s that writes each name in C<%computed> as
494 that column's SQL.
495  
496 =back
497  
498 =cut
499  
500 386 ▶sub _having_computed {
Subroutine _having_computed: called CC=7 SCAR=19.5
501 386 my $self = shift;
502 386 my ($dialect, $fields, $having) = @_;
503 386 ▶ return undef unless defined($having) && ref($fields) eq 'HASH' && %$fields;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $having and ref $fields eq "HASH" and %$fields 370 16 386
Condition83%5/6
defined $having and ref $fields eq "HASH" and %$fields
!ll&&!rl&&r
370016
defined $having and ref $fields eq "HASH"
!ll&&!rl&&r
369116
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
504 16 ▶ return undef if $dialect->having_uses_alias;
Branch: 1 branch
BranchTrueFalseTotal
if $dialect->having_uses_alias 1 15 16
505 15 my %computed;
506 15 for my $name (keys %$fields) {
507 30 my ($sql, @binds) = $self->_expr($dialect, $fields->{$name});
508 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
509 30 $computed{$name} = [$sql, @binds];
510 }
511 15 return \%computed;
512 }
513  
514 15 ▶sub _computed_sqla {
Subroutine _computed_sqla: called CC=1 SCAR=0.0
515 15 my $self = shift;
516 15 my ($dialect, $computed) = @_;
517  
518 15 my $sqla = SQL::Abstract->new(quote_char => $dialect->quote_char, name_sep => '.', bindtype => 'columns');
519 $sqla->wrap_expander(
520 15 ▶ ident => sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
521 15 my ($orig) = @_;
522 11 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
523 11 my ($sqla, $type, $ident) = @_;
524 11 ▶ my $name = ref($ident) ? join('.' => @$ident) : $ident;
Branch: 1 branch
BranchTrueFalseTotal
ref $ident ? : 0 11 11
525 11 ▶ if (my $expr = $computed->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expr = $computed->{$name}) 10 1 11
526 10 return {-literal => [@$expr]}; # the SQL, then its binds, already [undef, $value] pairs
527 }
528 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
529 15 };
530 },
531 15 );
532  
533 15 return $sqla;
534 }
535  
536 =pod
537  
538 =over 4
539  
540 =item $sql = $builder->_name($dialect, $name)
541  
542 A field name quoted through the dialect, a dot separating a qualifier.
543  
544 =back
545  
546 =cut
547  
548 978 ▶sub _name {
Subroutine _name: called CC=1 SCAR=0.0
549 978 my $self = shift;
550 978 my ($dialect, $name) = @_;
551 978 return join '.' => map { $dialect->quote_identifier($_) } split m/\./, $name, -1;
552 }
553  
554 =pod
555  
556 =over 4
557  
558 =item $n = $builder->_count_arg($name, $value)
559  
560 The value, or undef for undef. Croaks unless it is a non-negative integer.
561  
562 =back
563  
564 =cut
565  
566 765 ▶sub _count_arg {
Subroutine _count_arg: called CC=3 SCAR=11.0
567 765 my $self = shift;
568 765 my ($name, $value) = @_;
569 765 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 714 51 765
570 51 ▶ croak "'$name' must be a non-negative integer, got '$value'" unless $value =~ m/^\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\d+\z/ 2 49 51
571 49 return $value;
572 }
573  
574 =pod
575  
576 =over 4
577  
578 =item ($sql, @binds) = $builder->_value($field, $value)
579  
580 C<?> and the bind for one value, or an C<sql> marker's SQL and binds.
581  
582 =back
583  
584 =cut
585  
586 59 ▶sub _value {
Subroutine _value: called CC=2 SCAR=6.9
587 59 my $self = shift;
588 59 my ($field, $value) = @_;
589 59 ▶ return ('?', [$field => $value]) unless $self->_is_sql($value);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_sql($value) 16 43 59
590 43 return ($value->value, map { [undef, $_] } @{$value->binds});
591 }
592  
593 =pod
594  
595 =over 4
596  
597 =item $bool = $builder->_is_sql($value)
598  
599 True for an C<sql> marker.
600  
601 =back
602  
603 =cut
604  
605 431 ▶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' ? : 97 334 431
Condition83%5/6
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value") and $_[1]->kind eq "sql"
!ll&&!rl&&r
332297
builtin::blessed($_[1]) and $_[1]->isa("DBIx::QuickORM::Value")
!ll&&!rl&&r
332099
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
606  
607 =pod
608  
609 =over 4
610  
611 =item $side = $builder->_group($side)
612  
613 A side of C<combine_where> in parentheses when the whole side is literal SQL:
614 a plain string, an C<sql> marker, or C<< \[$sql, @binds] >>. Any other side
615 is returned as it is. Nothing inside a side is touched.
616  
617 =back
618  
619 =cut
620  
621 68 ▶sub _group {
Subroutine _group: called CC=5 SCAR=16.1
622 68 my $self = shift;
623 68 my ($side) = @_;
624 68 ▶ return \["( $side )"] unless ref($side);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $side 12 56 68
625 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
626 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
627 2 my ($sql, @binds) = @$$side;
628 2 return \["( $sql )", @binds];
629 }
630  
631 262 1;
632  
633 __END__
« blib/lib/DBIx/QuickORM/Source/Table/Temp.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Query.pm »