| 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 | 14 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 20 | 14 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||
| 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 | 680 | ▶ | sub _needs_quoting { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_needs_quoting: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||
| 68 | 680 | my ($self, $part) = @_; | |||||||||||||||||||||||||||||||||||
| 69 | 680 | ▶ | return 0 if $part eq '*'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 70 | 579 | ▶ | return 1 if $RESERVED{uc $part}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 71 | 576 | ▶ | return 1 if $part =~ /[A-Z]/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 72 | 576 | ▶ | return 1 if $part =~ /[^a-z0-9_]/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 73 | 576 | return 0; | |||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | 3 | ▶ | sub _quote_ident { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_quote_ident: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||||||||||
| 77 | 3 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | my $q = ($self->{dialect} || 'ansi') eq 'mysql' ? '`' : '"'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{'dialect'} || 'ansi'
MC/DC0%0/1
$self->{'dialect'} || 'ansi'
$self->{'dialect'}
Truth table50%1/2
A
A= $self->{'dialect'}
| |||||||||||||||||||||||||||||||||||||
| 79 | return join('.', map { | ||||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | $_ eq '*' ? $_ : $q . (s/\Q$q\E/$q$q/gr) . $q | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 81 | } split /\./, $name, -1); | ||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||
| 84 | 640 | ▶ | sub _quote_ident_if_needed { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_quote_ident_if_needed: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 85 | 640 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||
| 86 | 640 | my @parts = split /\./, $name, -1; | |||||||||||||||||||||||||||||||||||
| 87 | 640 | ▶ | return $name unless grep { $self->_needs_quoting($_) } @parts; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 88 | 3 | return $self->_quote_ident($name); | |||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | 325 | ▶ | sub _injection_guard { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_injection_guard: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||
| 92 | 325 | my ($self, $string) = @_; | |||||||||||||||||||||||||||||||||||
| 93 | 325 | ▶ | if ($string =~ $INJECTION_GUARD) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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 | 56 | ▶ | sub _assert_column { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_column: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||
| 100 | 56 | my ($self, $col) = @_; | |||||||||||||||||||||||||||||||||||
| 101 | 56 | ▶ | confess "Invalid column name '$col'" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 102 | unless $col =~ /^(\w+\.)*(\w+|\*)$/; | ||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||
| 105 | 14 | ▶ | sub _assert_order_column { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_order_column: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||
| 106 | 14 | my ($self, $col) = @_; | |||||||||||||||||||||||||||||||||||
| 107 | 14 | ▶ | confess "Invalid order_by column '$col'" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 108 | unless $col =~ /^(\w+\.)*\w+$/; | ||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||
| 111 | 9 | ▶ | sub _assert_integer { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_integer: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||
| 112 | 9 | my ($self, $name, $value) = @_; | |||||||||||||||||||||||||||||||||||
| 113 | 9 | ▶ | confess "$name must be an integer, got '$value'" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 114 | unless $value =~ /^\d+$/; | ||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||
| 117 | # Main dispatch | ||||||||||||||||||||||||||||||||||||
| 118 | 431 | ▶ | sub render { | ||||||||||||||||||||||||||||||||||
|
Subroutine
render: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 119 | 431 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 120 | 431 | my $type = ref $node; | |||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||
| 122 | 431 | 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 | 431 | ▶ | my $handler = $dispatch{$type} | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 142 | or croak "No renderer for node type: $type"; | ||||||||||||||||||||||||||||||||||||
| 143 | 431 | $handler->($self, $node); | |||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||
| 146 | # Render any expression or plain string (column name) | ||||||||||||||||||||||||||||||||||||
| 147 | 286 | ▶ | sub _render_expr { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_expr: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||
| 148 | 286 | my ($self, $thing) = @_; | |||||||||||||||||||||||||||||||||||
| 149 | 286 | ▶ | return ('', ()) unless defined $thing; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 150 | 286 | ▶ | if (blessed($thing) && $thing->isa('SQL::Wizard::Expr')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($thing) and $thing->isa("SQL::Wizard::Expr")
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")
| |||||||||||||||||||||||||||||||||||||
| 151 | 99 | return $self->render($thing); | |||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||
| 153 | # Plain string = column name | ||||||||||||||||||||||||||||||||||||
| 154 | 187 | $self->_injection_guard($thing); | |||||||||||||||||||||||||||||||||||
| 155 | 187 | return ($self->_quote_ident_if_needed($thing), ()); | |||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||
| 158 | # table|alias => table alias | ||||||||||||||||||||||||||||||||||||
| 159 | 168 | ▶ | sub _expand_table { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_expand_table: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||
| 160 | 168 | my ($self, $thing) = @_; | |||||||||||||||||||||||||||||||||||
| 161 | 168 | ▶ | if (blessed($thing) && $thing->isa('SQL::Wizard::Expr')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($thing) and $thing->isa("SQL::Wizard::Expr")
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")
| |||||||||||||||||||||||||||||||||||||
| 162 | 3 | return $self->render($thing); | |||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||
| 164 | 165 | ▶ | confess "Invalid table name '$thing'" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 165 | unless $thing =~ /^(\w+\.)*\w+(\|\w+)?$/; | ||||||||||||||||||||||||||||||||||||
| 166 | 165 | my ($table, $alias) = split /\|/, $thing, 2; | |||||||||||||||||||||||||||||||||||
| 167 | 165 | my $qt = $self->_quote_ident_if_needed($table); | |||||||||||||||||||||||||||||||||||
| 168 | 165 | ▶ | return $alias ? ("$qt " . $self->_quote_ident_if_needed($alias), ()) : ($qt, ()); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||
| 171 | ## Leaf renderers | ||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||
| 173 | 71 | ▶ | sub _render_column { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_column: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 174 | 71 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 175 | 71 | return ($self->_quote_ident_if_needed($node->{name}), ()); | |||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||
| 178 | 48 | ▶ | sub _render_value { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 179 | 48 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 180 | 48 | return ('?', $node->{value}); | |||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||
| 183 | 22 | ▶ | sub _render_raw { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_raw: called CC=18 SCAR=29.4
| |||||||||||||||||||||||||||||||||||||
| 184 | 22 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||
| 186 | # COMPARE | ||||||||||||||||||||||||||||||||||||
| 187 | 22 | ▶ | if ($node->{_compare}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 195 | 4 | ▶ | $rs = "($rs)" if $c->{right}->isa('SQL::Wizard::Expr::Select'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 196 | 4 | return ("$ls $sql_op $rs", @lb, @rb); | |||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||
| 199 | # TRUNCATE | ||||||||||||||||||||||||||||||||||||
| 200 | 18 | ▶ | if ($node->{_truncate}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 201 | 0 | my $table = $node->{_truncate}; | |||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | confess "Invalid table name '$table'" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$node->{"_between"} or $node->{"_not_between"}
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"}
| |||||||||||||||||||||||||||||||||||||
| 215 | 2 | ▶ | my $spec = $node->{_between} || $node->{_not_between}; | ||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$node->{'_between'} || $node->{'_not_between'}
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'}
| |||||||||||||||||||||||||||||||||||||
| 216 | 2 | ▶ | my $op = $node->{_between} ? 'BETWEEN' : 'NOT BETWEEN'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 225 | 1 | my $type = $node->{_cast}{type}; | |||||||||||||||||||||||||||||||||||
| 226 | 1 | ▶ | confess "Invalid CAST type '$type'" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 245 | 2 | return ($joined, @bind); | |||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||
| 248 | # NOT | ||||||||||||||||||||||||||||||||||||
| 249 | 6 | ▶ | if ($node->{_not}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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 | 28 | ▶ | sub _render_alias { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_alias: called CC=5 SCAR=18.3
| |||||||||||||||||||||||||||||||||||||
| 258 | 28 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 262 | 4 | $sql = "($sql)"; | |||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||
| 264 | 28 | my $alias = $node->{alias}; | |||||||||||||||||||||||||||||||||||
| 265 | 28 | my $quoted_alias; | |||||||||||||||||||||||||||||||||||
| 266 | 28 | ▶ | if ($alias =~ /\./) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 267 | # Dotted alias: quote as a single identifier (e.g. "d.domain"), not as schema.column | ||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | my $q = ($self->{dialect} || 'ansi') eq 'mysql' ? '`' : '"'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->{'dialect'} || 'ansi'
MC/DC0%0/1
$self->{'dialect'} || 'ansi'
$self->{'dialect'}
Truth table0%0/2
A
A= $self->{'dialect'}
| |||||||||||||||||||||||||||||||||||||
| 269 | 0 | $quoted_alias = $q . ($alias =~ s/\Q$q\E/$q$q/gr) . $q; | |||||||||||||||||||||||||||||||||||
| 270 | } else { | ||||||||||||||||||||||||||||||||||||
| 271 | 28 | $quoted_alias = $self->_quote_ident_if_needed($alias); | |||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||
| 273 | 28 | return ("$sql AS $quoted_alias", @bind); | |||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||
| 276 | 6 | ▶ | sub _render_order { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_order: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 277 | 6 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 278 | 6 | my ($sql, @bind) = $self->_render_expr($node->{expr}); | |||||||||||||||||||||||||||||||||||
| 279 | 6 | $sql .= " $node->{direction}"; | |||||||||||||||||||||||||||||||||||
| 280 | 6 | ▶ | $sql .= " NULLS $node->{nulls}" if $node->{nulls}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 281 | 6 | return ($sql, @bind); | |||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||
| 284 | 34 | ▶ | sub _render_func { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_func: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 285 | 34 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 286 | 34 | my @arg_sqls; | |||||||||||||||||||||||||||||||||||
| 287 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 288 | 34 | for my $arg (@{$node->{args}}) { | |||||||||||||||||||||||||||||||||||
| 289 | 31 | my ($s, @b) = $self->_render_expr($arg); | |||||||||||||||||||||||||||||||||||
| 290 | 31 | push @arg_sqls, $s; | |||||||||||||||||||||||||||||||||||
| 291 | 31 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||
| 293 | 34 | my $args_str = join(', ', @arg_sqls); | |||||||||||||||||||||||||||||||||||
| 294 | 34 | return ("$node->{name}($args_str)", @bind); | |||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||
| 297 | 14 | ▶ | sub _render_binop { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_binop: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 298 | 14 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 299 | 14 | my ($lsql, @lbind) = $self->render($node->{left}); | |||||||||||||||||||||||||||||||||||
| 300 | 14 | my ($rsql, @rbind) = $self->render($node->{right}); | |||||||||||||||||||||||||||||||||||
| 301 | 14 | return ("$lsql $node->{op} $rsql", @lbind, @rbind); | |||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||
| 304 | ## SELECT | ||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||
| 306 | 148 | ▶ | sub _render_select { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_select: called CC=33 SCAR=35.3
| |||||||||||||||||||||||||||||||||||||
| 307 | 148 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 308 | 148 | my @parts; | |||||||||||||||||||||||||||||||||||
| 309 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||
| 311 | # CTE | ||||||||||||||||||||||||||||||||||||
| 312 | 148 | ▶ | if ($node->{_cte}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 313 | 4 | my ($cte_sql, @cte_bind) = $self->render($node->{_cte}); | |||||||||||||||||||||||||||||||||||
| 314 | 4 | push @parts, $cte_sql; | |||||||||||||||||||||||||||||||||||
| 315 | 4 | push @bind, @cte_bind; | |||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||
| 318 | # SELECT columns | ||||||||||||||||||||||||||||||||||||
| 319 | 148 | my @col_sqls; | |||||||||||||||||||||||||||||||||||
| 320 | 148 | ▶ | for my $col (@{$node->{columns} || ['*']}) { | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$node->{'columns'} || ['*']
MC/DC100%1/1
$node->{'columns'} || ['*']
$node->{'columns'}
Truth table100%2/2
A
A= $node->{'columns'}
| |||||||||||||||||||||||||||||||||||||
| 321 | 180 | my ($s, @b) = $self->_render_expr($col); | |||||||||||||||||||||||||||||||||||
| 322 | 180 | push @col_sqls, $s; | |||||||||||||||||||||||||||||||||||
| 323 | 180 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||
| 325 | 148 | ▶ | my $select_keyword = $node->{distinct} ? "SELECT DISTINCT" : "SELECT"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 326 | 148 | push @parts, "$select_keyword " . join(', ', @col_sqls); | |||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||
| 328 | # FROM | ||||||||||||||||||||||||||||||||||||
| 329 | 148 | ▶ | if ($node->{from}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 330 | 148 | my @from_sqls; | |||||||||||||||||||||||||||||||||||
| 331 | 148 | ▶ | my @from_items = ref $node->{from} eq 'ARRAY' ? @{$node->{from}} : ($node->{from}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 332 | 148 | for my $i (0 .. $#from_items) { | |||||||||||||||||||||||||||||||||||
| 333 | 160 | my $item = $from_items[$i]; | |||||||||||||||||||||||||||||||||||
| 334 | 160 | ▶ | if (blessed($item) && $item->isa('SQL::Wizard::Expr::Join')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($item) and $item->isa("SQL::Wizard::Expr::Join")
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")
| |||||||||||||||||||||||||||||||||||||
| 335 | 12 | my ($s, @b) = $self->render($item); | |||||||||||||||||||||||||||||||||||
| 336 | 12 | push @from_sqls, $s; | |||||||||||||||||||||||||||||||||||
| 337 | 12 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 338 | } else { | ||||||||||||||||||||||||||||||||||||
| 339 | 148 | my ($s, @b) = $self->_expand_table($item); | |||||||||||||||||||||||||||||||||||
| 340 | # First item or non-join items | ||||||||||||||||||||||||||||||||||||
| 341 | 148 | ▶ | if ($i == 0) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 342 | 148 | push @from_sqls, $s; | |||||||||||||||||||||||||||||||||||
| 343 | } else { | ||||||||||||||||||||||||||||||||||||
| 344 | 0 | push @from_sqls, $s; | |||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||
| 346 | 148 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||
| 349 | 148 | push @parts, "FROM " . join(' ', @from_sqls); | |||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||
| 352 | # WHERE | ||||||||||||||||||||||||||||||||||||
| 353 | 148 | ▶ | if ($node->{where}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 354 | 80 | my ($wsql, @wbind) = $self->_render_where($node->{where}); | |||||||||||||||||||||||||||||||||||
| 355 | 79 | ▶ | if (defined $wsql && $wsql ne '') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $wsql and $wsql ne ""
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 ""
| |||||||||||||||||||||||||||||||||||||
| 356 | 77 | push @parts, "WHERE $wsql"; | |||||||||||||||||||||||||||||||||||
| 357 | 77 | push @bind, @wbind; | |||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||
| 361 | # GROUP BY | ||||||||||||||||||||||||||||||||||||
| 362 | 147 | ▶ | if ($node->{group_by}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 363 | 0 | ▶ | my @items = ref $node->{group_by} eq 'ARRAY' ? @{$node->{group_by}} : ($node->{group_by}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 364 | 7 | my @gsqls; | |||||||||||||||||||||||||||||||||||
| 365 | 7 | for my $g (@items) { | |||||||||||||||||||||||||||||||||||
| 366 | 7 | my ($s, @b) = $self->_render_expr($g); | |||||||||||||||||||||||||||||||||||
| 367 | 7 | push @gsqls, $s; | |||||||||||||||||||||||||||||||||||
| 368 | 7 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||||
| 370 | 7 | push @parts, "GROUP BY " . join(', ', @gsqls); | |||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||
| 373 | # HAVING | ||||||||||||||||||||||||||||||||||||
| 374 | 147 | ▶ | if ($node->{having}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 375 | 3 | my ($hsql, @hbind) = $self->_render_where($node->{having}); | |||||||||||||||||||||||||||||||||||
| 376 | 3 | ▶ | if (defined $hsql && $hsql ne '') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $hsql and $hsql ne ""
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 ""
| |||||||||||||||||||||||||||||||||||||
| 377 | 1 | push @parts, "HAVING $hsql"; | |||||||||||||||||||||||||||||||||||
| 378 | 1 | push @bind, @hbind; | |||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||
| 382 | # WINDOW | ||||||||||||||||||||||||||||||||||||
| 383 | 147 | ▶ | if ($node->{window}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 384 | 2 | my @wdefs; | |||||||||||||||||||||||||||||||||||
| 385 | 2 | for my $name (sort keys %{$node->{window}}) { | |||||||||||||||||||||||||||||||||||
| 386 | 2 | ▶ | confess "Invalid window name '$name'" unless $name =~ /^\w+$/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 387 | 2 | my $spec = $node->{window}{$name}; | |||||||||||||||||||||||||||||||||||
| 388 | 2 | my ($s, @b) = $self->_render_window_spec($spec); | |||||||||||||||||||||||||||||||||||
| 389 | 2 | push @wdefs, $self->_quote_ident_if_needed($name) . " AS ($s)"; | |||||||||||||||||||||||||||||||||||
| 390 | 2 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||
| 392 | 2 | push @parts, "WINDOW " . join(', ', @wdefs); | |||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||
| 395 | # ORDER BY | ||||||||||||||||||||||||||||||||||||
| 396 | 147 | ▶ | if ($node->{order_by}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 397 | 7 | ▶ | my @items = ref $node->{order_by} eq 'ARRAY' ? @{$node->{order_by}} : ($node->{order_by}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 398 | 7 | my @osqls; | |||||||||||||||||||||||||||||||||||
| 399 | 7 | for my $o (@items) { | |||||||||||||||||||||||||||||||||||
| 400 | 8 | ▶ | if (ref $o eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||
| 401 | # { -desc => 'col' } or { -asc => 'col' } | ||||||||||||||||||||||||||||||||||||
| 402 | 2 | my ($dir, $col) = each %$o; | |||||||||||||||||||||||||||||||||||
| 403 | 2 | $dir = uc($dir); | |||||||||||||||||||||||||||||||||||
| 404 | 2 | $dir =~ s/^-//; | |||||||||||||||||||||||||||||||||||
| 405 | 2 | ▶ | $self->_assert_order_column($col) unless ref $col; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 406 | 2 | my ($s, @b) = $self->_render_expr($col); | |||||||||||||||||||||||||||||||||||
| 407 | 2 | push @osqls, "$s $dir"; | |||||||||||||||||||||||||||||||||||
| 408 | 2 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 409 | ▶ | } elsif (!ref $o && $o =~ /^-(.+)/) { | |||||||||||||||||||||||||||||||||||
|
Condition66%2/3
not ref $o and $o =~ /^-(.+)/
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 =~ /^-(.+)/
| |||||||||||||||||||||||||||||||||||||
| 410 | # '-col' shorthand for col DESC | ||||||||||||||||||||||||||||||||||||
| 411 | 0 | $self->_assert_order_column($1); | |||||||||||||||||||||||||||||||||||
| 412 | 0 | my ($s, @b) = $self->_render_expr($1); | |||||||||||||||||||||||||||||||||||
| 413 | 0 | push @osqls, "$s DESC"; | |||||||||||||||||||||||||||||||||||
| 414 | 0 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 415 | } elsif (!ref $o) { | ||||||||||||||||||||||||||||||||||||
| 416 | 4 | $self->_assert_order_column($o); | |||||||||||||||||||||||||||||||||||
| 417 | 4 | my ($s, @b) = $self->_render_expr($o); | |||||||||||||||||||||||||||||||||||
| 418 | 4 | push @osqls, $s; | |||||||||||||||||||||||||||||||||||
| 419 | 4 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 420 | } else { | ||||||||||||||||||||||||||||||||||||
| 421 | 2 | my ($s, @b) = $self->_render_expr($o); | |||||||||||||||||||||||||||||||||||
| 422 | 2 | push @osqls, $s; | |||||||||||||||||||||||||||||||||||
| 423 | 2 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||||||||||
| 426 | 7 | push @parts, "ORDER BY " . join(', ', @osqls); | |||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||
| 429 | # LIMIT / OFFSET | ||||||||||||||||||||||||||||||||||||
| 430 | 147 | ▶ | if (defined $node->{limit}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 431 | 4 | $self->_assert_integer('-limit', $node->{limit}); | |||||||||||||||||||||||||||||||||||
| 432 | 4 | push @parts, "LIMIT ?"; | |||||||||||||||||||||||||||||||||||
| 433 | 4 | push @bind, $node->{limit}; | |||||||||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||||||||||
| 435 | 147 | ▶ | if (defined $node->{offset}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 436 | 3 | $self->_assert_integer('-offset', $node->{offset}); | |||||||||||||||||||||||||||||||||||
| 437 | 3 | push @parts, "OFFSET ?"; | |||||||||||||||||||||||||||||||||||
| 438 | 3 | push @bind, $node->{offset}; | |||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||
| 441 | 147 | return (join(' ', @parts), @bind); | |||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||
| 444 | ## JOIN | ||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||
| 446 | 13 | ▶ | sub _render_join { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_join: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||
| 447 | 13 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 448 | 13 | my @bind; | |||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||
| 450 | 13 | my ($table_sql, @tb) = $self->_expand_table($node->{table}); | |||||||||||||||||||||||||||||||||||
| 451 | 13 | push @bind, @tb; | |||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||
| 453 | 13 | my $sql = "$node->{type} $table_sql"; | |||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||
| 455 | 13 | ▶ | if (defined $node->{on}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 456 | 12 | ▶ | if (ref $node->{on} eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 457 | 1 | my ($on_sql, @ob) = $self->_render_where($node->{on}); | |||||||||||||||||||||||||||||||||||
| 458 | 1 | $sql .= " ON $on_sql"; | |||||||||||||||||||||||||||||||||||
| 459 | 1 | push @bind, @ob; | |||||||||||||||||||||||||||||||||||
| 460 | } else { | ||||||||||||||||||||||||||||||||||||
| 461 | # String ON condition | ||||||||||||||||||||||||||||||||||||
| 462 | 11 | $self->_injection_guard($node->{on}); | |||||||||||||||||||||||||||||||||||
| 463 | 11 | $sql .= " ON $node->{on}"; | |||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||
| 467 | 13 | return ($sql, @bind); | |||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||
| 470 | ## CASE | ||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||
| 472 | 6 | ▶ | sub _render_case { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_case: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||
| 473 | 6 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 474 | 6 | my @parts; | |||||||||||||||||||||||||||||||||||
| 475 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||
| 477 | 6 | push @parts, 'CASE'; | |||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||
| 479 | # CASE ON (simple case with operand) | ||||||||||||||||||||||||||||||||||||
| 480 | 6 | ▶ | if ($node->{operand}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 481 | 1 | my ($os, @ob) = $self->_render_expr($node->{operand}); | |||||||||||||||||||||||||||||||||||
| 482 | 1 | $parts[0] .= " $os"; | |||||||||||||||||||||||||||||||||||
| 483 | 1 | push @bind, @ob; | |||||||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||
| 486 | 6 | for my $when (@{$node->{whens}}) { | |||||||||||||||||||||||||||||||||||
| 487 | 9 | ▶ | if ($node->{operand}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 488 | # Simple CASE: WHEN value THEN result | ||||||||||||||||||||||||||||||||||||
| 489 | 2 | my ($ws, @wb) = $self->_render_expr($when->{condition}); | |||||||||||||||||||||||||||||||||||
| 490 | 2 | my ($ts, @tb) = $self->_render_expr($when->{then}); | |||||||||||||||||||||||||||||||||||
| 491 | 2 | push @parts, "WHEN $ws THEN $ts"; | |||||||||||||||||||||||||||||||||||
| 492 | 2 | push @bind, @wb, @tb; | |||||||||||||||||||||||||||||||||||
| 493 | } else { | ||||||||||||||||||||||||||||||||||||
| 494 | # Searched CASE: WHEN condition THEN result | ||||||||||||||||||||||||||||||||||||
| 495 | 7 | my ($ws, @wb) = $self->_render_where($when->{condition}); | |||||||||||||||||||||||||||||||||||
| 496 | 7 | my ($ts, @tb) = $self->_render_expr($when->{then}); | |||||||||||||||||||||||||||||||||||
| 497 | 7 | push @parts, "WHEN $ws THEN $ts"; | |||||||||||||||||||||||||||||||||||
| 498 | 7 | push @bind, @wb, @tb; | |||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||
| 502 | 6 | ▶ | if (defined $node->{else}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 503 | 5 | my ($es, @eb) = $self->_render_expr($node->{else}); | |||||||||||||||||||||||||||||||||||
| 504 | 5 | push @parts, "ELSE $es"; | |||||||||||||||||||||||||||||||||||
| 505 | 5 | push @bind, @eb; | |||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||
| 508 | 6 | push @parts, 'END'; | |||||||||||||||||||||||||||||||||||
| 509 | 6 | return (join(' ', @parts), @bind); | |||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||
| 512 | ## Window | ||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||
| 514 | 9 | ▶ | sub _render_window { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_window: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||
| 515 | 9 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 516 | 9 | my ($expr_sql, @bind) = $self->render($node->{expr}); | |||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||
| 518 | 9 | my $spec = $node->{spec}; | |||||||||||||||||||||||||||||||||||
| 519 | 9 | ▶ | if ($spec->{name}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 520 | 4 | ▶ | confess "Invalid window name '$spec->{name}'" unless $spec->{name} =~ /^\w+$/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 521 | 4 | return ("$expr_sql OVER " . $self->_quote_ident_if_needed($spec->{name}), @bind); | |||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||
| 524 | 5 | my ($spec_sql, @sb) = $self->_render_window_spec($spec); | |||||||||||||||||||||||||||||||||||
| 525 | 5 | push @bind, @sb; | |||||||||||||||||||||||||||||||||||
| 526 | 5 | return ("$expr_sql OVER ($spec_sql)", @bind); | |||||||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||
| 529 | 7 | ▶ | sub _render_window_spec { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_window_spec: called CC=13 SCAR=26.8
| |||||||||||||||||||||||||||||||||||||
| 530 | 7 | my ($self, $spec) = @_; | |||||||||||||||||||||||||||||||||||
| 531 | 7 | my @parts; | |||||||||||||||||||||||||||||||||||
| 532 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||
| 534 | 7 | ▶ | if ($spec->{'-partition_by'}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 535 | my @items = ref $spec->{'-partition_by'} eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||
| 536 | 6 | ▶ | ? @{$spec->{'-partition_by'}} : ($spec->{'-partition_by'}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 537 | 6 | my @sqls; | |||||||||||||||||||||||||||||||||||
| 538 | 6 | for my $p (@items) { | |||||||||||||||||||||||||||||||||||
| 539 | 7 | my ($s, @b) = $self->_render_expr($p); | |||||||||||||||||||||||||||||||||||
| 540 | 7 | push @sqls, $s; | |||||||||||||||||||||||||||||||||||
| 541 | 7 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||||||
| 543 | 6 | push @parts, "PARTITION BY " . join(', ', @sqls); | |||||||||||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||
| 546 | 7 | ▶ | if ($spec->{'-order_by'}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 547 | my @items = ref $spec->{'-order_by'} eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||
| 548 | 7 | ▶ | ? @{$spec->{'-order_by'}} : ($spec->{'-order_by'}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 549 | 7 | my @sqls; | |||||||||||||||||||||||||||||||||||
| 550 | 7 | for my $o (@items) { | |||||||||||||||||||||||||||||||||||
| 551 | 7 | ▶ | if (ref $o eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||
| 552 | 3 | my ($dir, $col) = each %$o; | |||||||||||||||||||||||||||||||||||
| 553 | 3 | $dir = uc($dir); | |||||||||||||||||||||||||||||||||||
| 554 | 3 | $dir =~ s/^-//; | |||||||||||||||||||||||||||||||||||
| 555 | 3 | ▶ | $self->_assert_order_column($col) unless ref $col; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 556 | 3 | my ($s, @b) = $self->_render_expr($col); | |||||||||||||||||||||||||||||||||||
| 557 | 3 | push @sqls, "$s $dir"; | |||||||||||||||||||||||||||||||||||
| 558 | 3 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 559 | ▶ | } elsif (!ref $o && $o =~ /^-(.+)/) { | |||||||||||||||||||||||||||||||||||
|
Condition33%1/3
not ref $o and $o =~ /^-(.+)/
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 =~ /^-(.+)/
| |||||||||||||||||||||||||||||||||||||
| 560 | 0 | $self->_assert_order_column($1); | |||||||||||||||||||||||||||||||||||
| 561 | 0 | my ($s, @b) = $self->_render_expr($1); | |||||||||||||||||||||||||||||||||||
| 562 | 0 | push @sqls, "$s DESC"; | |||||||||||||||||||||||||||||||||||
| 563 | 0 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 564 | } elsif (!ref $o) { | ||||||||||||||||||||||||||||||||||||
| 565 | 4 | $self->_assert_order_column($o); | |||||||||||||||||||||||||||||||||||
| 566 | 4 | my ($s, @b) = $self->_render_expr($o); | |||||||||||||||||||||||||||||||||||
| 567 | 4 | push @sqls, $s; | |||||||||||||||||||||||||||||||||||
| 568 | 4 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 569 | } else { | ||||||||||||||||||||||||||||||||||||
| 570 | 0 | my ($s, @b) = $self->_render_expr($o); | |||||||||||||||||||||||||||||||||||
| 571 | 0 | push @sqls, $s; | |||||||||||||||||||||||||||||||||||
| 572 | 0 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||||||||||
| 575 | 7 | push @parts, "ORDER BY " . join(', ', @sqls); | |||||||||||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||
| 578 | 7 | ▶ | if ($spec->{'-frame'}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 579 | 1 | $self->_injection_guard($spec->{'-frame'}); | |||||||||||||||||||||||||||||||||||
| 580 | 1 | push @parts, $spec->{'-frame'}; | |||||||||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||
| 583 | 7 | return (join(' ', @parts), @bind); | |||||||||||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||
| 586 | ## Compound (UNION/INTERSECT/EXCEPT) | ||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||
| 588 | 9 | ▶ | sub _render_compound { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_compound: called CC=13 SCAR=32.5
| |||||||||||||||||||||||||||||||||||||
| 589 | 9 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 590 | 9 | my @parts; | |||||||||||||||||||||||||||||||||||
| 591 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||
| 593 | 9 | for my $entry (@{$node->{queries}}) { | |||||||||||||||||||||||||||||||||||
| 594 | 20 | my ($s, @b) = $self->render($entry->{query}); | |||||||||||||||||||||||||||||||||||
| 595 | 20 | ▶ | if ($entry->{type}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 596 | 11 | push @parts, $entry->{type}; | |||||||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||||||
| 598 | 20 | push @parts, "($s)"; | |||||||||||||||||||||||||||||||||||
| 599 | 20 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||
| 602 | # ORDER BY / LIMIT / OFFSET on the compound | ||||||||||||||||||||||||||||||||||||
| 603 | 9 | ▶ | if ($node->{order_by}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 604 | 0 | ▶ | my @items = ref $node->{order_by} eq 'ARRAY' ? @{$node->{order_by}} : ($node->{order_by}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 605 | 1 | my @osqls; | |||||||||||||||||||||||||||||||||||
| 606 | 1 | for my $o (@items) { | |||||||||||||||||||||||||||||||||||
| 607 | 1 | ▶ | if (ref $o eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||
| 608 | 0 | my ($dir, $col) = each %$o; | |||||||||||||||||||||||||||||||||||
| 609 | 0 | $dir = uc($dir); | |||||||||||||||||||||||||||||||||||
| 610 | 0 | $dir =~ s/^-//; | |||||||||||||||||||||||||||||||||||
| 611 | 0 | ▶ | $self->_assert_order_column($col) unless ref $col; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 612 | 0 | my ($s, @b) = $self->_render_expr($col); | |||||||||||||||||||||||||||||||||||
| 613 | 0 | push @osqls, "$s $dir"; | |||||||||||||||||||||||||||||||||||
| 614 | 0 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 615 | ▶ | } elsif (!ref $o && $o =~ /^-(.+)/) { | |||||||||||||||||||||||||||||||||||
|
Condition33%1/3
not ref $o and $o =~ /^-(.+)/
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 =~ /^-(.+)/
| |||||||||||||||||||||||||||||||||||||
| 616 | 0 | $self->_assert_order_column($1); | |||||||||||||||||||||||||||||||||||
| 617 | 0 | my ($s, @b) = $self->_render_expr($1); | |||||||||||||||||||||||||||||||||||
| 618 | 0 | push @osqls, "$s DESC"; | |||||||||||||||||||||||||||||||||||
| 619 | 0 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 620 | } elsif (!ref $o) { | ||||||||||||||||||||||||||||||||||||
| 621 | 1 | $self->_assert_order_column($o); | |||||||||||||||||||||||||||||||||||
| 622 | 1 | my ($s, @b) = $self->_render_expr($o); | |||||||||||||||||||||||||||||||||||
| 623 | 1 | push @osqls, $s; | |||||||||||||||||||||||||||||||||||
| 624 | 1 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 625 | } else { | ||||||||||||||||||||||||||||||||||||
| 626 | 0 | my ($s, @b) = $self->_render_expr($o); | |||||||||||||||||||||||||||||||||||
| 627 | 0 | push @osqls, $s; | |||||||||||||||||||||||||||||||||||
| 628 | 0 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 629 | } | ||||||||||||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||||||||||
| 631 | 1 | push @parts, "ORDER BY " . join(', ', @osqls); | |||||||||||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||||||||||
| 633 | 9 | ▶ | if (defined $node->{limit}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 634 | 1 | $self->_assert_integer('-limit', $node->{limit}); | |||||||||||||||||||||||||||||||||||
| 635 | 1 | push @parts, "LIMIT ?"; | |||||||||||||||||||||||||||||||||||
| 636 | 1 | push @bind, $node->{limit}; | |||||||||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||||||||||
| 638 | 9 | ▶ | if (defined $node->{offset}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 639 | 0 | $self->_assert_integer('-offset', $node->{offset}); | |||||||||||||||||||||||||||||||||||
| 640 | 0 | push @parts, "OFFSET ?"; | |||||||||||||||||||||||||||||||||||
| 641 | 0 | push @bind, $node->{offset}; | |||||||||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||
| 644 | 9 | return (join(' ', @parts), @bind); | |||||||||||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||||||
| 647 | ## CTE | ||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||
| 649 | 4 | ▶ | sub _render_cte { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_cte: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||
| 650 | 4 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 651 | 4 | my @parts; | |||||||||||||||||||||||||||||||||||
| 652 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||
| 654 | 4 | ▶ | my $keyword = $node->{recursive} ? 'WITH RECURSIVE' : 'WITH'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||
| 656 | 4 | my @cte_sqls; | |||||||||||||||||||||||||||||||||||
| 657 | 4 | for my $cte (@{$node->{ctes}}) { | |||||||||||||||||||||||||||||||||||
| 658 | 5 | my $name = $cte->{name}; | |||||||||||||||||||||||||||||||||||
| 659 | 5 | $self->_injection_guard($name); | |||||||||||||||||||||||||||||||||||
| 660 | 5 | my $query = $cte->{query}; | |||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||
| 662 | # Recursive CTE with -initial and -recurse | ||||||||||||||||||||||||||||||||||||
| 663 | 5 | ▶ | if (ref $query eq 'HASH' && $query->{'-initial'}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $query eq "HASH" and $query->{"-initial"}
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"}
| |||||||||||||||||||||||||||||||||||||
| 664 | 1 | my ($is, @ib) = $self->render($query->{'-initial'}); | |||||||||||||||||||||||||||||||||||
| 665 | 1 | my ($rs, @rb) = $self->render($query->{'-recurse'}); | |||||||||||||||||||||||||||||||||||
| 666 | 1 | push @cte_sqls, $self->_quote_ident_if_needed($name) . " AS ($is UNION ALL $rs)"; | |||||||||||||||||||||||||||||||||||
| 667 | 1 | push @bind, @ib, @rb; | |||||||||||||||||||||||||||||||||||
| 668 | } else { | ||||||||||||||||||||||||||||||||||||
| 669 | 4 | my ($s, @b) = $self->render($query); | |||||||||||||||||||||||||||||||||||
| 670 | 4 | push @cte_sqls, $self->_quote_ident_if_needed($name) . " AS ($s)"; | |||||||||||||||||||||||||||||||||||
| 671 | 4 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 672 | } | ||||||||||||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||
| 675 | 4 | return ("$keyword " . join(', ', @cte_sqls), @bind); | |||||||||||||||||||||||||||||||||||
| 676 | } | ||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||
| 678 | ## INSERT | ||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||
| 680 | 7 | ▶ | sub _render_insert { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_insert: called CC=18 SCAR=28.9
| |||||||||||||||||||||||||||||||||||||
| 681 | 7 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 682 | 7 | my @parts; | |||||||||||||||||||||||||||||||||||
| 683 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||||||
| 685 | 7 | $self->_injection_guard($node->{into}); | |||||||||||||||||||||||||||||||||||
| 686 | 7 | push @parts, "INSERT INTO " . $self->_quote_ident_if_needed($node->{into}); | |||||||||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||||||||||
| 688 | 7 | ▶ | if ($node->{select}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||
| 689 | # INSERT ... SELECT | ||||||||||||||||||||||||||||||||||||
| 690 | 1 | ▶ | if ($node->{columns}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 691 | 1 | $self->_assert_column($_) for @{$node->{columns}}; | |||||||||||||||||||||||||||||||||||
| 692 | 1 | push @parts, "(" . join(', ', map { $self->_quote_ident_if_needed($_) } @{$node->{columns}}) . ")"; | |||||||||||||||||||||||||||||||||||
| 693 | } | ||||||||||||||||||||||||||||||||||||
| 694 | 1 | my ($s, @b) = $self->render($node->{select}); | |||||||||||||||||||||||||||||||||||
| 695 | 1 | push @parts, $s; | |||||||||||||||||||||||||||||||||||
| 696 | 1 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 697 | } elsif (ref $node->{values} eq 'HASH') { | ||||||||||||||||||||||||||||||||||||
| 698 | # Single row insert from hash | ||||||||||||||||||||||||||||||||||||
| 699 | 5 | my @cols = sort keys %{$node->{values}}; | |||||||||||||||||||||||||||||||||||
| 700 | 5 | $self->_assert_column($_) for @cols; | |||||||||||||||||||||||||||||||||||
| 701 | 5 | my @vals; | |||||||||||||||||||||||||||||||||||
| 702 | 5 | for my $col (@cols) { | |||||||||||||||||||||||||||||||||||
| 703 | 9 | my $v = $node->{values}{$col}; | |||||||||||||||||||||||||||||||||||
| 704 | 9 | my ($s, @b) = $self->_render_expr($v); | |||||||||||||||||||||||||||||||||||
| 705 | 9 | push @vals, $s; | |||||||||||||||||||||||||||||||||||
| 706 | 9 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 707 | } | ||||||||||||||||||||||||||||||||||||
| 708 | 5 | push @parts, "(" . join(', ', map { $self->_quote_ident_if_needed($_) } @cols) . ")"; | |||||||||||||||||||||||||||||||||||
| 709 | 5 | push @parts, "VALUES (" . join(', ', @vals) . ")"; | |||||||||||||||||||||||||||||||||||
| 710 | } elsif (ref $node->{values} eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||
| 711 | # Multi-row insert | ||||||||||||||||||||||||||||||||||||
| 712 | 1 | ▶ | if ($node->{columns}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 713 | 1 | $self->_assert_column($_) for @{$node->{columns}}; | |||||||||||||||||||||||||||||||||||
| 714 | 1 | push @parts, "(" . join(', ', map { $self->_quote_ident_if_needed($_) } @{$node->{columns}}) . ")"; | |||||||||||||||||||||||||||||||||||
| 715 | } | ||||||||||||||||||||||||||||||||||||
| 716 | 1 | my @row_sqls; | |||||||||||||||||||||||||||||||||||
| 717 | 1 | for my $row (@{$node->{values}}) { | |||||||||||||||||||||||||||||||||||
| 718 | 2 | my @vals; | |||||||||||||||||||||||||||||||||||
| 719 | 2 | for my $v (@$row) { | |||||||||||||||||||||||||||||||||||
| 720 | 4 | my ($s, @b) = $self->_render_expr($v); | |||||||||||||||||||||||||||||||||||
| 721 | 4 | push @vals, $s; | |||||||||||||||||||||||||||||||||||
| 722 | 4 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 723 | } | ||||||||||||||||||||||||||||||||||||
| 724 | 2 | push @row_sqls, "(" . join(', ', @vals) . ")"; | |||||||||||||||||||||||||||||||||||
| 725 | } | ||||||||||||||||||||||||||||||||||||
| 726 | 1 | push @parts, "VALUES " . join(', ', @row_sqls); | |||||||||||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||||||
| 729 | # ON CONFLICT (PostgreSQL) | ||||||||||||||||||||||||||||||||||||
| 730 | 7 | ▶ | if ($node->{on_conflict}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 731 | 1 | my $oc = $node->{on_conflict}; | |||||||||||||||||||||||||||||||||||
| 732 | 1 | my $target = $oc->{'-target'}; | |||||||||||||||||||||||||||||||||||
| 733 | 1 | $self->_injection_guard($target); | |||||||||||||||||||||||||||||||||||
| 734 | 1 | my $update = $oc->{'-update'}; | |||||||||||||||||||||||||||||||||||
| 735 | 1 | my @set_parts; | |||||||||||||||||||||||||||||||||||
| 736 | 1 | for my $col (sort keys %$update) { | |||||||||||||||||||||||||||||||||||
| 737 | 1 | $self->_assert_column($col); | |||||||||||||||||||||||||||||||||||
| 738 | 1 | my ($s, @b) = $self->_render_expr($update->{$col}); | |||||||||||||||||||||||||||||||||||
| 739 | 1 | push @set_parts, $self->_quote_ident_if_needed($col) . " = $s"; | |||||||||||||||||||||||||||||||||||
| 740 | 1 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 741 | } | ||||||||||||||||||||||||||||||||||||
| 742 | 1 | my $quoted_target = join(', ', map { $self->_quote_ident_if_needed(s/^\s+|\s+$//gr) } split /,/, $target); | |||||||||||||||||||||||||||||||||||
| 743 | 1 | push @parts, "ON CONFLICT ($quoted_target) DO UPDATE SET " . join(', ', @set_parts); | |||||||||||||||||||||||||||||||||||
| 744 | } | ||||||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||||||
| 746 | # ON DUPLICATE KEY (MySQL) | ||||||||||||||||||||||||||||||||||||
| 747 | 7 | ▶ | if ($node->{on_duplicate}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 748 | 1 | my @set_parts; | |||||||||||||||||||||||||||||||||||
| 749 | 1 | for my $col (sort keys %{$node->{on_duplicate}}) { | |||||||||||||||||||||||||||||||||||
| 750 | 1 | $self->_assert_column($col); | |||||||||||||||||||||||||||||||||||
| 751 | 1 | my ($s, @b) = $self->_render_expr($node->{on_duplicate}{$col}); | |||||||||||||||||||||||||||||||||||
| 752 | 1 | push @set_parts, $self->_quote_ident_if_needed($col) . " = $s"; | |||||||||||||||||||||||||||||||||||
| 753 | 1 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 754 | } | ||||||||||||||||||||||||||||||||||||
| 755 | 1 | push @parts, "ON DUPLICATE KEY UPDATE " . join(', ', @set_parts); | |||||||||||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||
| 758 | # RETURNING | ||||||||||||||||||||||||||||||||||||
| 759 | 7 | ▶ | if ($node->{returning}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 760 | 1 | $self->_assert_column($_) for @{$node->{returning}}; | |||||||||||||||||||||||||||||||||||
| 761 | 1 | push @parts, "RETURNING " . join(', ', map { $self->_quote_ident_if_needed($_) } @{$node->{returning}}); | |||||||||||||||||||||||||||||||||||
| 762 | } | ||||||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||||||
| 764 | 7 | return (join(' ', @parts), @bind); | |||||||||||||||||||||||||||||||||||
| 765 | } | ||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||
| 767 | ## UPDATE | ||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||
| 769 | 6 | ▶ | sub _render_update { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_update: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||||||||
| 770 | 6 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 771 | 6 | my @parts; | |||||||||||||||||||||||||||||||||||
| 772 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||||||
| 774 | # Table (possibly with joins) | ||||||||||||||||||||||||||||||||||||
| 775 | 6 | ▶ | if (ref $node->{table} eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 776 | 1 | my @table_parts; | |||||||||||||||||||||||||||||||||||
| 777 | 1 | for my $item (@{$node->{table}}) { | |||||||||||||||||||||||||||||||||||
| 778 | 2 | ▶ | if (blessed($item) && $item->isa('SQL::Wizard::Expr::Join')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($item) and $item->isa("SQL::Wizard::Expr::Join")
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")
| |||||||||||||||||||||||||||||||||||||
| 779 | 1 | my ($s, @b) = $self->render($item); | |||||||||||||||||||||||||||||||||||
| 780 | 1 | push @table_parts, $s; | |||||||||||||||||||||||||||||||||||
| 781 | 1 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 782 | } else { | ||||||||||||||||||||||||||||||||||||
| 783 | 1 | my ($s, @b) = $self->_expand_table($item); | |||||||||||||||||||||||||||||||||||
| 784 | 1 | push @table_parts, $s; | |||||||||||||||||||||||||||||||||||
| 785 | 1 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 786 | } | ||||||||||||||||||||||||||||||||||||
| 787 | } | ||||||||||||||||||||||||||||||||||||
| 788 | 1 | push @parts, "UPDATE " . join(' ', @table_parts); | |||||||||||||||||||||||||||||||||||
| 789 | } else { | ||||||||||||||||||||||||||||||||||||
| 790 | 5 | my ($ts, @tb) = $self->_expand_table($node->{table}); | |||||||||||||||||||||||||||||||||||
| 791 | 5 | push @parts, "UPDATE $ts"; | |||||||||||||||||||||||||||||||||||
| 792 | 5 | push @bind, @tb; | |||||||||||||||||||||||||||||||||||
| 793 | } | ||||||||||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||||||||||
| 795 | # SET | ||||||||||||||||||||||||||||||||||||
| 796 | 6 | my @set_parts; | |||||||||||||||||||||||||||||||||||
| 797 | 6 | for my $col (sort keys %{$node->{set}}) { | |||||||||||||||||||||||||||||||||||
| 798 | 7 | $self->_assert_column($col); | |||||||||||||||||||||||||||||||||||
| 799 | 7 | my ($s, @b) = $self->_render_expr($node->{set}{$col}); | |||||||||||||||||||||||||||||||||||
| 800 | 7 | push @set_parts, $self->_quote_ident_if_needed($col) . " = $s"; | |||||||||||||||||||||||||||||||||||
| 801 | 7 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 802 | } | ||||||||||||||||||||||||||||||||||||
| 803 | 6 | push @parts, "SET " . join(', ', @set_parts); | |||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||
| 805 | # FROM (PostgreSQL) | ||||||||||||||||||||||||||||||||||||
| 806 | 6 | ▶ | if ($node->{from}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 807 | 1 | ▶ | my @from_items = ref $node->{from} eq 'ARRAY' ? @{$node->{from}} : ($node->{from}); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 808 | 1 | my @from_sqls; | |||||||||||||||||||||||||||||||||||
| 809 | 1 | for my $item (@from_items) { | |||||||||||||||||||||||||||||||||||
| 810 | 1 | my ($s, @b) = $self->_expand_table($item); | |||||||||||||||||||||||||||||||||||
| 811 | 1 | push @from_sqls, $s; | |||||||||||||||||||||||||||||||||||
| 812 | 1 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 813 | } | ||||||||||||||||||||||||||||||||||||
| 814 | 1 | push @parts, "FROM " . join(', ', @from_sqls); | |||||||||||||||||||||||||||||||||||
| 815 | } | ||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||
| 817 | # WHERE | ||||||||||||||||||||||||||||||||||||
| 818 | 6 | ▶ | if ($node->{where}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 819 | 5 | my ($ws, @wb) = $self->_render_where($node->{where}); | |||||||||||||||||||||||||||||||||||
| 820 | 5 | ▶ | if (defined $ws && $ws ne '') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ws and $ws ne ""
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 ""
| |||||||||||||||||||||||||||||||||||||
| 821 | 5 | push @parts, "WHERE $ws"; | |||||||||||||||||||||||||||||||||||
| 822 | 5 | push @bind, @wb; | |||||||||||||||||||||||||||||||||||
| 823 | } | ||||||||||||||||||||||||||||||||||||
| 824 | } | ||||||||||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||||||||||
| 826 | # LIMIT (MySQL UPDATE ... LIMIT n) | ||||||||||||||||||||||||||||||||||||
| 827 | 6 | ▶ | if (defined $node->{limit}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 828 | 1 | $self->_assert_integer('-limit', $node->{limit}); | |||||||||||||||||||||||||||||||||||
| 829 | 1 | push @parts, "LIMIT ?"; | |||||||||||||||||||||||||||||||||||
| 830 | 1 | push @bind, $node->{limit}; | |||||||||||||||||||||||||||||||||||
| 831 | } | ||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||
| 833 | # RETURNING | ||||||||||||||||||||||||||||||||||||
| 834 | 6 | ▶ | if ($node->{returning}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 835 | 1 | $self->_assert_column($_) for @{$node->{returning}}; | |||||||||||||||||||||||||||||||||||
| 836 | 1 | push @parts, "RETURNING " . join(', ', map { $self->_quote_ident_if_needed($_) } @{$node->{returning}}); | |||||||||||||||||||||||||||||||||||
| 837 | } | ||||||||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||||||
| 839 | 6 | return (join(' ', @parts), @bind); | |||||||||||||||||||||||||||||||||||
| 840 | } | ||||||||||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||||||||||
| 842 | ## DELETE | ||||||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||||||
| 844 | 6 | ▶ | sub _render_delete { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_delete: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||
| 845 | 6 | my ($self, $node) = @_; | |||||||||||||||||||||||||||||||||||
| 846 | 6 | my @parts; | |||||||||||||||||||||||||||||||||||
| 847 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||||||||||
| 849 | 6 | $self->_injection_guard($node->{from}); | |||||||||||||||||||||||||||||||||||
| 850 | 6 | push @parts, "DELETE FROM " . $self->_quote_ident_if_needed($node->{from}); | |||||||||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||||||||||
| 852 | # USING (PostgreSQL) | ||||||||||||||||||||||||||||||||||||
| 853 | 6 | ▶ | if ($node->{using}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 854 | 1 | $self->_injection_guard($node->{using}); | |||||||||||||||||||||||||||||||||||
| 855 | 1 | push @parts, "USING " . $self->_quote_ident_if_needed($node->{using}); | |||||||||||||||||||||||||||||||||||
| 856 | } | ||||||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||||||
| 858 | # WHERE | ||||||||||||||||||||||||||||||||||||
| 859 | 6 | ▶ | if ($node->{where}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 860 | 5 | my ($ws, @wb) = $self->_render_where($node->{where}); | |||||||||||||||||||||||||||||||||||
| 861 | 5 | ▶ | if (defined $ws && $ws ne '') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ws and $ws ne ""
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 ""
| |||||||||||||||||||||||||||||||||||||
| 862 | 5 | push @parts, "WHERE $ws"; | |||||||||||||||||||||||||||||||||||
| 863 | 5 | push @bind, @wb; | |||||||||||||||||||||||||||||||||||
| 864 | } | ||||||||||||||||||||||||||||||||||||
| 865 | } | ||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||
| 867 | # RETURNING | ||||||||||||||||||||||||||||||||||||
| 868 | 6 | ▶ | if ($node->{returning}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 869 | 1 | $self->_assert_column($_) for @{$node->{returning}}; | |||||||||||||||||||||||||||||||||||
| 870 | 1 | push @parts, "RETURNING " . join(', ', map { $self->_quote_ident_if_needed($_) } @{$node->{returning}}); | |||||||||||||||||||||||||||||||||||
| 871 | } | ||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||
| 873 | 6 | return (join(' ', @parts), @bind); | |||||||||||||||||||||||||||||||||||
| 874 | } | ||||||||||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||||||||||
| 876 | ## WHERE clause rendering (self-contained, SQL::Abstract-compatible syntax) | ||||||||||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||||||||||
| 878 | 117 | ▶ | sub _render_where { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_where: called CC=20 SCAR=31.2
| |||||||||||||||||||||||||||||||||||||
| 879 | 117 | my ($self, $where) = @_; | |||||||||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||||||||||
| 881 | # Expression object | ||||||||||||||||||||||||||||||||||||
| 882 | 117 | ▶ | if (blessed($where) && $where->isa('SQL::Wizard::Expr')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($where) and $where->isa("SQL::Wizard::Expr")
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")
| |||||||||||||||||||||||||||||||||||||
| 883 | 1 | return $self->render($where); | |||||||||||||||||||||||||||||||||||
| 884 | } | ||||||||||||||||||||||||||||||||||||
| 885 | |||||||||||||||||||||||||||||||||||||
| 886 | # Hashref: { col => val, col2 => { '>' => 3 } } | ||||||||||||||||||||||||||||||||||||
| 887 | 116 | ▶ | if (ref $where eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 888 | 103 | my @parts; | |||||||||||||||||||||||||||||||||||
| 889 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 890 | 103 | for my $key (sort keys %$where) { | |||||||||||||||||||||||||||||||||||
| 891 | 105 | my $val = $where->{$key}; | |||||||||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||||||||||
| 893 | # Expression object as key (e.g. $q->func(...) => { '>' => 5 }) | ||||||||||||||||||||||||||||||||||||
| 894 | 105 | ▶ | if (blessed($key) && $key->isa('SQL::Wizard::Expr')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($key) and $key->isa("SQL::Wizard::Expr")
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")
| |||||||||||||||||||||||||||||||||||||
| 895 | 0 | my ($ks, @kb) = $self->render($key); | |||||||||||||||||||||||||||||||||||
| 896 | 0 | my ($vs, @vb) = $self->_render_where_value($ks, $val); | |||||||||||||||||||||||||||||||||||
| 897 | 0 | push @parts, $vs; | |||||||||||||||||||||||||||||||||||
| 898 | 0 | push @bind, @kb, @vb; | |||||||||||||||||||||||||||||||||||
| 899 | 0 | next; | |||||||||||||||||||||||||||||||||||
| 900 | } | ||||||||||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||||||||||
| 902 | 105 | $self->_injection_guard($key); | |||||||||||||||||||||||||||||||||||
| 903 | 105 | my $qkey = $self->_quote_ident_if_needed($key); | |||||||||||||||||||||||||||||||||||
| 904 | |||||||||||||||||||||||||||||||||||||
| 905 | 105 | ▶ | if (!defined $val) { | ||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||
| 906 | 2 | push @parts, "$qkey IS NULL"; | |||||||||||||||||||||||||||||||||||
| 907 | ▶ | } elsif (blessed($val) && $val->isa('SQL::Wizard::Expr')) { | |||||||||||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($val) and $val->isa("SQL::Wizard::Expr")
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")
| |||||||||||||||||||||||||||||||||||||
| 908 | 8 | my ($vs, @vb) = $self->render($val); | |||||||||||||||||||||||||||||||||||
| 909 | 8 | push @parts, "$qkey = $vs"; | |||||||||||||||||||||||||||||||||||
| 910 | 8 | push @bind, @vb; | |||||||||||||||||||||||||||||||||||
| 911 | } elsif (ref $val eq 'HASH') { | ||||||||||||||||||||||||||||||||||||
| 912 | 42 | my ($s, @b) = $self->_render_where_value($qkey, $val); | |||||||||||||||||||||||||||||||||||
| 913 | 41 | push @parts, $s; | |||||||||||||||||||||||||||||||||||
| 914 | 41 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 915 | } elsif (ref $val eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||
| 916 | # { col => [v1, v2, ...] } => col = ? OR col = ? OR ... | ||||||||||||||||||||||||||||||||||||
| 917 | # Each element is processed individually: | ||||||||||||||||||||||||||||||||||||
| 918 | # undef => col IS NULL | ||||||||||||||||||||||||||||||||||||
| 919 | # hashref => operator form (e.g. { '>' => 3 } => col > 3) | ||||||||||||||||||||||||||||||||||||
| 920 | # expr obj => col = <rendered expr> | ||||||||||||||||||||||||||||||||||||
| 921 | # scalar => col = ? | ||||||||||||||||||||||||||||||||||||
| 922 | 4 | ▶ | if (!@$val) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 923 | 1 | push @parts, '1 = 0'; | |||||||||||||||||||||||||||||||||||
| 924 | } else { | ||||||||||||||||||||||||||||||||||||
| 925 | 3 | my @or_parts; | |||||||||||||||||||||||||||||||||||
| 926 | my @or_bind; | ||||||||||||||||||||||||||||||||||||
| 927 | 3 | for my $v (@$val) { | |||||||||||||||||||||||||||||||||||
| 928 | 6 | ▶ | if (!defined $v) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||
| 929 | 1 | push @or_parts, "$qkey IS NULL"; | |||||||||||||||||||||||||||||||||||
| 930 | ▶ | } elsif (blessed($v) && $v->isa('SQL::Wizard::Expr')) { | |||||||||||||||||||||||||||||||||||
|
Condition33%1/3
builtin::blessed($v) and $v->isa("SQL::Wizard::Expr")
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")
| |||||||||||||||||||||||||||||||||||||
| 931 | 0 | my ($s, @b) = $self->render($v); | |||||||||||||||||||||||||||||||||||
| 932 | 0 | push @or_parts, "$qkey = $s"; | |||||||||||||||||||||||||||||||||||
| 933 | 0 | push @or_bind, @b; | |||||||||||||||||||||||||||||||||||
| 934 | } elsif (ref $v eq 'HASH') { | ||||||||||||||||||||||||||||||||||||
| 935 | 1 | my ($s, @b) = $self->_render_where_value($qkey, $v); | |||||||||||||||||||||||||||||||||||
| 936 | 1 | push @or_parts, $s; | |||||||||||||||||||||||||||||||||||
| 937 | 1 | push @or_bind, @b; | |||||||||||||||||||||||||||||||||||
| 938 | } else { | ||||||||||||||||||||||||||||||||||||
| 939 | 4 | push @or_parts, "$qkey = ?"; | |||||||||||||||||||||||||||||||||||
| 940 | 4 | push @or_bind, $v; | |||||||||||||||||||||||||||||||||||
| 941 | } | ||||||||||||||||||||||||||||||||||||
| 942 | } | ||||||||||||||||||||||||||||||||||||
| 943 | 3 | ▶ | if (@or_parts == 1) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 944 | 1 | push @parts, $or_parts[0]; | |||||||||||||||||||||||||||||||||||
| 945 | } else { | ||||||||||||||||||||||||||||||||||||
| 946 | 2 | push @parts, '(' . join(' OR ', @or_parts) . ')'; | |||||||||||||||||||||||||||||||||||
| 947 | } | ||||||||||||||||||||||||||||||||||||
| 948 | 3 | push @bind, @or_bind; | |||||||||||||||||||||||||||||||||||
| 949 | } | ||||||||||||||||||||||||||||||||||||
| 950 | } else { | ||||||||||||||||||||||||||||||||||||
| 951 | 49 | push @parts, "$qkey = ?"; | |||||||||||||||||||||||||||||||||||
| 952 | 49 | push @bind, $val; | |||||||||||||||||||||||||||||||||||
| 953 | } | ||||||||||||||||||||||||||||||||||||
| 954 | } | ||||||||||||||||||||||||||||||||||||
| 955 | 102 | return (join(' AND ', @parts), @bind); | |||||||||||||||||||||||||||||||||||
| 956 | } | ||||||||||||||||||||||||||||||||||||
| 957 | |||||||||||||||||||||||||||||||||||||
| 958 | # Arrayref: [-and => ..., -or => ...] | ||||||||||||||||||||||||||||||||||||
| 959 | 13 | ▶ | if (ref $where eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 960 | 12 | return $self->_render_where_array($where); | |||||||||||||||||||||||||||||||||||
| 961 | } | ||||||||||||||||||||||||||||||||||||
| 962 | |||||||||||||||||||||||||||||||||||||
| 963 | # Plain string | ||||||||||||||||||||||||||||||||||||
| 964 | 1 | $self->_injection_guard($where); | |||||||||||||||||||||||||||||||||||
| 965 | 1 | return ($where, ()); | |||||||||||||||||||||||||||||||||||
| 966 | } | ||||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||||
| 968 | 43 | ▶ | sub _render_where_value { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_where_value: called CC=21 SCAR=31.1
| |||||||||||||||||||||||||||||||||||||
| 969 | 43 | my ($self, $col, $val) = @_; | |||||||||||||||||||||||||||||||||||
| 970 | |||||||||||||||||||||||||||||||||||||
| 971 | 43 | ▶ | if (ref $val eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 972 | 43 | my @parts; | |||||||||||||||||||||||||||||||||||
| 973 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 974 | 43 | for my $op (sort keys %$val) { | |||||||||||||||||||||||||||||||||||
| 975 | 43 | my $rhs = $val->{$op}; | |||||||||||||||||||||||||||||||||||
| 976 | 43 | my $sql_op = uc($op); | |||||||||||||||||||||||||||||||||||
| 977 | |||||||||||||||||||||||||||||||||||||
| 978 | confess "Unknown operator '$op' in WHERE clause" | ||||||||||||||||||||||||||||||||||||
| 979 | 43 | ▶ | unless $VALID_OPS{$sql_op}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 980 | |||||||||||||||||||||||||||||||||||||
| 981 | # -in / -not_in | ||||||||||||||||||||||||||||||||||||
| 982 | 42 | ▶ | if ($sql_op eq '-IN' || $sql_op eq '-NOT_IN') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$sql_op eq "-IN" or $sql_op eq "-NOT_IN"
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"
| |||||||||||||||||||||||||||||||||||||
| 983 | 10 | ▶ | my $neg = $sql_op eq '-NOT_IN' ? 'NOT ' : ''; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 984 | 10 | ▶ | if (blessed($rhs) && $rhs->isa('SQL::Wizard::Expr')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($rhs) and $rhs->isa("SQL::Wizard::Expr")
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")
| |||||||||||||||||||||||||||||||||||||
| 985 | 4 | my ($s, @b) = $self->render($rhs); | |||||||||||||||||||||||||||||||||||
| 986 | 4 | push @parts, "$col ${neg}IN ($s)"; | |||||||||||||||||||||||||||||||||||
| 987 | 4 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 988 | } elsif (ref $rhs eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||
| 989 | 6 | ▶ | if (!@$rhs) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 990 | # Empty list: -in => always false, -not_in => always true | ||||||||||||||||||||||||||||||||||||
| 991 | 2 | ▶ | push @parts, $neg ? '1 = 1' : '1 = 0'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 992 | } else { | ||||||||||||||||||||||||||||||||||||
| 993 | 4 | my @ph; | |||||||||||||||||||||||||||||||||||
| 994 | 4 | for my $v (@$rhs) { | |||||||||||||||||||||||||||||||||||
| 995 | 9 | ▶ | if (blessed($v) && $v->isa('SQL::Wizard::Expr')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($v) and $v->isa("SQL::Wizard::Expr")
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")
| |||||||||||||||||||||||||||||||||||||
| 996 | 0 | my ($s, @b) = $self->render($v); | |||||||||||||||||||||||||||||||||||
| 997 | 0 | push @ph, $s; | |||||||||||||||||||||||||||||||||||
| 998 | 0 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 999 | } else { | ||||||||||||||||||||||||||||||||||||
| 1000 | 9 | push @ph, '?'; | |||||||||||||||||||||||||||||||||||
| 1001 | 9 | push @bind, $v; | |||||||||||||||||||||||||||||||||||
| 1002 | } | ||||||||||||||||||||||||||||||||||||
| 1003 | } | ||||||||||||||||||||||||||||||||||||
| 1004 | 4 | push @parts, "$col ${neg}IN (" . join(', ', @ph) . ")"; | |||||||||||||||||||||||||||||||||||
| 1005 | } | ||||||||||||||||||||||||||||||||||||
| 1006 | } | ||||||||||||||||||||||||||||||||||||
| 1007 | } elsif (!defined $rhs) { | ||||||||||||||||||||||||||||||||||||
| 1008 | 3 | ▶ | if ($sql_op eq '!=' || $sql_op eq '<>') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$sql_op eq "!=" or $sql_op eq "<>"
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 "<>"
| |||||||||||||||||||||||||||||||||||||
| 1009 | 2 | push @parts, "$col IS NOT NULL"; | |||||||||||||||||||||||||||||||||||
| 1010 | } else { | ||||||||||||||||||||||||||||||||||||
| 1011 | 1 | push @parts, "$col IS NULL"; | |||||||||||||||||||||||||||||||||||
| 1012 | } | ||||||||||||||||||||||||||||||||||||
| 1013 | ▶ | } elsif (blessed($rhs) && $rhs->isa('SQL::Wizard::Expr')) { | |||||||||||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($rhs) and $rhs->isa("SQL::Wizard::Expr")
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")
| |||||||||||||||||||||||||||||||||||||
| 1014 | 3 | my ($s, @b) = $self->render($rhs); | |||||||||||||||||||||||||||||||||||
| 1015 | 3 | ▶ | $s = "($s)" if $rhs->isa('SQL::Wizard::Expr::Select'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 1016 | 3 | push @parts, "$col $sql_op $s"; | |||||||||||||||||||||||||||||||||||
| 1017 | 3 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 1018 | } else { | ||||||||||||||||||||||||||||||||||||
| 1019 | 26 | push @parts, "$col $sql_op ?"; | |||||||||||||||||||||||||||||||||||
| 1020 | 26 | push @bind, $rhs; | |||||||||||||||||||||||||||||||||||
| 1021 | } | ||||||||||||||||||||||||||||||||||||
| 1022 | } | ||||||||||||||||||||||||||||||||||||
| 1023 | 42 | return (join(' AND ', @parts), @bind); | |||||||||||||||||||||||||||||||||||
| 1024 | } | ||||||||||||||||||||||||||||||||||||
| 1025 | |||||||||||||||||||||||||||||||||||||
| 1026 | 0 | return ("$col = ?", $val); | |||||||||||||||||||||||||||||||||||
| 1027 | } | ||||||||||||||||||||||||||||||||||||
| 1028 | |||||||||||||||||||||||||||||||||||||
| 1029 | 17 | ▶ | sub _render_where_array { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_render_where_array: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||
| 1030 | 17 | my ($self, $arr, $default_logic) = @_; | |||||||||||||||||||||||||||||||||||
| 1031 | 17 | my @items = @$arr; | |||||||||||||||||||||||||||||||||||
| 1032 | 17 | my @parts; | |||||||||||||||||||||||||||||||||||
| 1033 | my @bind; | ||||||||||||||||||||||||||||||||||||
| 1034 | |||||||||||||||||||||||||||||||||||||
| 1035 | 17 | ▶ | my $logic = $default_logic || 'AND'; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$default_logic || 'AND'
MC/DC100%1/1
$default_logic || 'AND'
$default_logic
Truth table100%2/2
A
A= $default_logic
| |||||||||||||||||||||||||||||||||||||
| 1036 | |||||||||||||||||||||||||||||||||||||
| 1037 | 17 | my $i = 0; | |||||||||||||||||||||||||||||||||||
| 1038 | 17 | ▶ | while ($i <= $#items) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 1039 | 23 | my $item = $items[$i]; | |||||||||||||||||||||||||||||||||||
| 1040 | |||||||||||||||||||||||||||||||||||||
| 1041 | 23 | ▶ | if (!ref $item && $item =~ /^-(and|or)$/i) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not ref $item and $item =~ /^-(and|or)$/i
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
| |||||||||||||||||||||||||||||||||||||
| 1042 | 6 | $logic = uc($1); | |||||||||||||||||||||||||||||||||||
| 1043 | 6 | $i++; | |||||||||||||||||||||||||||||||||||
| 1044 | # Next item could be arrayref of conditions | ||||||||||||||||||||||||||||||||||||
| 1045 | 6 | ▶ | if ($i <= $#items && ref $items[$i] eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i <= $#items and ref $items[$i] eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||||
| 1046 | 4 | my ($s, @b) = $self->_render_where_array($items[$i], $logic); | |||||||||||||||||||||||||||||||||||
| 1047 | 4 | push @parts, $s; | |||||||||||||||||||||||||||||||||||
| 1048 | 4 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 1049 | 4 | $i++; | |||||||||||||||||||||||||||||||||||
| 1050 | } | ||||||||||||||||||||||||||||||||||||
| 1051 | 6 | next; | |||||||||||||||||||||||||||||||||||
| 1052 | } | ||||||||||||||||||||||||||||||||||||
| 1053 | |||||||||||||||||||||||||||||||||||||
| 1054 | 17 | ▶ | if (ref $item eq 'HASH') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||
| 1055 | 11 | my ($s, @b) = $self->_render_where($item); | |||||||||||||||||||||||||||||||||||
| 1056 | 11 | push @parts, $s; | |||||||||||||||||||||||||||||||||||
| 1057 | 11 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 1058 | } elsif (ref $item eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||
| 1059 | 1 | my ($s, @b) = $self->_render_where_array($item); | |||||||||||||||||||||||||||||||||||
| 1060 | 1 | push @parts, "($s)"; | |||||||||||||||||||||||||||||||||||
| 1061 | 1 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 1062 | ▶ | } elsif (blessed($item) && $item->isa('SQL::Wizard::Expr')) { | |||||||||||||||||||||||||||||||||||
|
Condition33%1/3
builtin::blessed($item) and $item->isa("SQL::Wizard::Expr")
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")
| |||||||||||||||||||||||||||||||||||||
| 1063 | 5 | my ($s, @b) = $self->render($item); | |||||||||||||||||||||||||||||||||||
| 1064 | 5 | push @parts, $s; | |||||||||||||||||||||||||||||||||||
| 1065 | 5 | push @bind, @b; | |||||||||||||||||||||||||||||||||||
| 1066 | } | ||||||||||||||||||||||||||||||||||||
| 1067 | |||||||||||||||||||||||||||||||||||||
| 1068 | 17 | $i++; | |||||||||||||||||||||||||||||||||||
| 1069 | } | ||||||||||||||||||||||||||||||||||||
| 1070 | |||||||||||||||||||||||||||||||||||||
| 1071 | 17 | my $joined = join(" $logic ", @parts); | |||||||||||||||||||||||||||||||||||
| 1072 | 17 | ▶ | $joined = "($joined)" if @parts > 1; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 1073 | 17 | return ($joined, @bind); | |||||||||||||||||||||||||||||||||||
| 1074 | } | ||||||||||||||||||||||||||||||||||||
| 1075 | |||||||||||||||||||||||||||||||||||||
| 1076 | 14 | 1; | |||||||||||||||||||||||||||||||||||