| 1 | package SQL::Composer::Select; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 3 | require Carp; | |||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use SQL::Composer::Join; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use SQL::Composer::Expression; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use SQL::Composer::Quoter; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | sub new { | ||||||||||||||||||||||||||||||||||
| 13 | 31 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=29 SCAR=33.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 14 | 31 | my (%params) = @_; | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 31 | my $self = { table => $params{from} }; | |||||||||||||||||||||||||||||||||
| 17 | 31 | bless $self, $class; | |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | 31 | $self->{from} = $params{from}; | |||||||||||||||||||||||||||||||||
| 20 | 31 | $self->{columns} = $params{columns}; | |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 31 | $self->{join} = $params{join}; | |||||||||||||||||||||||||||||||||
| 23 | $self->{join} = [$self->{join}] | ||||||||||||||||||||||||||||||||||
| 24 | 31 | ▶ | if $self->{join} && ref $self->{join} ne 'ARRAY'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"join"} and ref $self->{"join"} ne "ARRAY"
MC/DC100%2/2
$self->{"join"} and ref $self->{"join"} ne "ARRAY"
$self->{"join"}
ref $self->{"join"} ne "ARRAY"
Truth table100%3/3
A and B
A= $self->{"join"}B= ref $self->{"join"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | $self->{quoter} = | ||||||||||||||||||||||||||||||||||
| 27 | 31 | ▶ | $params{quoter} || SQL::Composer::Quoter->new(driver => $params{driver}); | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$params{'quoter'} || 'SQL::Composer::Quoter'->new('driver', $params{'driver'})
MC/DC0%0/2
$params{'quoter'} || 'SQL::Composer::Quoter'->new('driver', $params{'driver'})
$params{'quoter'}
'SQL::Composer::Quoter'->new('driver', $params{'driver'})
Truth table33%1/3
A || B
A= $params{'quoter'}B= 'SQL::Composer::Quoter'->new('driver', $params{'driver'})
| |||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 31 | my $sql = ''; | |||||||||||||||||||||||||||||||||
| 30 | 31 | my @bind; | |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | my @columns = | ||||||||||||||||||||||||||||||||||
| 33 | 52 | map { $self->_prepare_column($_, $self->{from}, \@bind) } | |||||||||||||||||||||||||||||||||
| 34 | 31 | @{$self->{columns}}; | |||||||||||||||||||||||||||||||||
| 35 | 31 | push @columns, $self->_collect_columns_from_joins($self->{join}); | |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | 31 | $sql .= 'SELECT '; | |||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | 31 | ▶ | if (@columns) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 40 | 31 | $sql .= join ',', @columns; | |||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | 31 | $sql .= ' FROM '; | |||||||||||||||||||||||||||||||||
| 44 | 31 | $sql .= $self->_quote($params{from}); | |||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | 31 | ▶ | if (my $joins = $self->{join}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 7 | my ($join_sql, $join_bind) = $self->_build_join($joins); | |||||||||||||||||||||||||||||||||
| 48 | 7 | $sql .= $join_sql; | |||||||||||||||||||||||||||||||||
| 49 | 7 | push @bind, @$join_bind; | |||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | 31 | ▶ | if (my $where = $params{where}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | if (!Scalar::Util::blessed($where)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 54 | $where = SQL::Composer::Expression->new( | ||||||||||||||||||||||||||||||||||
| 55 | default_prefix => $self->{from}, | ||||||||||||||||||||||||||||||||||
| 56 | quoter => $self->{quoter}, | ||||||||||||||||||||||||||||||||||
| 57 | 1 | expr => $where | |||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | if (my $where_sql = $where->to_sql) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 62 | 1 | $sql .= ' WHERE ' . $where_sql; | |||||||||||||||||||||||||||||||||
| 63 | 1 | push @bind, $where->to_bind; | |||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||
| 67 | 31 | ▶ | if (my $group_bys = $params{group_by}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | $group_bys = [$group_bys] unless ref $group_bys eq 'ARRAY'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | 4 | my @group_by; | |||||||||||||||||||||||||||||||||
| 71 | 4 | foreach my $group_by (@$group_bys) { | |||||||||||||||||||||||||||||||||
| 72 | push @group_by, | ||||||||||||||||||||||||||||||||||
| 73 | ref($group_by) | ||||||||||||||||||||||||||||||||||
| 74 | ? $$group_by | ||||||||||||||||||||||||||||||||||
| 75 | 6 | ▶ | : $self->_quote($group_by, $self->{from}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | 4 | $sql .= ' GROUP BY ' . join(', ', @group_by); | |||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | 31 | ▶ | if (my $having = $params{having}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | if (!Scalar::Util::blessed($having)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 83 | $having = SQL::Composer::Expression->new( | ||||||||||||||||||||||||||||||||||
| 84 | default_prefix => $self->{from}, | ||||||||||||||||||||||||||||||||||
| 85 | quoter => $self->{quoter}, | ||||||||||||||||||||||||||||||||||
| 86 | 1 | expr => $having | |||||||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | if (my $having_sql = $having->to_sql) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 91 | 1 | $sql .= ' HAVING ' . $having->to_sql; | |||||||||||||||||||||||||||||||||
| 92 | 1 | push @bind, $having->to_bind; | |||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | 31 | ▶ | if (my $order_by = $params{order_by}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 97 | 8 | $sql .= ' ORDER BY '; | |||||||||||||||||||||||||||||||||
| 98 | 8 | ▶ | if (ref $order_by) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 99 | 7 | ▶ | if (ref($order_by) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 100 | 6 | my @order; | |||||||||||||||||||||||||||||||||
| 101 | 6 | ▶ | while (my ($key, $value) = splice @$order_by, 0, 2) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 102 | 7 | my $order_type; | |||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | 7 | ▶ | if (ref $value) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 105 | 1 | $order_type = ' ' . $$value; | |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | else { | ||||||||||||||||||||||||||||||||||
| 108 | 6 | ▶ | $order_type = uc($value // ''); | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$value // ""
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
| |||||||||||||||||||||||||||||||||||
| 109 | 6 | ▶ | if ($order_type eq 'ASC' || $order_type eq 'DESC') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$order_type eq "ASC" or $order_type eq "DESC"
MC/DC100%2/2
$order_type eq "ASC" or $order_type eq "DESC"
$order_type eq "ASC"
$order_type eq "DESC"
Truth table100%3/3
A or B
A= $order_type eq "ASC"B= $order_type eq "DESC"
| |||||||||||||||||||||||||||||||||||
| 110 | 4 | $order_type = " $order_type"; | |||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||||||||||||||||
| 113 | 2 | $order_type = ''; | |||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | 7 | ▶ | if (ref($key) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 118 | 1 | push @order, $$key . $order_type; | |||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||||||||||||||
| 121 | push @order, | ||||||||||||||||||||||||||||||||||
| 122 | 6 | $self->_quote($key, $self->{from}) . $order_type; | |||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||
| 125 | 6 | $sql .= join ',', @order; | |||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||
| 127 | elsif (ref($order_by) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||
| 128 | 1 | $sql .= $$order_by; | |||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||
| 130 | else { | ||||||||||||||||||||||||||||||||||
| 131 | 0 | Carp::croak('unexpected reference'); | |||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||||||||
| 135 | 1 | $sql .= $self->_quote($order_by); | |||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||
| 139 | 31 | ▶ | if (defined(my $limit = $params{limit})) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 140 | 4 | $sql .= ' LIMIT ' . $limit; | |||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||
| 143 | 31 | ▶ | if (defined(my $offset = $params{offset})) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 144 | 2 | $sql .= ' OFFSET ' . $offset; | |||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||
| 147 | 31 | ▶ | if ($params{for_update}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 148 | 1 | $sql .= ' FOR UPDATE'; | |||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||
| 151 | 31 | $self->{sql} = $sql; | |||||||||||||||||||||||||||||||||
| 152 | 31 | $self->{bind} = \@bind; | |||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | 31 | return $self; | |||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | 1 | ▶ | sub table { shift->{table} } | ||||||||||||||||||||||||||||||||
|
Subroutine
table: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | 30 | ▶ | sub to_sql { shift->{sql} } | ||||||||||||||||||||||||||||||||
|
Subroutine
to_sql: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 160 | 30 | ▶ | sub to_bind { @{shift->{bind} || []} } | ||||||||||||||||||||||||||||||||
|
Subroutine
to_bind: called CC=2 SCAR=7.2
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | sub from_rows { | ||||||||||||||||||||||||||||||||||
| 163 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
from_rows: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 164 | 10 | my ($rows) = @_; | |||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | 10 | my $result = []; | |||||||||||||||||||||||||||||||||
| 167 | 10 | foreach my $row (@$rows) { | |||||||||||||||||||||||||||||||||
| 168 | 10 | my $set = {}; | |||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||
| 170 | 10 | $self->_populate($set, $row, $self->{columns}); | |||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||
| 172 | 10 | $self->_populate_joins($set, $row, $self->{join}); | |||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | 10 | push @$result, $set; | |||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||
| 177 | 10 | return $result; | |||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||
| 180 | sub _prepare_column { | ||||||||||||||||||||||||||||||||||
| 181 | 61 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_prepare_column: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||
| 182 | 61 | my ($column, $prefix, $bind) = @_; | |||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | 61 | ▶ | if (ref $column eq 'SCALAR') { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 185 | 1 | return $$column; | |||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||
| 187 | elsif (ref $column eq 'HASH') { | ||||||||||||||||||||||||||||||||||
| 188 | return ( | ||||||||||||||||||||||||||||||||||
| 189 | ref($column->{-col}) | ||||||||||||||||||||||||||||||||||
| 190 | ? ( | ||||||||||||||||||||||||||||||||||
| 191 | do { | ||||||||||||||||||||||||||||||||||
| 192 | 2 | my $value = $column->{-col}; | |||||||||||||||||||||||||||||||||
| 193 | 2 | ▶ | if (ref $$value eq 'ARRAY') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 194 | 1 | my $sql = $$value->[0]; | |||||||||||||||||||||||||||||||||
| 195 | 1 | push @$bind, @$$value[1 .. $#{$$value}]; | |||||||||||||||||||||||||||||||||
| 196 | 1 | $sql; | |||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||
| 198 | else { | ||||||||||||||||||||||||||||||||||
| 199 | 1 | $$value; | |||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||
| 202 | ) | ||||||||||||||||||||||||||||||||||
| 203 | : $self->_quote($column->{-col}, $prefix) | ||||||||||||||||||||||||||||||||||
| 204 | ) | ||||||||||||||||||||||||||||||||||
| 205 | . ' AS ' | ||||||||||||||||||||||||||||||||||
| 206 | 3 | ▶ | . $self->_quote($column->{-as}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||
| 208 | else { | ||||||||||||||||||||||||||||||||||
| 209 | 57 | return $self->_quote($column, $prefix); | |||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||
| 213 | sub _populate { | ||||||||||||||||||||||||||||||||||
| 214 | 19 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_populate: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 215 | 19 | my ($set, $row, $columns) = @_; | |||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||
| 217 | 19 | my $name; | |||||||||||||||||||||||||||||||||
| 218 | 19 | foreach my $column (@$columns) { | |||||||||||||||||||||||||||||||||
| 219 | 20 | ▶ | if (ref($column) eq 'HASH') { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 220 | 2 | $name = $column->{-as}; | |||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||
| 222 | elsif (ref($column) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||
| 223 | 1 | $name = $$column; | |||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||
| 225 | else { | ||||||||||||||||||||||||||||||||||
| 226 | 17 | $name = $column; | |||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||
| 229 | 20 | $set->{$name} = shift @$row; | |||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||
| 233 | sub _populate_joins { | ||||||||||||||||||||||||||||||||||
| 234 | 13 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_populate_joins: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||||||||
| 235 | 13 | my ($set, $row, $joins) = @_; | |||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||
| 237 | 13 | foreach my $join (@$joins) { | |||||||||||||||||||||||||||||||||
| 238 | 9 | ▶ | my $join_source = $join->{rel_name} || $join->{as} || $join->{source}; | ||||||||||||||||||||||||||||||||
|
Condition83%5/6
$join->{'rel_name'} || $join->{'as'} || $join->{'source'}
$join->{"rel_name"} or $join->{"as"}
MC/DC0%0/3
$join->{'rel_name'} || $join->{'as'} || $join->{'source'}
$join->{"rel_name"}
$join->{"as"}
$join->{'source'}
Truth table75%3/4
A or B || C
A= $join->{"rel_name"}B= $join->{"as"}C= $join->{'source'}
| |||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||
| 240 | 9 | ▶ | $set->{$join_source} ||= {}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$set->{$join_source} ||= {}
MC/DC0%0/1
$set->{$join_source} ||= {}
$set->{$join_source}
Truth table50%1/2
A
A= $set->{$join_source}
| |||||||||||||||||||||||||||||||||||
| 241 | 9 | $self->_populate($set->{$join_source}, $row, $join->{columns}); | |||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||
| 243 | 9 | ▶ | if (my $subjoins = $join->{join}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 244 | 3 | ▶ | $subjoins = [$subjoins] unless ref $subjoins eq 'ARRAY'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||
| 246 | 3 | $self->_populate_joins($set->{$join_source}, $row, $subjoins); | |||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||
| 251 | sub _collect_columns_from_joins { | ||||||||||||||||||||||||||||||||||
| 252 | 34 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_collect_columns_from_joins: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||
| 253 | 34 | my ($joins) = @_; | |||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||
| 255 | 34 | ▶ | return () unless $joins && @$joins; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$joins and @$joins
MC/DC50%1/2
$joins and @$joins
$joins
@$joins
Truth table66%2/3
A and B
A= $joinsB= @$joins
| |||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||
| 257 | 10 | my @join_columns; | |||||||||||||||||||||||||||||||||
| 258 | 10 | foreach my $join_params (@$joins) { | |||||||||||||||||||||||||||||||||
| 259 | 11 | ▶ | if (my $join_columns = $join_params->{columns}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 260 | push @join_columns, map { | ||||||||||||||||||||||||||||||||||
| 261 | 9 | $self->_prepare_column($_, | |||||||||||||||||||||||||||||||||
| 262 | $join_params->{as} | ||||||||||||||||||||||||||||||||||
| 263 | ? $join_params->{as} | ||||||||||||||||||||||||||||||||||
| 264 | : $join_params->{source}) | ||||||||||||||||||||||||||||||||||
| 265 | 9 | ▶ | } @$join_columns; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||
| 268 | 11 | ▶ | if (my $subjoins = $join_params->{join}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 269 | 3 | ▶ | $subjoins = [$subjoins] unless ref $subjoins eq 'ARRAY'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||
| 271 | 3 | push @join_columns, $self->_collect_columns_from_joins($subjoins); | |||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||
| 275 | 10 | return @join_columns; | |||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||
| 278 | sub _build_join { | ||||||||||||||||||||||||||||||||||
| 279 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_build_join: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 280 | 10 | my ($joins) = @_; | |||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||
| 282 | 10 | ▶ | $joins = [$joins] unless ref $joins eq 'ARRAY'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||
| 284 | 10 | my $sql = ''; | |||||||||||||||||||||||||||||||||
| 285 | 10 | my @bind; | |||||||||||||||||||||||||||||||||
| 286 | 10 | foreach my $join_params (@$joins) { | |||||||||||||||||||||||||||||||||
| 287 | my $join = | ||||||||||||||||||||||||||||||||||
| 288 | 11 | SQL::Composer::Join->new(quoter => $self->{quoter}, %$join_params); | |||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||
| 290 | 11 | $sql .= ' ' . $join->to_sql; | |||||||||||||||||||||||||||||||||
| 291 | 11 | push @bind, $join->to_bind; | |||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||
| 293 | 11 | ▶ | if (my $subjoin = $join_params->{join}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 294 | 3 | my ($subsql, $subbind) = $self->_build_join($subjoin); | |||||||||||||||||||||||||||||||||
| 295 | 3 | $sql .= $subsql; | |||||||||||||||||||||||||||||||||
| 296 | 3 | push @bind, @$subbind; | |||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||
| 300 | 10 | return ($sql, \@bind); | |||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||
| 303 | sub _quote { | ||||||||||||||||||||||||||||||||||
| 304 | 103 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
_quote: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 305 | 103 | my ($column, $prefix) = @_; | |||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||
| 307 | 103 | return $self->{quoter}->quote($column, $prefix); | |||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||
| 310 | 3 | 1; | |||||||||||||||||||||||||||||||||
| 311 | __END__ | ||||||||||||||||||||||||||||||||||