... | ... |
@@ -10,9 +10,16 @@ use overload '""' => sub { shift->to_string }, fallback => 1; |
10 | 10 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
11 | 11 |
|
12 | 12 |
has [qw/dbi param/], |
13 |
- map => sub { {} }, |
|
14 | 13 |
clause => sub { [] }; |
15 | 14 |
|
15 |
+sub map { |
|
16 |
+ my ($self, %map) = @_; |
|
17 |
+ |
|
18 |
+ my $param = $self->_map_param($self->param, %map); |
|
19 |
+ $self->param($param); |
|
20 |
+ return $self; |
|
21 |
+} |
|
22 |
+ |
|
16 | 23 |
sub _map_param { |
17 | 24 |
my $self = shift; |
18 | 25 |
my $param = shift; |
... | ... |
@@ -145,8 +152,6 @@ sub to_string { |
145 | 152 |
unless $if; |
146 | 153 |
$self->{_if} = $if; |
147 | 154 |
|
148 |
- $self->{_param} = $self->_map_param($self->param, %{$self->map}); |
|
149 |
- |
|
150 | 155 |
# Parse |
151 | 156 |
my $where = []; |
152 | 157 |
my $count = {}; |
... | ... |
@@ -226,7 +231,7 @@ sub _parse { |
226 | 231 |
my $count = ++$count->{$column}; |
227 | 232 |
|
228 | 233 |
# Push |
229 |
- my $param = $self->{_param}; |
|
234 |
+ my $param = $self->param; |
|
230 | 235 |
if (ref $param eq 'HASH') { |
231 | 236 |
if (exists $param->{$column}) { |
232 | 237 |
my $if = $self->{_if}; |
... | ... |
@@ -280,17 +285,45 @@ If all parameter names is exists. |
280 | 285 |
|
281 | 286 |
"where ( title = :title and ( date < :date or date > :date ) )" |
282 | 287 |
|
288 |
+=head2 C<param> |
|
289 |
+ |
|
290 |
+ my $param = $where->param; |
|
291 |
+ $where = $where->param({ |
|
292 |
+ title => 'Perl', |
|
293 |
+ date => ['2010-11-11', '2011-03-05'], |
|
294 |
+ }); |
|
295 |
+ |
|
296 |
+=head2 C<dbi> |
|
297 |
+ |
|
298 |
+ my $dbi = $where->dbi; |
|
299 |
+ $where = $where->dbi($dbi); |
|
300 |
+ |
|
301 |
+L<DBIx::Custom> object. |
|
302 |
+ |
|
303 |
+=head1 METHODS |
|
304 |
+ |
|
305 |
+L<DBIx::Custom::Where> inherits all methods from L<Object::Simple> |
|
306 |
+and implements the following new ones. |
|
307 |
+ |
|
308 |
+=head2 C<if EXPERIMENTAL> |
|
309 |
+ |
|
310 |
+ my $if = $where->if($condition); |
|
311 |
+ $where->if($condition); |
|
312 |
+ |
|
313 |
+C<if> is default of C<map> method C<if> option. |
|
314 |
+ |
|
283 | 315 |
=head2 C<map EXPERIMENTAL> |
284 | 316 |
|
285 |
-Mapping parameter key and value when C<to_stirng> method is executed. |
|
317 |
+Mapping parameter key and value. C<param> is converted, |
|
318 |
+so this method must be called after C<param> is set. |
|
286 | 319 |
|
287 |
- $where->map({ |
|
320 |
+ $where->map( |
|
288 | 321 |
'id' => 'book.id', |
289 | 322 |
'author' => ['book.author' => sub { '%' . $_[0] . '%' }], |
290 | 323 |
'price' => [ |
291 | 324 |
'book.price', {if => sub { length $_[0] } |
292 | 325 |
] |
293 |
- }); |
|
326 |
+ ); |
|
294 | 327 |
|
295 | 328 |
The following option is available. |
296 | 329 |
|
... | ... |
@@ -318,33 +351,6 @@ You can also subroutine like C<sub { defined $_[0] }> for mappging. |
318 | 351 |
|
319 | 352 |
=back |
320 | 353 |
|
321 |
-=head2 C<param> |
|
322 |
- |
|
323 |
- my $param = $where->param; |
|
324 |
- $where = $where->param({ |
|
325 |
- title => 'Perl', |
|
326 |
- date => ['2010-11-11', '2011-03-05'], |
|
327 |
- }); |
|
328 |
- |
|
329 |
-=head2 C<dbi> |
|
330 |
- |
|
331 |
- my $dbi = $where->dbi; |
|
332 |
- $where = $where->dbi($dbi); |
|
333 |
- |
|
334 |
-L<DBIx::Custom> object. |
|
335 |
- |
|
336 |
-=head1 METHODS |
|
337 |
- |
|
338 |
-L<DBIx::Custom::Where> inherits all methods from L<Object::Simple> |
|
339 |
-and implements the following new ones. |
|
340 |
- |
|
341 |
-=head2 C<if EXPERIMENTAL> |
|
342 |
- |
|
343 |
- my $if = $where->if($condition); |
|
344 |
- $where->if($condition); |
|
345 |
- |
|
346 |
-C<if> is default of C<map> method C<if> option. |
|
347 |
- |
|
348 | 354 |
=head2 C<to_string> |
349 | 355 |
|
350 | 356 |
$where->to_string; |
... | ... |
@@ -934,7 +934,7 @@ is(ref $query, 'DBIx::Custom::Query'); |
934 | 934 |
$query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query => 1); |
935 | 935 |
is(ref $query, 'DBIx::Custom::Query'); |
936 | 936 |
|
937 |
-test 'DBIx::Custom::Where'; |
|
937 |
+test 'where'; |
|
938 | 938 |
$dbi = DBIx::Custom->connect(%memory); |
939 | 939 |
$dbi->execute($create_table_default); |
940 | 940 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
... | ... |
@@ -1226,6 +1226,7 @@ $where = $dbi->where; |
1226 | 1226 |
$where->clause(['and', ':key1{=}']); |
1227 | 1227 |
$where->param({key1 => undef}); |
1228 | 1228 |
$where->if('defined'); |
1229 |
+$where->map; |
|
1229 | 1230 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
1230 | 1231 |
$row = $result->all; |
1231 | 1232 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
... | ... |
@@ -1244,6 +1245,7 @@ $where = $dbi->where; |
1244 | 1245 |
$where->clause(['and', ':key1{=}']); |
1245 | 1246 |
$where->param({key1 => [undef, undef]}); |
1246 | 1247 |
$where->if('defined'); |
1248 |
+$where->map; |
|
1247 | 1249 |
$result = $dbi->execute("select * from table1 $where", {key1 => [1, 0]}); |
1248 | 1250 |
$row = $result->all; |
1249 | 1251 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
... | ... |
@@ -1255,6 +1257,7 @@ $where = $dbi->where; |
1255 | 1257 |
$where->clause(['and', ':key1{=}']); |
1256 | 1258 |
$where->param({key1 => 0}); |
1257 | 1259 |
$where->if('length'); |
1260 |
+$where->map; |
|
1258 | 1261 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
1259 | 1262 |
$row = $result->all; |
1260 | 1263 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
... | ... |
@@ -1263,6 +1266,7 @@ $where = $dbi->where; |
1263 | 1266 |
$where->clause(['and', ':key1{=}']); |
1264 | 1267 |
$where->param({key1 => ''}); |
1265 | 1268 |
$where->if('length'); |
1269 |
+$where->map; |
|
1266 | 1270 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
1267 | 1271 |
$row = $result->all; |
1268 | 1272 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
... | ... |
@@ -1271,6 +1275,7 @@ $where = $dbi->where; |
1271 | 1275 |
$where->clause(['and', ':key1{=}']); |
1272 | 1276 |
$where->param({key1 => 5}); |
1273 | 1277 |
$where->if(sub { ($_[0] || '') eq 5 }); |
1278 |
+$where->map; |
|
1274 | 1279 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
1275 | 1280 |
$row = $result->all; |
1276 | 1281 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
... | ... |
@@ -1279,6 +1284,7 @@ $where = $dbi->where; |
1279 | 1284 |
$where->clause(['and', ':key1{=}']); |
1280 | 1285 |
$where->param({key1 => 7}); |
1281 | 1286 |
$where->if(sub { ($_[0] || '') eq 5 }); |
1287 |
+$where->map; |
|
1282 | 1288 |
$result = $dbi->execute("select * from table1 $where", {key1 => 1}); |
1283 | 1289 |
$row = $result->all; |
1284 | 1290 |
is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |