improved delete() and update() where option. you can...
...use DBIx::Custom::Where object
| ... | ... |
@@ -1,4 +1,5 @@ |
| 1 | 1 |
0.1639 |
| 2 |
+ improved delete() and update() where option. you can use DBIx::Custom::Where object |
|
| 2 | 3 |
added experimental not_exists() |
| 3 | 4 |
0.1638 |
| 4 | 5 |
table object call dbi object method if not found method. |
| ... | ... |
@@ -259,12 +259,15 @@ sub delete {
|
| 259 | 259 |
push @$clause, "{= $_}" for keys %$where;
|
| 260 | 260 |
$w = $self->where; |
| 261 | 261 |
$w->clause($clause); |
| 262 |
+ $w->param($where); |
|
| 262 | 263 |
} |
| 263 | 264 |
else { $w = $where }
|
| 264 | 265 |
|
| 265 | 266 |
croak qq{"where" must be hash refernce or DBIx::Custom::Where object}
|
| 266 | 267 |
unless ref $w eq 'DBIx::Custom::Where'; |
| 267 | 268 |
|
| 269 |
+ $where = $w->param; |
|
| 270 |
+ |
|
| 268 | 271 |
croak qq{"where" must be specified}
|
| 269 | 272 |
if "$w" eq '' && !$allow_delete_all; |
| 270 | 273 |
|
| ... | ... |
@@ -640,33 +643,34 @@ sub update {
|
| 640 | 643 |
# Update keys |
| 641 | 644 |
my @update_keys = keys %$param; |
| 642 | 645 |
|
| 643 |
- # Where keys |
|
| 644 |
- my @where_keys = keys %$where; |
|
| 645 |
- |
|
| 646 |
- # Not exists where keys |
|
| 647 |
- croak qq{"where" argument must be specified and } .
|
|
| 648 |
- qq{contains the pairs of column name and value}
|
|
| 649 |
- if !@where_keys && !$allow_update_all; |
|
| 650 |
- |
|
| 651 | 646 |
# Update clause |
| 652 | 647 |
my $update_clause = '{update_param ' . join(' ', @update_keys) . '}';
|
| 648 |
+ |
|
| 649 |
+ # Where |
|
| 650 |
+ my $w; |
|
| 651 |
+ if (ref $where eq 'HASH') {
|
|
| 652 |
+ my $clause = ['and']; |
|
| 653 |
+ push @$clause, "{= $_}" for keys %$where;
|
|
| 654 |
+ $w = $self->where; |
|
| 655 |
+ $w->clause($clause); |
|
| 656 |
+ $w->param($where); |
|
| 657 |
+ } |
|
| 658 |
+ else { $w = $where }
|
|
| 653 | 659 |
|
| 654 |
- # Where clause |
|
| 655 |
- my $where_clause = ''; |
|
| 656 |
- my $new_where = {};
|
|
| 660 |
+ croak qq{"where" must be hash refernce or DBIx::Custom::Where object}
|
|
| 661 |
+ unless ref $w eq 'DBIx::Custom::Where'; |
|
| 657 | 662 |
|
| 658 |
- if (@where_keys) {
|
|
| 659 |
- $where_clause = 'where '; |
|
| 660 |
- $where_clause .= "{= $_} and " for @where_keys;
|
|
| 661 |
- $where_clause =~ s/ and $//; |
|
| 662 |
- } |
|
| 663 |
+ $where = $w->param; |
|
| 664 |
+ |
|
| 665 |
+ croak qq{"where" must be specified}
|
|
| 666 |
+ if "$w" eq '' && !$allow_update_all; |
|
| 663 | 667 |
|
| 664 | 668 |
# Source of SQL |
| 665 |
- my $source = "update $table $update_clause $where_clause"; |
|
| 669 |
+ my $source = "update $table $update_clause $w"; |
|
| 666 | 670 |
$source .= " $append" if $append; |
| 667 | 671 |
|
| 668 | 672 |
# Rearrange parameters |
| 669 |
- foreach my $wkey (@where_keys) {
|
|
| 673 |
+ foreach my $wkey (keys %$where) {
|
|
| 670 | 674 |
|
| 671 | 675 |
if (exists $param->{$wkey}) {
|
| 672 | 676 |
$param->{$wkey} = [$param->{$wkey}]
|
| ... | ... |
@@ -280,6 +280,25 @@ like($@, qr/noexist/, "invalid argument"); |
| 280 | 280 |
eval{$dbi->update(table => 'table1')};
|
| 281 | 281 |
like($@, qr/where/, "not contain where"); |
| 282 | 282 |
|
| 283 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
|
|
| 284 |
+$dbi->execute($CREATE_TABLE->{0});
|
|
| 285 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 286 |
+$where = $dbi->where; |
|
| 287 |
+$where->clause(['and', '{= key1}', '{= key2}']);
|
|
| 288 |
+$where->param({key1 => 1, key2 => 2});
|
|
| 289 |
+$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
|
|
| 290 |
+$result = $dbi->select(table => 'table1'); |
|
| 291 |
+is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'delete() where');
|
|
| 292 |
+ |
|
| 293 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
|
|
| 294 |
+$dbi->execute($CREATE_TABLE->{0});
|
|
| 295 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 296 |
+$where = $dbi->where; |
|
| 297 |
+$where->clause(['and', '{= key2}']);
|
|
| 298 |
+$where->param({key2 => 2});
|
|
| 299 |
+$dbi->update(table => 'table1', param => {key1 => 3}, where => $where);
|
|
| 300 |
+$result = $dbi->select(table => 'table1'); |
|
| 301 |
+is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 2}], 'delete() where');
|
|
| 283 | 302 |
|
| 284 | 303 |
test 'update_all'; |
| 285 | 304 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
|
| ... | ... |
@@ -326,6 +345,16 @@ is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
|
| 326 | 345 |
eval{$dbi->delete(table => 'table1', noexist => 1)};
|
| 327 | 346 |
like($@, qr/noexist/, "invalid argument"); |
| 328 | 347 |
|
| 348 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
|
|
| 349 |
+$dbi->execute($CREATE_TABLE->{0});
|
|
| 350 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 351 |
+$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 352 |
+$where = $dbi->where; |
|
| 353 |
+$where->clause(['and', '{= key1}', '{= key2}']);
|
|
| 354 |
+$where->param({ke1 => 1, key2 => 2});
|
|
| 355 |
+$dbi->delete(table => 'table1', where => $where); |
|
| 356 |
+$result = $dbi->select(table => 'table1'); |
|
| 357 |
+is_deeply($result->fetch_hash_all, [{key1 => 3, key2 => 4}], 'delete() where');
|
|
| 329 | 358 |
|
| 330 | 359 |
test 'delete error'; |
| 331 | 360 |
$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
|