Easy/SQL.pm

filter: Statementstmt 80.3% 196 / 244 Branchbran 66.0% 70 / 106 Conditioncond 43.3% 36 / 83 MC/DCmcdc 14.0% 7 / 50 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 16 totaltotal 62.4% 323 / 517 CC 89 SCAR 26.7
CC
89
Cov
70%
sql_select
31.8
sql_order
20.0
sql_chunks_for_fields
17.4
CRAP 14.5
« Easy/DBH.pm ↑ Summary Easy.pm »
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
l!l
20
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
21
22 2 ▶ if (ref $length eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $length eq "ARRAY") 1 1 2
23 1 $length = $#$length + 1;
24 }
25
26 return
27 2 ▶ unless $length > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $length > 0 0 2 2
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
BranchTrueFalseTotal
unless ($field and $sort_col and $dir =~ /^(?:asc|desc)$/i) 0 0 0
Condition0%0/6
$field and $sort_col and $dir =~ /^(?:asc|desc)$/i
!ll&&!rl&&r
000
$field and $sort_col
!ll&&!rl&&r
000
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
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if $s > 1 or $s == -1 0 0 0
Condition0%0/3
$s > 1 or $s == -1
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
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'
l!l
730
MC/DC0%0/1
shift() || 'where'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
67  
68 73 my @sql;
69 my @bind;
70
71 73 foreach my $k (keys %$hash) {
72 105 ▶ next if $k =~ /^\:/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^\:/ 2 103 105
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
BranchTrueFalseTotal
if ($k =~ /^_(\w+)$/) 11 92 103
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
BranchTrueFalseTotal
if $self->dbh_vendor eq "oracle" 0 103 103
85 if $self->dbh_vendor eq 'oracle';
86
87 103 ▶ if (ref $v eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $v eq 'ARRAY') { } 1 102 103
elsif ($is_sql) { } 11 91 102
88
89 1 ▶ die "can't use sql statement as arrayref"
Branch: 1 branch
BranchTrueFalseTotal
if $is_sql 0 1 1
90 if $is_sql;
91
92 1 ▶ die "can't set/insert multiple values: " . join (', ', @$v)
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "where" 0 1 1
93 unless $mode eq 'where';
94
95 1 my $range;
96 1 ▶ if (! scalar @$v) {
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar @$v) { } 0 1 1
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
BranchTrueFalseTotal
if ($v =~ /$re/gs) 2 11 13
107 2 push @ph, $2;
108 }
109
110 11 $v =~ s/$re/ \? /gs;
111
112 11 ▶ if ($mode eq 'insert') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'insert') { } 0 11 11
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
BranchTrueFalseTotal
if ($mode eq 'insert') { } 41 50 91
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
BranchTrueFalseTotal
if defined $type and exists $DBI::Easy::BIND_TYPES{$type} 0 91 91
Condition66%2/3
defined $type and exists $DBI::Easy::BIND_TYPES{$type}
!ll&&!rl&&r
2890
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}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (ref $where_hash eq "ARRAY") 39 93 132
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
BranchTrueFalseTotal
if defined $where and $where ne "" 44 34 78
Condition66%2/3
defined $where and $where ne ""
!ll&&!rl&&r
34044
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 ""
ABresult
0X0
100
111
151 if defined $where and $where ne '';
152 78 ▶ push @bind_acc, @{$bind || []};
Condition100%2/2
$bind || []
l!l
3543
MC/DC100%1/1
$bind || []
$bind
Truth table100%2/2
A
A= $bind
Aresult
11
00
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
BranchTrueFalseTotal
if not defined $where_hash or $where_hash eq "" 19 74 93
Condition66%2/3
not defined $where_hash or $where_hash eq ""
l!l&&r!l&&!r
19074
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 ""
ABresult
1X1
011
000
159 74 ▶ return $where_hash if !ref $where_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $where_hash 9 65 74
160 65 ▶ return if ref $where_hash ne 'HASH' || scalar keys %$where_hash == 0;
Branch: 1 branch
BranchTrueFalseTotal
if ref $where_hash ne "HASH" or scalar keys %$where_hash == 0 15 50 65
Condition66%2/3
ref $where_hash ne "HASH" or scalar keys %$where_hash == 0
l!l&&r!l&&!r
01550
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless (ref $param_hash eq "HASH") 0 5 5
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
BranchTrueFalseTotal
if (not defined $where_hash && ref($where_hash) =~ /HASH|ARRAY/) { } 0 5 5
Condition33%1/3
defined $where_hash && ref($where_hash) =~ /HASH|ARRAY/
!ll&&!rl&&r
005
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/
ABresult
0X0
100
111
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
l!l&&r!l&&!r
1800
MC/DC0%0/2
shift() || $self->field_values
shift() $self->field_values
Truth table33%1/3
A || B
A= shift()B= $self->field_values
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (ref $_[0]) { } 0 6 6
220 0 $set_values = shift;
221 0 $where_values = shift;
222 0 ▶ $suffix = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
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'} || ''
l!l
06
MC/DC0%0/1
$params->{'suffix'} || ''
$params->{'suffix'}
Truth table50%1/2
A
A= $params->{'suffix'}
Aresult
11
00
228 6 $update_all = $params->{all};
229 }
230
231 6 ▶ warn ("nothing to set in update"), return unless $set_values;
Branch: 1 branch
BranchTrueFalseTotal
unless $set_values 0 6 6
232 6 ▶ warn ("please use update_all for update everything"), return unless $where_values;
Branch: 1 branch
BranchTrueFalseTotal
unless $where_values 1 5 6
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
BranchTrueFalseTotal
if $where_statement 5 0 5
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
BranchTrueFalseTotal
if (ref $_[0]) { } 0 2 2
255 0 $where_values = shift;
256 0 ▶ $suffix = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
257 } else {
258 2 $params = {@_};
259 2 $where_values = $params->{where};
260 2 ▶ $suffix = $params->{suffix} || '';
Condition50%1/2
$params->{'suffix'} || ''
l!l
02
MC/DC0%0/1
$params->{'suffix'} || ''
$params->{'suffix'}
Truth table50%1/2
A
A= $params->{'suffix'}
Aresult
11
00
261 2 $update_all = $params->{all};
262 }
263
264 2 ▶ warn ("please use delete_all for delete everything"), return
Branch: 1 branch
BranchTrueFalseTotal
unless $where_values 0 2 2
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
BranchTrueFalseTotal
unless ($where_statement) 0 2 2
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() || {}
l!l
01
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
287 1 ▶ my $suffix = shift || '';
Condition50%1/2
shift() || ''
l!l
01
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
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
BranchTrueFalseTotal
if ($params{"where"}) 0 0 0
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
l!l&&r!l&&!r
030
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless $self->{"column_values"}{$_pk_column_} or $self->{"field_values"}{$_pk_} 0 3 3
Condition33%1/3
$self->{"column_values"}{$_pk_column_} or $self->{"field_values"}{$_pk_}
l!l&&r!l&&!r
300
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_}
ABresult
1X1
011
000
326
327 my $where_hash = {
328 3 ▶ %{$params{where} || {}},
Condition40%2/5
$params{'where'} || {}
l!l
03
$self->{'column_values'}{$_pk_column_} || $self->{'field_values'}{$_pk_}
l!l&&r!l&&!r
300
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'}
Aresult
11
00
Truth table33%1/3
A || B
A= $self->{'column_values'}{$_pk_column_}B= $self->{'field_values'}{$_pk_}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless (defined $fieldset) 28 17 45
344 28 $fieldset = $self->fieldset;
345 }
346
347 45 ▶ return '*'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fieldset and $fieldset 22 23 45
Condition66%2/3
defined $fieldset and $fieldset
!ll&&!rl&&r
22023
MC/DC50%1/2
defined $fieldset and $fieldset
defined $fieldset $fieldset
Truth table66%2/3
A and B
A= defined $fieldsetB= $fieldset
ABresult
0X0
100
111
348 if !defined $fieldset or !$fieldset;
349
350 23 ▶ return $fieldset
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fieldset 20 3 23
351 unless ref $fieldset;
352
353 3 ▶ die "can't recognize what you want, provide arrayref or string as fetch fields"
Branch: 1 branch
BranchTrueFalseTotal
if ref $fieldset ne "ARRAY" or not scalar @$fieldset 0 3 3
Condition33%1/3
ref $fieldset ne "ARRAY" or not scalar @$fieldset
l!l&&r!l&&!r
003
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if (exists $fields->{$field}) { } 5 0 5
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'} || {}
l!l
450
MC/DC0%0/1
$params{'where'} || {}
$params{'where'}
Truth table50%1/2
A
A= $params{'where'}
Aresult
11
00
378 45 ▶ my $suffix = $params{suffix} || '';
Condition50%1/2
$params{'suffix'} || ''
l!l
045
MC/DC0%0/1
$params{'suffix'} || ''
$params{'suffix'}
Truth table50%1/2
A
A= $params{'suffix'}
Aresult
11
00
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
BranchTrueFalseTotal
if (defined $fieldset and defined $fieldset_method) 0 45 45
Condition66%2/3
defined $fieldset and defined $fieldset_method
!ll&&!rl&&r
28170
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (defined $fieldset_method) 0 45 45
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
BranchTrueFalseTotal
if (defined $params{"group_by"}) 1 44 45
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
BranchTrueFalseTotal
unless $group 0 1 1
403
404 1 ▶ $group_by = "group by $group"
Branch: 1 branch
BranchTrueFalseTotal
if $group 1 0 1
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
BranchTrueFalseTotal
if (defined $params{"limit"} and $params{"limit"} =~ /^(\d+)$/) 1 44 45
Condition66%2/3
defined $params{"limit"} and $params{"limit"} =~ /^(\d+)$/
!ll&&!rl&&r
4401
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+)$/
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (defined $params{"offset"} and $params{"offset"} =~ /^(\d+)$/) 0 1 1
Condition33%1/3
defined $params{"offset"} and $params{"offset"} =~ /^(\d+)$/
!ll&&!rl&&r
100
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+)$/
ABresult
0X0
100
111
417 0 $limits .= " offset $1";
418 }
419
420 1 ▶ unless ($params{sort_field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"sort_field"}) 0 1 1
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
BranchTrueFalseTotal
if (defined $params{"sort_field"}) 1 44 45
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
BranchTrueFalseTotal
unless $order_expr 0 1 1
433
434 1 my $order = $params{sort_order};
435 1 ▶ $order = 'desc' unless $order;
Branch: 1 branch
BranchTrueFalseTotal
unless $order 1 0 1
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
BranchTrueFalseTotal
if ($self->can("join_table")) 38 7 45
445 38 my $join = $self->join_table;
446 38 ▶ if (defined $join and $join !~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $join and not $join =~ /^\s*$/) 0 38 38
Condition33%1/3
defined $join and not $join =~ /^\s*$/
!ll&&!rl&&r
3800
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*$/
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if defined $where_statement and not $where_statement =~ /^\s*$/ 38 7 45
Condition66%2/3
defined $where_statement and not $where_statement =~ /^\s*$/
!ll&&!rl&&r
0738
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*$/
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (ref $_[0]) { } 0 16 16
471 0 $where = shift;
472 0 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 0 0
473 0 $suffix = shift;
474 }
475 0 %params = (@_);
476 } else {
477 16 %params = (@_);
478 16 ▶ $where = $params{where} || {};
Condition50%1/2
$params{'where'} || {}
l!l
160
MC/DC0%0/1
$params{'where'} || {}
$params{'where'}
Truth table50%1/2
A
A= $params{'where'}
Aresult
11
00
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
« Easy/DBH.pm ↑ Summary Easy.pm »