Showing 3 changed files with 48 additions and 10 deletions
+2 -1
lib/DBIx/Custom.pm
... ...
@@ -724,7 +724,8 @@ sub new {
724 724
     return $self;
725 725
 }
726 726
 
727
-sub not_exists { bless {}, 'DBIx::Custom::NotExists' }
727
+my $not_exists = bless {}, 'DBIx::Custom::NotExists';
728
+sub not_exists { $not_exists }
728 729
 
729 730
 sub order {
730 731
     my $self = shift;
+16 -9
lib/DBIx/Custom/Where.pm
... ...
@@ -55,6 +55,7 @@ sub _map_param {
55 55
         
56 56
         $value_cb ||= sub { $_[0] };
57 57
         $condition ||= $self->if || 'exists';
58
+        $condition = $self->_if_to_sub($condition);
58 59
 
59 60
         # Map parameter
60 61
         my $value;
... ...
@@ -97,15 +98,7 @@ sub if {
97 98
     if (@_) {
98 99
         my $if = $_[0];
99 100
         
100
-        $if = $if eq 'exists' ? $if
101
-                : $if eq 'defined' ? sub { defined $_[0] }
102
-                : $if eq 'length'  ? sub { defined $_[0] && length $_[0] }
103
-                : ref $if eq 'CODE' ? $if
104
-                : undef;
105
-
106
-        croak "You can must specify right value to C<if> " . _subname
107
-          unless $if;
108
-
101
+        $if = $self->_if_to_sub($if);
109 102
         $self->{if} = $if;
110 103
         return $self;
111 104
     }
... ...
@@ -113,6 +106,20 @@ sub if {
113 106
     return $self->{if};
114 107
 }
115 108
 
109
+sub _if_to_sub {
110
+    my ($self, $if) = @_;
111
+    $if = $if eq 'exists' ? $if
112
+            : $if eq 'defined' ? sub { defined $_[0] }
113
+            : $if eq 'length'  ? sub { defined $_[0] && length $_[0] }
114
+            : ref $if eq 'CODE' ? $if
115
+            : undef;
116
+
117
+    croak "You can must specify right value to C<if> " . _subname
118
+      unless $if;
119
+    
120
+    return $if;
121
+}
122
+
116 123
 sub new {
117 124
     my $self = shift->SUPER::new(@_);
118 125
     
+30
t/sqlite.t
... ...
@@ -1337,6 +1337,36 @@ $where->map(
1337 1337
 );
1338 1338
 is_deeply($where->param, {'book.price' => '%a'});
1339 1339
 
1340
+$where = $dbi->where;
1341
+$where->param({id => [1, 2], author => 'Ken', price => 1900});
1342
+$where->map(
1343
+    id => 'book.id',
1344
+    author => ['book.author', sub { '%' . $_[0] . '%' }],
1345
+    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1346
+);
1347
+is_deeply($where->param, {'book.id' => [1, 2], 'book.author' => '%Ken%',
1348
+  'book.price' => 1900});
1349
+
1350
+$where = $dbi->where;
1351
+$where->if('length');
1352
+$where->param({id => ['', ''], author => 'Ken', price => 1900});
1353
+$where->map(
1354
+    id => 'book.id',
1355
+    author => ['book.author', sub { '%' . $_[0] . '%' }],
1356
+    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1357
+);
1358
+is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1359
+  'book.price' => 1900});
1360
+
1361
+$where = $dbi->where;
1362
+$where->param({id => ['', ''], author => 'Ken', price => 1900});
1363
+$where->map(
1364
+    id => ['book.id', {if => 'length'}],
1365
+    author => ['book.author', sub { '%' . $_[0] . '%' }, {if => 'defined'}],
1366
+    price => ['book.price', {if => sub { $_[0] eq 1900 }}]
1367
+);
1368
+is_deeply($where->param, {'book.id' => [$dbi->not_exists, $dbi->not_exists], 'book.author' => '%Ken%',
1369
+  'book.price' => 1900});
1340 1370
 
1341 1371
 test 'dbi_option default';
1342 1372
 $dbi = DBIx::Custom->new;