Showing 2 changed files with 53 additions and 3 deletions
+5 -3
lib/DBIx/Custom/Where.pm
... ...
@@ -15,8 +15,10 @@ has [qw/dbi param/],
15 15
 sub map {
16 16
     my ($self, %map) = @_;
17 17
     
18
-    my $param = $self->_map_param($self->param, %map);
19
-    $self->param($param);
18
+    if ($self->if ne 'exists' || keys %map) {
19
+        my $param = $self->_map_param($self->param, %map);
20
+        $self->param($param);
21
+    }
20 22
     return $self;
21 23
 }
22 24
 
... ...
@@ -97,7 +99,7 @@ sub if {
97 99
         
98 100
         $if = $if eq 'exists' ? $if
99 101
                 : $if eq 'defined' ? sub { defined $_[0] }
100
-                : $if eq 'length'  ? sub { length $_[0] }
102
+                : $if eq 'length'  ? sub { defined $_[0] && length $_[0] }
101 103
                 : ref $if eq 'CODE' ? $if
102 104
                 : undef;
103 105
 
+48
t/sqlite.t
... ...
@@ -1289,6 +1289,54 @@ $result = $dbi->execute("select * from table1 $where", {key1 => 1});
1289 1289
 $row = $result->all;
1290 1290
 is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
1291 1291
 
1292
+$where = $dbi->where;
1293
+$where->param({id => 1, author => 'Ken', price => 1900});
1294
+$where->map(id => 'book.id',
1295
+    author => ['book.author', sub { '%' . $_[0] . '%' }],
1296
+    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1297
+);
1298
+is_deeply($where->param, {'book.id' => 1, 'book.author' => '%Ken%',
1299
+  'book.price' => 1900});
1300
+
1301
+$where = $dbi->where;
1302
+$where->param({id => 0, author => 0, price => 0});
1303
+$where->map(
1304
+    id => 'book.id',
1305
+    author => ['book.author', sub { '%' . $_[0] . '%' }],
1306
+    price => ['book.price', sub { '%' . $_[0] . '%' },
1307
+      {if => sub { $_[0] eq 0 }}]
1308
+);
1309
+is_deeply($where->param, {'book.id' => 0, 'book.author' => '%0%', 'book.price' => '%0%'});
1310
+
1311
+$where = $dbi->where;
1312
+$where->param({id => '', author => '', price => ''});
1313
+$where->if('length');
1314
+$where->map(
1315
+    id => 'book.id',
1316
+    author => ['book.author', sub { '%' . $_[0] . '%' }],
1317
+    price => ['book.price', sub { '%' . $_[0] . '%' },
1318
+      {if => sub { $_[0] eq 1 }}]
1319
+);
1320
+is_deeply($where->param, {});
1321
+
1322
+$where = $dbi->where;
1323
+$where->param({id => undef, author => undef, price => undef});
1324
+$where->if('length');
1325
+$where->map(
1326
+    id => 'book.id',
1327
+    price => ['book.price', {if => 'exists'}]
1328
+);
1329
+is_deeply($where->param, {'book.price' => undef});
1330
+
1331
+$where = $dbi->where;
1332
+$where->param({price => 'a'});
1333
+$where->if('length');
1334
+$where->map(
1335
+    id => ['book.id', {if => 'exists'}],
1336
+    price => ['book.price', sub { '%' . $_[0] }, {if => 'exists'}]
1337
+);
1338
+is_deeply($where->param, {'book.price' => '%a'});
1339
+
1292 1340
 
1293 1341
 test 'dbi_option default';
1294 1342
 $dbi = DBIx::Custom->new;