Showing 4 changed files with 50 additions and 12 deletions
+4
Changes
... ...
@@ -1,3 +1,7 @@
1
+0.1738
2
+    - insert method id value is not copied to parameter
3
+      if the key exists in parameter
4
+    
1 5
 0.1737
2 6
     - micro optimization
3 7
     - fixed DEBUG messsage bug
+29 -10
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.1737';
4
+our $VERSION = '0.1738';
5 5
 use 5.008001;
6 6
 
7 7
 use Carp 'croak';
... ...
@@ -252,6 +252,7 @@ sub delete {
252 252
     $sql .= "from " . $self->_q($opt{table}) . " $w->{clause} ";
253 253
     
254 254
     # Execute query
255
+    $opt{statement} = 'delete';
255 256
     $self->execute($sql, $w->{param}, %opt);
256 257
 }
257 258
 
... ...
@@ -372,6 +373,7 @@ sub execute {
372 373
         $query = $opt{reuse}->{$sql} if $opt{reuse};
373 374
         $query = $self->_create_query($sql,$opt{after_build_sql} || $opt{sqlfilter})
374 375
           unless $query;
376
+        $query->statement($opt{statement} || '');
375 377
         $opt{reuse}->{$sql} = $query if $opt{reuse};
376 378
     }
377 379
         
... ...
@@ -387,13 +389,23 @@ sub execute {
387 389
     # Tables
388 390
     unshift @$tables, @{$query->{tables} || []};
389 391
     my $main_table = @{$tables}[-1];
390
-    
391
-    # Convert id to parameter
392
+
393
+    # Merge id to parameter
394
+    my @cleanup;
392 395
     if (defined $opt{id}) {
393
-        $opt{statement} ||= '';
394
-        my $id_param = $self->_id_to_param($opt{id}, $opt{primary_key},
395
-          $opt{statement} eq 'insert' ? undef : $main_table);
396
-        $param = $self->merge_param($id_param, $param);
396
+        croak "execute id option must be specified with primary_key option"
397
+          unless $opt{primary_key};
398
+        $opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key};
399
+        $opt{id} = [$opt{id}] unless ref $opt{id};
400
+        my $statement = $query->statement;
401
+        for (my $i = 0; $i < @{$opt{primary_key}}; $i++) {
402
+           my $key = $opt{primary_key}->[$i];
403
+           $key = "$main_table.$key" if $statement eq 'update' ||
404
+             $statement eq 'delete' || $statement eq 'select';
405
+           next if exists $param->{$key};
406
+           $param->{$key} = $opt{id}->[$i];
407
+           push @cleanup, $key;
408
+        }
397 409
     }
398 410
     
399 411
     # Cleanup tables(DEPRECATED!)
... ...
@@ -472,6 +484,9 @@ sub execute {
472 484
     
473 485
     $self->_croak($@, qq{. Following SQL is executed.\n}
474 486
       . qq{$query->{sql}\n} . _subname) if $@;
487
+
488
+    # Remove id from parameter
489
+    delete $param->{$_} for @cleanup;
475 490
     
476 491
     # DEBUG message
477 492
     if ($ENV{DBIX_CUSTOM_DEBUG}) {
... ...
@@ -579,15 +594,17 @@ sub insert {
579 594
     }
580 595
     
581 596
     # Merge id to parameter
597
+    my @cleanup;
582 598
     if (defined $opt{id}) {
583
-        croak "insert primary_key option must be specified with id option"
599
+        croak "insert id option must be specified with primary_key option"
584 600
           unless $opt{primary_key};
585 601
         $opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key};
586 602
         $opt{id} = [$opt{id}] unless ref $opt{id};
587 603
         for (my $i = 0; $i < @{$opt{primary_key}}; $i++) {
588 604
            my $key = $opt{primary_key}->[$i];
589
-           croak "id already contain in parameter" if exists $param->{$key};
605
+           next if exists $param->{$key};
590 606
            $param->{$key} = $opt{id}->[$i];
607
+           push @cleanup, $key;
591 608
         }
592 609
     }
593 610
     
... ...
@@ -598,7 +615,7 @@ sub insert {
598 615
       . $self->values_clause($param, {wrap => $opt{wrap}}) . " ";
599 616
 
600 617
     # Remove id from parameter
601
-    if (defined $opt{id}) { delete $param->{$_} for @{$opt{primary_key}} }
618
+    delete $param->{$_} for @cleanup;
602 619
     
603 620
     # Execute query
604 621
     $opt{statement} = 'insert';
... ...
@@ -879,6 +896,7 @@ sub select {
879 896
       if $opt{relation};
880 897
     
881 898
     # Execute query
899
+    $opt{statement} = 'select';
882 900
     my $result = $self->execute($sql, $w->{param}, %opt);
883 901
     
884 902
     $result;
... ...
@@ -1050,6 +1068,7 @@ sub update {
1050 1068
     $sql .= $self->_q($opt{table}) . " set $assign_clause $w->{clause} ";
1051 1069
     
1052 1070
     # Execute query
1071
+    $opt{statement} = 'update';
1053 1072
     $self->execute($sql, $param, %opt);
1054 1073
 }
1055 1074
 
+1 -1
lib/DBIx/Custom/Query.pm
... ...
@@ -4,7 +4,7 @@ use Object::Simple -base;
4 4
 use Carp 'croak';
5 5
 use DBIx::Custom::Util '_subname';
6 6
 
7
-has 'sth',
7
+has [qw/sth statement/],
8 8
     sql => '',
9 9
     columns => sub { [] };
10 10
 
+16 -1
t/common.t
... ...
@@ -577,7 +577,6 @@ $dbi->update_or_insert(
577 577
 $row = $dbi->select(id => 1, table => $table1, primary_key => $key1)->one;
578 578
 is_deeply($row, {$key1 => 1, $key2 => 2}, "basic");
579 579
 
580
-
581 580
 test 'default_bind_filter';
582 581
 $dbi->execute("delete from $table1");
583 582
 $dbi->register_filter(
... ...
@@ -2308,6 +2307,22 @@ is($dbi->select(table => $table1)->one->{$key1}, 1);
2308 2307
 is($dbi->select(table => $table1)->one->{$key2}, 2);
2309 2308
 is($dbi->select(table => $table1)->one->{$key3}, 3);
2310 2309
 
2310
+$dbi = DBIx::Custom->connect;
2311
+eval { $dbi->execute("drop table $table1") };
2312
+$dbi->execute($create_table1_2);
2313
+$param = {$key3 => 3, $key2 => 4};
2314
+$DB::single = 1;
2315
+$dbi->insert(
2316
+    $param,
2317
+    primary_key => [$key1, $key2], 
2318
+    table => $table1,
2319
+    id => [1, 2],
2320
+);
2321
+is($dbi->select(table => $table1)->one->{$key1}, 1);
2322
+is($dbi->select(table => $table1)->one->{$key2}, 4);
2323
+is($dbi->select(table => $table1)->one->{$key3}, 3);
2324
+is_deeply($param, {$key3 => 3, $key2 => 4});
2325
+
2311 2326
 test 'model insert id and primary_key option';
2312 2327
 $dbi = MyDBI6->connect;
2313 2328
 eval { $dbi->execute("drop table $table1") };