| 1 | package SQL::Wizard; | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 7 | 14 | our $VERSION = '0.11'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 14 | ▶ | use SQL::Wizard::Renderer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use SQL::Wizard::Expr::Column; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 14 | ▶ | use SQL::Wizard::Expr::Value; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 14 | ▶ | use SQL::Wizard::Expr::Raw; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 14 | ▶ | use SQL::Wizard::Expr::Func; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 14 | ▶ | use SQL::Wizard::Expr::Case; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 14 | ▶ | use SQL::Wizard::Expr::Join; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 14 | ▶ | use SQL::Wizard::Expr::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 14 | ▶ | use SQL::Wizard::Expr::Insert; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 14 | ▶ | use SQL::Wizard::Expr::Update; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 14 | ▶ | use SQL::Wizard::Expr::Delete; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 14 | ▶ | use SQL::Wizard::Expr::CTE; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 14 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 14 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 24 | my $self = bless { | ||||||||||||||||||||||||||||
| 25 | dialect => $args{dialect} || 'ansi', | ||||||||||||||||||||||||||||
| 26 | 14 | ▶ | renderer => SQL::Wizard::Renderer->new(dialect => $args{dialect} || 'ansi'), | ||||||||||||||||||||||||||
|
Condition50%2/4
$args{'dialect'} || 'ansi'
$args{'dialect'} || 'ansi'
MC/DC0%0/1
$args{'dialect'} || 'ansi'
$args{'dialect'}
MC/DC0%0/1
$args{'dialect'} || 'ansi'
$args{'dialect'}
Truth table50%1/2
A
A= $args{'dialect'}
Truth table50%1/2
A
A= $args{'dialect'}
| |||||||||||||||||||||||||||||
| 27 | }, $class; | ||||||||||||||||||||||||||||
| 28 | 14 | $self; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | ## Expression primitives | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 42 | ▶ | sub col { | ||||||||||||||||||||||||||
|
Subroutine
col: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 42 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 35 | SQL::Wizard::Expr::Column->new( | ||||||||||||||||||||||||||||
| 36 | name => $name, | ||||||||||||||||||||||||||||
| 37 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 38 | 42 | ); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 29 | ▶ | sub val { | ||||||||||||||||||||||||||
|
Subroutine
val: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 29 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 43 | SQL::Wizard::Expr::Value->new( | ||||||||||||||||||||||||||||
| 44 | value => $value, | ||||||||||||||||||||||||||||
| 45 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 46 | 29 | ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 5 | ▶ | sub raw { | ||||||||||||||||||||||||||
|
Subroutine
raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 5 | my ($self, $sql, @bind) = @_; | |||||||||||||||||||||||||||
| 51 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 52 | sql => $sql, | ||||||||||||||||||||||||||||
| 53 | bind => \@bind, | ||||||||||||||||||||||||||||
| 54 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 55 | 5 | ); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 34 | ▶ | sub func { | ||||||||||||||||||||||||||
|
Subroutine
func: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 34 | my ($self, $name, @args) = @_; | |||||||||||||||||||||||||||
| 60 | 34 | ▶ | confess "func name must be a word (\\w+), got '$name'" unless $name =~ /^\w+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | # Coerce plain strings/values: strings in func args are column refs | ||||||||||||||||||||||||||||
| 62 | my @coerced = map { | ||||||||||||||||||||||||||||
| 63 | 34 | ref $_ ? $_ : SQL::Wizard::Expr::Column->new( | |||||||||||||||||||||||||||
| 64 | name => $_, | ||||||||||||||||||||||||||||
| 65 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 66 | ) | ||||||||||||||||||||||||||||
| 67 | 31 | ▶ | } @args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | SQL::Wizard::Expr::Func->new( | ||||||||||||||||||||||||||||
| 69 | name => $name, | ||||||||||||||||||||||||||||
| 70 | args => \@coerced, | ||||||||||||||||||||||||||||
| 71 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 72 | 34 | ); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | ## Query builders | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 127 | ▶ | sub select { | ||||||||||||||||||||||||||
|
Subroutine
select: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 127 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 79 | 127 | SQL::Wizard::Expr::Select->from_args(%args, _renderer => $self->{renderer}); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 7 | ▶ | sub insert { | ||||||||||||||||||||||||||
|
Subroutine
insert: called CC=16 SCAR=27.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 7 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 84 | 7 | ▶ | confess "insert requires -into" unless $args{'-into'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 7 | my %node; | |||||||||||||||||||||||||||
| 86 | 7 | ▶ | $node{into} = $args{'-into'} if $args{'-into'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 7 | ▶ | $node{values} = $args{'-values'} if $args{'-values'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 7 | ▶ | $node{columns} = $args{'-columns'} if $args{'-columns'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 7 | ▶ | $node{select} = $args{'-select'} if $args{'-select'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 7 | ▶ | $node{on_conflict} = $args{'-on_conflict'} if $args{'-on_conflict'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 7 | ▶ | $node{on_duplicate} = $args{'-on_duplicate'} if $args{'-on_duplicate'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 7 | ▶ | $node{returning} = $args{'-returning'} if $args{'-returning'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | # Coerce hash values to Value nodes for bind params | ||||||||||||||||||||||||||||
| 94 | 7 | ▶ | if (ref $node{values} eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 95 | 5 | for my $k (keys %{$node{values}}) { | |||||||||||||||||||||||||||
| 96 | 9 | my $v = $node{values}{$k}; | |||||||||||||||||||||||||||
| 97 | 9 | ▶ | next if ref $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | $node{values}{$k} = SQL::Wizard::Expr::Value->new( | ||||||||||||||||||||||||||||
| 99 | value => $v, | ||||||||||||||||||||||||||||
| 100 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 101 | 8 | ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } elsif (ref $node{values} eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 104 | # Multi-row: coerce each cell | ||||||||||||||||||||||||||||
| 105 | 1 | for my $row (@{$node{values}}) { | |||||||||||||||||||||||||||
| 106 | 2 | for my $i (0 .. $#$row) { | |||||||||||||||||||||||||||
| 107 | 4 | ▶ | next if ref $row->[$i]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | $row->[$i] = SQL::Wizard::Expr::Value->new( | ||||||||||||||||||||||||||||
| 109 | value => $row->[$i], | ||||||||||||||||||||||||||||
| 110 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 111 | 4 | ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | SQL::Wizard::Expr::Insert->new( | ||||||||||||||||||||||||||||
| 116 | %node, | ||||||||||||||||||||||||||||
| 117 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 118 | 7 | ); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 6 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: called CC=12 SCAR=25.1
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 6 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 123 | 6 | ▶ | confess "update requires -table" unless $args{'-table'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 6 | ▶ | confess "update requires -set" unless $args{'-set'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 6 | my %node; | |||||||||||||||||||||||||||
| 126 | 6 | ▶ | $node{table} = $args{'-table'} if $args{'-table'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 6 | ▶ | $node{set} = $args{'-set'} if $args{'-set'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 6 | ▶ | $node{where} = $args{'-where'} if $args{'-where'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 6 | ▶ | $node{from} = $args{'-from'} if $args{'-from'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 6 | ▶ | $node{limit} = $args{'-limit'} if defined $args{'-limit'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 6 | ▶ | $node{returning} = $args{'-returning'} if $args{'-returning'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | # Coerce set values | ||||||||||||||||||||||||||||
| 133 | 6 | ▶ | if (ref $node{set} eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 6 | for my $k (keys %{$node{set}}) { | |||||||||||||||||||||||||||
| 135 | 7 | my $v = $node{set}{$k}; | |||||||||||||||||||||||||||
| 136 | 7 | ▶ | next if ref $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | $node{set}{$k} = SQL::Wizard::Expr::Value->new( | ||||||||||||||||||||||||||||
| 138 | value => $v, | ||||||||||||||||||||||||||||
| 139 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 140 | 4 | ); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | SQL::Wizard::Expr::Update->new( | ||||||||||||||||||||||||||||
| 144 | %node, | ||||||||||||||||||||||||||||
| 145 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 146 | 6 | ); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 6 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 6 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 151 | 6 | ▶ | confess "delete requires -from" unless $args{'-from'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 6 | my %node; | |||||||||||||||||||||||||||
| 153 | 6 | ▶ | $node{from} = $args{'-from'} if $args{'-from'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 6 | ▶ | $node{where} = $args{'-where'} if $args{'-where'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 6 | ▶ | $node{using} = $args{'-using'} if $args{'-using'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 6 | ▶ | $node{returning} = $args{'-returning'} if $args{'-returning'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | SQL::Wizard::Expr::Delete->new( | ||||||||||||||||||||||||||||
| 158 | %node, | ||||||||||||||||||||||||||||
| 159 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 160 | 6 | ); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | sub truncate { | ||||||||||||||||||||||||||
|
Subroutine
truncate: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 164 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | confess "truncate requires -table" unless $args{'-table'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 167 | sql => '', | ||||||||||||||||||||||||||||
| 168 | bind => [], | ||||||||||||||||||||||||||||
| 169 | _truncate => $args{'-table'}, | ||||||||||||||||||||||||||||
| 170 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 171 | 0 | ); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | ## Join helpers | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 7 | ▶ | sub join { | ||||||||||||||||||||||||||
|
Subroutine
join: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 177 | 7 | my ($self, $table, $on) = @_; | |||||||||||||||||||||||||||
| 178 | SQL::Wizard::Expr::Join->new( | ||||||||||||||||||||||||||||
| 179 | type => 'JOIN', | ||||||||||||||||||||||||||||
| 180 | table => $table, | ||||||||||||||||||||||||||||
| 181 | on => $on, | ||||||||||||||||||||||||||||
| 182 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 183 | 7 | ); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 3 | ▶ | sub left_join { | ||||||||||||||||||||||||||
|
Subroutine
left_join: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 187 | 3 | my ($self, $table, $on) = @_; | |||||||||||||||||||||||||||
| 188 | SQL::Wizard::Expr::Join->new( | ||||||||||||||||||||||||||||
| 189 | type => 'LEFT JOIN', | ||||||||||||||||||||||||||||
| 190 | table => $table, | ||||||||||||||||||||||||||||
| 191 | on => $on, | ||||||||||||||||||||||||||||
| 192 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 193 | 3 | ); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 1 | ▶ | sub right_join { | ||||||||||||||||||||||||||
|
Subroutine
right_join: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 197 | 1 | my ($self, $table, $on) = @_; | |||||||||||||||||||||||||||
| 198 | SQL::Wizard::Expr::Join->new( | ||||||||||||||||||||||||||||
| 199 | type => 'RIGHT JOIN', | ||||||||||||||||||||||||||||
| 200 | table => $table, | ||||||||||||||||||||||||||||
| 201 | on => $on, | ||||||||||||||||||||||||||||
| 202 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 203 | 1 | ); | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 1 | ▶ | sub full_join { | ||||||||||||||||||||||||||
|
Subroutine
full_join: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 207 | 1 | my ($self, $table, $on) = @_; | |||||||||||||||||||||||||||
| 208 | SQL::Wizard::Expr::Join->new( | ||||||||||||||||||||||||||||
| 209 | type => 'FULL OUTER JOIN', | ||||||||||||||||||||||||||||
| 210 | table => $table, | ||||||||||||||||||||||||||||
| 211 | on => $on, | ||||||||||||||||||||||||||||
| 212 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 213 | 1 | ); | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 1 | ▶ | sub cross_join { | ||||||||||||||||||||||||||
|
Subroutine
cross_join: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 217 | 1 | my ($self, $table) = @_; | |||||||||||||||||||||||||||
| 218 | SQL::Wizard::Expr::Join->new( | ||||||||||||||||||||||||||||
| 219 | type => 'CROSS JOIN', | ||||||||||||||||||||||||||||
| 220 | table => $table, | ||||||||||||||||||||||||||||
| 221 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 222 | 1 | ); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | ## CASE expressions | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 5 | ▶ | sub case { | ||||||||||||||||||||||||||
|
Subroutine
case: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 228 | 5 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 229 | 5 | my ($whens, $else) = $self->_parse_case_args(@args); | |||||||||||||||||||||||||||
| 230 | SQL::Wizard::Expr::Case->new( | ||||||||||||||||||||||||||||
| 231 | whens => $whens, | ||||||||||||||||||||||||||||
| 232 | ($else ? (else => $else) : ()), | ||||||||||||||||||||||||||||
| 233 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 234 | 5 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 1 | ▶ | sub case_on { | ||||||||||||||||||||||||||
|
Subroutine
case_on: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 238 | 1 | my ($self, $operand, @args) = @_; | |||||||||||||||||||||||||||
| 239 | 1 | my ($whens, $else) = $self->_parse_case_args(@args); | |||||||||||||||||||||||||||
| 240 | SQL::Wizard::Expr::Case->new( | ||||||||||||||||||||||||||||
| 241 | operand => $operand, | ||||||||||||||||||||||||||||
| 242 | whens => $whens, | ||||||||||||||||||||||||||||
| 243 | ($else ? (else => $else) : ()), | ||||||||||||||||||||||||||||
| 244 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 245 | 1 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 6 | ▶ | sub _parse_case_args { | ||||||||||||||||||||||||||
|
Subroutine
_parse_case_args: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||
| 249 | 6 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 250 | 6 | my @whens; | |||||||||||||||||||||||||||
| 251 | my $else; | ||||||||||||||||||||||||||||
| 252 | 6 | for my $arg (@args) { | |||||||||||||||||||||||||||
| 253 | 14 | ▶ | if (ref $arg eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 254 | # [$q->when(...)] — a when clause | ||||||||||||||||||||||||||||
| 255 | 9 | push @whens, @$arg; | |||||||||||||||||||||||||||
| 256 | ▶ | } elsif (ref $arg eq 'HASH' && exists $arg->{_else}) { | |||||||||||||||||||||||||||
|
Condition33%1/3
ref $arg eq "HASH" and exists $arg->{"_else"}
MC/DC0%0/2
ref $arg eq "HASH" and exists $arg->{"_else"}
ref $arg eq "HASH"
exists $arg->{"_else"}
Truth table33%1/3
A and B
A= ref $arg eq "HASH"B= exists $arg->{"_else"}
| |||||||||||||||||||||||||||||
| 257 | 5 | $else = $arg->{_else}; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | 6 | return (\@whens, $else); | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 9 | ▶ | sub when { | ||||||||||||||||||||||||||
|
Subroutine
when: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 264 | 9 | my ($self, $condition, $then) = @_; | |||||||||||||||||||||||||||
| 265 | # Coerce then value | ||||||||||||||||||||||||||||
| 266 | 9 | ▶ | $then = $self->val($then) unless ref $then; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | 9 | return { condition => $condition, then => $then }; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 5 | ▶ | sub else { | ||||||||||||||||||||||||||
|
Subroutine
else: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 271 | 5 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 272 | 5 | ▶ | $value = $self->val($value) unless ref $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 5 | return { _else => $value }; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | ## Condition helpers | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 2 | ▶ | sub exists { | ||||||||||||||||||||||||||
|
Subroutine
exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 279 | 2 | my ($self, $subquery) = @_; | |||||||||||||||||||||||||||
| 280 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 281 | sql => 'EXISTS', | ||||||||||||||||||||||||||||
| 282 | bind => [], | ||||||||||||||||||||||||||||
| 283 | _subquery => $subquery, | ||||||||||||||||||||||||||||
| 284 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 285 | 2 | ); | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 1 | ▶ | sub not_exists { | ||||||||||||||||||||||||||
|
Subroutine
not_exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 289 | 1 | my ($self, $subquery) = @_; | |||||||||||||||||||||||||||
| 290 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 291 | sql => 'NOT EXISTS', | ||||||||||||||||||||||||||||
| 292 | bind => [], | ||||||||||||||||||||||||||||
| 293 | _subquery => $subquery, | ||||||||||||||||||||||||||||
| 294 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 295 | 1 | ); | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 2 | ▶ | sub any { | ||||||||||||||||||||||||||
|
Subroutine
any: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 299 | 2 | my ($self, $subquery) = @_; | |||||||||||||||||||||||||||
| 300 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 301 | sql => 'ANY', | ||||||||||||||||||||||||||||
| 302 | bind => [], | ||||||||||||||||||||||||||||
| 303 | _subquery => $subquery, | ||||||||||||||||||||||||||||
| 304 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 305 | 2 | ); | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 2 | ▶ | sub all { | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 309 | 2 | my ($self, $subquery) = @_; | |||||||||||||||||||||||||||
| 310 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 311 | sql => 'ALL', | ||||||||||||||||||||||||||||
| 312 | bind => [], | ||||||||||||||||||||||||||||
| 313 | _subquery => $subquery, | ||||||||||||||||||||||||||||
| 314 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 315 | 2 | ); | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 1 | ▶ | sub between { | ||||||||||||||||||||||||||
|
Subroutine
between: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 319 | 1 | my ($self, $col, $lo, $hi) = @_; | |||||||||||||||||||||||||||
| 320 | 1 | ▶ | $col = $self->col($col) unless ref $col; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | 1 | ▶ | $lo = $self->val($lo) unless ref $lo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 1 | ▶ | $hi = $self->val($hi) unless ref $hi; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 324 | sql => 'BETWEEN', | ||||||||||||||||||||||||||||
| 325 | bind => [], | ||||||||||||||||||||||||||||
| 326 | _between => { col => $col, lo => $lo, hi => $hi }, | ||||||||||||||||||||||||||||
| 327 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 328 | 1 | ); | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 1 | ▶ | sub not_between { | ||||||||||||||||||||||||||
|
Subroutine
not_between: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 332 | 1 | my ($self, $col, $lo, $hi) = @_; | |||||||||||||||||||||||||||
| 333 | 1 | ▶ | $col = $self->col($col) unless ref $col; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | 1 | ▶ | $lo = $self->val($lo) unless ref $lo; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | 1 | ▶ | $hi = $self->val($hi) unless ref $hi; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 336 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 337 | sql => 'NOT BETWEEN', | ||||||||||||||||||||||||||||
| 338 | bind => [], | ||||||||||||||||||||||||||||
| 339 | _not_between => { col => $col, lo => $lo, hi => $hi }, | ||||||||||||||||||||||||||||
| 340 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 341 | 1 | ); | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 4 | ▶ | sub compare { | ||||||||||||||||||||||||||
|
Subroutine
compare: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 345 | 4 | my ($self, $left, $op, $right) = @_; | |||||||||||||||||||||||||||
| 346 | 4 | ▶ | $left = $self->col($left) if (!ref $left && $left =~ /^[\w.]+$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
CORE::not ref $left and $left =~ /^[\w.]+$/
MC/DC50%1/2
CORE::not ref $left and $left =~ /^[\w.]+$/
CORE::not ref $left
$left =~ /^[\w.]+$/
Truth table66%2/3
not(A) and B
A= CORE::not ref $leftB= $left =~ /^[\w.]+$/
| |||||||||||||||||||||||||||||
| 347 | 4 | ▶ | $left = $self->val($left) if (!ref $left); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 4 | ▶ | $right = $self->val($right) if (!ref $right); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 350 | sql => '', | ||||||||||||||||||||||||||||
| 351 | bind => [], | ||||||||||||||||||||||||||||
| 352 | _compare => { left => $left, op => $op, right => $right }, | ||||||||||||||||||||||||||||
| 353 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 354 | 4 | ); | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | ## Function shortcuts | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 1 | ▶ | sub cast { | ||||||||||||||||||||||||||
|
Subroutine
cast: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 360 | 1 | my ($self, $expr, $type) = @_; | |||||||||||||||||||||||||||
| 361 | 1 | ▶ | $expr = $self->col($expr) unless ref $expr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 363 | sql => "CAST", | ||||||||||||||||||||||||||||
| 364 | bind => [], | ||||||||||||||||||||||||||||
| 365 | _cast => { expr => $expr, type => $type }, | ||||||||||||||||||||||||||||
| 366 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 367 | 1 | ); | |||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 1 | ▶ | sub coalesce { my $self = shift; $self->func('COALESCE', @_) } | ||||||||||||||||||||||||||
|
Subroutine
coalesce: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 371 | 1 | ▶ | sub greatest { my $self = shift; $self->func('GREATEST', @_) } | ||||||||||||||||||||||||||
|
Subroutine
greatest: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 372 | 1 | ▶ | sub least { my $self = shift; $self->func('LEAST', @_) } | ||||||||||||||||||||||||||
|
Subroutine
least: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 373 | 2 | ▶ | sub now { my $self = shift; $self->func('NOW') } | ||||||||||||||||||||||||||
|
Subroutine
now: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | ## Boolean operators | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 1 | ▶ | sub and { | ||||||||||||||||||||||||||
|
Subroutine
and: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 378 | 1 | my ($self, @conds) = @_; | |||||||||||||||||||||||||||
| 379 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 380 | sql => 'AND', | ||||||||||||||||||||||||||||
| 381 | bind => [], | ||||||||||||||||||||||||||||
| 382 | _logic => { op => 'AND', conds => \@conds }, | ||||||||||||||||||||||||||||
| 383 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 384 | 1 | ); | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 1 | ▶ | sub or { | ||||||||||||||||||||||||||
|
Subroutine
or: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 388 | 1 | my ($self, @conds) = @_; | |||||||||||||||||||||||||||
| 389 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 390 | sql => 'OR', | ||||||||||||||||||||||||||||
| 391 | bind => [], | ||||||||||||||||||||||||||||
| 392 | _logic => { op => 'OR', conds => \@conds }, | ||||||||||||||||||||||||||||
| 393 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 394 | 1 | ); | |||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 1 | ▶ | sub not { | ||||||||||||||||||||||||||
|
Subroutine
not: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 398 | 1 | my ($self, $cond) = @_; | |||||||||||||||||||||||||||
| 399 | SQL::Wizard::Expr::Raw->new( | ||||||||||||||||||||||||||||
| 400 | sql => 'NOT', | ||||||||||||||||||||||||||||
| 401 | bind => [], | ||||||||||||||||||||||||||||
| 402 | _not => $cond, | ||||||||||||||||||||||||||||
| 403 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 404 | 1 | ); | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | ## CTEs | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 3 | ▶ | sub with { | ||||||||||||||||||||||||||
|
Subroutine
with: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 410 | 3 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 411 | 3 | my @ctes; | |||||||||||||||||||||||||||
| 412 | 3 | ▶ | while (@args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | 4 | my $name = shift @args; | |||||||||||||||||||||||||||
| 414 | 4 | my $query = shift @args; | |||||||||||||||||||||||||||
| 415 | 4 | push @ctes, { name => $name, query => $query }; | |||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||
| 417 | SQL::Wizard::Expr::CTE->new( | ||||||||||||||||||||||||||||
| 418 | ctes => \@ctes, | ||||||||||||||||||||||||||||
| 419 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 420 | 3 | ); | |||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 1 | ▶ | sub with_recursive { | ||||||||||||||||||||||||||
|
Subroutine
with_recursive: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 424 | 1 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 425 | 1 | my @ctes; | |||||||||||||||||||||||||||
| 426 | 1 | ▶ | while (@args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 427 | 1 | my $name = shift @args; | |||||||||||||||||||||||||||
| 428 | 1 | my $query = shift @args; | |||||||||||||||||||||||||||
| 429 | 1 | push @ctes, { name => $name, query => $query }; | |||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | SQL::Wizard::Expr::CTE->new( | ||||||||||||||||||||||||||||
| 432 | ctes => \@ctes, | ||||||||||||||||||||||||||||
| 433 | recursive => 1, | ||||||||||||||||||||||||||||
| 434 | _renderer => $self->{renderer}, | ||||||||||||||||||||||||||||
| 435 | 1 | ); | |||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | 14 | 1; | |||||||||||||||||||||||||||