... | ... |
@@ -2,6 +2,8 @@ |
2 | 2 |
added experimental DBIx::Custom::Result end_filter method |
3 | 3 |
experimental extended select method's where option |
4 | 4 |
fix select method empty where failed bug |
5 |
+ added experimental suger method query option |
|
6 |
+ added experimental or method |
|
5 | 7 |
0.1630 |
6 | 8 |
fix test bug |
7 | 9 |
0.1629 |
... | ... |
@@ -13,6 +13,7 @@ use DBI; |
13 | 13 |
use DBIx::Custom::Result; |
14 | 14 |
use DBIx::Custom::Query; |
15 | 15 |
use DBIx::Custom::QueryBuilder; |
16 |
+use DBIx::Custom::Or; |
|
16 | 17 |
use Encode qw/encode_utf8 decode_utf8/; |
17 | 18 |
|
18 | 19 |
__PACKAGE__->attr( |
... | ... |
@@ -461,6 +462,12 @@ sub new { |
461 | 462 |
return $self; |
462 | 463 |
} |
463 | 464 |
|
465 |
+sub or { |
|
466 |
+ my $self = shift; |
|
467 |
+ my $values = ref $_[0] eq 'ARRAY' ? $_[0] : [@_]; |
|
468 |
+ return DBIx::Custom::Or->new(values => $values); |
|
469 |
+} |
|
470 |
+ |
|
464 | 471 |
sub register_filter { |
465 | 472 |
my $invocant = shift; |
466 | 473 |
|
... | ... |
@@ -534,7 +541,35 @@ sub select { |
534 | 541 |
$param = $where->[1]; |
535 | 542 |
|
536 | 543 |
if (ref $w eq 'HASH') { |
544 |
+ $wexists = keys %$param; |
|
545 |
+ if ($wexists) { |
|
546 |
+ $source .= 'where ('; |
|
537 | 547 |
|
548 |
+ foreach my $column (keys %$param) { |
|
549 |
+ croak qq{"$column" don't correspond to where clause} |
|
550 |
+ unless exists $w->{$column}; |
|
551 |
+ |
|
552 |
+ my $value = $param->{$column}; |
|
553 |
+ if (ref $value eq 'DBIx::Custom::Or') { |
|
554 |
+ my $values = $value->values; |
|
555 |
+ |
|
556 |
+ $source .= '( '; |
|
557 |
+ foreach my $value (@$values) { |
|
558 |
+ $source .= $w->{$column}; |
|
559 |
+ $source .= ' or '; |
|
560 |
+ } |
|
561 |
+ $source =~ s/ or $//; |
|
562 |
+ $source .= ' )'; |
|
563 |
+ $source .= ' and '; |
|
564 |
+ $param->{$column} = $values; |
|
565 |
+ } |
|
566 |
+ elsif ($w->{$column}) { |
|
567 |
+ $source .= $w->{$column} . ' and '; |
|
568 |
+ } |
|
569 |
+ } |
|
570 |
+ $source =~ s/ and $//; |
|
571 |
+ $source .= ') '; |
|
572 |
+ } |
|
538 | 573 |
} |
539 | 574 |
else { |
540 | 575 |
$wexists = $w =~ /\S/; |
... | ... |
@@ -1199,6 +1234,13 @@ Create a new L<DBIx::Custom> object. |
1199 | 1234 |
Iterate all columns of all tables. Argument is callback. |
1200 | 1235 |
You can do anything by callback. |
1201 | 1236 |
|
1237 |
+=head2 C<(experimental) or> |
|
1238 |
+ |
|
1239 |
+ $or = $dbi->or(1, 5); |
|
1240 |
+ |
|
1241 |
+Create L<DBIx::Custom::Or> object. This is used with select method's |
|
1242 |
+where option. |
|
1243 |
+ |
|
1202 | 1244 |
=head2 C<register_filter> |
1203 | 1245 |
|
1204 | 1246 |
$dbi->register_filter(%filters); |
... | ... |
@@ -0,0 +1,26 @@ |
1 |
+package DBIx::Custom::Or; |
|
2 |
+ |
|
3 |
+use strict; |
|
4 |
+use warnings; |
|
5 |
+ |
|
6 |
+use base 'Object::Simple'; |
|
7 |
+ |
|
8 |
+__PACKAGE__->attr(values => sub { [] }); |
|
9 |
+ |
|
10 |
+1; |
|
11 |
+ |
|
12 |
+=head1 NAME |
|
13 |
+ |
|
14 |
+DBIx::Custom::Or - or condition |
|
15 |
+ |
|
16 |
+=head1 SYNOPSYS |
|
17 |
+ |
|
18 |
+ my $or = DBIx::Custom::Or->new; |
|
19 |
+ |
|
20 |
+=head1 ATTRIBUTES |
|
21 |
+ |
|
22 |
+=head2 C<values> |
|
23 |
+ |
|
24 |
+ my $values = $or->values; |
|
25 |
+ $or = $or->values([1, 2]); |
|
26 |
+ |
... | ... |
@@ -814,3 +814,71 @@ $query = $dbi->select(table => 'table1', where => {key1 => 1, key2 => 2}, query |
814 | 814 |
is(ref $query, 'DBIx::Custom::Query'); |
815 | 815 |
|
816 | 816 |
1; |
817 |
+ |
|
818 |
+test 'select where option'; |
|
819 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
820 |
+$dbi->execute($CREATE_TABLE->{0}); |
|
821 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
822 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
823 |
+$result = $dbi->select( |
|
824 |
+ table => 'table1', |
|
825 |
+ where => [ |
|
826 |
+ { key1 => '{= key1}', key2 => '{= key2}' }, |
|
827 |
+ {key1 => 1} |
|
828 |
+ ] |
|
829 |
+); |
|
830 |
+$row = $result->fetch_hash_all; |
|
831 |
+is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
832 |
+ |
|
833 |
+$result = $dbi->select( |
|
834 |
+ table => 'table1', |
|
835 |
+ where => [ |
|
836 |
+ { key1 => '{= key1}', key2 => '{= key2}' }, |
|
837 |
+ {key1 => 1, key2 => 2} |
|
838 |
+ ] |
|
839 |
+); |
|
840 |
+$row = $result->fetch_hash_all; |
|
841 |
+is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
842 |
+ |
|
843 |
+$result = $dbi->select( |
|
844 |
+ table => 'table1', |
|
845 |
+ where => [ |
|
846 |
+ { key1 => '{= key1}', key2 => '{= key2}' }, |
|
847 |
+ {} |
|
848 |
+ ] |
|
849 |
+); |
|
850 |
+$row = $result->fetch_hash_all; |
|
851 |
+is_deeply($row, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]); |
|
852 |
+ |
|
853 |
+$result = $dbi->select( |
|
854 |
+ table => 'table1', |
|
855 |
+ where => [ |
|
856 |
+ { key1 => '{= key1}', key2 => '{= key2}' }, |
|
857 |
+ { key1 => 1, key2 => $dbi->or(1, 2)} |
|
858 |
+ ] |
|
859 |
+); |
|
860 |
+$row = $result->fetch_hash_all; |
|
861 |
+is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
862 |
+ |
|
863 |
+$result = $dbi->select( |
|
864 |
+ table => 'table1', |
|
865 |
+ where => [ |
|
866 |
+ { key1 => '{= key1}', key2 => '{= key2}' }, |
|
867 |
+ { key1 => 1, key2 => $dbi->or(2)} |
|
868 |
+ ] |
|
869 |
+); |
|
870 |
+$row = $result->fetch_hash_all; |
|
871 |
+is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
872 |
+ |
|
873 |
+eval { |
|
874 |
+$result = $dbi->select( |
|
875 |
+ table => 'table1', |
|
876 |
+ where => [ |
|
877 |
+ { key2 => '{= key2}' }, |
|
878 |
+ { key1 => 1} |
|
879 |
+ ] |
|
880 |
+); |
|
881 |
+}; |
|
882 |
+ok($@); |
|
883 |
+ |
|
884 |
+ |