Wizard/Renderer.pm

filter: Statementstmt 92.4% 575 / 622 Branchbran 81.9% 218 / 266 Conditioncond 62.2% 51 / 82 MC/DCmcdc 38.8% 21 / 54 Subroutinesub 100% 35 / 35 Podpod 0.0% 0 / 2 totaltotal 84.8% 900 / 1061 CC 193 SCAR 28.0
CC
193
Cov
87%
_render_select
34.1
_render_compound
25.7
_render_where_value
22.3
CRAP 16.5
↑ Summary Wizard/Expr/Select.pm »
1 package SQL::Wizard::Renderer;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 my $INJECTION_GUARD = qr/
9 \;
10 |
11 ^ \s* go \s
12 /xmi;
13  
14 14 my %VALID_OPS = map { $_ => 1 }
15 '=', '!=', '<>', '<', '>', '<=', '>=',
16 'LIKE', 'NOT LIKE', 'ILIKE', 'NOT ILIKE',
17 '-IN', '-NOT_IN';
18  
19 sub new {
20 14 ▶ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
21 14 bless \%args, $class;
22 }
23  
24 # Combined reserved words (PostgreSQL + MySQL + ANSI SQL)
25 14 my %RESERVED = map { $_ => 1 } qw(
26 ACCESSIBLE ADD ALL ALTER ANALYZE AND ANY ARRAY AS ASC ASENSITIVE ASYMMETRIC
27 BEFORE BETWEEN BIGINT BINARY BLOB BOTH BY
28 CALL CASCADE CASE CAST CHANGE CHAR CHARACTER CHECK COLLATE COLUMN CONCURRENTLY
29 CONDITION CONSTRAINT CONTINUE CONVERT CREATE CROSS CUBE CUME_DIST CURRENT_DATE
30 CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR
31 DATABASE DATABASES DAY_HOUR DAY_MICROSECOND DAY_MINUTE DAY_SECOND DEC DECIMAL
32 DECLARE DEFAULT DEFERRABLE DELAYED DELETE DENSE_RANK DESC DESCRIBE DETERMINISTIC
33 DISTINCT DISTINCTROW DIV DO DOUBLE DROP DUAL
34 EACH ELSE ELSEIF EMPTY ENCLOSED END ESCAPED EXCEPT EXISTS EXIT EXPLAIN
35 FALSE FETCH FIRST_VALUE FLOAT FLOAT4 FLOAT8 FOR FORCE FOREIGN FREEZE FROM
36 FULL FULLTEXT FUNCTION
37 GENERATED GET GRANT GROUP GROUPING GROUPS
38 HAVING HIGH_PRIORITY HOUR_MICROSECOND HOUR_MINUTE HOUR_SECOND
39 IF IGNORE IN INDEX INFILE INITIALLY INNER INOUT INSENSITIVE INSERT INT INT1
40 INT2 INT3 INT4 INT8 INTEGER INTERSECT INTERVAL INTO IO_AFTER_GTIDS
41 IO_BEFORE_GTIDS IS ISNULL ITERATE
42 JOIN JSON_TABLE
43 KEY KEYS KILL
44 LAG LAST_VALUE LATERAL LEAD LEADING LEAVE LEFT LIKE LIMIT LINEAR LINES LOAD
45 LOCALTIME LOCALTIMESTAMP LOCK LONG LONGBLOB LONGTEXT LOOP LOW_PRIORITY
46 MASTER_BIND MASTER_SSL_VERIFY_SERVER_CERT MATCH MAXVALUE MEDIUMBLOB MEDIUMINT
47 MEDIUMTEXT MIDDLEINT MOD MODIFIES
48 NATURAL NOT NOTNULL NO_WRITE_TO_BINLOG NTH_VALUE NTILE NULL NUMERIC
49 OF ON ONLY OPTIMIZE OPTIMIZER_COSTS OPTION OPTIONALLY OR ORDER OUT OUTER
50 OUTFILE OVER OVERLAPS
51 PARTITION PRECISION PRIMARY PROCEDURE PURGE
52 RANGE READ READS READ_WRITE REAL RECURSIVE REFERENCES REGEXP RELEASE RENAME
53 REPEAT REPLACE REQUIRE RESIGNAL RESTRICT RETURN REVOKE RIGHT RLIKE ROW ROWS
54 ROW_NUMBER
55 SCHEMA SCHEMAS SELECT SENSITIVE SEPARATOR SET SHOW SIGNAL SIMILAR SOME SPATIAL
56 SPECIFIC SQL SQLEXCEPTION SQLSTATE SQLWARNING SQL_BIG_RESULT SQL_CALC_FOUND_ROWS
57 SQL_SMALL_RESULT SSL STARTING STORED STRAIGHT_JOIN SYMMETRIC SYSTEM
58 TABLE TABLESAMPLE TERMINATED THEN TINYBLOB TINYINT TINYTEXT TO TRAILING
59 TRIGGER TRUE
60 UNDO UNION UNIQUE UNLOCK UNSIGNED UPDATE USAGE USE USING UTC_DATE UTC_TIME
61 UTC_TIMESTAMP
62 VALUES VARBINARY VARCHAR VARCHARACTER VARIADIC VARYING VERBOSE VIRTUAL
63 WHEN WHERE WHILE WINDOW WITH WRITE
64 XOR YEAR_MONTH ZEROFILL
65 );
66  
67 sub _needs_quoting {
68 674 ▶ my ($self, $part) = @_;
Subroutine _needs_quoting: called CC=5 SCAR=16.2
69 674 ▶ return 0 if $part eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $part eq "*" 99 575 674
70 575 ▶ return 1 if $RESERVED{uc $part};
Branch: 1 branch
BranchTrueFalseTotal
if $RESERVED{uc $part} 3 572 575
71 572 ▶ return 1 if $part =~ /[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $part =~ /[A-Z]/ 0 572 572
72 572 ▶ return 1 if $part =~ /[^a-z0-9_]/;
Branch: 1 branch
BranchTrueFalseTotal
if $part =~ /[^a-z0-9_]/ 0 572 572
73 572 return 0;
74 }
75  
76 sub _quote_ident {
77 3 ▶ my ($self, $name) = @_;
Subroutine _quote_ident: called CC=4 SCAR=14.9
78 3 ▶ my $q = ($self->{dialect} || 'ansi') eq 'mysql' ? '`' : '"';
Branch: 1 branch
BranchTrueFalseTotal
($self->{'dialect'} || 'ansi') eq 'mysql' ? : 0 3 3
Condition50%1/2
$self->{'dialect'} || 'ansi'
l!l
30
MC/DC0%0/1
$self->{'dialect'} || 'ansi'
$self->{'dialect'}
Truth table50%1/2
A
A= $self->{'dialect'}
Aresult
11
00
79 return join('.', map {
80 3 ▶ $_ eq '*' ? $_ : $q . (s/\Q$q\E/$q$q/gr) . $q
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '*' ? : 0 3 3
81 } split /\./, $name, -1);
82 }
83  
84 sub _quote_ident_if_needed {
85 634 ▶ my ($self, $name) = @_;
Subroutine _quote_ident_if_needed: called CC=2 SCAR=6.9
86 634 my @parts = split /\./, $name, -1;
87 634 ▶ return $name unless grep { $self->_needs_quoting($_) } @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$self->_needs_quoting($_);} @parts 631 3 634
88 3 return $self->_quote_ident($name);
89 }
90  
91 sub _injection_guard {
92 321 ▶ my ($self, $string) = @_;
Subroutine _injection_guard: called CC=2 SCAR=8.1
93 321 ▶ if ($string =~ $INJECTION_GUARD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$INJECTION_GUARD/mix) 0 321 321
94 0 confess "Possible SQL injection attempt '$string'. "
95 . "If this is indeed a part of the desired SQL, use raw()";
96 }
97 }
98  
99 sub _assert_column {
100 28 ▶ my ($self, $col) = @_;
Subroutine _assert_column: called CC=2 SCAR=7.2
101 28 ▶ confess "Invalid column name '$col'"
Branch: 1 branch
BranchTrueFalseTotal
unless $col =~ /^(\w+\.)*(\w+|\*)$/ 0 28 28
102 unless $col =~ /^(\w+\.)*(\w+|\*)$/;
103 }
104  
105 sub _assert_order_column {
106 14 ▶ my ($self, $col) = @_;
Subroutine _assert_order_column: called CC=2 SCAR=7.2
107 14 ▶ confess "Invalid order_by column '$col'"
Branch: 1 branch
BranchTrueFalseTotal
unless $col =~ /^(\w+\.)*\w+$/ 0 14 14
108 unless $col =~ /^(\w+\.)*\w+$/;
109 }
110  
111 sub _assert_integer {
112 9 ▶ my ($self, $name, $value) = @_;
Subroutine _assert_integer: called CC=2 SCAR=7.2
113 9 ▶ confess "$name must be an integer, got '$value'"
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\d+$/ 0 9 9
114 unless $value =~ /^\d+$/;
115 }
116  
117 # Main dispatch
118 sub render {
119 429 ▶ my ($self, $node) = @_;
Subroutine render: called CC=2 SCAR=7.0
Pod undocumented
120 429 my $type = ref $node;
121  
122 429 my %dispatch = (
123 'SQL::Wizard::Expr::Column' => \&_render_column,
124 'SQL::Wizard::Expr::Value' => \&_render_value,
125 'SQL::Wizard::Expr::Raw' => \&_render_raw,
126 'SQL::Wizard::Expr::Alias' => \&_render_alias,
127 'SQL::Wizard::Expr::Order' => \&_render_order,
128 'SQL::Wizard::Expr::Func' => \&_render_func,
129 'SQL::Wizard::Expr::BinaryOp' => \&_render_binop,
130 'SQL::Wizard::Expr::Select' => \&_render_select,
131 'SQL::Wizard::Expr::Join' => \&_render_join,
132 'SQL::Wizard::Expr::Case' => \&_render_case,
133 'SQL::Wizard::Expr::Window' => \&_render_window,
134 'SQL::Wizard::Expr::Compound' => \&_render_compound,
135 'SQL::Wizard::Expr::CTE' => \&_render_cte,
136 'SQL::Wizard::Expr::Insert' => \&_render_insert,
137 'SQL::Wizard::Expr::Update' => \&_render_update,
138 'SQL::Wizard::Expr::Delete' => \&_render_delete,
139 );
140  
141 429 ▶ my $handler = $dispatch{$type}
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $dispatch{$type} or croak("No renderer for node type: $type") 0 429 429
142 or croak "No renderer for node type: $type";
143 429 $handler->($self, $node);
144 }
145  
146 # Render any expression or plain string (column name)
147 sub _render_expr {
148 284 ▶ my ($self, $thing) = @_;
Subroutine _render_expr: called CC=4 SCAR=14.0
149 284 ▶ return ('', ()) unless defined $thing;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thing 0 284 284
150 284 ▶ if (blessed($thing) && $thing->isa('SQL::Wizard::Expr')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thing) and $thing->isa("SQL::Wizard::Expr")) 99 185 284
Condition66%2/3
builtin::blessed($thing) and $thing->isa("SQL::Wizard::Expr")
!ll&&!rl&&r
185099
MC/DC50%1/2
builtin::blessed($thing) and $thing->isa("SQL::Wizard::Expr")
builtin::blessed($thing) $thing->isa("SQL::Wizard::Expr")
Truth table66%2/3
A and B
A= builtin::blessed($thing)B= $thing->isa("SQL::Wizard::Expr")
ABresult
0X0
100
111
151 99 return $self->render($thing);
152 }
153 # Plain string = column name
154 185 $self->_injection_guard($thing);
155 185 return ($self->_quote_ident_if_needed($thing), ());
156 }
157  
158 # table|alias => table alias
159 sub _expand_table {
160 166 ▶ my ($self, $thing) = @_;
Subroutine _expand_table: called CC=5 SCAR=16.2
161 166 ▶ if (blessed($thing) && $thing->isa('SQL::Wizard::Expr')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thing) and $thing->isa("SQL::Wizard::Expr")) 3 163 166
Condition66%2/3
builtin::blessed($thing) and $thing->isa("SQL::Wizard::Expr")
!ll&&!rl&&r
16303
MC/DC50%1/2
builtin::blessed($thing) and $thing->isa("SQL::Wizard::Expr")
builtin::blessed($thing) $thing->isa("SQL::Wizard::Expr")
Truth table66%2/3
A and B
A= builtin::blessed($thing)B= $thing->isa("SQL::Wizard::Expr")
ABresult
0X0
100
111
162 3 return $self->render($thing);
163 }
164 163 ▶ confess "Invalid table name '$thing'"
Branch: 1 branch
BranchTrueFalseTotal
unless $thing =~ /^(\w+\.)*\w+(\|\w+)?$/ 0 163 163
165 unless $thing =~ /^(\w+\.)*\w+(\|\w+)?$/;
166 163 my ($table, $alias) = split /\|/, $thing, 2;
167 163 my $qt = $self->_quote_ident_if_needed($table);
168 163 ▶ return $alias ? ("$qt " . $self->_quote_ident_if_needed($alias), ()) : ($qt, ());
Branch: 1 branch
BranchTrueFalseTotal
$alias ? : 30 133 163
169 }
170  
171 ## Leaf renderers
172  
173 sub _render_column {
174 71 ▶ my ($self, $node) = @_;
Subroutine _render_column: called CC=1 SCAR=0.0
175 71 return ($self->_quote_ident_if_needed($node->{name}), ());
176 }
177  
178 sub _render_value {
179 48 ▶ my ($self, $node) = @_;
Subroutine _render_value: called CC=1 SCAR=0.0
180 48 return ('?', $node->{value});
181 }
182  
183 sub _render_raw {
184 22 ▶ my ($self, $node) = @_;
Subroutine _render_raw: called CC=18 SCAR=29.4
185  
186 # COMPARE
187 22 ▶ if ($node->{_compare}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"_compare"}) 4 18 22
188 4 my $c = $node->{_compare};
189 4 my $sql_op = uc($c->{op});
190 confess "Unknown operator '$c->{op}' in compare()"
191 4 ▶ unless $VALID_OPS{$sql_op};
Branch: 1 branch
BranchTrueFalseTotal
unless $VALID_OPS{$sql_op} 0 4 4
192 4 my ($ls, @lb) = $self->render($c->{left});
193 4 my ($rs, @rb) = $self->render($c->{right});
194 4 ▶ $ls = "($ls)" if $c->{left}->isa('SQL::Wizard::Expr::Select');
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"left"}->isa("SQL::Wizard::Expr::Select") 2 2 4
195 4 ▶ $rs = "($rs)" if $c->{right}->isa('SQL::Wizard::Expr::Select');
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"right"}->isa("SQL::Wizard::Expr::Select") 0 4 4
196 4 return ("$ls $sql_op $rs", @lb, @rb);
197 }
198  
199 # TRUNCATE
200 18 ▶ if ($node->{_truncate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"_truncate"}) 0 18 18
201 0 my $table = $node->{_truncate};
202 0 ▶ confess "Invalid table name '$table'"
Branch: 1 branch
BranchTrueFalseTotal
unless $table =~ /^(\w+\.)*\w+$/ 0 0 0
203 unless $table =~ /^(\w+\.)*\w+$/;
204 0 return ("TRUNCATE TABLE " . $self->_quote_ident_if_needed($table), ());
205 }
206  
207 # EXISTS / NOT EXISTS
208 18 ▶ if ($node->{_subquery}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"_subquery"}) 7 11 18
209 7 my ($s, @b) = $self->render($node->{_subquery});
210 7 return ("$node->{sql}($s)", @b);
211 }
212  
213 # BETWEEN / NOT BETWEEN
214 11 ▶ if ($node->{_between} || $node->{_not_between}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"_between"} or $node->{"_not_between"}) 2 9 11
Condition100%3/3
$node->{"_between"} or $node->{"_not_between"}
l!l&&r!l&&!r
119
MC/DC100%2/2
$node->{"_between"} or $node->{"_not_between"}
$node->{"_between"} $node->{"_not_between"}
Truth table100%3/3
A or B
A= $node->{"_between"}B= $node->{"_not_between"}
ABresult
1X1
011
000
215 2 ▶ my $spec = $node->{_between} || $node->{_not_between};
Condition66%2/3
$node->{'_between'} || $node->{'_not_between'}
l!l&&r!l&&!r
110
MC/DC0%0/2
$node->{'_between'} || $node->{'_not_between'}
$node->{'_between'} $node->{'_not_between'}
Truth table66%2/3
A || B
A= $node->{'_between'}B= $node->{'_not_between'}
ABresult
1X1
011
000
216 2 ▶ my $op = $node->{_between} ? 'BETWEEN' : 'NOT BETWEEN';
Branch: 1 branch
BranchTrueFalseTotal
$node->{'_between'} ? : 1 1 2
217 2 my ($cs, @cb) = $self->render($spec->{col});
218 2 my ($ls, @lb) = $self->render($spec->{lo});
219 2 my ($hs, @hb) = $self->render($spec->{hi});
220 2 return ("$cs $op $ls AND $hs", @cb, @lb, @hb);
221 }
222  
223 # CAST
224 9 ▶ if ($node->{_cast}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"_cast"}) 1 8 9
225 1 my $type = $node->{_cast}{type};
226 1 ▶ confess "Invalid CAST type '$type'"
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^\w[\w\s(),]*$/ 0 1 1
227 unless $type =~ /^\w[\w\s(),]*$/;
228 1 my ($es, @eb) = $self->render($node->{_cast}{expr});
229 1 return ("CAST($es AS $type)", @eb);
230 }
231  
232 # AND / OR
233 8 ▶ if ($node->{_logic}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"_logic"}) 2 6 8
234 2 my $op = $node->{_logic}{op};
235 2 my @conds = @{$node->{_logic}{conds}};
236 2 my @parts;
237 my @bind;
238 2 for my $c (@conds) {
239 4 my ($s, @b) = $self->_render_where($c);
240 4 push @parts, $s;
241 4 push @bind, @b;
242 }
243 2 my $joined = join(" $op ", @parts);
244 2 ▶ $joined = "($joined)" if @parts > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parts > 1 2 0 2
245 2 return ($joined, @bind);
246 }
247  
248 # NOT
249 6 ▶ if ($node->{_not}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"_not"}) 1 5 6
250 1 my ($s, @b) = $self->_render_where($node->{_not});
251 1 return ("NOT ($s)", @b);
252 }
253  
254 5 return ($node->{sql}, @{$node->{bind}});
255 }
256  
257 sub _render_alias {
258 28 ▶ my ($self, $node) = @_;
Subroutine _render_alias: called CC=2 SCAR=6.9
259 28 my ($sql, @bind) = $self->render($node->{expr});
260 # Wrap subselects in parens
261 28 ▶ if ($node->{expr}->isa('SQL::Wizard::Expr::Select')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"expr"}->isa("SQL::Wizard::Expr::Select")) 4 24 28
262 4 $sql = "($sql)";
263 }
264 28 return ("$sql AS " . $self->_quote_ident_if_needed($node->{alias}), @bind);
265 }
266  
267 sub _render_order {
268 6 ▶ my ($self, $node) = @_;
Subroutine _render_order: called CC=2 SCAR=6.9
269 6 my ($sql, @bind) = $self->_render_expr($node->{expr});
270 6 $sql .= " $node->{direction}";
271 6 ▶ $sql .= " NULLS $node->{nulls}" if $node->{nulls};
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"nulls"} 2 4 6
272 6 return ($sql, @bind);
273 }
274  
275 sub _render_func {
276 34 ▶ my ($self, $node) = @_;
Subroutine _render_func: called CC=2 SCAR=6.9
277 34 my @arg_sqls;
278 my @bind;
279 34 for my $arg (@{$node->{args}}) {
280 31 my ($s, @b) = $self->_render_expr($arg);
281 31 push @arg_sqls, $s;
282 31 push @bind, @b;
283 }
284 34 my $args_str = join(', ', @arg_sqls);
285 34 return ("$node->{name}($args_str)", @bind);
286 }
287  
288 sub _render_binop {
289 14 ▶ my ($self, $node) = @_;
Subroutine _render_binop: called CC=1 SCAR=0.0
290 14 my ($lsql, @lbind) = $self->render($node->{left});
291 14 my ($rsql, @rbind) = $self->render($node->{right});
292 14 return ("$lsql $node->{op} $rsql", @lbind, @rbind);
293 }
294  
295 ## SELECT
296  
297 sub _render_select {
298 146 ▶ my ($self, $node) = @_;
Subroutine _render_select: called CC=33 SCAR=35.3
299 146 my @parts;
300 my @bind;
301  
302 # CTE
303 146 ▶ if ($node->{_cte}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"_cte"}) 4 142 146
304 4 my ($cte_sql, @cte_bind) = $self->render($node->{_cte});
305 4 push @parts, $cte_sql;
306 4 push @bind, @cte_bind;
307 }
308  
309 # SELECT columns
310 146 my @col_sqls;
311 146 ▶ for my $col (@{$node->{columns} || ['*']}) {
Branch: 1 branch
BranchTrueFalseTotal
$node->{"columns"} or ["*"] 60 86 146
312 178 my ($s, @b) = $self->_render_expr($col);
313 178 push @col_sqls, $s;
314 178 push @bind, @b;
315 }
316 146 ▶ my $select_keyword = $node->{distinct} ? "SELECT DISTINCT" : "SELECT";
Branch: 1 branch
BranchTrueFalseTotal
$node->{'distinct'} ? : 2 144 146
317 146 push @parts, "$select_keyword " . join(', ', @col_sqls);
318  
319 # FROM
320 146 ▶ if ($node->{from}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"from"}) 146 0 146
321 146 my @from_sqls;
322 146 ▶ my @from_items = ref $node->{from} eq 'ARRAY' ? @{$node->{from}} : ($node->{from});
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'from'} eq 'ARRAY' ? : 12 134 146
323 146 for my $i (0 .. $#from_items) {
324 158 my $item = $from_items[$i];
325 158 ▶ if (blessed($item) && $item->isa('SQL::Wizard::Expr::Join')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($item) and $item->isa('SQL::Wizard::Expr::Join')) { } 12 146 158
Condition100%3/3
builtin::blessed($item) and $item->isa("SQL::Wizard::Expr::Join")
!ll&&!rl&&r
145112
MC/DC100%2/2
builtin::blessed($item) and $item->isa("SQL::Wizard::Expr::Join")
builtin::blessed($item) $item->isa("SQL::Wizard::Expr::Join")
Truth table100%3/3
A and B
A= builtin::blessed($item)B= $item->isa("SQL::Wizard::Expr::Join")
ABresult
0X0
100
111
326 12 my ($s, @b) = $self->render($item);
327 12 push @from_sqls, $s;
328 12 push @bind, @b;
329 } else {
330 146 my ($s, @b) = $self->_expand_table($item);
331 # First item or non-join items
332 146 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 146 0 146
333 146 push @from_sqls, $s;
334 } else {
335 0 push @from_sqls, $s;
336 }
337 146 push @bind, @b;
338 }
339 }
340 146 push @parts, "FROM " . join(' ', @from_sqls);
341 }
342  
343 # WHERE
344 146 ▶ if ($node->{where}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"where"}) 78 68 146
345 78 my ($wsql, @wbind) = $self->_render_where($node->{where});
346 77 ▶ if (defined $wsql && $wsql ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wsql and $wsql ne "") 75 2 77
Condition66%2/3
defined $wsql and $wsql ne ""
!ll&&!rl&&r
0275
MC/DC50%1/2
defined $wsql and $wsql ne ""
defined $wsql $wsql ne ""
Truth table66%2/3
A and B
A= defined $wsqlB= $wsql ne ""
ABresult
0X0
100
111
347 75 push @parts, "WHERE $wsql";
348 75 push @bind, @wbind;
349 }
350 }
351  
352 # GROUP BY
353 145 ▶ if ($node->{group_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"group_by"}) 7 138 145
354 7 ▶ my @items = ref $node->{group_by} eq 'ARRAY' ? @{$node->{group_by}} : ($node->{group_by});
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'group_by'} eq 'ARRAY' ? : 0 7 7
355 7 my @gsqls;
356 7 for my $g (@items) {
357 7 my ($s, @b) = $self->_render_expr($g);
358 7 push @gsqls, $s;
359 7 push @bind, @b;
360 }
361 7 push @parts, "GROUP BY " . join(', ', @gsqls);
362 }
363  
364 # HAVING
365 145 ▶ if ($node->{having}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"having"}) 3 142 145
366 3 my ($hsql, @hbind) = $self->_render_where($node->{having});
367 3 ▶ if (defined $hsql && $hsql ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hsql and $hsql ne "") 1 2 3
Condition66%2/3
defined $hsql and $hsql ne ""
!ll&&!rl&&r
021
MC/DC50%1/2
defined $hsql and $hsql ne ""
defined $hsql $hsql ne ""
Truth table66%2/3
A and B
A= defined $hsqlB= $hsql ne ""
ABresult
0X0
100
111
368 1 push @parts, "HAVING $hsql";
369 1 push @bind, @hbind;
370 }
371 }
372  
373 # WINDOW
374 145 ▶ if ($node->{window}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"window"}) 2 143 145
375 2 my @wdefs;
376 2 for my $name (sort keys %{$node->{window}}) {
377 2 ▶ confess "Invalid window name '$name'" unless $name =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^\w+$/ 0 2 2
378 2 my $spec = $node->{window}{$name};
379 2 my ($s, @b) = $self->_render_window_spec($spec);
380 2 push @wdefs, $self->_quote_ident_if_needed($name) . " AS ($s)";
381 2 push @bind, @b;
382 }
383 2 push @parts, "WINDOW " . join(', ', @wdefs);
384 }
385  
386 # ORDER BY
387 145 ▶ if ($node->{order_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"order_by"}) 7 138 145
388 7 ▶ my @items = ref $node->{order_by} eq 'ARRAY' ? @{$node->{order_by}} : ($node->{order_by});
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'order_by'} eq 'ARRAY' ? : 3 4 7
389 7 my @osqls;
390 7 for my $o (@items) {
391 8 ▶ if (ref $o eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $o eq 'HASH') { } 2 6 8
elsif (not ref $o and $o =~ /^-(.+)/) { } 0 6 6
elsif (not ref $o) { } 4 2 6
392 # { -desc => 'col' } or { -asc => 'col' }
393 2 my ($dir, $col) = each %$o;
394 2 $dir = uc($dir);
395 2 $dir =~ s/^-//;
396 2 ▶ $self->_assert_order_column($col) unless ref $col;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $col 2 0 2
397 2 my ($s, @b) = $self->_render_expr($col);
398 2 push @osqls, "$s $dir";
399 2 push @bind, @b;
400 ▶ } elsif (!ref $o && $o =~ /^-(.+)/) {
Condition66%2/3
not ref $o and $o =~ /^-(.+)/
!ll&&!rl&&r
240
MC/DC0%0/2
not ref $o and $o =~ /^-(.+)/
not ref $o $o =~ /^-(.+)/
Truth table66%2/3
not(A) and B
A= not ref $oB= $o =~ /^-(.+)/
ABresult
0X0
100
111
401 # '-col' shorthand for col DESC
402 0 $self->_assert_order_column($1);
403 0 my ($s, @b) = $self->_render_expr($1);
404 0 push @osqls, "$s DESC";
405 0 push @bind, @b;
406 } elsif (!ref $o) {
407 4 $self->_assert_order_column($o);
408 4 my ($s, @b) = $self->_render_expr($o);
409 4 push @osqls, $s;
410 4 push @bind, @b;
411 } else {
412 2 my ($s, @b) = $self->_render_expr($o);
413 2 push @osqls, $s;
414 2 push @bind, @b;
415 }
416 }
417 7 push @parts, "ORDER BY " . join(', ', @osqls);
418 }
419  
420 # LIMIT / OFFSET
421 145 ▶ if (defined $node->{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"limit"}) 4 141 145
422 4 $self->_assert_integer('-limit', $node->{limit});
423 4 push @parts, "LIMIT ?";
424 4 push @bind, $node->{limit};
425 }
426 145 ▶ if (defined $node->{offset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"offset"}) 3 142 145
427 3 $self->_assert_integer('-offset', $node->{offset});
428 3 push @parts, "OFFSET ?";
429 3 push @bind, $node->{offset};
430 }
431  
432 145 return (join(' ', @parts), @bind);
433 }
434  
435 ## JOIN
436  
437 sub _render_join {
438 13 ▶ my ($self, $node) = @_;
Subroutine _render_join: called CC=3 SCAR=11.0
439 13 my @bind;
440  
441 13 my ($table_sql, @tb) = $self->_expand_table($node->{table});
442 13 push @bind, @tb;
443  
444 13 my $sql = "$node->{type} $table_sql";
445  
446 13 ▶ if (defined $node->{on}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"on"}) 12 1 13
447 12 ▶ if (ref $node->{on} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node->{'on'} eq 'HASH') { } 1 11 12
448 1 my ($on_sql, @ob) = $self->_render_where($node->{on});
449 1 $sql .= " ON $on_sql";
450 1 push @bind, @ob;
451 } else {
452 # String ON condition
453 11 $self->_injection_guard($node->{on});
454 11 $sql .= " ON $node->{on}";
455 }
456 }
457  
458 13 return ($sql, @bind);
459 }
460  
461 ## CASE
462  
463 sub _render_case {
464 6 ▶ my ($self, $node) = @_;
Subroutine _render_case: called CC=5 SCAR=16.1
465 6 my @parts;
466 my @bind;
467  
468 6 push @parts, 'CASE';
469  
470 # CASE ON (simple case with operand)
471 6 ▶ if ($node->{operand}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"operand"}) 1 5 6
472 1 my ($os, @ob) = $self->_render_expr($node->{operand});
473 1 $parts[0] .= " $os";
474 1 push @bind, @ob;
475 }
476  
477 6 for my $when (@{$node->{whens}}) {
478 9 ▶ if ($node->{operand}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{'operand'}) { } 2 7 9
479 # Simple CASE: WHEN value THEN result
480 2 my ($ws, @wb) = $self->_render_expr($when->{condition});
481 2 my ($ts, @tb) = $self->_render_expr($when->{then});
482 2 push @parts, "WHEN $ws THEN $ts";
483 2 push @bind, @wb, @tb;
484 } else {
485 # Searched CASE: WHEN condition THEN result
486 7 my ($ws, @wb) = $self->_render_where($when->{condition});
487 7 my ($ts, @tb) = $self->_render_expr($when->{then});
488 7 push @parts, "WHEN $ws THEN $ts";
489 7 push @bind, @wb, @tb;
490 }
491 }
492  
493 6 ▶ if (defined $node->{else}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"else"}) 5 1 6
494 5 my ($es, @eb) = $self->_render_expr($node->{else});
495 5 push @parts, "ELSE $es";
496 5 push @bind, @eb;
497 }
498  
499 6 push @parts, 'END';
500 6 return (join(' ', @parts), @bind);
501 }
502  
503 ## Window
504  
505 sub _render_window {
506 9 ▶ my ($self, $node) = @_;
Subroutine _render_window: called CC=3 SCAR=11.0
507 9 my ($expr_sql, @bind) = $self->render($node->{expr});
508  
509 9 my $spec = $node->{spec};
510 9 ▶ if ($spec->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"name"}) 4 5 9
511 4 ▶ confess "Invalid window name '$spec->{name}'" unless $spec->{name} =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"name"} =~ /^\w+$/ 0 4 4
512 4 return ("$expr_sql OVER " . $self->_quote_ident_if_needed($spec->{name}), @bind);
513 }
514  
515 5 my ($spec_sql, @sb) = $self->_render_window_spec($spec);
516 5 push @bind, @sb;
517 5 return ("$expr_sql OVER ($spec_sql)", @bind);
518 }
519  
520 sub _render_window_spec {
521 7 ▶ my ($self, $spec) = @_;
Subroutine _render_window_spec: called CC=13 SCAR=26.8
522 7 my @parts;
523 my @bind;
524  
525 7 ▶ if ($spec->{'-partition_by'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"-partition_by"}) 6 1 7
526 my @items = ref $spec->{'-partition_by'} eq 'ARRAY'
527 6 ▶ ? @{$spec->{'-partition_by'}} : ($spec->{'-partition_by'});
Branch: 1 branch
BranchTrueFalseTotal
ref $spec->{'-partition_by'} eq 'ARRAY' ? : 1 5 6
528 6 my @sqls;
529 6 for my $p (@items) {
530 7 my ($s, @b) = $self->_render_expr($p);
531 7 push @sqls, $s;
532 7 push @bind, @b;
533 }
534 6 push @parts, "PARTITION BY " . join(', ', @sqls);
535 }
536  
537 7 ▶ if ($spec->{'-order_by'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"-order_by"}) 7 0 7
538 my @items = ref $spec->{'-order_by'} eq 'ARRAY'
539 7 ▶ ? @{$spec->{'-order_by'}} : ($spec->{'-order_by'});
Branch: 1 branch
BranchTrueFalseTotal
ref $spec->{'-order_by'} eq 'ARRAY' ? : 3 4 7
540 7 my @sqls;
541 7 for my $o (@items) {
542 7 ▶ if (ref $o eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $o eq 'HASH') { } 3 4 7
elsif (not ref $o and $o =~ /^-(.+)/) { } 0 4 4
elsif (not ref $o) { } 4 0 4
543 3 my ($dir, $col) = each %$o;
544 3 $dir = uc($dir);
545 3 $dir =~ s/^-//;
546 3 ▶ $self->_assert_order_column($col) unless ref $col;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $col 3 0 3
547 3 my ($s, @b) = $self->_render_expr($col);
548 3 push @sqls, "$s $dir";
549 3 push @bind, @b;
550 ▶ } elsif (!ref $o && $o =~ /^-(.+)/) {
Condition33%1/3
not ref $o and $o =~ /^-(.+)/
!ll&&!rl&&r
040
MC/DC0%0/2
not ref $o and $o =~ /^-(.+)/
not ref $o $o =~ /^-(.+)/
Truth table33%1/3
not(A) and B
A= not ref $oB= $o =~ /^-(.+)/
ABresult
0X0
100
111
551 0 $self->_assert_order_column($1);
552 0 my ($s, @b) = $self->_render_expr($1);
553 0 push @sqls, "$s DESC";
554 0 push @bind, @b;
555 } elsif (!ref $o) {
556 4 $self->_assert_order_column($o);
557 4 my ($s, @b) = $self->_render_expr($o);
558 4 push @sqls, $s;
559 4 push @bind, @b;
560 } else {
561 0 my ($s, @b) = $self->_render_expr($o);
562 0 push @sqls, $s;
563 0 push @bind, @b;
564 }
565 }
566 7 push @parts, "ORDER BY " . join(', ', @sqls);
567 }
568  
569 7 ▶ if ($spec->{'-frame'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"-frame"}) 1 6 7
570 1 $self->_injection_guard($spec->{'-frame'});
571 1 push @parts, $spec->{'-frame'};
572 }
573  
574 7 return (join(' ', @parts), @bind);
575 }
576  
577 ## Compound (UNION/INTERSECT/EXCEPT)
578  
579 sub _render_compound {
580 9 ▶ my ($self, $node) = @_;
Subroutine _render_compound: called CC=13 SCAR=32.5
581 9 my @parts;
582 my @bind;
583  
584 9 for my $entry (@{$node->{queries}}) {
585 20 my ($s, @b) = $self->render($entry->{query});
586 20 ▶ if ($entry->{type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"type"}) 11 9 20
587 11 push @parts, $entry->{type};
588 }
589 20 push @parts, "($s)";
590 20 push @bind, @b;
591 }
592  
593 # ORDER BY / LIMIT / OFFSET on the compound
594 9 ▶ if ($node->{order_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"order_by"}) 1 8 9
595 1 ▶ my @items = ref $node->{order_by} eq 'ARRAY' ? @{$node->{order_by}} : ($node->{order_by});
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'order_by'} eq 'ARRAY' ? : 0 1 1
596 1 my @osqls;
597 1 for my $o (@items) {
598 1 ▶ if (ref $o eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $o eq 'HASH') { } 0 1 1
elsif (not ref $o and $o =~ /^-(.+)/) { } 0 1 1
elsif (not ref $o) { } 1 0 1
599 0 my ($dir, $col) = each %$o;
600 0 $dir = uc($dir);
601 0 $dir =~ s/^-//;
602 0 ▶ $self->_assert_order_column($col) unless ref $col;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $col 0 0 0
603 0 my ($s, @b) = $self->_render_expr($col);
604 0 push @osqls, "$s $dir";
605 0 push @bind, @b;
606 ▶ } elsif (!ref $o && $o =~ /^-(.+)/) {
Condition33%1/3
not ref $o and $o =~ /^-(.+)/
!ll&&!rl&&r
010
MC/DC0%0/2
not ref $o and $o =~ /^-(.+)/
not ref $o $o =~ /^-(.+)/
Truth table33%1/3
not(A) and B
A= not ref $oB= $o =~ /^-(.+)/
ABresult
0X0
100
111
607 0 $self->_assert_order_column($1);
608 0 my ($s, @b) = $self->_render_expr($1);
609 0 push @osqls, "$s DESC";
610 0 push @bind, @b;
611 } elsif (!ref $o) {
612 1 $self->_assert_order_column($o);
613 1 my ($s, @b) = $self->_render_expr($o);
614 1 push @osqls, $s;
615 1 push @bind, @b;
616 } else {
617 0 my ($s, @b) = $self->_render_expr($o);
618 0 push @osqls, $s;
619 0 push @bind, @b;
620 }
621 }
622 1 push @parts, "ORDER BY " . join(', ', @osqls);
623 }
624 9 ▶ if (defined $node->{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"limit"}) 1 8 9
625 1 $self->_assert_integer('-limit', $node->{limit});
626 1 push @parts, "LIMIT ?";
627 1 push @bind, $node->{limit};
628 }
629 9 ▶ if (defined $node->{offset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"offset"}) 0 9 9
630 0 $self->_assert_integer('-offset', $node->{offset});
631 0 push @parts, "OFFSET ?";
632 0 push @bind, $node->{offset};
633 }
634  
635 9 return (join(' ', @parts), @bind);
636 }
637  
638 ## CTE
639  
640 sub _render_cte {
641 4 ▶ my ($self, $node) = @_;
Subroutine _render_cte: called CC=5 SCAR=16.1
642 4 my @parts;
643 my @bind;
644  
645 4 ▶ my $keyword = $node->{recursive} ? 'WITH RECURSIVE' : 'WITH';
Branch: 1 branch
BranchTrueFalseTotal
$node->{'recursive'} ? : 1 3 4
646  
647 4 my @cte_sqls;
648 4 for my $cte (@{$node->{ctes}}) {
649 5 my $name = $cte->{name};
650 5 $self->_injection_guard($name);
651 5 my $query = $cte->{query};
652  
653 # Recursive CTE with -initial and -recurse
654 5 ▶ if (ref $query eq 'HASH' && $query->{'-initial'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $query eq 'HASH' and $query->{'-initial'}) { } 1 4 5
Condition66%2/3
ref $query eq "HASH" and $query->{"-initial"}
!ll&&!rl&&r
401
MC/DC50%1/2
ref $query eq "HASH" and $query->{"-initial"}
ref $query eq "HASH" $query->{"-initial"}
Truth table66%2/3
A and B
A= ref $query eq "HASH"B= $query->{"-initial"}
ABresult
0X0
100
111
655 1 my ($is, @ib) = $self->render($query->{'-initial'});
656 1 my ($rs, @rb) = $self->render($query->{'-recurse'});
657 1 push @cte_sqls, $self->_quote_ident_if_needed($name) . " AS ($is UNION ALL $rs)";
658 1 push @bind, @ib, @rb;
659 } else {
660 4 my ($s, @b) = $self->render($query);
661 4 push @cte_sqls, $self->_quote_ident_if_needed($name) . " AS ($s)";
662 4 push @bind, @b;
663 }
664 }
665  
666 4 return ("$keyword " . join(', ', @cte_sqls), @bind);
667 }
668  
669 ## INSERT
670  
671 sub _render_insert {
672 7 ▶ my ($self, $node) = @_;
Subroutine _render_insert: called CC=18 SCAR=28.9
673 7 my @parts;
674 my @bind;
675  
676 7 $self->_injection_guard($node->{into});
677 7 push @parts, "INSERT INTO " . $self->_quote_ident_if_needed($node->{into});
678  
679 7 ▶ if ($node->{select}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->{'select'}) { } 1 6 7
elsif (ref $node->{'values'} eq 'HASH') { } 5 1 6
elsif (ref $node->{'values'} eq 'ARRAY') { } 1 0 1
680 # INSERT ... SELECT
681 1 ▶ if ($node->{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"columns"}) 1 0 1
682 1 $self->_assert_column($_) for @{$node->{columns}};
683 1 push @parts, "(" . join(', ', map { $self->_quote_ident_if_needed($_) } @{$node->{columns}}) . ")";
684 }
685 1 my ($s, @b) = $self->render($node->{select});
686 1 push @parts, $s;
687 1 push @bind, @b;
688 } elsif (ref $node->{values} eq 'HASH') {
689 # Single row insert from hash
690 5 my @cols = sort keys %{$node->{values}};
691 5 $self->_assert_column($_) for @cols;
692 5 my @vals;
693 5 for my $col (@cols) {
694 9 my $v = $node->{values}{$col};
695 9 my ($s, @b) = $self->_render_expr($v);
696 9 push @vals, $s;
697 9 push @bind, @b;
698 }
699 5 push @parts, "(" . join(', ', map { $self->_quote_ident_if_needed($_) } @cols) . ")";
700 5 push @parts, "VALUES (" . join(', ', @vals) . ")";
701 } elsif (ref $node->{values} eq 'ARRAY') {
702 # Multi-row insert
703 1 ▶ if ($node->{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"columns"}) 1 0 1
704 1 $self->_assert_column($_) for @{$node->{columns}};
705 1 push @parts, "(" . join(', ', map { $self->_quote_ident_if_needed($_) } @{$node->{columns}}) . ")";
706 }
707 1 my @row_sqls;
708 1 for my $row (@{$node->{values}}) {
709 2 my @vals;
710 2 for my $v (@$row) {
711 4 my ($s, @b) = $self->_render_expr($v);
712 4 push @vals, $s;
713 4 push @bind, @b;
714 }
715 2 push @row_sqls, "(" . join(', ', @vals) . ")";
716 }
717 1 push @parts, "VALUES " . join(', ', @row_sqls);
718 }
719  
720 # ON CONFLICT (PostgreSQL)
721 7 ▶ if ($node->{on_conflict}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"on_conflict"}) 1 6 7
722 1 my $oc = $node->{on_conflict};
723 1 my $target = $oc->{'-target'};
724 1 $self->_injection_guard($target);
725 1 my $update = $oc->{'-update'};
726 1 my @set_parts;
727 1 for my $col (sort keys %$update) {
728 1 $self->_assert_column($col);
729 1 my ($s, @b) = $self->_render_expr($update->{$col});
730 1 push @set_parts, $self->_quote_ident_if_needed($col) . " = $s";
731 1 push @bind, @b;
732 }
733 1 my $quoted_target = join(', ', map { $self->_quote_ident_if_needed(s/^\s+|\s+$//gr) } split /,/, $target);
734 1 push @parts, "ON CONFLICT ($quoted_target) DO UPDATE SET " . join(', ', @set_parts);
735 }
736  
737 # ON DUPLICATE KEY (MySQL)
738 7 ▶ if ($node->{on_duplicate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"on_duplicate"}) 1 6 7
739 1 my @set_parts;
740 1 for my $col (sort keys %{$node->{on_duplicate}}) {
741 1 $self->_assert_column($col);
742 1 my ($s, @b) = $self->_render_expr($node->{on_duplicate}{$col});
743 1 push @set_parts, $self->_quote_ident_if_needed($col) . " = $s";
744 1 push @bind, @b;
745 }
746 1 push @parts, "ON DUPLICATE KEY UPDATE " . join(', ', @set_parts);
747 }
748  
749 # RETURNING
750 7 ▶ if ($node->{returning}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"returning"}) 1 6 7
751 1 $self->_assert_column($_) for @{$node->{returning}};
752 1 push @parts, "RETURNING " . join(', ', map { $self->_quote_ident_if_needed($_) } @{$node->{returning}});
753 }
754  
755 7 return (join(' ', @parts), @bind);
756 }
757  
758 ## UPDATE
759  
760 sub _render_update {
761 6 ▶ my ($self, $node) = @_;
Subroutine _render_update: called CC=15 SCAR=27.1
762 6 my @parts;
763 my @bind;
764  
765 # Table (possibly with joins)
766 6 ▶ if (ref $node->{table} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node->{'table'} eq 'ARRAY') { } 1 5 6
767 1 my @table_parts;
768 1 for my $item (@{$node->{table}}) {
769 2 ▶ if (blessed($item) && $item->isa('SQL::Wizard::Expr::Join')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($item) and $item->isa('SQL::Wizard::Expr::Join')) { } 1 1 2
Condition66%2/3
builtin::blessed($item) and $item->isa("SQL::Wizard::Expr::Join")
!ll&&!rl&&r
101
MC/DC50%1/2
builtin::blessed($item) and $item->isa("SQL::Wizard::Expr::Join")
builtin::blessed($item) $item->isa("SQL::Wizard::Expr::Join")
Truth table66%2/3
A and B
A= builtin::blessed($item)B= $item->isa("SQL::Wizard::Expr::Join")
ABresult
0X0
100
111
770 1 my ($s, @b) = $self->render($item);
771 1 push @table_parts, $s;
772 1 push @bind, @b;
773 } else {
774 1 my ($s, @b) = $self->_expand_table($item);
775 1 push @table_parts, $s;
776 1 push @bind, @b;
777 }
778 }
779 1 push @parts, "UPDATE " . join(' ', @table_parts);
780 } else {
781 5 my ($ts, @tb) = $self->_expand_table($node->{table});
782 5 push @parts, "UPDATE $ts";
783 5 push @bind, @tb;
784 }
785  
786 # SET
787 6 my @set_parts;
788 6 for my $col (sort keys %{$node->{set}}) {
789 7 $self->_assert_column($col);
790 7 my ($s, @b) = $self->_render_expr($node->{set}{$col});
791 7 push @set_parts, $self->_quote_ident_if_needed($col) . " = $s";
792 7 push @bind, @b;
793 }
794 6 push @parts, "SET " . join(', ', @set_parts);
795  
796 # FROM (PostgreSQL)
797 6 ▶ if ($node->{from}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"from"}) 1 5 6
798 1 ▶ my @from_items = ref $node->{from} eq 'ARRAY' ? @{$node->{from}} : ($node->{from});
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'from'} eq 'ARRAY' ? : 1 0 1
799 1 my @from_sqls;
800 1 for my $item (@from_items) {
801 1 my ($s, @b) = $self->_expand_table($item);
802 1 push @from_sqls, $s;
803 1 push @bind, @b;
804 }
805 1 push @parts, "FROM " . join(', ', @from_sqls);
806 }
807  
808 # WHERE
809 6 ▶ if ($node->{where}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"where"}) 5 1 6
810 5 my ($ws, @wb) = $self->_render_where($node->{where});
811 5 ▶ if (defined $ws && $ws ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ws and $ws ne "") 5 0 5
Condition33%1/3
defined $ws and $ws ne ""
!ll&&!rl&&r
005
MC/DC0%0/2
defined $ws and $ws ne ""
defined $ws $ws ne ""
Truth table33%1/3
A and B
A= defined $wsB= $ws ne ""
ABresult
0X0
100
111
812 5 push @parts, "WHERE $ws";
813 5 push @bind, @wb;
814 }
815 }
816  
817 # LIMIT (MySQL UPDATE ... LIMIT n)
818 6 ▶ if (defined $node->{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->{"limit"}) 1 5 6
819 1 $self->_assert_integer('-limit', $node->{limit});
820 1 push @parts, "LIMIT ?";
821 1 push @bind, $node->{limit};
822 }
823  
824 # RETURNING
825 6 ▶ if ($node->{returning}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"returning"}) 1 5 6
826 1 $self->_assert_column($_) for @{$node->{returning}};
827 1 push @parts, "RETURNING " . join(', ', map { $self->_quote_ident_if_needed($_) } @{$node->{returning}});
828 }
829  
830 6 return (join(' ', @parts), @bind);
831 }
832  
833 ## DELETE
834  
835 sub _render_delete {
836 6 ▶ my ($self, $node) = @_;
Subroutine _render_delete: called CC=7 SCAR=19.5
837 6 my @parts;
838 my @bind;
839  
840 6 $self->_injection_guard($node->{from});
841 6 push @parts, "DELETE FROM " . $self->_quote_ident_if_needed($node->{from});
842  
843 # USING (PostgreSQL)
844 6 ▶ if ($node->{using}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"using"}) 1 5 6
845 1 $self->_injection_guard($node->{using});
846 1 push @parts, "USING " . $self->_quote_ident_if_needed($node->{using});
847 }
848  
849 # WHERE
850 6 ▶ if ($node->{where}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"where"}) 5 1 6
851 5 my ($ws, @wb) = $self->_render_where($node->{where});
852 5 ▶ if (defined $ws && $ws ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ws and $ws ne "") 5 0 5
Condition33%1/3
defined $ws and $ws ne ""
!ll&&!rl&&r
005
MC/DC0%0/2
defined $ws and $ws ne ""
defined $ws $ws ne ""
Truth table33%1/3
A and B
A= defined $wsB= $ws ne ""
ABresult
0X0
100
111
853 5 push @parts, "WHERE $ws";
854 5 push @bind, @wb;
855 }
856 }
857  
858 # RETURNING
859 6 ▶ if ($node->{returning}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"returning"}) 1 5 6
860 1 $self->_assert_column($_) for @{$node->{returning}};
861 1 push @parts, "RETURNING " . join(', ', map { $self->_quote_ident_if_needed($_) } @{$node->{returning}});
862 }
863  
864 6 return (join(' ', @parts), @bind);
865 }
866  
867 ## WHERE clause rendering (self-contained, SQL::Abstract-compatible syntax)
868  
869 sub _render_where {
870 115 ▶ my ($self, $where) = @_;
Subroutine _render_where: called CC=17 SCAR=30.0
871  
872 # Expression object
873 115 ▶ if (blessed($where) && $where->isa('SQL::Wizard::Expr')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($where) and $where->isa("SQL::Wizard::Expr")) 1 114 115
Condition66%2/3
builtin::blessed($where) and $where->isa("SQL::Wizard::Expr")
!ll&&!rl&&r
11401
MC/DC50%1/2
builtin::blessed($where) and $where->isa("SQL::Wizard::Expr")
builtin::blessed($where) $where->isa("SQL::Wizard::Expr")
Truth table66%2/3
A and B
A= builtin::blessed($where)B= $where->isa("SQL::Wizard::Expr")
ABresult
0X0
100
111
874 1 return $self->render($where);
875 }
876  
877 # Hashref: { col => val, col2 => { '>' => 3 } }
878 114 ▶ if (ref $where eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $where eq "HASH") 101 13 114
879 101 my @parts;
880 my @bind;
881 101 for my $key (sort keys %$where) {
882 103 my $val = $where->{$key};
883  
884 # Expression object as key (e.g. $q->func(...) => { '>' => 5 })
885 103 ▶ if (blessed($key) && $key->isa('SQL::Wizard::Expr')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($key) and $key->isa("SQL::Wizard::Expr")) 0 103 103
Condition33%1/3
builtin::blessed($key) and $key->isa("SQL::Wizard::Expr")
!ll&&!rl&&r
10300
MC/DC0%0/2
builtin::blessed($key) and $key->isa("SQL::Wizard::Expr")
builtin::blessed($key) $key->isa("SQL::Wizard::Expr")
Truth table33%1/3
A and B
A= builtin::blessed($key)B= $key->isa("SQL::Wizard::Expr")
ABresult
0X0
100
111
886 0 my ($ks, @kb) = $self->render($key);
887 0 my ($vs, @vb) = $self->_render_where_value($ks, $val);
888 0 push @parts, $vs;
889 0 push @bind, @kb, @vb;
890 0 next;
891 }
892  
893 103 $self->_injection_guard($key);
894 103 my $qkey = $self->_quote_ident_if_needed($key);
895  
896 103 ▶ if (!defined $val) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $val) { } 2 101 103
elsif (builtin::blessed($val) and $val->isa('SQL::Wizard::Expr')) { } 8 93 101
elsif (ref $val eq 'HASH') { } 42 51 93
elsif (ref $val eq 'ARRAY') { } 2 49 51
897 2 push @parts, "$qkey IS NULL";
898 ▶ } elsif (blessed($val) && $val->isa('SQL::Wizard::Expr')) {
Condition66%2/3
builtin::blessed($val) and $val->isa("SQL::Wizard::Expr")
!ll&&!rl&&r
9308
MC/DC50%1/2
builtin::blessed($val) and $val->isa("SQL::Wizard::Expr")
builtin::blessed($val) $val->isa("SQL::Wizard::Expr")
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->isa("SQL::Wizard::Expr")
ABresult
0X0
100
111
899 8 my ($vs, @vb) = $self->render($val);
900 8 push @parts, "$qkey = $vs";
901 8 push @bind, @vb;
902 } elsif (ref $val eq 'HASH') {
903 42 my ($s, @b) = $self->_render_where_value($qkey, $val);
904 41 push @parts, $s;
905 41 push @bind, @b;
906 } elsif (ref $val eq 'ARRAY') {
907 # { col => [1,2,3] } => col IN (?,?,?)
908 2 ▶ if (!@$val) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$val) { } 1 1 2
909 1 push @parts, '1 = 0';
910 } else {
911 1 my @placeholders;
912 1 for my $v (@$val) {
913 3 ▶ if (blessed($v) && $v->isa('SQL::Wizard::Expr')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v) and $v->isa('SQL::Wizard::Expr')) { } 0 3 3
Condition33%1/3
builtin::blessed($v) and $v->isa("SQL::Wizard::Expr")
!ll&&!rl&&r
300
MC/DC0%0/2
builtin::blessed($v) and $v->isa("SQL::Wizard::Expr")
builtin::blessed($v) $v->isa("SQL::Wizard::Expr")
Truth table33%1/3
A and B
A= builtin::blessed($v)B= $v->isa("SQL::Wizard::Expr")
ABresult
0X0
100
111
914 0 my ($s, @b) = $self->render($v);
915 0 push @placeholders, $s;
916 0 push @bind, @b;
917 } else {
918 3 push @placeholders, '?';
919 3 push @bind, $v;
920 }
921 }
922 1 push @parts, "$qkey IN (" . join(', ', @placeholders) . ")";
923 }
924 } else {
925 49 push @parts, "$qkey = ?";
926 49 push @bind, $val;
927 }
928 }
929 100 return (join(' AND ', @parts), @bind);
930 }
931  
932 # Arrayref: [-and => ..., -or => ...]
933 13 ▶ if (ref $where eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $where eq "ARRAY") 12 1 13
934 12 return $self->_render_where_array($where);
935 }
936  
937 # Plain string
938 1 $self->_injection_guard($where);
939 1 return ($where, ());
940 }
941  
942 sub _render_where_value {
943 42 ▶ my ($self, $col, $val) = @_;
Subroutine _render_where_value: called CC=21 SCAR=31.1
944  
945 42 ▶ if (ref $val eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "HASH") 42 0 42
946 42 my @parts;
947 my @bind;
948 42 for my $op (sort keys %$val) {
949 42 my $rhs = $val->{$op};
950 42 my $sql_op = uc($op);
951  
952 confess "Unknown operator '$op' in WHERE clause"
953 42 ▶ unless $VALID_OPS{$sql_op};
Branch: 1 branch
BranchTrueFalseTotal
unless $VALID_OPS{$sql_op} 1 41 42
954  
955 # -in / -not_in
956 41 ▶ if ($sql_op eq '-IN' || $sql_op eq '-NOT_IN') {
Branch: 3 branches
BranchTrueFalseTotal
if ($sql_op eq '-IN' or $sql_op eq '-NOT_IN') { } 10 31 41
elsif (not defined $rhs) { } 3 28 31
elsif (builtin::blessed($rhs) and $rhs->isa('SQL::Wizard::Expr')) { } 3 25 28
Condition100%3/3
$sql_op eq "-IN" or $sql_op eq "-NOT_IN"
l!l&&r!l&&!r
5531
MC/DC100%2/2
$sql_op eq "-IN" or $sql_op eq "-NOT_IN"
$sql_op eq "-IN" $sql_op eq "-NOT_IN"
Truth table100%3/3
A or B
A= $sql_op eq "-IN"B= $sql_op eq "-NOT_IN"
ABresult
1X1
011
000
957 10 ▶ my $neg = $sql_op eq '-NOT_IN' ? 'NOT ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$sql_op eq '-NOT_IN' ? : 5 5 10
958 10 ▶ if (blessed($rhs) && $rhs->isa('SQL::Wizard::Expr')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($rhs) and $rhs->isa('SQL::Wizard::Expr')) { } 4 6 10
elsif (ref $rhs eq 'ARRAY') { } 6 0 6
Condition66%2/3
builtin::blessed($rhs) and $rhs->isa("SQL::Wizard::Expr")
!ll&&!rl&&r
604
MC/DC50%1/2
builtin::blessed($rhs) and $rhs->isa("SQL::Wizard::Expr")
builtin::blessed($rhs) $rhs->isa("SQL::Wizard::Expr")
Truth table66%2/3
A and B
A= builtin::blessed($rhs)B= $rhs->isa("SQL::Wizard::Expr")
ABresult
0X0
100
111
959 4 my ($s, @b) = $self->render($rhs);
960 4 push @parts, "$col ${neg}IN ($s)";
961 4 push @bind, @b;
962 } elsif (ref $rhs eq 'ARRAY') {
963 6 ▶ if (!@$rhs) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$rhs) { } 2 4 6
964 # Empty list: -in => always false, -not_in => always true
965 2 ▶ push @parts, $neg ? '1 = 1' : '1 = 0';
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 1 1 2
966 } else {
967 4 my @ph;
968 4 for my $v (@$rhs) {
969 9 ▶ if (blessed($v) && $v->isa('SQL::Wizard::Expr')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v) and $v->isa('SQL::Wizard::Expr')) { } 0 9 9
Condition33%1/3
builtin::blessed($v) and $v->isa("SQL::Wizard::Expr")
!ll&&!rl&&r
900
MC/DC0%0/2
builtin::blessed($v) and $v->isa("SQL::Wizard::Expr")
builtin::blessed($v) $v->isa("SQL::Wizard::Expr")
Truth table33%1/3
A and B
A= builtin::blessed($v)B= $v->isa("SQL::Wizard::Expr")
ABresult
0X0
100
111
970 0 my ($s, @b) = $self->render($v);
971 0 push @ph, $s;
972 0 push @bind, @b;
973 } else {
974 9 push @ph, '?';
975 9 push @bind, $v;
976 }
977 }
978 4 push @parts, "$col ${neg}IN (" . join(', ', @ph) . ")";
979 }
980 }
981 } elsif (!defined $rhs) {
982 3 ▶ if ($sql_op eq '!=' || $sql_op eq '<>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql_op eq '!=' or $sql_op eq '<>') { } 2 1 3
Condition100%3/3
$sql_op eq "!=" or $sql_op eq "<>"
l!l&&r!l&&!r
111
MC/DC100%2/2
$sql_op eq "!=" or $sql_op eq "<>"
$sql_op eq "!=" $sql_op eq "<>"
Truth table100%3/3
A or B
A= $sql_op eq "!="B= $sql_op eq "<>"
ABresult
1X1
011
000
983 2 push @parts, "$col IS NOT NULL";
984 } else {
985 1 push @parts, "$col IS NULL";
986 }
987 ▶ } elsif (blessed($rhs) && $rhs->isa('SQL::Wizard::Expr')) {
Condition66%2/3
builtin::blessed($rhs) and $rhs->isa("SQL::Wizard::Expr")
!ll&&!rl&&r
2503
MC/DC50%1/2
builtin::blessed($rhs) and $rhs->isa("SQL::Wizard::Expr")
builtin::blessed($rhs) $rhs->isa("SQL::Wizard::Expr")
Truth table66%2/3
A and B
A= builtin::blessed($rhs)B= $rhs->isa("SQL::Wizard::Expr")
ABresult
0X0
100
111
988 3 my ($s, @b) = $self->render($rhs);
989 3 ▶ $s = "($s)" if $rhs->isa('SQL::Wizard::Expr::Select');
Branch: 1 branch
BranchTrueFalseTotal
if $rhs->isa("SQL::Wizard::Expr::Select") 1 2 3
990 3 push @parts, "$col $sql_op $s";
991 3 push @bind, @b;
992 } else {
993 25 push @parts, "$col $sql_op ?";
994 25 push @bind, $rhs;
995 }
996 }
997 41 return (join(' AND ', @parts), @bind);
998 }
999  
1000 0 return ("$col = ?", $val);
1001 }
1002  
1003 sub _render_where_array {
1004 17 ▶ my ($self, $arr, $default_logic) = @_;
Subroutine _render_where_array: called CC=12 SCAR=24.9
1005 17 my @items = @$arr;
1006 17 my @parts;
1007 my @bind;
1008  
1009 17 ▶ my $logic = $default_logic || 'AND';
Condition100%2/2
$default_logic || 'AND'
l!l
413
MC/DC100%1/1
$default_logic || 'AND'
$default_logic
Truth table100%2/2
A
A= $default_logic
Aresult
11
00
1010  
1011 17 my $i = 0;
1012 17 ▶ while ($i <= $#items) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#items) 23 17 40
1013 23 my $item = $items[$i];
1014  
1015 23 ▶ if (!ref $item && $item =~ /^-(and|or)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $item and $item =~ /^-(and|or)$/i) 6 17 23
Condition66%2/3
not ref $item and $item =~ /^-(and|or)$/i
!ll&&!rl&&r
1706
MC/DC50%1/2
not ref $item and $item =~ /^-(and|or)$/i
not ref $item $item =~ /^-(and|or)$/i
Truth table66%2/3
not(A) and B
A= not ref $itemB= $item =~ /^-(and|or)$/i
ABresult
0X0
100
111
1016 6 $logic = uc($1);
1017 6 $i++;
1018 # Next item could be arrayref of conditions
1019 6 ▶ if ($i <= $#items && ref $items[$i] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#items and ref $items[$i] eq "ARRAY") 4 2 6
Condition66%2/3
$i <= $#items and ref $items[$i] eq "ARRAY"
!ll&&!rl&&r
024
MC/DC50%1/2
$i <= $#items and ref $items[$i] eq "ARRAY"
$i <= $#items ref $items[$i] eq "ARRAY"
Truth table66%2/3
A and B
A= $i <= $#itemsB= ref $items[$i] eq "ARRAY"
ABresult
0X0
100
111
1020 4 my ($s, @b) = $self->_render_where_array($items[$i], $logic);
1021 4 push @parts, $s;
1022 4 push @bind, @b;
1023 4 $i++;
1024 }
1025 6 next;
1026 }
1027  
1028 17 ▶ if (ref $item eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $item eq 'HASH') { } 11 6 17
elsif (ref $item eq 'ARRAY') { } 1 5 6
elsif (builtin::blessed($item) and $item->isa('SQL::Wizard::Expr')) { } 5 0 5
1029 11 my ($s, @b) = $self->_render_where($item);
1030 11 push @parts, $s;
1031 11 push @bind, @b;
1032 } elsif (ref $item eq 'ARRAY') {
1033 1 my ($s, @b) = $self->_render_where_array($item);
1034 1 push @parts, "($s)";
1035 1 push @bind, @b;
1036 ▶ } elsif (blessed($item) && $item->isa('SQL::Wizard::Expr')) {
Condition33%1/3
builtin::blessed($item) and $item->isa("SQL::Wizard::Expr")
!ll&&!rl&&r
005
MC/DC0%0/2
builtin::blessed($item) and $item->isa("SQL::Wizard::Expr")
builtin::blessed($item) $item->isa("SQL::Wizard::Expr")
Truth table33%1/3
A and B
A= builtin::blessed($item)B= $item->isa("SQL::Wizard::Expr")
ABresult
0X0
100
111
1037 5 my ($s, @b) = $self->render($item);
1038 5 push @parts, $s;
1039 5 push @bind, @b;
1040 }
1041  
1042 17 $i++;
1043 }
1044  
1045 17 my $joined = join(" $logic ", @parts);
1046 17 ▶ $joined = "($joined)" if @parts > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parts > 1 6 11 17
1047 17 return ($joined, @bind);
1048 }
1049  
1050 14 1;
↑ Summary Wizard/Expr/Select.pm »