Showing 3 changed files with 86 additions and 34 deletions
+3 -2
Changes
... ...
@@ -1,6 +1,7 @@
1 1
 0.1685
2
-    - insert, insert_at, update, update_at can receive odd number arguments,
3
-      first one is parameter.
2
+    - insert_at, update_at, delete_at, select_at is DEPRECATED!
3
+      use insert, update, delete, select method and id option.
4
+    - insert, insert_at, update, update_at can receive odd number arguments,      first one is parameter.
4 5
 0.1684
5 6
     - added DBIx::Custom::Result all method, this is alias for fetch_hash_all
6 7
     - added DBIx::Custom::Result one method, this is alias for fetch_hash_first
+45 -32
lib/DBIx/Custom.pm
... ...
@@ -22,7 +22,7 @@ use Encode qw/encode encode_utf8 decode_utf8/;
22 22
 use constant DEBUG => $ENV{DBIX_CUSTOM_DEBUG} || 0;
23 23
 use constant DEBUG_ENCODING => $ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8';
24 24
 
25
-our @COMMON_ARGS = qw/table query filter type/;
25
+our @COMMON_ARGS = qw/table query filter type id primary_key/;
26 26
 
27 27
 __PACKAGE__->attr(
28 28
     [qw/connector dsn password user/],
... ...
@@ -571,7 +571,7 @@ sub execute {
571 571
     else { return $affected }
572 572
 }
573 573
 
574
-our %INSERT_ARGS = map { $_ => 1 } @COMMON_ARGS, qw/param append/;
574
+our %INSERT_ARGS = map { $_ => 1 } @COMMON_ARGS, qw/param/;
575 575
 
576 576
 sub insert {
577 577
     my $self = shift;
... ...
@@ -587,6 +587,11 @@ sub insert {
587 587
     $param  ||= $p;
588 588
     my $append = delete $args{append} || '';
589 589
     my $query_return  = delete $args{query};
590
+    my $id = delete $args{id};
591
+    my $primary_key = delete $args{primary_key};
592
+    croak "primary_key must be specified when id is specified"
593
+        if defined $id && !defined $primary_key;
594
+    $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
590 595
 
591 596
     # Check arguments
592 597
     foreach my $name (keys %args) {
... ...
@@ -594,6 +599,12 @@ sub insert {
594 599
           unless $INSERT_ARGS{$name};
595 600
     }
596 601
 
602
+    # Merge parameter
603
+    if ($id) {
604
+        my $id_param = $self->_create_where_param($id, $primary_key);
605
+        $param = $self->merge_param($id_param, $param);
606
+    }
607
+
597 608
     # Reserved word quote
598 609
     my $q = $self->reserved_word_quote;
599 610
     
... ...
@@ -616,34 +627,6 @@ sub insert {
616 627
     );
617 628
 }
618 629
 
619
-our %INSERT_AT_ARGS = (%INSERT_ARGS, where => 1, primary_key => 1);
620
-
621
-sub insert_at {
622
-    my $self = shift;
623
-
624
-    # Arguments
625
-    my $param;
626
-    $param = shift if @_ % 2;
627
-    my %args = @_;
628
-    my $primary_keys = delete $args{primary_key};
629
-    $primary_keys = [$primary_keys] unless ref $primary_keys;
630
-    my $where = delete $args{where};
631
-    my $p = delete $args{param} || {};
632
-    $param  ||= $p;
633
-    
634
-    # Check arguments
635
-    foreach my $name (keys %args) {
636
-        croak qq{"$name" is wrong option } . _subname
637
-          unless $INSERT_AT_ARGS{$name};
638
-    }
639
-    
640
-    # Create where parameter
641
-    my $where_param = $self->_create_where_param($where, $primary_keys);
642
-    $param = $self->merge_param($where_param, $param);
643
-    
644
-    return $self->insert(param => $param, %args);
645
-}
646
-
647 630
 sub insert_param {
648 631
     my ($self, $param) = @_;
649 632
     
... ...
@@ -835,7 +818,7 @@ sub register_filter {
835 818
 
836 819
 our %SELECT_ARGS
837 820
   = map { $_ => 1 } @COMMON_ARGS,
838
-                    qw/column where append relation join param where_param wrap/;
821
+                    qw/column where relation join param where_param wrap/;
839 822
 
840 823
 sub select {
841 824
     my ($self, %args) = @_;
... ...
@@ -1006,7 +989,7 @@ sub setup_model {
1006 989
 }
1007 990
 
1008 991
 our %UPDATE_ARGS
1009
-  = map { $_ => 1 } @COMMON_ARGS, qw/param where append allow_update_all where_param/;
992
+  = map { $_ => 1 } @COMMON_ARGS, qw/param where allow_update_all where_param/;
1010 993
 
1011 994
 sub update {
1012 995
     my $self = shift;
... ...
@@ -1369,6 +1352,36 @@ sub _where_to_obj {
1369 1352
     return $obj;
1370 1353
 }
1371 1354
 
1355
+# DEPRECATED!
1356
+our %INSERT_AT_ARGS = (%INSERT_ARGS, where => 1, primary_key => 1);
1357
+sub insert_at {
1358
+    my $self = shift;
1359
+    
1360
+    warn "insert_at is DEPRECATED! use insert and id option instead";
1361
+    
1362
+    # Arguments
1363
+    my $param;
1364
+    $param = shift if @_ % 2;
1365
+    my %args = @_;
1366
+    my $primary_key = delete $args{primary_key};
1367
+    $primary_key = [$primary_key] unless ref $primary_key;
1368
+    my $where = delete $args{where};
1369
+    my $p = delete $args{param} || {};
1370
+    $param  ||= $p;
1371
+    
1372
+    # Check arguments
1373
+    foreach my $name (keys %args) {
1374
+        croak qq{"$name" is wrong option } . _subname
1375
+          unless $INSERT_AT_ARGS{$name};
1376
+    }
1377
+    
1378
+    # Create where parameter
1379
+    my $where_param = $self->_create_where_param($where, $primary_key);
1380
+    $param = $self->merge_param($where_param, $param);
1381
+    
1382
+    return $self->insert(param => $param, %args);
1383
+}
1384
+
1372 1385
 # DEPRECATED!
1373 1386
 sub register_tag {
1374 1387
     warn "register_tag is DEPRECATED!";
+38
t/dbix-custom-core-sqlite.t
... ...
@@ -2323,4 +2323,42 @@ $dbi->update(
2323 2323
 $rows = $dbi->select(table => 'table1')->fetch_hash_all;
2324 2324
 is_deeply($rows, [{key1 => 5, key2 => 2}]);
2325 2325
 
2326
+test 'insert id and primary_key option';
2327
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2328
+$dbi->execute($CREATE_TABLE->{1});
2329
+$dbi->insert(
2330
+    primary_key => ['key1', 'key2'], 
2331
+    table => 'table1',
2332
+    id => [1, 2],
2333
+    param => {key3 => 3}
2334
+);
2335
+is($dbi->select(table => 'table1')->one->{key1}, 1);
2336
+is($dbi->select(table => 'table1')->one->{key2}, 2);
2337
+is($dbi->select(table => 'table1')->one->{key3}, 3);
2338
+
2339
+$dbi->delete_all(table => 'table1');
2340
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2341
+$dbi->insert(
2342
+    primary_key => 'key1', 
2343
+    table => 'table1',
2344
+    id => 1,
2345
+    param => {key2 => 2, key3 => 3}
2346
+);
2347
+
2348
+is($dbi->select(table => 'table1')->one->{key1}, 1);
2349
+is($dbi->select(table => 'table1')->one->{key2}, 2);
2350
+is($dbi->select(table => 'table1')->one->{key3}, 3);
2351
+
2352
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2353
+$dbi->execute($CREATE_TABLE->{1});
2354
+$dbi->insert(
2355
+    {key3 => 3},
2356
+    primary_key => ['key1', 'key2'], 
2357
+    table => 'table1',
2358
+    id => [1, 2],
2359
+);
2360
+is($dbi->select(table => 'table1')->one->{key1}, 1);
2361
+is($dbi->select(table => 'table1')->one->{key2}, 2);
2362
+is($dbi->select(table => 'table1')->one->{key3}, 3);
2363
+
2326 2364
 =cut