Showing 1 changed files with 29 additions and 6 deletions
+29 -6
lib/DBIx/Custom.pm
... ...
@@ -1,7 +1,7 @@
1 1
 package DBIx::Custom;
2 2
 use Object::Simple -base;
3 3
 
4
-our $VERSION = '0.1735';
4
+our $VERSION = '0.1736';
5 5
 use 5.008001;
6 6
 
7 7
 use Carp 'croak';
... ...
@@ -393,7 +393,9 @@ sub execute {
393 393
     
394 394
     # Convert id to parameter
395 395
     if (defined $opt{id}) {
396
-        my $id_param = $self->_id_to_param($opt{id}, $opt{primary_key}, $main_table);
396
+        $opt{statement} ||= '';
397
+        my $id_param = $self->_id_to_param($opt{id}, $opt{primary_key},
398
+          $opt{statement} eq 'insert' ? undef : $main_table);
397 399
         $param = $self->merge_param($id_param, $param);
398 400
     }
399 401
     
... ...
@@ -581,20 +583,41 @@ sub insert {
581 583
     }
582 584
     
583 585
     # Merge id to parameter
584
-    $param = $self->merge_param(
585
-        $self->_id_to_param(delete $opt{id}, $opt{primary_key}), $param)
586
-      if defined $opt{id};
586
+    if (defined $opt{id}) {
587
+        croak "insert primary_key option must be specified with id option"
588
+          unless $opt{primary_key};
589
+        $opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key};
590
+        $opt{id} = [$opt{id}] unless ref $opt{id};
591
+        for (my $i = 0; $i < @{$opt{primary_key}}; $i++) {
592
+           my $key = $opt{primary_key}->[$i];
593
+           croak "id already contain in parameter" if exists $param->{$key};
594
+           $param->{$key} = $opt{id}->[$i];
595
+        }
596
+    }
587 597
     
588 598
     # Insert statement
589 599
     my $sql = "insert ";
590 600
     $sql .= "$opt{prefix} " if defined $opt{prefix};
591 601
     $sql .= "into " . $self->_q($opt{table}) . " "
592 602
       . $self->values_clause($param, {wrap => $opt{wrap}}) . " ";
603
+
604
+    # Remove id from parameter
605
+    if (defined $opt{id}) { delete $param->{$_} for @{$opt{primary_key}} }
593 606
     
594 607
     # Execute query
608
+    $opt{statement} = 'insert';
595 609
     $self->execute($sql, $param, %opt);
596 610
 }
597 611
 
612
+sub _merge_id_to_param {
613
+    my ($self, $id, $primary_keys, $param) = @_;
614
+    
615
+    # Create parameter
616
+    $id = [$id] unless ref $id;
617
+    
618
+    return $param;
619
+}
620
+
598 621
 sub insert_timestamp {
599 622
     my $self = shift;
600 623
     
... ...
@@ -2618,7 +2641,7 @@ on alias table name.
2618 2641
 
2619 2642
 =item C<reuse EXPERIMENTAL>
2620 2643
     
2621
-    reuse_query => $has_ref
2644
+    reuse => $has_ref
2622 2645
 
2623 2646
 Reuse query object if the hash reference variable is set.
2624 2647