Skip to content

Commit 4a4b6a0

Browse files
authored
Merge pull request #47 from charsbar/fix/illegal_where_clauses
Fix illegal where clauses
2 parents 3dc5ab2 + 918ae0d commit 4a4b6a0

2 files changed

Lines changed: 40 additions & 9 deletions

File tree

lib/Data/ObjectDriver/SQL.pm

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ sub add_complex_where {
171171
my $stmt = shift;
172172
my ($terms) = @_;
173173
my ($where, $bind) = $stmt->_parse_array_terms($terms);
174-
push @{ $stmt->{where} }, $where;
175-
push @{ $stmt->{bind} }, @$bind;
174+
if ($where) {
175+
push @{ $stmt->{where} }, $where;
176+
push @{ $stmt->{bind} }, @$bind;
177+
}
176178
}
177179

178180
sub _parse_array_terms {
@@ -195,18 +197,22 @@ sub _parse_array_terms {
195197
foreach my $t2 ( keys %$t ) {
196198
my ($term, $bind, $col) = $stmt->_mk_term($t2, $t->{$t2});
197199
$stmt->where_values->{$col} = $t->{$t2};
198-
push @out, "($term)";
199-
push @bind, @$bind;
200+
if ($term) {
201+
push @out, "($term)";
202+
push @bind, @$bind;
203+
}
200204
}
201-
$out .= '(' . join(" AND ", @out) . ")";
205+
$out .= '(' . join(" AND ", @out) . ")" if @out;
202206
}
203207
elsif (ref $t eq 'ARRAY') {
204208
# another array of terms to process!
205209
my ($where, $bind) = $stmt->_parse_array_terms( $t );
206-
push @bind, @$bind;
207-
$out = '(' . $where . ')';
210+
if ($where) {
211+
push @bind, @$bind;
212+
$out = '(' . $where . ')';
213+
}
208214
}
209-
push @out, (@out ? ' ' . $logic . ' ' : '') . $out;
215+
push @out, (@out ? ' ' . $logic . ' ' : '') . $out if $out;
210216
}
211217
return (join("", @out), \@bind);
212218
}

t/11-sql.t

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use strict;
44

55
use Data::ObjectDriver::SQL;
6-
use Test::More tests => 103;
6+
use Test::More tests => 113;
77

88
my $stmt = ns();
99
ok($stmt, 'Created SQL object');
@@ -223,6 +223,31 @@ $stmt->add_where(foo => []);
223223
is($stmt->as_sql_where, "WHERE (0 = 1)\n"); # foo IN ()
224224
is(scalar @{ $stmt->bind }, 0);
225225

226+
$stmt = ns();
227+
$stmt->add_complex_where([]);
228+
is($stmt->as_sql_where, ""); # no WHERE without expression
229+
is(scalar @{ $stmt->bind }, 0);
230+
231+
$stmt = ns();
232+
$stmt->add_complex_where([[]]);
233+
is($stmt->as_sql_where, ""); # no WHERE without expression
234+
is(scalar @{ $stmt->bind }, 0);
235+
236+
$stmt = ns();
237+
$stmt->add_complex_where([{}]);
238+
is($stmt->as_sql_where, ""); # no WHERE without expression
239+
is(scalar @{ $stmt->bind }, 0);
240+
241+
$stmt = ns();
242+
$stmt->add_complex_where([{id => 1}, {}]);
243+
is($stmt->as_sql_where, "WHERE ((id = ?))\n"); # no empty expression
244+
is(scalar @{ $stmt->bind }, 1);
245+
246+
$stmt = ns();
247+
$stmt->add_complex_where([{id => 1}, []]);
248+
is($stmt->as_sql_where, "WHERE ((id = ?))\n"); # no empty expression
249+
is(scalar @{ $stmt->bind }, 1);
250+
226251
## regression bug. modified parameters
227252
my %terms = ( foo => [-and => 'foo', 'bar', 'baz']);
228253
$stmt = ns();

0 commit comments

Comments
 (0)