| 1 | package DBI::Easy; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # $Id: SQL.pm,v 1.6 2009/07/20 18:00:08 apla Exp $ | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Class::Easy; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use DBI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # sql generation stuff | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | our %BIND_TYPES = ( | |||||||||||||||||||||||||||||||||||||||||
| 13 | # VARCHAR2 => {ora_type => 1}, | ||||||||||||||||||||||||||||||||||||||||||
| 14 | CLOB => {ora_type => 112}, | ||||||||||||||||||||||||||||||||||||||||||
| 15 | BLOB => {ora_type => 113} | ||||||||||||||||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | sub sql_range { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_range: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | my $length = shift || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | if (ref $length eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | $length = $#$length + 1; | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | return | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | ▶ | unless $length > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | return join ', ', ('?') x $length; | |||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub sql_names_range { # now with AI | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_names_range: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $list = shift; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my $fields = shift; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | return join ', ', @$list; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub sql_order { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_order: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $field = shift; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my $dir = shift; | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $sort_col = $self->fields->{$field}->{quoted_column_name}; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if (!$field || !$sort_col || $dir !~ /^(?:asc|desc)$/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$field and $sort_col and $dir =~ /^(?:asc|desc)$/i
$field and $sort_col
MC/DC0%0/3
$field and $sort_col and $dir =~ /^(?:asc|desc)$/i
$field
$sort_col
$dir =~ /^(?:asc|desc)$/i
Truth table0%0/4
A and B and C
A= $fieldB= $sort_colC= $dir =~ /^(?:asc|desc)$/i
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | return "order by $sort_col $dir"; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub sql_limit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_limit: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my $s = $#_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | die if $s > 1 || $s == -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$s > 1 or $s == -1
MC/DC0%0/2
$s > 1 or $s == -1
$s > 1
$s == -1
Truth table0%0/3
A or B
A= $s > 1B= $s == -1
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | return "limit " . join ', ', @_; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 73 | ▶ | sub sql_chunks_for_fields { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_chunks_for_fields: called CC=16 SCAR=28.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 73 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 73 | my $hash = shift; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 73 | ▶ | my $mode = shift || 'where'; # also 'set' and 'insert' | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'where'
MC/DC0%0/1
shift() || 'where'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 73 | my @sql; | |||||||||||||||||||||||||||||||||||||||||
| 69 | my @bind; | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 73 | foreach my $k (keys %$hash) { | |||||||||||||||||||||||||||||||||||||||||
| 72 | 105 | ▶ | next if $k =~ /^\:/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 103 | my $v = $hash->{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 103 | my $is_sql = 0; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 103 | ▶ | if ($k =~ /^_(\w+)$/) { # when we get param as _param, then we interpret it as sql | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 11 | $is_sql = 1; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 11 | $k = $1; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 103 | my $type = $self->columns->{$k}->{type_name}; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 103 | my $qk = $self->dbh->quote_identifier ($k); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 103 | ▶ | $qk = $k | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | if $self->dbh_vendor eq 'oracle'; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 103 | ▶ | if (ref $v eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | die "can't use sql statement as arrayref" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | if $is_sql; | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | die "can't set/insert multiple values: " . join (', ', @$v) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | unless $mode eq 'where'; | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | my $range; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | if (! scalar @$v) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $range = 'null'; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | $range = $self->sql_range ($v); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | push @sql, qq($qk in ($range)); | |||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | push @bind, @$v; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } elsif ($is_sql) { | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 11 | my @ph; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 11 | my $re = '(^|[\=\,\s\(])(:\w+)([\=\,\s\)]|$)'; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 11 | ▶ | while ($v =~ /$re/gs) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | push @ph, $2; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 11 | $v =~ s/$re/ \? /gs; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 11 | ▶ | if ($mode eq 'insert') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | push @{$sql[0]}, $qk; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | push @{$sql[1]}, $v; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 11 | push @sql, qq($qk $v); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 11 | push @bind, map {$hash->{$_}} @ph; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 91 | ▶ | if ($mode eq 'insert') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 41 | push @{$sql[0]}, $qk; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 41 | push @{$sql[1]}, '?'; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 50 | push @sql, qq($qk = ?); | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | $v = [$v, $type, $k] | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 91 | ▶ | if defined $type and exists $BIND_TYPES{$type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $type and exists $DBI::Easy::BIND_TYPES{$type}
MC/DC0%0/2
defined $type and exists $DBI::Easy::BIND_TYPES{$type}
defined $type
exists $DBI::Easy::BIND_TYPES{$type}
Truth table66%2/3
A and B
A= defined $typeB= exists $DBI::Easy::BIND_TYPES{$type}
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 91 | push @bind, $v; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 73 | return \@sql, \@bind; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | # get sql statement for insert | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 132 | ▶ | sub sql_where { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_where: called CC=11 SCAR=24.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 132 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 132 | my $where_hash = shift; | |||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 132 | ▶ | if (ref $where_hash eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 39 | my (@where_acc, @bind_acc); | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 39 | foreach (@$where_hash) { | |||||||||||||||||||||||||||||||||||||||||
| 149 | 78 | my ($where, $bind) = $self->sql_where ($_); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 78 | ▶ | push @where_acc, $where | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $where and $where ne ""
MC/DC50%1/2
defined $where and $where ne ""
defined $where
$where ne ""
Truth table66%2/3
A and B
A= defined $whereB= $where ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | if defined $where and $where ne ''; | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 78 | ▶ | push @bind_acc, @{$bind || []}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$bind || []
MC/DC100%1/1
$bind || []
$bind
Truth table100%2/2
A
A= $bind
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 39 | return join (' and ', @where_acc), \@bind_acc; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 93 | ▶ | return if ! defined $where_hash or $where_hash eq ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $where_hash or $where_hash eq ""
MC/DC50%1/2
not defined $where_hash or $where_hash eq ""
not defined $where_hash
$where_hash eq ""
Truth table66%2/3
not(A) or B
A= not defined $where_hashB= $where_hash eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 74 | ▶ | return $where_hash if !ref $where_hash; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 65 | ▶ | return if ref $where_hash ne 'HASH' || scalar keys %$where_hash == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $where_hash ne "HASH" or scalar keys %$where_hash == 0
MC/DC50%1/2
ref $where_hash ne "HASH" or scalar keys %$where_hash == 0
ref $where_hash ne "HASH"
scalar keys %$where_hash == 0
Truth table66%2/3
A or B
A= ref $where_hash ne "HASH"B= scalar keys %$where_hash == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 50 | my ($where, $bind) = $self->sql_chunks_for_fields ($where_hash, 'where'); | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 50 | return join (' and ', @$where), $bind; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | # Получаем список выражений для SET | ||||||||||||||||||||||||||||||||||||||||||
| 168 | # как строку вида 'param1 = ?, param2 = ?' | ||||||||||||||||||||||||||||||||||||||||||
| 169 | # и массив значений для bind, | ||||||||||||||||||||||||||||||||||||||||||
| 170 | # построенные на основе заданного хэша | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 5 | ▶ | sub sql_set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_set: called CC=4 SCAR=15.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 5 | my $param_hash = shift; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 5 | my $where_hash = shift; | |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 5 | ▶ | unless (ref($param_hash) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | warn "please specify parameters hash"; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 5 | my ($set, $bind) = $self->sql_chunks_for_fields ($param_hash, 'set'); | |||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 5 | my $sql_set = join ', ', @$set; | |||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 5 | ▶ | if (!defined $where_hash or ref($where_hash) !~ /HASH|ARRAY/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $where_hash && ref($where_hash) =~ /HASH|ARRAY/
MC/DC0%0/2
defined $where_hash && ref($where_hash) =~ /HASH|ARRAY/
defined $where_hash
ref($where_hash) =~ /HASH|ARRAY/
Truth table33%1/3
A && B
A= defined $where_hashB= ref($where_hash) =~ /HASH|ARRAY/
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | return ($sql_set, $bind); | |||||||||||||||||||||||||||||||||||||||||
| 187 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 5 | my ($where_set, $bind_add) = $self->sql_where ($where_hash); | |||||||||||||||||||||||||||||||||||||||||
| 189 | 5 | push @$bind, @$bind_add; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 5 | return ($sql_set, $bind, $where_set); | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | # real sql statements | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 18 | ▶ | sub sql_insert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_insert: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 18 | ▶ | my $hash = shift || $self->field_values; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $self->field_values
MC/DC0%0/2
shift() || $self->field_values
shift()
$self->field_values
Truth table33%1/3
A || B
A= shift()B= $self->field_values
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 18 | my ($set, $bind) = $self->sql_chunks_for_fields ($hash, 'insert'); | |||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 18 | my $table_name = $self->table_quoted; | |||||||||||||||||||||||||||||||||||||||||
| 203 | my $statement = "insert into $table_name (" . | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 18 | join (', ', @{$set->[0]}) . ") values (" . | |||||||||||||||||||||||||||||||||||||||||
| 205 | 18 | join (', ', @{$set->[1]}) . ")"; | |||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 18 | return $statement, $bind; | |||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 6 | ▶ | sub sql_update { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_update: called CC=7 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 6 | my $set_values; | |||||||||||||||||||||||||||||||||||||||||
| 213 | my $where_values; | ||||||||||||||||||||||||||||||||||||||||||
| 214 | 6 | my $suffix = ''; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 6 | my $update_all = 0; | |||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 6 | my $params; | |||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 6 | ▶ | if (ref $_[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | $set_values = shift; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $where_values = shift; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | $suffix = shift || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 224 | 6 | $params = {@_}; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 6 | $set_values = $params->{set}; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 6 | $where_values = $params->{where}; | |||||||||||||||||||||||||||||||||||||||||
| 227 | 6 | ▶ | $suffix = $params->{suffix} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{'suffix'} || ''
MC/DC0%0/1
$params->{'suffix'} || ''
$params->{'suffix'}
Truth table50%1/2
A
A= $params->{'suffix'}
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 6 | $update_all = $params->{all}; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 6 | ▶ | warn ("nothing to set in update"), return unless $set_values; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 6 | ▶ | warn ("please use update_all for update everything"), return unless $where_values; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 5 | my $table_name = $self->table_quoted; | |||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 5 | my ($set_statement, $bind, $where_statement) | |||||||||||||||||||||||||||||||||||||||||
| 237 | = $self->sql_set ($set_values, $where_values); | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 5 | my $statement = "update $table_name set $set_statement"; | |||||||||||||||||||||||||||||||||||||||||
| 240 | 5 | ▶ | $statement .= " where $where_statement" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | if $where_statement; | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | 5 | return $statement . ' ' . $suffix, $bind; | |||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 2 | ▶ | sub sql_delete { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_delete: called CC=6 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 2 | my $where_values; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 2 | my $suffix = ''; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 2 | my $update_all = 0; | |||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | 2 | my $params; | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 2 | ▶ | if (ref $_[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | $where_values = shift; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | $suffix = shift || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 2 | $params = {@_}; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 2 | $where_values = $params->{where}; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 2 | ▶ | $suffix = $params->{suffix} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params->{'suffix'} || ''
MC/DC0%0/1
$params->{'suffix'} || ''
$params->{'suffix'}
Truth table50%1/2
A
A= $params->{'suffix'}
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 2 | $update_all = $params->{all}; | |||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 2 | ▶ | warn ("please use delete_all for delete everything"), return | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | unless $where_values; | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 2 | my $table_name = $self->table_quoted; | |||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | 2 | my ($where_statement, $bind) | |||||||||||||||||||||||||||||||||||||||||
| 270 | = $self->sql_where ($where_values); | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 2 | my $statement = "delete from $table_name"; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 2 | ▶ | if (!$where_statement) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | warn "you can't delete all data from table, use delete_all or truncate"; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | 2 | $statement .= " where $where_statement"; | |||||||||||||||||||||||||||||||||||||||||
| 279 | 2 | debug $statement; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 2 | return $statement . ' ' . $suffix, $bind; | |||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 1 | ▶ | sub sql_delete_by_pk { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_delete_by_pk: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 285 | # BAD!!! needs to be rewritten | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 1 | ▶ | my $where = shift || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 1 | ▶ | my $suffix = shift || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | my $_pk_column_ = $self->_pk_column_; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | my $_pk_ = $self->_pk_; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 1 | my $where_hash = {%$where, $_pk_column_ => $self->$_pk_}; | |||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | return $self->sql_delete (where => $where_hash, suffix => $suffix); | |||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | sub sql_select_by_pk { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_select_by_pk: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | my $where = {}; | |||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | ▶ | if ($params{where}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | $where = delete $params{where}; | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | my $_pk_column_ = $self->_pk_column_; | |||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | return $self->sql_select (where => [ | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | $where, $_pk_column_ => $self->{$self->_pk_} | |||||||||||||||||||||||||||||||||||||||||
| 312 | ], %params); | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | 3 | ▶ | sub sql_update_by_pk { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_update_by_pk: called CC=6 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 318 | 3 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 3 | ▶ | my $set_hash = $params{set} || $self->fields_to_columns; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$params{'set'} || $self->fields_to_columns
MC/DC0%0/2
$params{'set'} || $self->fields_to_columns
$params{'set'}
$self->fields_to_columns
Truth table33%1/3
A || B
A= $params{'set'}B= $self->fields_to_columns
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | 3 | my $_pk_column_ = $self->_pk_column_; | |||||||||||||||||||||||||||||||||||||||||
| 323 | 3 | my $_pk_ = $self->_pk_; | |||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | 3 | ▶ | return unless $self->{column_values}->{$_pk_column_} || $self->{field_values}->{$_pk_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"column_values"}{$_pk_column_} or $self->{"field_values"}{$_pk_}
MC/DC0%0/2
$self->{"column_values"}{$_pk_column_} or $self->{"field_values"}{$_pk_}
$self->{"column_values"}{$_pk_column_}
$self->{"field_values"}{$_pk_}
Truth table33%1/3
A or B
A= $self->{"column_values"}{$_pk_column_}B= $self->{"field_values"}{$_pk_}
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | my $where_hash = { | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 3 | ▶ | %{$params{where} || {}}, | ||||||||||||||||||||||||||||||||||||||||
|
Condition40%2/5
$params{'where'} || {}
$self->{'column_values'}{$_pk_column_} || $self->{'field_values'}{$_pk_}
MC/DC0%0/1
$params{'where'} || {}
$params{'where'}
MC/DC0%0/2
$self->{'column_values'}{$_pk_column_} || $self->{'field_values'}{$_pk_}
$self->{'column_values'}{$_pk_column_}
$self->{'field_values'}{$_pk_}
Truth table50%1/2
A
A= $params{'where'}
Truth table33%1/3
A || B
A= $self->{'column_values'}{$_pk_column_}B= $self->{'field_values'}{$_pk_}
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 3 | $_pk_column_ => $self->{column_values}->{$_pk_column_} || $self->{field_values}->{$_pk_} | |||||||||||||||||||||||||||||||||||||||||
| 330 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | my ($sql, $bind) = $self->sql_update ( | ||||||||||||||||||||||||||||||||||||||||||
| 333 | set => $set_hash, where => $where_hash, suffix => $params{suffix} | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 3 | return $sql, $bind; | |||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | 45 | ▶ | sub sql_column_list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_column_list: called CC=9 SCAR=22.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 45 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 45 | my $fieldset = shift; | |||||||||||||||||||||||||||||||||||||||||
| 343 | 45 | ▶ | unless (defined $fieldset) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 28 | $fieldset = $self->fieldset; | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | 45 | ▶ | return '*' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $fieldset and $fieldset
MC/DC50%1/2
defined $fieldset and $fieldset
defined $fieldset
$fieldset
Truth table66%2/3
A and B
A= defined $fieldsetB= $fieldset
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | if !defined $fieldset or !$fieldset; | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | 23 | ▶ | return $fieldset | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | unless ref $fieldset; | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | 3 | ▶ | die "can't recognize what you want, provide arrayref or string as fetch fields" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $fieldset ne "ARRAY" or not scalar @$fieldset
MC/DC0%0/2
ref $fieldset ne "ARRAY" or not scalar @$fieldset
ref $fieldset ne "ARRAY"
not scalar @$fieldset
Truth table33%1/3
A or not(B)
A= ref $fieldset ne "ARRAY"B= not scalar @$fieldset
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | if ref $fieldset ne 'ARRAY' or ! scalar @$fieldset; | ||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||
| 356 | 3 | my $col_list = []; | |||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | 3 | my $fields = $self->fields; | |||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 3 | foreach my $field (@$fieldset) { | |||||||||||||||||||||||||||||||||||||||||
| 361 | 5 | ▶ | if (exists $fields->{$field}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 5 | push @$col_list, $fields->{$field}->{quoted_column_name}; | |||||||||||||||||||||||||||||||||||||||||
| 363 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 364 | # may be erratical | ||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | push @$col_list, $field; | |||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | 3 | return join ', ', @$col_list; | |||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | 45 | ▶ | sub sql_select { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_select: called CC=22 SCAR=34.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 373 | 45 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | 45 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | 45 | ▶ | my $where = $params{where} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params{'where'} || {}
MC/DC0%0/1
$params{'where'} || {}
$params{'where'}
Truth table50%1/2
A
A= $params{'where'}
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | 45 | ▶ | my $suffix = $params{suffix} || ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params{'suffix'} || ''
MC/DC0%0/1
$params{'suffix'} || ''
$params{'suffix'}
Truth table50%1/2
A
A= $params{'suffix'}
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 45 | my $fieldset = $params{fieldset}; | |||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | 45 | my $fieldset_method = $params{fieldset_name}; | |||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | 45 | ▶ | if (defined $fieldset and defined $fieldset_method) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $fieldset and defined $fieldset_method
MC/DC0%0/2
defined $fieldset and defined $fieldset_method
defined $fieldset
defined $fieldset_method
Truth table66%2/3
A and B
A= defined $fieldsetB= defined $fieldset_method
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | critical "you must use only fieldset with field names/expressions or fieldset_name for method who return fieldset"; | |||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | 45 | ▶ | if (defined $fieldset_method) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | $fieldset = $self->$fieldset_method; | |||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | 45 | my $cols_statement = $self->sql_column_list ($fieldset); | |||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | # TODO: multiple condition/expression (group|order)_by | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | 45 | my $group_by = ''; | |||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 45 | ▶ | if (defined $params{group_by}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | # try to translate field => column, and copy field if failed | ||||||||||||||||||||||||||||||||||||||||||
| 400 | 1 | my $group = $self->fields->{$params{group_by}}->{quoted_column_name}; | |||||||||||||||||||||||||||||||||||||||||
| 401 | $group = $params{group_by} | ||||||||||||||||||||||||||||||||||||||||||
| 402 | 1 | ▶ | unless $group; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | 1 | ▶ | $group_by = "group by $group" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | if $group; | ||||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | 45 | my $limits = ''; | |||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | # parameter expansion here | ||||||||||||||||||||||||||||||||||||||||||
| 411 | 45 | ▶ | if (defined $params{limit} and $params{limit} =~ /^(\d+)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $params{"limit"} and $params{"limit"} =~ /^(\d+)$/
MC/DC50%1/2
defined $params{"limit"} and $params{"limit"} =~ /^(\d+)$/
defined $params{"limit"}
$params{"limit"} =~ /^(\d+)$/
Truth table66%2/3
A and B
A= defined $params{"limit"}B= $params{"limit"} =~ /^(\d+)$/
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | # TODO: limit/offset by driver | ||||||||||||||||||||||||||||||||||||||||||
| 413 | # and SQL:2008 OFFSET start { ROW | ROWS } | ||||||||||||||||||||||||||||||||||||||||||
| 414 | # FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 1 | $limits .= "limit $1"; | |||||||||||||||||||||||||||||||||||||||||
| 416 | 1 | ▶ | if (defined $params{offset} and $params{offset} =~ /^(\d+)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $params{"offset"} and $params{"offset"} =~ /^(\d+)$/
MC/DC0%0/2
defined $params{"offset"} and $params{"offset"} =~ /^(\d+)$/
defined $params{"offset"}
$params{"offset"} =~ /^(\d+)$/
Truth table33%1/3
A and B
A= defined $params{"offset"}B= $params{"offset"} =~ /^(\d+)$/
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | $limits .= " offset $1"; | |||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | 1 | ▶ | unless ($params{sort_field}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | warn "you must use sort_(field|order) when you use limit/offset, we decide to set sort_field to primary key"; | |||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | $params{sort_field} = $self->_pk_; | |||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | 45 | my $order_by = ''; | |||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | 45 | ▶ | if (defined $params{sort_field}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | 1 | my $order_expr = $self->fields->{$params{sort_field}}->{quoted_column_name}; | |||||||||||||||||||||||||||||||||||||||||
| 431 | $order_expr = $params{sort_field} | ||||||||||||||||||||||||||||||||||||||||||
| 432 | 1 | ▶ | unless $order_expr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | 1 | my $order = $params{sort_order}; | |||||||||||||||||||||||||||||||||||||||||
| 435 | 1 | ▶ | $order = 'desc' unless $order; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 1 | $order_by = "order by $order_expr $order"; | |||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | 45 | my $table_name = $self->table_quoted; | |||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | 45 | my $statement = "select $cols_statement from $table_name"; | |||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | 45 | ▶ | if ($self->can ('join_table')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 38 | my $join = $self->join_table; | |||||||||||||||||||||||||||||||||||||||||
| 446 | 38 | ▶ | if (defined $join and $join !~ /^\s*$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $join and not $join =~ /^\s*$/
MC/DC0%0/2
defined $join and not $join =~ /^\s*$/
defined $join
not $join =~ /^\s*$/
Truth table33%1/3
A and not(B)
A= defined $joinB= not $join =~ /^\s*$/
| |||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | $statement .= ' ' . $join; | |||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | 45 | my ($where_statement, $bind); | |||||||||||||||||||||||||||||||||||||||||
| 452 | 45 | ($where_statement, $bind) = $self->sql_where ($where); | |||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | 45 | ▶ | $statement .= " where $where_statement" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $where_statement and not $where_statement =~ /^\s*$/
MC/DC50%1/2
defined $where_statement and not $where_statement =~ /^\s*$/
defined $where_statement
not $where_statement =~ /^\s*$/
Truth table66%2/3
A and not(B)
A= defined $where_statementB= not $where_statement =~ /^\s*$/
| |||||||||||||||||||||||||||||||||||||||||||
| 455 | if defined $where_statement and $where_statement !~ /^\s*$/; | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | return | ||||||||||||||||||||||||||||||||||||||||||
| 458 | 45 | join (' ', $statement, $group_by, $suffix, $order_by, $limits), | |||||||||||||||||||||||||||||||||||||||||
| 459 | $bind; | ||||||||||||||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | 16 | ▶ | sub sql_select_count { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql_select_count: called CC=4 SCAR=16.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 463 | 16 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | 16 | my $where = {}; | |||||||||||||||||||||||||||||||||||||||||
| 466 | 16 | my $suffix = ''; | |||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | 16 | my %params; | |||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | 16 | ▶ | if (ref $_[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | $where = shift; | |||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | ▶ | if (@_ % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | $suffix = shift; | |||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | %params = (@_); | |||||||||||||||||||||||||||||||||||||||||
| 476 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 477 | 16 | %params = (@_); | |||||||||||||||||||||||||||||||||||||||||
| 478 | 16 | ▶ | $where = $params{where} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params{'where'} || {}
MC/DC0%0/1
$params{'where'} || {}
$params{'where'}
Truth table50%1/2
A
A= $params{'where'}
| |||||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | 16 | return $self->sql_select (where => $where, suffix => $suffix, %params, fieldset => 'count(*)'); | |||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||
| 490 | DBI::Easy::SQL - handling sql for DBI::Easy | ||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | =head1 ABSTRACT | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | This module is a SQL expressions constructor for DBI::Easy. | ||||||||||||||||||||||||||||||||||||||||||
| 495 | So DBI::Easy::SQL is a wrapper between SQL and the rest part of DBI::Easy | ||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||
| 503 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | =head2 sql_column_list | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | returns a list of SQL columns | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | =head2 sql_delete, sql_delete_by_pk | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | creates a SQL DELETE query | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | =head2 sql_insert | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | creates a SQL INSERT query | ||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||
| 523 | =head2 sql_limit | ||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||
| 525 | adds limits to SQL query | ||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | =head2 sql_names_range | ||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | TODO | ||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||
| 533 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | =head2 sql_order | ||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||
| 537 | add ORDER BY to SQL query | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||
| 541 | =head2 sql_range | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | create placeholders for ranged sql statements, as example by | ||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||
| 545 | ... where column in (?, ?) ... | ||||||||||||||||||||||||||||||||||||||||||
| 546 | insert into table (col1, col2) values (?, ?) ... | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | receive number of placeholders to generate or arrayref, returns | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | join ', ', ('?' x $num) | ||||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||
| 552 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||
| 554 | =head2 sql_select, sql_select_by_pk, sql_select_count | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | creates SELECT SQL query | ||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | =head2 sql_set | ||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||
| 562 | creates SET SQL expression (for UPDATE query as an example) | ||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||
| 566 | =head2 sql_update, sql_update_by_pk | ||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | creates UPDATE SQL query | ||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | =head2 sql_where | ||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||
| 574 | creates WHERE SQL expression | ||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||
| 579 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | Ivan Baktsheev, C<< <apla at the-singlers.us> >> | ||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | Please report any bugs or feature requests to my email address, | ||||||||||||||||||||||||||||||||||||||||||
| 586 | or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Easy>. | ||||||||||||||||||||||||||||||||||||||||||
| 587 | I will be notified, and then you'll automatically be notified | ||||||||||||||||||||||||||||||||||||||||||
| 588 | of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||
| 590 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||
| 594 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||||||||
| 600 | Copyright 2008-2009 Ivan Baktsheev | ||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 603 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||
| 605 | =cut | ||||||||||||||||||||||||||||||||||||||||||