where can recieve array refrence, which contains where...
...cluase and paramter.
... | ... |
@@ -1,3 +1,5 @@ |
1 |
+0.1664 |
|
2 |
+ - where can recieve array refrence, which contains where cluase and paramter. |
|
1 | 3 |
0.1663 |
2 | 4 |
- added EXPERIMENTAL type() attribute to DBIx::Custom::Model |
3 | 5 |
- added EXPERIMENTAL bind_param_option can set bind_param option |
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
package DBIx::Custom; |
2 | 2 |
|
3 |
-our $VERSION = '0.1663'; |
|
3 |
+our $VERSION = '0.1664'; |
|
4 | 4 |
|
5 | 5 |
use 5.008001; |
6 | 6 |
use strict; |
... | ... |
@@ -260,20 +260,8 @@ sub delete { |
260 | 260 |
my $allow_delete_all = delete $args{allow_delete_all}; |
261 | 261 |
|
262 | 262 |
# Where |
263 |
- my $w; |
|
264 |
- if (ref $where eq 'HASH') { |
|
265 |
- my $clause = ['and']; |
|
266 |
- push @$clause, "{= $_}" for keys %$where; |
|
267 |
- $w = $self->where; |
|
268 |
- $w->clause($clause); |
|
269 |
- $w->param($where); |
|
270 |
- } |
|
271 |
- elsif (ref $where eq 'DBIx::Custom::Where') { |
|
272 |
- $w = $where; |
|
273 |
- $where = $w->param; |
|
274 |
- } |
|
275 |
- croak qq{"where" must be hash refernce or DBIx::Custom::Where object} |
|
276 |
- unless ref $w eq 'DBIx::Custom::Where'; |
|
263 |
+ my $w = $self->_where($where); |
|
264 |
+ $where = $w->param; |
|
277 | 265 |
|
278 | 266 |
# String where |
279 | 267 |
my $swhere = "$w"; |
... | ... |
@@ -896,19 +884,8 @@ sub select { |
896 | 884 |
croak "Not found table name" unless $tables->[-1]; |
897 | 885 |
|
898 | 886 |
# Where |
899 |
- my $w; |
|
900 |
- if (ref $where eq 'HASH') { |
|
901 |
- my $clause = ['and']; |
|
902 |
- push @$clause, "{= $_}" for keys %$where; |
|
903 |
- $w = $self->where(clause => $clause, param => $where); |
|
904 |
- } |
|
905 |
- elsif (ref $where eq 'DBIx::Custom::Where') { |
|
906 |
- $w = $where; |
|
907 |
- $where = $w->param; |
|
908 |
- } |
|
909 |
- |
|
910 |
- croak qq{"where" must be hash reference or DBIx::Custom::Where object} |
|
911 |
- unless ref $w eq 'DBIx::Custom::Where'; |
|
887 |
+ my $w = $self->_where($where); |
|
888 |
+ $where = $w->param; |
|
912 | 889 |
|
913 | 890 |
# String where |
914 | 891 |
my $swhere = "$w"; |
... | ... |
@@ -1041,21 +1018,8 @@ sub update { |
1041 | 1018 |
my $update_clause = '{update_param ' . join(' ', @clumns) . '}'; |
1042 | 1019 |
|
1043 | 1020 |
# Where |
1044 |
- my $w; |
|
1045 |
- if (ref $where eq 'HASH') { |
|
1046 |
- my $clause = ['and']; |
|
1047 |
- push @$clause, "{= $_}" for keys %$where; |
|
1048 |
- $w = $self->where; |
|
1049 |
- $w->clause($clause); |
|
1050 |
- $w->param($where); |
|
1051 |
- } |
|
1052 |
- elsif (ref $where eq 'DBIx::Custom::Where') { |
|
1053 |
- $w = $where; |
|
1054 |
- $where = $w->param; |
|
1055 |
- } |
|
1056 |
- |
|
1057 |
- croak qq{"where" must be hash refernce or DBIx::Custom::Where object} |
|
1058 |
- unless ref $w eq 'DBIx::Custom::Where'; |
|
1021 |
+ my $w = $self->_where($where); |
|
1022 |
+ $where = $w->param; |
|
1059 | 1023 |
|
1060 | 1024 |
# String where |
1061 | 1025 |
my $swhere = "$w"; |
... | ... |
@@ -1329,6 +1293,32 @@ sub _push_join { |
1329 | 1293 |
} |
1330 | 1294 |
} |
1331 | 1295 |
|
1296 |
+sub _where { |
|
1297 |
+ my ($self, $where) = @_; |
|
1298 |
+ |
|
1299 |
+ my $w; |
|
1300 |
+ if (ref $where eq 'HASH') { |
|
1301 |
+ my $clause = ['and']; |
|
1302 |
+ push @$clause, "{= $_}" for keys %$where; |
|
1303 |
+ $w = $self->where(clause => $clause, param => $where); |
|
1304 |
+ } |
|
1305 |
+ elsif (ref $where eq 'DBIx::Custom::Where') { |
|
1306 |
+ $w = $where; |
|
1307 |
+ } |
|
1308 |
+ elsif (ref $where eq 'ARRAY') { |
|
1309 |
+ $w = $self->where( |
|
1310 |
+ clause => $where->[0], |
|
1311 |
+ param => $where->[1] |
|
1312 |
+ ); |
|
1313 |
+ } |
|
1314 |
+ |
|
1315 |
+ croak qq{"where" must be hash reference or DBIx::Custom::Where object} . |
|
1316 |
+ qq{or array reference, which contains where clause and paramter} |
|
1317 |
+ unless ref $w eq 'DBIx::Custom::Where'; |
|
1318 |
+ |
|
1319 |
+ return $w; |
|
1320 |
+} |
|
1321 |
+ |
|
1332 | 1322 |
# DEPRECATED! |
1333 | 1323 |
__PACKAGE__->attr( |
1334 | 1324 |
dbi_options => sub { {} }, |
... | ... |
@@ -1814,7 +1804,8 @@ Table name. |
1814 | 1804 |
|
1815 | 1805 |
=item C<where> |
1816 | 1806 |
|
1817 |
-Where clause. This is hash reference or L<DBIx::Custom::Where> object. |
|
1807 |
+Where clause. This is hash reference or L<DBIx::Custom::Where> object |
|
1808 |
+or array refrence, which contains where clause and paramter. |
|
1818 | 1809 |
|
1819 | 1810 |
# Hash reference |
1820 | 1811 |
$dbi->delete(where => {title => 'Perl'}); |
... | ... |
@@ -1826,6 +1817,14 @@ Where clause. This is hash reference or L<DBIx::Custom::Where> object. |
1826 | 1817 |
); |
1827 | 1818 |
$dbi->delete(where => $where); |
1828 | 1819 |
|
1820 |
+ # Array refrendce (where clause and parameter) |
|
1821 |
+ $dbi->delete(where => |
|
1822 |
+ [ |
|
1823 |
+ ['and', '{= author}', '{like title}'], |
|
1824 |
+ {author => 'Ken', title => '%Perl%'} |
|
1825 |
+ ] |
|
1826 |
+ ); |
|
1827 |
+ |
|
1829 | 1828 |
=item C<append> |
1830 | 1829 |
|
1831 | 1830 |
Append statement to last of SQL. This is string. |
... | ... |
@@ -2326,7 +2325,8 @@ You can add before created statement |
2326 | 2325 |
|
2327 | 2326 |
=item C<where> |
2328 | 2327 |
|
2329 |
-Where clause. This is hash reference or L<DBIx::Custom::Where> object. |
|
2328 |
+Where clause. This is hash reference or L<DBIx::Custom::Where> object, |
|
2329 |
+or array refrence, which contains where clause and paramter. |
|
2330 | 2330 |
|
2331 | 2331 |
# Hash reference |
2332 | 2332 |
$dbi->select(where => {author => 'Ken', 'title' => 'Perl'}); |
... | ... |
@@ -2338,6 +2338,14 @@ Where clause. This is hash reference or L<DBIx::Custom::Where> object. |
2338 | 2338 |
); |
2339 | 2339 |
$dbi->select(where => $where); |
2340 | 2340 |
|
2341 |
+ # Array refrendce (where clause and parameter) |
|
2342 |
+ $dbi->select(where => |
|
2343 |
+ [ |
|
2344 |
+ ['and', '{= author}', '{like title}'], |
|
2345 |
+ {author => 'Ken', title => '%Perl%'} |
|
2346 |
+ ] |
|
2347 |
+ ); |
|
2348 |
+ |
|
2341 | 2349 |
=item C<join> EXPERIMENTAL |
2342 | 2350 |
|
2343 | 2351 |
Join clause used in need. This is array reference. |
... | ... |
@@ -2505,7 +2513,8 @@ Update data. This is hash reference. |
2505 | 2513 |
|
2506 | 2514 |
=item C<where> |
2507 | 2515 |
|
2508 |
-Where clause. This is hash reference or L<DBIx::Custom::Where> object. |
|
2516 |
+Where clause. This is hash reference or L<DBIx::Custom::Where> object |
|
2517 |
+or array refrence. |
|
2509 | 2518 |
|
2510 | 2519 |
# Hash reference |
2511 | 2520 |
$dbi->update(where => {author => 'Ken', 'title' => 'Perl'}); |
... | ... |
@@ -2516,6 +2525,14 @@ Where clause. This is hash reference or L<DBIx::Custom::Where> object. |
2516 | 2525 |
param => {author => 'Ken', title => '%Perl%'} |
2517 | 2526 |
); |
2518 | 2527 |
$dbi->update(where => $where); |
2528 |
+ |
|
2529 |
+ # Array refrendce (where clause and parameter) |
|
2530 |
+ $dbi->update(where => |
|
2531 |
+ [ |
|
2532 |
+ ['and', '{= author}', '{like title}'], |
|
2533 |
+ {author => 'Ken', title => '%Perl%'} |
|
2534 |
+ ] |
|
2535 |
+ ); |
|
2519 | 2536 |
|
2520 | 2537 |
=item C<append> |
2521 | 2538 |
|
... | ... |
@@ -156,7 +156,7 @@ C<delete_at()>,C<select_at()>. |
156 | 156 |
|
157 | 157 |
Database data type, this is used as type optioon of C<insert()>, C<insert_at()>, |
158 | 158 |
C<update()>, C<update_at()>, C<update_all>, C<delete()>, C<delete_all()>, |
159 |
-C<select(), C<select_at()> |
|
159 |
+C<select()>, C<select_at()> |
|
160 | 160 |
|
161 | 161 |
=head2 C<view> |
162 | 162 |
|
... | ... |
@@ -298,7 +298,21 @@ $where->clause(['and', '{= key1}', '{= key2}']); |
298 | 298 |
$where->param({key1 => 1, key2 => 2}); |
299 | 299 |
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where); |
300 | 300 |
$result = $dbi->select(table => 'table1'); |
301 |
-is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'delete() where'); |
|
301 |
+is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'update() where'); |
|
302 |
+ |
|
303 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
304 |
+$dbi->execute($CREATE_TABLE->{0}); |
|
305 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
306 |
+$dbi->update( |
|
307 |
+ table => 'table1', |
|
308 |
+ param => {key1 => 3}, |
|
309 |
+ where => [ |
|
310 |
+ ['and', '{= key1}', '{= key2}'], |
|
311 |
+ {key1 => 1, key2 => 2} |
|
312 |
+ ] |
|
313 |
+); |
|
314 |
+$result = $dbi->select(table => 'table1'); |
|
315 |
+is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'update() where'); |
|
302 | 316 |
|
303 | 317 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
304 | 318 |
$dbi->execute($CREATE_TABLE->{0}); |
... | ... |
@@ -308,7 +322,7 @@ $where->clause(['and', '{= key2}']); |
308 | 322 |
$where->param({key2 => 2}); |
309 | 323 |
$dbi->update(table => 'table1', param => {key1 => 3}, where => $where); |
310 | 324 |
$result = $dbi->select(table => 'table1'); |
311 |
-is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'delete() where'); |
|
325 |
+is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'update() where'); |
|
312 | 326 |
|
313 | 327 |
eval{$dbi->update(table => 'table1', param => {';' => 1})}; |
314 | 328 |
like($@, qr/safety/); |
... | ... |
@@ -372,6 +386,20 @@ $dbi->delete(table => 'table1', where => $where); |
372 | 386 |
$result = $dbi->select(table => 'table1'); |
373 | 387 |
is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 4}], 'delete() where'); |
374 | 388 |
|
389 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
390 |
+$dbi->execute($CREATE_TABLE->{0}); |
|
391 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
392 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
393 |
+$dbi->delete( |
|
394 |
+ table => 'table1', |
|
395 |
+ where => [ |
|
396 |
+ ['and', '{= key1}', '{= key2}'], |
|
397 |
+ {ke1 => 1, key2 => 2} |
|
398 |
+ ] |
|
399 |
+); |
|
400 |
+$result = $dbi->select(table => 'table1'); |
|
401 |
+is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 4}], 'delete() where'); |
|
402 |
+ |
|
375 | 403 |
test 'delete error'; |
376 | 404 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
377 | 405 |
$dbi->execute($CREATE_TABLE->{0}); |
... | ... |
@@ -931,6 +959,16 @@ $result = $dbi->select( |
931 | 959 |
$row = $result->fetch_hash_all; |
932 | 960 |
is_deeply($row, [{key1 => 1, key2 => 2}]); |
933 | 961 |
|
962 |
+$result = $dbi->select( |
|
963 |
+ table => 'table1', |
|
964 |
+ where => [ |
|
965 |
+ ['and', '{= key1}', '{= key2}'], |
|
966 |
+ {key1 => 1} |
|
967 |
+ ] |
|
968 |
+); |
|
969 |
+$row = $result->fetch_hash_all; |
|
970 |
+is_deeply($row, [{key1 => 1, key2 => 2}]); |
|
971 |
+ |
|
934 | 972 |
$where = $dbi->where |
935 | 973 |
->clause(['and', '{= key1}', '{= key2}']) |
936 | 974 |
->param({key1 => 1, key2 => 2}); |