| 1 | package DBIx::Skinny::SQL; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Class::Accessor::Lite; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | Class::Accessor::Lite->mk_accessors( | |||||||||||||||||||||||||||||||||||||||||||||
| 7 | qw/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 8 | select distinct select_map select_map_reverse | ||||||||||||||||||||||||||||||||||||||||||||||
| 9 | from joins where bind bind_col limit offset group order | ||||||||||||||||||||||||||||||||||||||||||||||
| 10 | having where_values column_mutator index_hint | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | comment for_update | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | skinny | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 | / | ||||||||||||||||||||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 33 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=27.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 33 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | my %args = @_ == 1 ? %{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 33 | my $self = bless {%args}, $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 33 | for my $name (qw/ select from joins bind bind_col group order where having /) { | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | 297 | ▶ | unless ($self->$name && ref $self->$name eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->$name and ref $self->$name eq "ARRAY"
MC/DC0%0/2
$self->$name and ref $self->$name eq "ARRAY"
$self->$name
ref $self->$name eq "ARRAY"
Truth table33%1/3
A and B
A= $self->$nameB= ref $self->$name eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 297 | ▶ | $self->$name ? $self->$name([ $self->$name ]) : $self->$name([]); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 33 | for my $name (qw/ select_map select_map_reverse where_values index_hint/) { | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 132 | ▶ | $self->$name( {} ) unless $self->$name && ref $self->$name eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->$name and ref $self->$name eq "HASH"
MC/DC0%0/2
$self->$name and ref $self->$name eq "HASH"
$self->$name
ref $self->$name eq "HASH"
Truth table33%1/3
A and B
A= $self->$nameB= ref $self->$name eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 33 | ▶ | $self->distinct(0) unless $self->distinct; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 33 | $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 12 | ▶ | sub add_select { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_select: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | 12 | my($term, $col) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 12 | ▶ | $col ||= $term; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$col ||= $term
MC/DC0%0/2
$col ||= $term
$col
$term
Truth table66%2/3
A ||= B
A= $colB= $term
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 12 | push @{ $self->select }, $term; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 12 | $self->select_map->{$term} = $col; | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | 12 | $self->select_map_reverse->{$col} = $term; | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 8 | ▶ | sub add_join { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_join: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | 8 | my($table, $joins) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | 8 | ▶ | push @{ $self->joins }, { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | table => $table, | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | joins => ref($joins) eq 'ARRAY' ? $joins : [ $joins ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub add_index_hint { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_index_hint: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | my($table, $hint) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | $self->index_hint->{$table} = { | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | type => $hint->{type} || 'USE', | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | list => ref($hint->{list}) eq 'ARRAY' ? $hint->{list} : [ $hint->{list} ], | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$hint->{'type'} || 'USE'
MC/DC0%0/1
$hint->{'type'} || 'USE'
$hint->{'type'}
Truth table50%1/2
A
A= $hint->{'type'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 33 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=18 SCAR=29.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 33 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 33 | my $sql = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | 33 | ▶ | if (@{ $self->select }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 15 | $sql .= 'SELECT '; | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | 15 | ▶ | $sql .= 'DISTINCT ' if $self->distinct; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | $sql .= join(', ', map { | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 18 | my $alias = $self->select_map->{$_}; | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | 18 | ▶ | !$alias ? $_ : | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
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$/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | $alias && /(?:^|\.)\Q$alias\E$/ ? $_ : "$_ AS $alias"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 15 | } @{ $self->select }) . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 33 | $sql .= 'FROM '; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | ## Add any explicit JOIN statements before the non-joined tables. | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 33 | ▶ | 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->joinsB= @{$self->joins;}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 8 | my $initial_table_written = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | 8 | for my $j (@{ $self->joins }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 11 | my($table, $joins) = map { $j->{$_} } qw( table joins ); | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 11 | $table = $self->_add_index_hint($table); ## index hint handling | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 11 | ▶ | $sql .= $table unless $initial_table_written++; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 11 | for my $join (@{ $j->{joins} }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 13 | $sql .= ' ' . uc($join->{type}) . ' JOIN ' . $join->{table}; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 13 | ▶ | if (ref $join->{condition}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | $sql .= ' USING ('. join(', ', @{ $join->{condition} }) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 12 | $sql .= ' ON ' . $join->{condition}; | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 8 | ▶ | $sql .= ', ' if @{ $self->from }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 33 | ▶ | 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->fromB= @{$self->from;}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 26 | $sql .= join ', ', map { $self->_add_index_hint($_) } @{ $self->from }; | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 33 | $sql .= "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 33 | $sql .= $self->as_sql_where; | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 33 | $sql .= $self->as_aggregate('group'); | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | 33 | $sql .= $self->as_sql_having; | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | 33 | $sql .= $self->as_aggregate('order'); | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 33 | $sql .= $self->as_limit; | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | 32 | my $comment = $self->comment; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 32 | ▶ | if ($comment && $comment =~ /([ 0-9a-zA-Z.:;()_#&,]+)/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$comment and $comment =~ /([ 0-9a-zA-Z.:;()_#&,]+)/
MC/DC50%1/2
$comment and $comment =~ /([ 0-9a-zA-Z.:;()_#&,]+)/
$comment
$comment =~ /([ 0-9a-zA-Z.:;()_#&,]+)/
Truth table66%2/3
A and B
A= $commentB= $comment =~ /([ 0-9a-zA-Z.:;()_#&,]+)/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | ▶ | $sql .= "-- $1" if $1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 32 | $sql .= $self->as_for_update; | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 32 | return $sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 33 | ▶ | sub as_limit { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_limit: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 33 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | 33 | ▶ | my $n = $self->limit or | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | return ''; | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 4 | ▶ | die "Non-numerics in limit clause ($n)" if $n =~ /\D/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | ▶ | return sprintf "LIMIT %d%s\n", $n, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | ($self->offset ? " OFFSET " . int($self->offset) : ""); | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 66 | ▶ | sub as_aggregate { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_aggregate: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 66 | my ($self, $set) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 66 | ▶ | return '' unless my $attribute = $self->$set(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 66 | my $ref = ref $attribute; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 66 | ▶ | if ($ref eq 'ARRAY' && scalar @$attribute == 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ref eq "ARRAY" and scalar @$attribute == 0
MC/DC100%2/2
$ref eq "ARRAY" and scalar @$attribute == 0
$ref eq "ARRAY"
scalar @$attribute == 0
Truth table100%3/3
A and B
A= $ref eq "ARRAY"B= scalar @$attribute == 0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 56 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 10 | ▶ | my $elements = ($ref eq 'ARRAY') ? $attribute : [ $attribute ]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | return uc($set) | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | . ' BY ' | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 10 | ▶ | . join(', ', map { $_->{column} . ($_->{desc} ? (' ' . $_->{desc}) : '') } @$elements) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | . "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 48 | ▶ | sub as_sql_where { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql_where: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 48 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | 48 | $self->where && @{ $self->where } ? | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | 48 | ▶ | 'WHERE ' . join(' AND ', @{ $self->where }) . "\n" : | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->where and @{$self->where;}
MC/DC50%1/2
$self->where and @{$self->where;}
$self->where
@{$self->where;}
Truth table66%2/3
A and B
A= $self->whereB= @{$self->where;}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | ''; | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 33 | ▶ | sub as_sql_having { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql_having: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 33 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | 33 | $self->having && @{ $self->having } ? | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | 33 | ▶ | 'HAVING ' . join(' AND ', @{ $self->having }) . "\n" : | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->having and @{$self->having;}
MC/DC50%1/2
$self->having and @{$self->having;}
$self->having
@{$self->having;}
Truth table66%2/3
A and B
A= $self->havingB= @{$self->having;}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | ''; | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 15 | ▶ | sub add_where { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_where: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 15 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | ## xxx Need to support old range and transform behaviors. | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 15 | my($col, $val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 165 | # XXX; DATE_FORMAT(member.created_at,'%Y-%m') | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # Carp::croak("Invalid/unsafe column name $col") unless $col =~ /^[\w\.]+$/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 15 | my($term, $bind, $tcol) = $self->_mk_term($col, $val); | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 15 | push @{ $self->{where} }, "($term)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | 15 | push @{ $self->{bind} }, @$bind; | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | 15 | push @{ $self->{bind_col} }, $tcol; | |||||||||||||||||||||||||||||||||||||||||||||
| 171 | 15 | $self->where_values->{$tcol} = $bind; | |||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 2 | ▶ | sub add_complex_where { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_complex_where: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 2 | my ($terms) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | 2 | my ($where, $bind) = $self->_parse_array_terms($terms); | |||||||||||||||||||||||||||||||||||||||||||||
| 178 | 2 | push @{ $self->{where} }, $where; | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | 2 | push @{ $self->{bind} }, @$bind; | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 3 | ▶ | sub add_where_raw { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_where_raw: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 3 | my($term, $bind) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | 3 | push @{ $self->{where} }, "($term)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | 3 | ▶ | push @{ $self->{bind} }, @$bind if $bind; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 3 | ▶ | sub _parse_array_terms { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_array_terms: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 191 | 3 | my ($term_list) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 3 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | 3 | my $logic = 'AND'; | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | 3 | my @bind; | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | 3 | foreach my $t ( @$term_list ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | 9 | ▶ | if (! ref $t ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 3 | ▶ | $logic = $1 if uc($t) =~ m/^-?(OR|AND|OR_NOT|AND_NOT)$/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 3 | $logic =~ s/_/ /; | |||||||||||||||||||||||||||||||||||||||||||||
| 200 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 6 | my $out; | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | 6 | ▶ | if (ref $t eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # bag of terms to apply $logic with | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 5 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||
| 206 | 5 | foreach my $t2 ( keys %$t ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | 5 | my ($term, $bind, $col) = $self->_mk_term($t2, $t->{$t2}); | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | 5 | push @{ $self->{bind_col} }, $col; | |||||||||||||||||||||||||||||||||||||||||||||
| 209 | 5 | $self->where_values->{$col} = $t->{$t2}; | |||||||||||||||||||||||||||||||||||||||||||||
| 210 | 5 | push @out, $term; | |||||||||||||||||||||||||||||||||||||||||||||
| 211 | 5 | push @bind, @$bind; | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 5 | $out .= '(' . join(" AND ", @out) . ")"; | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | elsif (ref $t eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # another array of terms to process! | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | my ($where, $bind) = $self->_parse_array_terms( $t ); | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | 1 | push @bind, @$bind; | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | 1 | $out = '(' . $where . ')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 6 | ▶ | push @out, (@out ? ' ' . $logic . ' ' : '') . $out; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 3 | return (join("", @out), \@bind); | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | sub has_where { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
has_where: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | my($col, $val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # TODO: should check if the value is same with $val? | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | exists $self->where_values->{$col}; | |||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | ▶ | sub add_having { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_having: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | my($col, $val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 1 | ▶ | if (my $orig = $self->select_map_reverse->{$col}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 1 | $col = $orig; | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 1 | my($term, $bind) = $self->_mk_term($col, $val); | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | 1 | push @{ $self->{having} }, "($term)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | 1 | push @{ $self->{bind} }, @$bind; | |||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 32 | ▶ | sub as_for_update { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_for_update: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 32 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 249 | 32 | ▶ | $self->for_update ? ' FOR UPDATE' : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 34 | ▶ | sub _mk_term { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mk_term: called CC=20 SCAR=31.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 34 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 254 | 34 | my($col, $val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 255 | 34 | my $term = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 256 | 34 | my (@bind, $m); | |||||||||||||||||||||||||||||||||||||||||||||
| 257 | 34 | ▶ | if (ref($val) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 7 | ▶ | if (ref $val->[0] or (($val->[0] || '') eq '-and')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
ref $val->[0] or ($val->[0] || "") eq "-and"
$val->[0] || ''
MC/DC0%0/2
ref $val->[0] or ($val->[0] || "") eq "-and"
ref $val->[0]
$val->[0]
Truth table66%2/3
A or B
A= ref $val->[0]B= $val->[0]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 5 | my $logic = 'OR'; | |||||||||||||||||||||||||||||||||||||||||||||
| 260 | 5 | my @values = @$val; | |||||||||||||||||||||||||||||||||||||||||||||
| 261 | 5 | ▶ | if ($val->[0] eq '-and') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 4 | $logic = 'AND'; | |||||||||||||||||||||||||||||||||||||||||||||
| 263 | 4 | shift @values; | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 5 | my @terms; | |||||||||||||||||||||||||||||||||||||||||||||
| 267 | 5 | for my $v (@values) { | |||||||||||||||||||||||||||||||||||||||||||||
| 268 | 13 | my($term, $bind) = $self->_mk_term($col, $v); | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | 13 | push @terms, "($term)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 270 | 13 | push @bind, @$bind; | |||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 5 | $term = join " $logic ", @terms; | |||||||||||||||||||||||||||||||||||||||||||||
| 273 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 2 | ▶ | $col = $m->($col) if $m = $self->column_mutator; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 2 | ▶ | if (scalar(@$val)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 2 | $term = "$col IN (".join(',', ('?') x scalar @$val).')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 277 | 2 | @bind = @$val; | |||||||||||||||||||||||||||||||||||||||||||||
| 278 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | $term = '1=0'; | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 282 | } elsif (ref($val) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 7 | ▶ | my $c = $val->{column} || $col; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$val->{'column'} || $col
MC/DC0%0/2
$val->{'column'} || $col
$val->{'column'}
$col
Truth table33%1/3
A || B
A= $val->{'column'}B= $col
| |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 7 | ▶ | $c = $m->($c) if $m = $self->column_mutator; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 7 | my($op, $v) = (%{ $val }); | |||||||||||||||||||||||||||||||||||||||||||||
| 287 | 7 | $op = uc($op); | |||||||||||||||||||||||||||||||||||||||||||||
| 288 | 7 | ▶ | if (($op eq 'IN' || $op eq 'NOT IN') && ref($v) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$op eq "IN" || $op eq "NOT IN" and ref $v eq "ARRAY"
$op eq "IN" or $op eq "NOT IN"
MC/DC66%2/3
$op eq "IN" || $op eq "NOT IN" and ref $v eq "ARRAY"
$op eq "IN"
$op eq "NOT IN"
ref $v eq "ARRAY"
Truth table60%3/5
A or B and C
A= $op eq "IN"B= $op eq "NOT IN"C= ref $v eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 2 | ▶ | if (scalar(@$v)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 2 | $term = "$c $op (".join(',', ('?') x scalar @$v).')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 291 | 2 | @bind = @$v; | |||||||||||||||||||||||||||||||||||||||||||||
| 292 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | if ($op eq 'IN') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | $term = '1=0'; | |||||||||||||||||||||||||||||||||||||||||||||
| 295 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | $term = '1=1'; | |||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 5 | $term = "$c $op ?"; | |||||||||||||||||||||||||||||||||||||||||||||
| 301 | 5 | push @bind, $v; | |||||||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | } elsif (ref($val) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | ▶ | $col = $m->($col) if $m = $self->column_mutator; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 1 | $term = "$col $$val"; | |||||||||||||||||||||||||||||||||||||||||||||
| 306 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 19 | ▶ | $col = $m->($col) if $m = $self->column_mutator; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 19 | $term = "$col = ?"; | |||||||||||||||||||||||||||||||||||||||||||||
| 309 | 19 | push @bind, $val; | |||||||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 34 | ($term, \@bind, $col); | |||||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 38 | ▶ | sub _add_index_hint { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_index_hint: called CC=6 SCAR=19.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 38 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 316 | 38 | my ($tbl_name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 317 | 38 | my $hint = $self->index_hint->{$tbl_name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 318 | 38 | ▶ | return $tbl_name 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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 3 | ▶ | 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"};}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 320 | return $tbl_name . ' ' . uc($hint->{type} || 'USE') . ' INDEX (' . | ||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 3 | ▶ | join (',', @{ $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'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 322 | ')'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | return $tbl_name; | |||||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | sub retrieve { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
retrieve: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | my ($self, $table) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | ▶ | $self->skinny->search_by_sql($self->as_sql, $self->bind, ($table || $self->from->[0])); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$table || $self->from->[0]
MC/DC0%0/2
$table || $self->from->[0]
$table
$self->from->[0]
Truth table0%0/3
A || B
A= $tableB= $self->from->[0]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 1 | 'base code from Data::ObjectDriver::SQL'; | |||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||