| 1 | package SQL::Maker::Select; | ||||||||||||||||||||||||||||||||
| 2 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use utf8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 19 | ▶ | use SQL::Maker::Condition; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use SQL::Maker::Util; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 19 | ▶ | use Class::Accessor::Lite ( | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 19 | new => 0, | |||||||||||||||||||||||||||||||
| 9 | wo => [qw/distinct for_update/], | ||||||||||||||||||||||||||||||||
| 10 | rw => [qw/prefix/], | ||||||||||||||||||||||||||||||||
| 11 | ro => [qw/quote_char name_sep new_line strict/], | ||||||||||||||||||||||||||||||||
| 12 | 19 | ); | |||||||||||||||||||||||||||||||
| 13 | 19 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 7 | ▶ | sub offset { | ||||||||||||||||||||||||||||||
|
Subroutine
offset: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 16 | 7 | ▶ | if (@_==1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 17 | 0 | return $_[0]->{offset}; | |||||||||||||||||||||||||||||||
| 18 | } else { | ||||||||||||||||||||||||||||||||
| 19 | 7 | $_[0]->{offset} = $_[1]; | |||||||||||||||||||||||||||||||
| 20 | 7 | return $_[0]; | |||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 22 | ▶ | sub limit { | ||||||||||||||||||||||||||||||
|
Subroutine
limit: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 25 | 22 | ▶ | if (@_==1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 26 | 0 | $_[0]->{limit}; | |||||||||||||||||||||||||||||||
| 27 | } else { | ||||||||||||||||||||||||||||||||
| 28 | 22 | $_[0]->{limit} = $_[1]; | |||||||||||||||||||||||||||||||
| 29 | 22 | return $_[0]; | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 209 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 34 | 209 | my $class = shift; | |||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my %args = @_ == 1 ? %{$_[0]} : @_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 36 | 209 | my $self = bless { | |||||||||||||||||||||||||||||||
| 37 | select => +[], | ||||||||||||||||||||||||||||||||
| 38 | distinct => 0, | ||||||||||||||||||||||||||||||||
| 39 | select_map => +{}, | ||||||||||||||||||||||||||||||||
| 40 | select_map_reverse => +{}, | ||||||||||||||||||||||||||||||||
| 41 | from => +[], | ||||||||||||||||||||||||||||||||
| 42 | joins => +[], | ||||||||||||||||||||||||||||||||
| 43 | index_hint => +{}, | ||||||||||||||||||||||||||||||||
| 44 | group_by => +[], | ||||||||||||||||||||||||||||||||
| 45 | order_by => +[], | ||||||||||||||||||||||||||||||||
| 46 | prefix => 'SELECT ', | ||||||||||||||||||||||||||||||||
| 47 | new_line => "\n", | ||||||||||||||||||||||||||||||||
| 48 | strict => 0, | ||||||||||||||||||||||||||||||||
| 49 | %args | ||||||||||||||||||||||||||||||||
| 50 | }, $class; | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 209 | return $self; | |||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 42 | ▶ | sub new_condition { | ||||||||||||||||||||||||||||||
|
Subroutine
new_condition: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 56 | 42 | my $self = shift; | |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | SQL::Maker::Condition->new( | ||||||||||||||||||||||||||||||||
| 59 | quote_char => $self->{quote_char}, | ||||||||||||||||||||||||||||||||
| 60 | name_sep => $self->{name_sep}, | ||||||||||||||||||||||||||||||||
| 61 | strict => $self->{strict}, | ||||||||||||||||||||||||||||||||
| 62 | 42 | ); | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 194 | ▶ | sub bind { | ||||||||||||||||||||||||||||||
|
Subroutine
bind: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 66 | 194 | my $self = shift; | |||||||||||||||||||||||||||||||
| 67 | 194 | my @bind; | |||||||||||||||||||||||||||||||
| 68 | 194 | ▶ | push @bind, @{$self->{subqueries}} if $self->{subqueries}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | 194 | ▶ | push @bind, $self->{where}->bind if $self->{where}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 194 | ▶ | push @bind, $self->{having}->bind if $self->{having}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 194 | ▶ | return wantarray ? @bind : \@bind; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 209 | ▶ | sub add_select { | ||||||||||||||||||||||||||||||
|
Subroutine
add_select: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 75 | 209 | my ($self, $term, $col) = @_; | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 209 | push @{ $self->{select} }, $term; | |||||||||||||||||||||||||||||||
| 78 | 209 | ▶ | if ($col) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 38 | ▶ | $col ||= $term; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$col ||= $term
MC/DC0%0/2
$col ||= $term
$col
$term
Truth table33%1/3
A ||= B
A= $colB= $term
| |||||||||||||||||||||||||||||||||
| 80 | 38 | $self->{select_map_reverse}->{$col} = $term; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | 209 | return $self; | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 177 | ▶ | sub add_from { | ||||||||||||||||||||||||||||||
|
Subroutine
add_from: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 86 | 177 | my ($self, $table, $alias) = @_; | |||||||||||||||||||||||||||||||
| 87 | 177 | ▶ | if ( Scalar::Util::blessed( $table ) and $table->can('as_sql') ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($table) and $table->can("as_sql")
MC/DC50%1/2
builtin::blessed($table) and $table->can("as_sql")
builtin::blessed($table)
$table->can("as_sql")
Truth table66%2/3
A and B
A= builtin::blessed($table)B= $table->can("as_sql")
| |||||||||||||||||||||||||||||||||
| 88 | 6 | push @{ $self->{subqueries} }, $table->bind; | |||||||||||||||||||||||||||||||
| 89 | 6 | push @{$self->{from}}, [ \do{ '(' . $table->as_sql . ')' }, $alias ]; | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||||||
| 92 | 171 | push @{$self->{from}}, [$table, $alias]; | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | 177 | return $self; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 44 | ▶ | sub add_join { | ||||||||||||||||||||||||||||||
|
Subroutine
add_join: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 98 | 44 | my ($self, $table_ref, $joins) = @_; | |||||||||||||||||||||||||||||||
| 99 | 44 | ▶ | my ($table, $alias) = ref($table_ref) eq 'ARRAY' ? @$table_ref : ($table_ref); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 44 | ▶ | if ( Scalar::Util::blessed( $table ) and $table->can('as_sql') ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($table) and $table->can("as_sql")
MC/DC50%1/2
builtin::blessed($table) and $table->can("as_sql")
builtin::blessed($table)
$table->can("as_sql")
Truth table66%2/3
A and B
A= builtin::blessed($table)B= $table->can("as_sql")
| |||||||||||||||||||||||||||||||||
| 102 | 7 | push @{ $self->{subqueries} }, $table->bind; | |||||||||||||||||||||||||||||||
| 103 | 7 | $table = \do{ '(' . $table->as_sql . ')' }; | |||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 44 | ▶ | if ( Scalar::Util::blessed( $joins->{table} ) and $joins->{table}->can('as_sql') ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($joins->{"table"}) and $joins->{"table"}->can("as_sql")
MC/DC50%1/2
builtin::blessed($joins->{"table"}) and $joins->{"table"}->can("as_sql")
builtin::blessed($joins->{"table"})
$joins->{"table"}->can("as_sql")
Truth table66%2/3
A and B
A= builtin::blessed($joins->{"table"})B= $joins->{"table"}->can("as_sql")
| |||||||||||||||||||||||||||||||||
| 107 | 2 | push @{ $self->{subqueries} }, $joins->{table}->bind; | |||||||||||||||||||||||||||||||
| 108 | 2 | $joins->{table} = \do{ '(' . $joins->{table}->as_sql . ')' }; | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 44 | push @{ $self->{joins} }, { | |||||||||||||||||||||||||||||||
| 112 | table => [ $table, $alias ], | ||||||||||||||||||||||||||||||||
| 113 | joins => $joins, | ||||||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||||||
| 115 | 44 | return $self; | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 14 | ▶ | sub add_index_hint { | ||||||||||||||||||||||||||||||
|
Subroutine
add_index_hint: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 119 | 14 | my ($self, $table, $hint) = @_; | |||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 14 | my ($type, $list); | |||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 14 | ▶ | if (ref $hint eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 124 | # { type => '...', list => ['foo'] } | ||||||||||||||||||||||||||||||||
| 125 | 8 | ▶ | $type = $hint->{type} || 'USE'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$hint->{'type'} || 'USE'
MC/DC0%0/1
$hint->{'type'} || 'USE'
$hint->{'type'}
Truth table50%1/2
A
A= $hint->{'type'}
| |||||||||||||||||||||||||||||||||
| 126 | 8 | ▶ | $list = ref($hint->{list}) eq 'ARRAY' ? $hint->{list} : [ $hint->{list} ]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||||||
| 128 | # ['foo, 'bar'] or just 'foo' | ||||||||||||||||||||||||||||||||
| 129 | 6 | $type = 'USE'; | |||||||||||||||||||||||||||||||
| 130 | 6 | ▶ | $list = ref($hint) eq 'ARRAY' ? $hint : [ $hint ]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 14 | $self->{index_hint}->{$table} = { | |||||||||||||||||||||||||||||||
| 134 | type => $type, | ||||||||||||||||||||||||||||||||
| 135 | list => $list, | ||||||||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | 14 | return $self; | |||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 774 | ▶ | sub _quote { | ||||||||||||||||||||||||||||||
|
Subroutine
_quote: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 142 | 774 | my ($self, $label) = @_; | |||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 774 | ▶ | return $$label if ref $label eq 'SCALAR'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | SQL::Maker::Util::quote_identifier($label, $self->{quote_char}, $self->{name_sep}) | ||||||||||||||||||||||||||||||||
| 146 | 724 | } | |||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 267 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=26 SCAR=32.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 149 | 267 | my $self = shift; | |||||||||||||||||||||||||||||||
| 150 | 267 | my $sql = ''; | |||||||||||||||||||||||||||||||
| 151 | 267 | my $new_line = $self->new_line; | |||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 267 | ▶ | if (@{ $self->{select} }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 154 | 216 | $self->{select_map} = { reverse %{$self->{select_map_reverse}} }; | |||||||||||||||||||||||||||||||
| 155 | 216 | $sql .= $self->{prefix}; | |||||||||||||||||||||||||||||||
| 156 | 216 | ▶ | $sql .= 'DISTINCT ' if $self->{distinct}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 157 | $sql .= join(', ', map { | ||||||||||||||||||||||||||||||||
| 158 | 280 | my $alias = $self->{select_map}->{$_}; | |||||||||||||||||||||||||||||||
| 159 | 280 | ▶ | if (!$alias) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 160 | 233 | $self->_quote($_) | |||||||||||||||||||||||||||||||
| 161 | ▶ | } elsif ($alias && $_ =~ /(?:^|\.)\Q$alias\E$/) { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
$alias and $_ =~ /(?:^|\.)\Q$alias\E$/
MC/DC50%1/2
$alias and $_ =~ /(?:^|\.)\Q$alias\E$/
$alias
$_ =~ /(?:^|\.)\Q$alias\E$/
Truth table66%2/3
A and B
A= $aliasB= $_ =~ /(?:^|\.)\Q$alias\E$/
| |||||||||||||||||||||||||||||||||
| 162 | 30 | $self->_quote($_) | |||||||||||||||||||||||||||||||
| 163 | } else { | ||||||||||||||||||||||||||||||||
| 164 | 17 | $self->_quote($_) . ' AS ' . $self->_quote($alias) | |||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||
| 166 | 216 | } @{ $self->{select} }) . $new_line; | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 267 | $sql .= 'FROM '; | |||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | ## Add any explicit JOIN statements before the non-joined tables. | ||||||||||||||||||||||||||||||||
| 172 | 267 | ▶ | if ($self->{joins} && @{ $self->{joins} }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"joins"} and @{$self->{"joins"};}
MC/DC50%1/2
$self->{"joins"} and @{$self->{"joins"};}
$self->{"joins"}
@{$self->{"joins"};}
Truth table66%2/3
A and B
A= $self->{"joins"}B= @{$self->{"joins"};}
| |||||||||||||||||||||||||||||||||
| 173 | 37 | my $initial_table_written = 0; | |||||||||||||||||||||||||||||||
| 174 | 37 | for my $j (@{ $self->{joins} }) { | |||||||||||||||||||||||||||||||
| 175 | 46 | my ($table, $join) = map { $j->{$_} } qw( table joins ); | |||||||||||||||||||||||||||||||
| 176 | 46 | $table = $self->_add_index_hint(@$table); ## index hint handling | |||||||||||||||||||||||||||||||
| 177 | 46 | ▶ | $sql .= $table unless $initial_table_written++; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | 46 | ▶ | $sql .= ' ' . uc($join->{type}) if $join->{type}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | 46 | $sql .= ' JOIN ' . $self->_quote($join->{table}); | |||||||||||||||||||||||||||||||
| 180 | 46 | ▶ | $sql .= ' ' . $self->_quote($join->{alias}) if $join->{alias}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | 46 | ▶ | if ( defined $join->{condition} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | 44 | ▶ | if (ref $join->{condition} && ref $join->{condition} eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $join->{"condition"} and ref $join->{"condition"} eq "ARRAY"
MC/DC100%2/2
ref $join->{"condition"} and ref $join->{"condition"} eq "ARRAY"
ref $join->{"condition"}
ref $join->{"condition"} eq "ARRAY"
Truth table100%3/3
A and B
A= ref $join->{"condition"}B= ref $join->{"condition"} eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 184 | 3 | $sql .= ' USING ('. join(', ', map { $self->_quote($_) } @{ $join->{condition} }) . ')'; | |||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | ▶ | elsif (ref $join->{condition} && ref $join->{condition} eq 'HASH') { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $join->{"condition"} and ref $join->{"condition"} eq "HASH"
MC/DC50%1/2
ref $join->{"condition"} and ref $join->{"condition"} eq "HASH"
ref $join->{"condition"}
ref $join->{"condition"} eq "HASH"
Truth table66%2/3
A and B
A= ref $join->{"condition"}B= ref $join->{"condition"} eq "HASH"
| |||||||||||||||||||||||||||||||||
| 187 | 3 | my @conds; | |||||||||||||||||||||||||||||||
| 188 | 3 | for my $key (keys %{ $join->{condition} }) { | |||||||||||||||||||||||||||||||
| 189 | 4 | push @conds, $self->_quote($key) . ' = ' . $self->_quote($join->{condition}{$key}); | |||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||
| 191 | 3 | $sql .= ' ON ' . join(' AND ', @conds); | |||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | else { | ||||||||||||||||||||||||||||||||
| 194 | 38 | $sql .= ' ON ' . $join->{condition}; | |||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||
| 198 | 37 | ▶ | $sql .= ', ' if @{ $self->{from} }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | 267 | ▶ | if ($self->{from} && @{ $self->{from} }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"from"} and @{$self->{"from"};}
MC/DC50%1/2
$self->{"from"} and @{$self->{"from"};}
$self->{"from"}
@{$self->{"from"};}
Truth table66%2/3
A and B
A= $self->{"from"}B= @{$self->{"from"};}
| |||||||||||||||||||||||||||||||||
| 202 | $sql .= join ', ', | ||||||||||||||||||||||||||||||||
| 203 | 241 | map { $self->_add_index_hint($_->[0], $_->[1]) } | |||||||||||||||||||||||||||||||
| 204 | 231 | @{ $self->{from} }; | |||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | 267 | $sql .= $new_line; | |||||||||||||||||||||||||||||||
| 208 | 267 | ▶ | $sql .= $self->as_sql_where() if $self->{where}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | 267 | ▶ | $sql .= $self->as_sql_group_by if $self->{group_by}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 211 | 267 | ▶ | $sql .= $self->as_sql_having if $self->{having}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 212 | 267 | ▶ | $sql .= $self->as_sql_order_by if $self->{order_by}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||
| 214 | 267 | ▶ | $sql .= $self->as_sql_limit if defined $self->{limit}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | 262 | $sql .= $self->as_sql_for_update; | |||||||||||||||||||||||||||||||
| 217 | 262 | $sql =~ s/${new_line}+$//; | |||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | 262 | return $sql; | |||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | 23 | ▶ | sub as_sql_limit { | ||||||||||||||||||||||||||||||
|
Subroutine
as_sql_limit: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 223 | 23 | my $self = shift; | |||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | 23 | my $n = $self->{limit}; | |||||||||||||||||||||||||||||||
| 226 | 23 | ▶ | return '' unless defined $n; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||
| 228 | 23 | ▶ | die "Non-numerics in limit clause ($n)" if $n =~ /\D/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 229 | return sprintf "LIMIT %d%s" . $self->new_line, $n, | ||||||||||||||||||||||||||||||||
| 230 | 18 | ▶ | ($self->{offset} ? " OFFSET " . int($self->{offset}) : ""); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | 48 | ▶ | sub add_order_by { | ||||||||||||||||||||||||||||||
|
Subroutine
add_order_by: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 234 | 48 | my ($self, $col, $type) = @_; | |||||||||||||||||||||||||||||||
| 235 | 48 | push @{$self->{order_by}}, [$col, $type]; | |||||||||||||||||||||||||||||||
| 236 | 48 | return $self; | |||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | 267 | ▶ | sub as_sql_order_by { | ||||||||||||||||||||||||||||||
|
Subroutine
as_sql_order_by: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 240 | 267 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | 267 | my @attrs = @{$self->{order_by}}; | |||||||||||||||||||||||||||||||
| 243 | 267 | ▶ | return '' unless @attrs; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | return 'ORDER BY ' | ||||||||||||||||||||||||||||||||
| 246 | . join(', ', map { | ||||||||||||||||||||||||||||||||
| 247 | 39 | my ($col, $type) = @$_; | |||||||||||||||||||||||||||||||
| 248 | 48 | ▶ | if (ref $col) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 249 | 29 | $$col | |||||||||||||||||||||||||||||||
| 250 | } else { | ||||||||||||||||||||||||||||||||
| 251 | 19 | ▶ | $type ? $self->_quote($col) . " $type" : $self->_quote($col) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||
| 253 | } @attrs) | ||||||||||||||||||||||||||||||||
| 254 | . $self->new_line; | ||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | 36 | ▶ | sub add_group_by { | ||||||||||||||||||||||||||||||
|
Subroutine
add_group_by: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 258 | 36 | my ($self, $group, $order) = @_; | |||||||||||||||||||||||||||||||
| 259 | 36 | ▶ | push @{$self->{group_by}}, $order ? $self->_quote($group) . " $order" : $self->_quote($group); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 260 | 36 | return $self; | |||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||
| 263 | 267 | ▶ | sub as_sql_group_by { | ||||||||||||||||||||||||||||||
|
Subroutine
as_sql_group_by: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 264 | 267 | my ($self,) = @_; | |||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | 267 | my $elems = $self->{group_by}; | |||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | 267 | ▶ | return '' if @$elems == 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | 26 | return 'GROUP BY ' | |||||||||||||||||||||||||||||||
| 271 | . join(', ', @$elems) | ||||||||||||||||||||||||||||||||
| 272 | . $self->new_line; | ||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||
| 275 | 84 | ▶ | sub set_where { | ||||||||||||||||||||||||||||||
|
Subroutine
set_where: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 276 | 84 | my ($self, $where) = @_; | |||||||||||||||||||||||||||||||
| 277 | 84 | $self->{where} = $where; | |||||||||||||||||||||||||||||||
| 278 | 84 | return $self; | |||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | 29 | ▶ | sub add_where { | ||||||||||||||||||||||||||||||
|
Subroutine
add_where: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 282 | 29 | my ($self, $col, $val) = @_; | |||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | 29 | ▶ | $self->{where} ||= $self->new_condition(); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"where"} ||= $self->new_condition
MC/DC0%0/2
$self->{"where"} ||= $self->new_condition
$self->{"where"}
$self->new_condition
Truth table66%2/3
A ||= B
A= $self->{"where"}B= $self->new_condition
| |||||||||||||||||||||||||||||||||
| 285 | 29 | $self->{where}->add($col, $val); | |||||||||||||||||||||||||||||||
| 286 | 29 | return $self; | |||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | 6 | ▶ | sub add_where_raw { | ||||||||||||||||||||||||||||||
|
Subroutine
add_where_raw: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 290 | 6 | my ($self, $term, $bind) = @_; | |||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||
| 292 | 6 | ▶ | $self->{where} ||= $self->new_condition(); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"where"} ||= $self->new_condition
MC/DC0%0/2
$self->{"where"} ||= $self->new_condition
$self->{"where"}
$self->new_condition
Truth table66%2/3
A ||= B
A= $self->{"where"}B= $self->new_condition
| |||||||||||||||||||||||||||||||||
| 293 | 6 | $self->{where}->add_raw($term, $bind); | |||||||||||||||||||||||||||||||
| 294 | 6 | return $self; | |||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | 173 | ▶ | sub as_sql_where { | ||||||||||||||||||||||||||||||
|
Subroutine
as_sql_where: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 298 | 173 | my $self = shift; | |||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | 173 | ▶ | my $where = $self->{where}->as_sql(undef, sub { $self->_quote($_[0]) }); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 301 | 173 | ▶ | $where ? "WHERE $where" . $self->new_line : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||
| 304 | 7 | ▶ | sub as_sql_having { | ||||||||||||||||||||||||||||||
|
Subroutine
as_sql_having: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 305 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||
| 306 | 7 | ▶ | if ($self->{having}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 307 | 7 | 'HAVING ' . $self->{having}->as_sql . $self->new_line; | |||||||||||||||||||||||||||||||
| 308 | } else { | ||||||||||||||||||||||||||||||||
| 309 | 0 | '' | |||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | 3 | ▶ | sub add_having { | ||||||||||||||||||||||||||||||
|
Subroutine
add_having: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 314 | 3 | my ($self, $col, $val) = @_; | |||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||
| 316 | 3 | ▶ | if (my $orig = $self->{select_map_reverse}->{$col}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 317 | 3 | $col = $orig; | |||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||
| 320 | 3 | ▶ | $self->{having} ||= $self->new_condition(); | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"having"} ||= $self->new_condition
MC/DC0%0/2
$self->{"having"} ||= $self->new_condition
$self->{"having"}
$self->new_condition
Truth table33%1/3
A ||= B
A= $self->{"having"}B= $self->new_condition
| |||||||||||||||||||||||||||||||||
| 321 | 3 | $self->{having}->add($col, $val); | |||||||||||||||||||||||||||||||
| 322 | 3 | return $self; | |||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | 4 | ▶ | sub add_having_raw { | ||||||||||||||||||||||||||||||
|
Subroutine
add_having_raw: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 326 | 4 | my ($self, $term, $bind) = @_; | |||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||
| 328 | 4 | ▶ | $self->{having} ||= $self->new_condition(); | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"having"} ||= $self->new_condition
MC/DC0%0/2
$self->{"having"} ||= $self->new_condition
$self->{"having"}
$self->new_condition
Truth table33%1/3
A ||= B
A= $self->{"having"}B= $self->new_condition
| |||||||||||||||||||||||||||||||||
| 329 | 4 | $self->{having}->add_raw($term, $bind); | |||||||||||||||||||||||||||||||
| 330 | 4 | return $self; | |||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | 262 | ▶ | sub as_sql_for_update { | ||||||||||||||||||||||||||||||
|
Subroutine
as_sql_for_update: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 334 | 262 | my $self = shift; | |||||||||||||||||||||||||||||||
| 335 | 262 | ▶ | $self->{for_update} ? ' FOR UPDATE' : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||
| 338 | 287 | ▶ | sub _add_index_hint { | ||||||||||||||||||||||||||||||
|
Subroutine
_add_index_hint: called CC=7 SCAR=20.5
| |||||||||||||||||||||||||||||||||
| 339 | 287 | my ($self, $tbl_name, $alias) = @_; | |||||||||||||||||||||||||||||||
| 340 | 287 | ▶ | my $quoted = $alias ? $self->_quote($tbl_name) . ' ' . $self->_quote($alias) : $self->_quote($tbl_name); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 341 | 287 | my $hint = $self->{index_hint}->{$tbl_name}; | |||||||||||||||||||||||||||||||
| 342 | 287 | ▶ | return $quoted unless $hint && ref($hint) eq 'HASH'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$hint and ref $hint eq "HASH"
MC/DC50%1/2
$hint and ref $hint eq "HASH"
$hint
ref $hint eq "HASH"
Truth table66%2/3
A and B
A= $hintB= ref $hint eq "HASH"
| |||||||||||||||||||||||||||||||||
| 343 | 16 | ▶ | if ($hint->{list} && @{ $hint->{list} }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$hint->{"list"} and @{$hint->{"list"};}
MC/DC0%0/2
$hint->{"list"} and @{$hint->{"list"};}
$hint->{"list"}
@{$hint->{"list"};}
Truth table33%1/3
A and B
A= $hint->{"list"}B= @{$hint->{"list"};}
| |||||||||||||||||||||||||||||||||
| 344 | return $quoted . ' ' . uc($hint->{type} || 'USE') . ' INDEX (' . | ||||||||||||||||||||||||||||||||
| 345 | 16 | ▶ | join (',', map { $self->_quote($_) } @{ $hint->{list} }) . | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$hint->{'type'} || 'USE'
MC/DC0%0/1
$hint->{'type'} || 'USE'
$hint->{'type'}
Truth table50%1/2
A
A= $hint->{'type'}
| |||||||||||||||||||||||||||||||||
| 346 | ')'; | ||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||
| 348 | 0 | return $quoted; | |||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | 19 | 1; | |||||||||||||||||||||||||||||||
| 352 | __END__ | ||||||||||||||||||||||||||||||||