Showing 2 changed files with 67 additions and 24 deletions
+30 -23
lib/DBIx/Custom.pm
... ...
@@ -302,8 +302,15 @@ sub delete {
302 302
     my $allow_delete_all = delete $args{allow_delete_all};
303 303
     my $query_return     = delete $args{query};
304 304
     my $where_param      = delete $args{where_param} || {};
305
-
305
+    my $id = delete $args{id};
306
+    my $primary_key = delete $args{primary_key};
307
+    croak "update method primary_key option " .
308
+          "must be specified when id is specified " . _subname
309
+      if defined $id && !defined $primary_key;
310
+    $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
311
+    
306 312
     # Where
313
+    $where = $self->_create_param_from_id($id, $primary_key) if $id;
307 314
     my $where_clause = '';
308 315
     if (ref $where) {
309 316
         $where = $self->_where_to_obj($where);
... ...
@@ -340,28 +347,6 @@ sub delete {
340 347
 
341 348
 sub delete_all { shift->delete(allow_delete_all => 1, @_) }
342 349
 
343
-our %DELETE_AT_ARGS = (%DELETE_ARGS, where => 1, primary_key => 1);
344
-
345
-sub delete_at {
346
-    my ($self, %args) = @_;
347
-    
348
-    # Arguments
349
-    my $primary_keys = delete $args{primary_key};
350
-    $primary_keys = [$primary_keys] unless ref $primary_keys;
351
-    my $where = delete $args{where};
352
-    
353
-    # Check arguments
354
-    foreach my $name (keys %args) {
355
-        croak qq{"$name" is wrong option } . _subname
356
-          unless $DELETE_AT_ARGS{$name};
357
-    }
358
-    
359
-    # Create where parameter
360
-    my $where_param = $self->_create_param_from_id($where, $primary_keys);
361
-    
362
-    return $self->delete(where => $where_param, %args);
363
-}
364
-
365 350
 sub DESTROY { }
366 351
 
367 352
 sub create_model {
... ...
@@ -1331,6 +1316,28 @@ sub _where_to_obj {
1331 1316
     return $obj;
1332 1317
 }
1333 1318
 
1319
+# DEPRECATED!
1320
+our %DELETE_AT_ARGS = (%DELETE_ARGS, where => 1, primary_key => 1);
1321
+sub delete_at {
1322
+    my ($self, %args) = @_;
1323
+    
1324
+    # Arguments
1325
+    my $primary_keys = delete $args{primary_key};
1326
+    $primary_keys = [$primary_keys] unless ref $primary_keys;
1327
+    my $where = delete $args{where};
1328
+    
1329
+    # Check arguments
1330
+    foreach my $name (keys %args) {
1331
+        croak qq{"$name" is wrong option } . _subname
1332
+          unless $DELETE_AT_ARGS{$name};
1333
+    }
1334
+    
1335
+    # Create where parameter
1336
+    my $where_param = $self->_create_param_from_id($where, $primary_keys);
1337
+    
1338
+    return $self->delete(where => $where_param, %args);
1339
+}
1340
+
1334 1341
 # DEPRECATED!
1335 1342
 our %UPDATE_AT_ARGS = (%UPDATE_ARGS, where => 1, primary_key => 1);
1336 1343
 sub update_at {
+37 -1
t/dbix-custom-core-sqlite.t
... ...
@@ -2416,7 +2416,7 @@ is($dbi->select(table => 'table1')->one->{key2}, 2);
2416 2416
 is($dbi->select(table => 'table1')->one->{key3}, 4);
2417 2417
 
2418 2418
 
2419
-test 'model update_at';
2419
+test 'model update and id option';
2420 2420
 $dbi = MyDBI6->connect($NEW_ARGS->{0});
2421 2421
 $dbi->execute($CREATE_TABLE->{1});
2422 2422
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
... ...
@@ -2430,4 +2430,40 @@ is($row->{key1}, 1);
2430 2430
 is($row->{key2}, 2);
2431 2431
 is($row->{key3}, 4);
2432 2432
 
2433
+
2434
+test 'delete and id option';
2435
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2436
+$dbi->execute($CREATE_TABLE->{1});
2437
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2438
+$dbi->delete(
2439
+    table => 'table1',
2440
+    primary_key => ['key1', 'key2'],
2441
+    id => [1, 2],
2442
+);
2443
+is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
2444
+
2445
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2446
+$dbi->delete(
2447
+    table => 'table1',
2448
+    primary_key => 'key1',
2449
+    id => 1,
2450
+);
2451
+is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
2452
+
2453
+
2454
+test 'model delete and id option';
2455
+$dbi = MyDBI6->connect($NEW_ARGS->{0});
2456
+$dbi->execute($CREATE_TABLE->{1});
2457
+$dbi->execute("create table table2 (key1, key2, key3)");
2458
+$dbi->execute("create table table3 (key1, key2, key3)");
2459
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2460
+$dbi->model('table1')->delete(id => [1, 2]);
2461
+is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
2462
+$dbi->insert(table => 'table2', param => {key1 => 1, key2 => 2, key3 => 3});
2463
+$dbi->model('table1_1')->delete(id => [1, 2]);
2464
+is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
2465
+$dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2466
+$dbi->model('table1_3')->delete(id => [1, 2]);
2467
+is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
2468
+
2433 2469
 =cut