Showing 1 changed files with 28 additions and 44 deletions
+28 -44
lib/DBIx/Custom.pm
... ...
@@ -540,20 +540,21 @@ our %INSERT_ARGS = map { $_ => 1 } @COMMON_ARGS, qw/param append/;
540 540
 sub insert {
541 541
     my ($self, %args) = @_;
542 542
     
543
-    # Reserved word quote
544
-    my $q = $self->reserved_word_quote;
545
-
546
-    # Check argument names
547
-    foreach my $name (keys %args) {
548
-        croak qq{Argument "$name" is wrong name}
549
-          unless $INSERT_ARGS{$name};
550
-    }
551
-    
552 543
     # Arguments
553 544
     my $table  = delete $args{table};
554 545
     croak qq{"table" option must be specified} unless $table;
555 546
     my $param  = delete $args{param} || {};
556 547
     my $append = delete $args{append} || '';
548
+    my $query_return  = delete $args{query};
549
+
550
+    # Check arguments
551
+    foreach my $name (keys %args) {
552
+        croak qq{Argument "$name" is wrong name}
553
+          unless $INSERT_ARGS{$name};
554
+    }
555
+
556
+    # Reserved word quote
557
+    my $q = $self->reserved_word_quote;
557 558
     
558 559
     # Columns
559 560
     my @columns;
... ...
@@ -566,70 +567,53 @@ sub insert {
566 567
         push @columns, $column;
567 568
     }
568 569
     
569
-    # SQL stack
570
+    # Insert statement
570 571
     my @sql;
571
-    
572
-    # Insert
573 572
     push @sql, "insert into $q$table$q {insert_param ". join(' ', @columns) . '}';
574 573
     push @sql, $append if $append;
575
-    
576
-    # SQL
577 574
     my $sql = join (' ', @sql);
578 575
     
579 576
     # Create query
580 577
     my $query = $self->create_query($sql);
581
-    return $query if $args{query};
578
+    return $query if $query_return;
582 579
     
583 580
     # Execute query
584
-    my $ret_val = $self->execute(
581
+    return $self->execute(
585 582
         $query,
586
-        param  => $param,
583
+        param => $param,
587 584
         table => $table,
588 585
         %args
589 586
     );
590
-    
591
-    return $ret_val;
592 587
 }
593 588
 
594 589
 our %INSERT_AT_ARGS = (%INSERT_ARGS, where => 1, primary_key => 1);
595 590
 
596 591
 sub insert_at {
597 592
     my ($self, %args) = @_;
593
+
594
+    # Arguments
595
+    my $primary_keys = delete $args{primary_key};
596
+    $primary_keys = [$primary_keys] unless ref $primary_keys;
597
+    my $where = delete $args{where};
598
+    my $param = delete $args{param};
598 599
     
599
-    # Check argument names
600
+    # Check arguments
600 601
     foreach my $name (keys %args) {
601 602
         croak qq{Argument "$name" is wrong name}
602 603
           unless $INSERT_AT_ARGS{$name};
603 604
     }
604 605
     
605
-    # Primary key
606
-    my $primary_keys = delete $args{primary_key};
607
-    $primary_keys = [$primary_keys] unless ref $primary_keys;
608
-    
609
-    # Where clause
610
-    my $where = {};
611
-    my $param = {};
612
-    
613
-    if (exists $args{where}) {
614
-        my $where_columns = delete $args{where};
615
-        $where_columns = [$where_columns] unless ref $where_columns;
616
-
606
+    # Where
607
+    my $where_param = {};
608
+    if ($where) {
609
+        $where = [$where] unless ref $where;
617 610
         croak qq{"where" must be constant value or array reference}
618
-          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
619
-        
611
+          unless !ref $where || ref $where eq 'ARRAY';
620 612
         for(my $i = 0; $i < @$primary_keys; $i ++) {
621
-           $where->{$primary_keys->[$i]} = $where_columns->[$i];
613
+           $where_param->{$primary_keys->[$i]} = $where->[$i];
622 614
         }
623 615
     }
624
-    
625
-    if (exists $args{param}) {
626
-        $param = delete $args{param};
627
-        for(my $i = 0; $i < @$primary_keys; $i ++) {
628
-             delete $param->{$primary_keys->[$i]};
629
-        }
630
-    }
631
-    
632
-    $param = {%$param, %$where};
616
+    $param = $self->merge_param($where_param, $param);
633 617
     
634 618
     return $self->insert(param => $param, %args);
635 619
 }