| 1 | package Data::Model::SQL; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use base qw(Data::Model::Accessor); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | $Carp::Internal{(__PACKAGE__)}++; | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | __PACKAGE__->mk_accessors(qw/ select where having bind bind_column limit offset select_map select_map_reverse column_mutator where_values /); | |||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | for my $name (qw/ from joins /) { | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 8 | ▶ | *{$name} = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 354 | ▶ | return $_[0]->{$name} unless @_ > 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 98 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 98 | ▶ | $self->{$name} = ((@_ == 1 && ref($_[0]) eq 'ARRAY') ? $_[0] : [@_]); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 and ref $_[0] eq "ARRAY"
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1
ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | for my $name (qw/ group order /) { | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 8 | ▶ | *{$name} = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 295 | ▶ | return $_[0]->{$name} unless @_ > 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 85 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | 85 | ▶ | $self->{$name} = ((@_ == 1 && ref($_[0]) eq 'ARRAY') ? $_[0] : [@_]); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 and ref $_[0] eq "ARRAY"
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1
ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 37 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=20 SCAR=51.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 37 | my($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 37 | my $self = bless { %args }, $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 37 | for my $name (qw/ select from joins bind bind_column group order where /) { | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 296 | ▶ | unless ($self->$name && ref $self->$name eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->$name and ref $self->$name eq "ARRAY"
MC/DC50%1/2
$self->$name and ref $self->$name eq "ARRAY"
$self->$name
ref $self->$name eq "ARRAY"
Truth table66%2/3
A and B
A= $self->$nameB= ref $self->$name eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 290 | ▶ | $self->$name ? $self->$name([ $self->$name ]) : $self->$name([]);; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 37 | for my $name (qw/ select_map select_map_reverse where_values /) { | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 111 | ▶ | $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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # ここで select, join, where クエリ 等を%args から構築する | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # where | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 37 | ▶ | if (exists $args{where}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my @wheres; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if (ref($args{where}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | while (my($column, $value) = splice @{ $args{where} }, 0, 2) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | push @wheres, +[ $column, $value ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } elsif (ref($args{where}) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | while (my($column, $value) = each %{ $args{where} }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | push @wheres, +[ $column, $value ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | Carp::croak('where requires the type of ARRAY or HASH reference'); | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | for my $where (@wheres) { | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | $self->add_where(@{ $where }); | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # where_sql | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 37 | ▶ | if (exists $args{where_sql}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my @wheres; | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if (ref($args{where_sql}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | while (my($sql, $values) = splice @{ $args{where_sql} }, 0, 2) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | push @wheres, +[ $sql, $values ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } elsif (ref($args{where_sql}) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | while (my($sql, $values) = each %{ $args{where} }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | push @wheres, +[ $sql, $values ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | Carp::croak('where_sql requires the type of ARRAY or HASH reference'); | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | for my $where (@wheres) { | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my($sql, $values) = @{ $where }; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $self->add_where_sql( $sql => @{ $values }); | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | Data::Model::SQL->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | where => +[ | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | foo => [ -and => 'foo', 'bar', 'baz'], | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | bar => 'baz', | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | baz => +{ '!=' => 2 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | order => [ | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | +{ foo => 'ASC' }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | joins => [ | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | foo => [ | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | { inner => { 'baz b1' => 'foo.baz_id = b1.baz_id AND b1.quux_id = 1' }} | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | group => [qw/ foo bar /], | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 37 | $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 10 | ▶ | sub add_select { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_select: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 10 | my($self, $term, $col) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 10 | push @{ $self->{select} }, $term; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 10 | ▶ | return unless $col; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 7 | $self->select_map->{$term} = $col; | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 7 | $self->select_map_reverse->{$col} = $term; | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | ▶ | sub add_join { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_join: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | my($self, $table, $joins) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | 5 | ▶ | push @{ $self->joins }, { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | table => $table, | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | joins => ref($joins) eq 'ARRAY' ? $joins : [ $joins ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 33 | ▶ | sub _add_where { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_where: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 33 | my($self, $col, $val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | 33 | ▶ | if (lc($col) eq '-and' || lc($col) eq '-or') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
lc $col eq "-and" or lc $col eq "-or"
MC/DC100%2/2
lc $col eq "-and" or lc $col eq "-or"
lc $col eq "-and"
lc $col eq "-or"
Truth table100%3/3
A or B
A= lc $col eq "-and"B= lc $col eq "-or"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 5 | ▶ | my $op = lc($col) eq '-and' ? 'AND' : 'OR'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 5 | my(@terms, @binds, @bind_columns, @tcols); | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | 5 | ▶ | while (my($ccol, $cval) = splice @{ $val }, 0, 2) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 10 | my($term, $bind, $bind_column, $tcol) = $self->_add_where( $ccol => $cval ); | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | 10 | push @terms, "($term)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | 10 | push @binds, @{ $bind }; | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | 10 | push @bind_columns, @{ $bind_column }; | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | 10 | push @tcols, @{ $tcol }; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 5 | my $term = join " $op ", @terms; | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | 5 | return $term, \@binds, \@bind_columns, \@tcols; | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | ## xxx Need to support old range and transform behaviors. | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 28 | ▶ | Carp::croak("Invalid/unsafe column name $col") unless $col =~ /^[\w\.]+$/ || ref($col) eq 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$col =~ /^[\w\.]+$/ or ref $col eq "SCALAR"
MC/DC0%0/2
$col =~ /^[\w\.]+$/ or ref $col eq "SCALAR"
$col =~ /^[\w\.]+$/
ref $col eq "SCALAR"
Truth table66%2/3
A or B
A= $col =~ /^[\w\.]+$/B= ref $col eq "SCALAR"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 28 | my($term, $bind, $tcol) = $self->_mk_term($col, $val); | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | 28 | my @bind_column = (($tcol) x scalar(@$bind)); | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | 28 | return $term, $bind, \@bind_column, [ $tcol => $val ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 23 | ▶ | sub add_where { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_where: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 23 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | 23 | my($term, $binds, $bind_columns, $tcols) = $self->_add_where(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 23 | push @{ $self->{where} }, "($term)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | 23 | push @{ $self->{bind} }, @{ $binds }; | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | 23 | push @{ $self->{bind_column} }, @{ $bind_columns }; | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | 23 | my @tcols = @{ $tcols }; | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | 23 | ▶ | while (my($tcol, $tval) = splice @tcols, 0, 2) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 28 | ▶ | $self->where_values->{$tcol} = $tval if defined $tcol; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 2 | ▶ | sub add_where_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_where_sql: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 2 | my($self, $term, @bind) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 2 | my(@columns, @values); | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | 2 | ▶ | while (my($column, $value) = splice @bind, 0, 2) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 5 | $self->where_values->{$column} = $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 5 | push @columns, $column; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | 5 | push @values, $value; | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 2 | push @{ $self->{where} }, sprintf("($term)", @columns); | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | 2 | push @{ $self->{bind_column} }, @columns; | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | 2 | push @{ $self->{bind} }, @values; | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 1 | ▶ | sub add_having { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_having: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | my $stmt = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | 1 | my($col, $val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | ▶ | if (my $orig = $stmt->select_map_reverse->{$col}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | $col = $orig; | |||||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | my($term, $bind, $tcol) = $stmt->_mk_term($col, $val); | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | 1 | push @{ $stmt->{having} }, "($term)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 187 | 1 | push @{ $stmt->{bind_column} }, (($tcol) x scalar(@$bind)); | |||||||||||||||||||||||||||||||||||||||||||||
| 188 | 1 | push @{ $stmt->{bind} }, @$bind; | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 31 | ▶ | sub as_select { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_select: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 31 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | 31 | my $sql = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | 31 | ▶ | if (@{ $self->select }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 8 | $sql .= 'SELECT '; | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | $sql .= join(', ', map { | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 13 | my $alias = $self->select_map->{$_}; | |||||||||||||||||||||||||||||||||||||||||||||
| 198 | 13 | ▶ | $alias ? /(?:^|\.)\Q$alias\E$/ ? $_ : "$_ $alias" : $_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 8 | } @{ $self->select }); | |||||||||||||||||||||||||||||||||||||||||||||
| 200 | 8 | $sql .= "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 31 | $sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 31 | ▶ | sub as_join { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_join: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 31 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | 31 | my $sql = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | 31 | ▶ | 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;}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 5 | my $initial_table_written = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 210 | 5 | for my $data (@{ $self->joins }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 211 | 8 | my($table, $joins) = map { $data->{$_} } qw( table joins ); | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | 8 | ▶ | $sql .= $table unless $initial_table_written++; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 8 | for my $join (@{ $joins }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | 9 | my($type, $target) = (%{ $join }); | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | 9 | my $condition = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 216 | 9 | ▶ | if (ref $target eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 9 | my($key, $val) = (%{ $target }); | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | 9 | $target = $key; | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | 9 | $condition = $val; | |||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 9 | $sql .= ' ' . uc($type) . ' JOIN ' . $target; | |||||||||||||||||||||||||||||||||||||||||||||
| 222 | 9 | ▶ | $sql .= ' ON ' . $condition if $condition; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 5 | ▶ | $sql .= ', ' if @{ $self->from }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 31 | $sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 46 | ▶ | sub as_sql_where { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql_where: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 46 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 232 | 46 | ▶ | if ($self->where && @{ $self->where }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
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;}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 24 | return 'WHERE ' . join(' AND ', @{ $self->where }) . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 22 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 31 | ▶ | sub as_sql_having { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql_having: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 31 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | 31 | ▶ | if ($self->having && @{ $self->having }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
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;}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | return 'HAVING ' . join(' AND ', @{ $self->having }) . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 30 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 31 | ▶ | sub as_limit { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_limit: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 31 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 248 | 31 | ▶ | my $n = $self->limit or return ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 4 | ▶ | Carp::croak "Non-numerics in limit clause ($n)" if $n =~ /\D/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 3 | ▶ | return sprintf "LIMIT %d%s\n", $n, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | ($self->offset ? " OFFSET " . int($self->offset) : ""); | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 62 | ▶ | sub as_aggregate { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_aggregate: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 62 | my($self, $set) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 256 | 62 | ▶ | return '' unless my $attribute = $self->$set; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 62 | my @sqls; | |||||||||||||||||||||||||||||||||||||||||||||
| 259 | 62 | for my $element (@{ $attribute }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 260 | 14 | my $ref = ref $element; | |||||||||||||||||||||||||||||||||||||||||||||
| 261 | 14 | ▶ | if (!$ref) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 5 | push @sqls, $element; | |||||||||||||||||||||||||||||||||||||||||||||
| 263 | } elsif ($ref eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 9 | ▶ | while (my($column, $desc) = each %{ $element }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 10 | push @sqls, $column . ' ' . uc($desc); | |||||||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 62 | ▶ | return '' unless @sqls; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 11 | return uc($set) . ' BY ' . join(', ', @sqls) . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 31 | ▶ | sub as_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_sql: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 31 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 31 | my $sql = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 277 | 31 | $sql .= $self->as_select; | |||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 31 | $sql .= 'FROM '; | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | ## Add any explicit JOIN statements before the non-joined tables. | ||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 31 | $sql .= $self->as_join; | |||||||||||||||||||||||||||||||||||||||||||||
| 282 | 31 | $sql .= join(', ', @{ $self->from }) . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 31 | $sql .= $self->as_sql_where; | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 31 | $sql .= $self->as_aggregate('group'); | |||||||||||||||||||||||||||||||||||||||||||||
| 287 | 31 | $sql .= $self->as_sql_having; | |||||||||||||||||||||||||||||||||||||||||||||
| 288 | 31 | $sql .= $self->as_aggregate('order'); | |||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 31 | $sql .= $self->as_limit; | |||||||||||||||||||||||||||||||||||||||||||||
| 291 | 30 | $sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 50 | ▶ | sub _mk_term { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mk_term: called CC=17 SCAR=28.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 50 | my($self, $col, $val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 296 | 50 | my $term = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 297 | 50 | my (@bind, $m); | |||||||||||||||||||||||||||||||||||||||||||||
| 298 | 50 | ▶ | if (ref($col) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 1 | $term = ${ $col }; | |||||||||||||||||||||||||||||||||||||||||||||
| 300 | 1 | $col = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 301 | } elsif (ref($val) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 10 | ▶ | 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]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 8 | my $logic = 'OR'; | |||||||||||||||||||||||||||||||||||||||||||||
| 304 | 8 | my @values = @$val; | |||||||||||||||||||||||||||||||||||||||||||||
| 305 | 8 | ▶ | if ($val->[0] eq '-and') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 7 | $logic = 'AND'; | |||||||||||||||||||||||||||||||||||||||||||||
| 307 | 7 | shift @values; | |||||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 8 | my @terms; | |||||||||||||||||||||||||||||||||||||||||||||
| 311 | 8 | for my $v (@values) { | |||||||||||||||||||||||||||||||||||||||||||||
| 312 | 21 | my($term, $bind) = $self->_mk_term($col, $v); | |||||||||||||||||||||||||||||||||||||||||||||
| 313 | 21 | push @terms, "($term)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 314 | 21 | push @bind, @$bind; | |||||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 8 | $term = join " $logic ", @terms; | |||||||||||||||||||||||||||||||||||||||||||||
| 317 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 2 | ▶ | $col = $m->($col) if $m = $self->column_mutator; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 2 | $term = "$col IN (".join(',', ('?') x scalar @$val).')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 320 | 2 | @bind = @$val; | |||||||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 322 | } elsif (ref($val) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 7 | ▶ | $col = $m->($col) if $m = $self->column_mutator; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 7 | my($op, $v) = (%{ $val }); | |||||||||||||||||||||||||||||||||||||||||||||
| 325 | 7 | ▶ | if ((uc($op) eq 'IN' || uc($op) eq 'NOT IN') && ref($v) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
uc $op eq "IN" || uc $op eq "NOT IN" and ref $v eq "ARRAY"
uc $op eq "IN" or uc $op eq "NOT IN"
MC/DC66%2/3
uc $op eq "IN" || uc $op eq "NOT IN" and ref $v eq "ARRAY"
uc $op eq "IN"
uc $op eq "NOT IN"
ref $v eq "ARRAY"
Truth table60%3/5
A or B and C
A= uc $op eq "IN"B= uc $op eq "NOT IN"C= ref $v eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 2 | $term = "$col $op (".join(',', ('?') x scalar @$v).')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 327 | 2 | @bind = @$v; | |||||||||||||||||||||||||||||||||||||||||||||
| 328 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 5 | $term = "$col $op ?"; | |||||||||||||||||||||||||||||||||||||||||||||
| 330 | 5 | push @bind, $v; | |||||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 332 | } elsif (ref($val) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 1 | ▶ | $col = $m->($col) if $m = $self->column_mutator; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 1 | $term = "$col $$val"; | |||||||||||||||||||||||||||||||||||||||||||||
| 335 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 31 | ▶ | $col = $m->($col) if $m = $self->column_mutator; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 31 | $term = "$col = ?"; | |||||||||||||||||||||||||||||||||||||||||||||
| 338 | 31 | push @bind, $val; | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 50 | ($term, \@bind, $col); | |||||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||
| 345 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||