| ... | ... |
@@ -362,7 +362,7 @@ sub execute {
|
| 362 | 362 |
# Merge second parameter |
| 363 | 363 |
my @cleanup; |
| 364 | 364 |
my $saved_param; |
| 365 |
- if (ref $param eq 'ARRAY') {
|
|
| 365 |
+ if (($opt{statement} || '') ne 'insert' && ref $param eq 'ARRAY') {
|
|
| 366 | 366 |
my $param2 = $param->[1]; |
| 367 | 367 |
$param = $param->[0]; |
| 368 | 368 |
for my $column (keys %$param2) {
|
| ... | ... |
@@ -380,6 +380,7 @@ sub execute {
|
| 380 | 380 |
} |
| 381 | 381 |
} |
| 382 | 382 |
} |
| 383 |
+ $param = [$param] unless ref $param eq 'ARRAY'; |
|
| 383 | 384 |
|
| 384 | 385 |
# Append |
| 385 | 386 |
$sql .= $opt{append} if defined $opt{append} && !ref $sql;
|
| ... | ... |
@@ -396,8 +397,8 @@ sub execute {
|
| 396 | 397 |
unless ($query) {
|
| 397 | 398 |
my $c = $self->{safety_character};
|
| 398 | 399 |
# Check unsafety keys |
| 399 |
- unless ((join('', keys %$param) || '') =~ /^[$c\.]+$/) {
|
|
| 400 |
- for my $column (keys %$param) {
|
|
| 400 |
+ unless ((join('', keys %{$param->[0]}) || '') =~ /^[$c\.]+$/) {
|
|
| 401 |
+ for my $column (keys %{$param->[0]}) {
|
|
| 401 | 402 |
croak qq{"$column" is not safety column name } . _subname
|
| 402 | 403 |
unless $column =~ /^[$c\.]+$/; |
| 403 | 404 |
} |
| ... | ... |
@@ -413,7 +414,9 @@ sub execute {
|
| 413 | 414 |
|
| 414 | 415 |
# Return query |
| 415 | 416 |
if ($opt{query}) {
|
| 416 |
- delete $param->{$_} for (@cleanup, @{$opt{cleanup} || []});
|
|
| 417 |
+ for my $column (@cleanup, @{$opt{cleanup} || []}) {
|
|
| 418 |
+ delete $_->{$column} for @$param;
|
|
| 419 |
+ } |
|
| 417 | 420 |
return $query; |
| 418 | 421 |
}; |
| 419 | 422 |
|
| ... | ... |
@@ -436,8 +439,8 @@ sub execute {
|
| 436 | 439 |
my $key = $opt{primary_key}->[$i];
|
| 437 | 440 |
$key = "$main_table.$key" if $statement eq 'update' || |
| 438 | 441 |
$statement eq 'delete' || $statement eq 'select'; |
| 439 |
- next if exists $param->{$key};
|
|
| 440 |
- $param->{$key} = $opt{id}->[$i];
|
|
| 442 |
+ next if exists $param->[0]->{$key};
|
|
| 443 |
+ $param->[0]->{$key} = $opt{id}->[$i];
|
|
| 441 | 444 |
push @cleanup, $key;1 |
| 442 | 445 |
} |
| 443 | 446 |
} |
| ... | ... |
@@ -502,11 +505,11 @@ sub execute {
|
| 502 | 505 |
if (!$query->{duplicate} && $type_rule_off && !keys %$filter && !$self->{default_out_filter}
|
| 503 | 506 |
&& !$opt{bind_type} && !$opt{type} && !$ENV{DBIX_CUSTOM_DEBUG})
|
| 504 | 507 |
{
|
| 505 |
- eval { $affected = $sth->execute(map { $param->{$_} } @{$query->{columns}}) };
|
|
| 508 |
+ eval { $affected = $sth->execute(map { $param->[0]->{$_} } @{$query->{columns}}) };
|
|
| 506 | 509 |
} |
| 507 | 510 |
else {
|
| 508 | 511 |
# Create bind values |
| 509 |
- my ($bind, $bind_types) = $self->_create_bind_values($param, $query->{columns},
|
|
| 512 |
+ my ($bind, $bind_types) = $self->_create_bind_values($param->[0], $query->{columns},
|
|
| 510 | 513 |
$filter, $type_filters, $opt{bind_type} || $opt{type} || {});
|
| 511 | 514 |
|
| 512 | 515 |
# Execute |
| ... | ... |
@@ -540,7 +543,9 @@ sub execute {
|
| 540 | 543 |
. qq{$query->{sql}\n} . _subname) if $@;
|
| 541 | 544 |
|
| 542 | 545 |
# Remove id from parameter |
| 543 |
- delete $param->{$_} for (@cleanup, @{$opt{cleanup} || []});
|
|
| 546 |
+ for my $column (@cleanup, @{$opt{cleanup} || []}) {
|
|
| 547 |
+ delete $_->{$column} for @$param;
|
|
| 548 |
+ } |
|
| 544 | 549 |
|
| 545 | 550 |
# Not select statement |
| 546 | 551 |
return $affected unless $sth->{NUM_OF_FIELDS};
|
| ... | ... |
@@ -619,19 +624,23 @@ sub insert {
|
| 619 | 624 |
my $self = shift; |
| 620 | 625 |
|
| 621 | 626 |
# Options |
| 622 |
- my $param = @_ % 2 ? shift : undef; |
|
| 627 |
+ my $params = @_ % 2 ? shift : undef; |
|
| 623 | 628 |
my %opt = @_; |
| 624 | 629 |
warn "insert method param option is DEPRECATED!" if $opt{param};
|
| 625 |
- $param ||= delete $opt{param} || {};
|
|
| 630 |
+ $params ||= delete $opt{param} || {};
|
|
| 631 |
+ |
|
| 632 |
+ my $multi; |
|
| 633 |
+ if (ref $params eq 'ARRAY') { $multi = 1 }
|
|
| 634 |
+ else { $params = [$params] }
|
|
| 626 | 635 |
|
| 627 | 636 |
# Timestamp(DEPRECATED!) |
| 628 |
- if ($opt{timestamp} && (my $insert_timestamp = $self->insert_timestamp)) {
|
|
| 637 |
+ if (!$multi && $opt{timestamp} && (my $insert_timestamp = $self->insert_timestamp)) {
|
|
| 629 | 638 |
warn "insert timestamp option is DEPRECATED! use created_at with now attribute"; |
| 630 | 639 |
my $columns = $insert_timestamp->[0]; |
| 631 | 640 |
$columns = [$columns] unless ref $columns eq 'ARRAY'; |
| 632 | 641 |
my $value = $insert_timestamp->[1]; |
| 633 | 642 |
$value = $value->() if ref $value eq 'CODE'; |
| 634 |
- $param->{$_} = $value for @$columns;
|
|
| 643 |
+ $params->[0]->{$_} = $value for @$columns;
|
|
| 635 | 644 |
} |
| 636 | 645 |
|
| 637 | 646 |
# Created time and updated time |
| ... | ... |
@@ -640,11 +649,11 @@ sub insert {
|
| 640 | 649 |
my $now = $self->now; |
| 641 | 650 |
$now = $now->() if ref $now eq 'CODE'; |
| 642 | 651 |
if (defined $opt{created_at}) {
|
| 643 |
- $param->{$opt{created_at}} = $now;
|
|
| 652 |
+ $_->{$opt{created_at}} = $now for @$params;
|
|
| 644 | 653 |
push @timestamp_cleanup, $opt{created_at};
|
| 645 | 654 |
} |
| 646 | 655 |
if (defined $opt{updated_at}) {
|
| 647 |
- $param->{$opt{updated_at}} = $now;
|
|
| 656 |
+ $_->{$opt{updated_at}} = $now for @$params;
|
|
| 648 | 657 |
push @timestamp_cleanup, $opt{updated_at};
|
| 649 | 658 |
} |
| 650 | 659 |
} |
| ... | ... |
@@ -652,15 +661,15 @@ sub insert {
|
| 652 | 661 |
# Merge id to parameter |
| 653 | 662 |
my @cleanup; |
| 654 | 663 |
my $id_param = {};
|
| 655 |
- if (defined $opt{id}) {
|
|
| 664 |
+ if (defined $opt{id} && !$multi) {
|
|
| 656 | 665 |
croak "insert id option must be specified with primary_key option" |
| 657 | 666 |
unless $opt{primary_key};
|
| 658 | 667 |
$opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key};
|
| 659 | 668 |
$opt{id} = [$opt{id}] unless ref $opt{id};
|
| 660 | 669 |
for (my $i = 0; $i < @{$opt{primary_key}}; $i++) {
|
| 661 | 670 |
my $key = $opt{primary_key}->[$i];
|
| 662 |
- next if exists $param->{$key};
|
|
| 663 |
- $param->{$key} = $opt{id}->[$i];
|
|
| 671 |
+ next if exists $params->[0]->{$key};
|
|
| 672 |
+ $params->[0]->{$key} = $opt{id}->[$i];
|
|
| 664 | 673 |
push @cleanup, $key; |
| 665 | 674 |
} |
| 666 | 675 |
} |
| ... | ... |
@@ -669,15 +678,15 @@ sub insert {
|
| 669 | 678 |
my $sql = "insert "; |
| 670 | 679 |
$sql .= "$opt{prefix} " if defined $opt{prefix};
|
| 671 | 680 |
$sql .= "into " . $self->q($opt{table}) . " "
|
| 672 |
- . $self->values_clause($param, {wrap => $opt{wrap}}) . " ";
|
|
| 681 |
+ . $self->values_clause($params->[0], {wrap => $opt{wrap}}) . " ";
|
|
| 673 | 682 |
|
| 674 | 683 |
# Remove id from parameter |
| 675 |
- delete $param->{$_} for @cleanup;
|
|
| 684 |
+ delete $params->[0]->{$_} for @cleanup;
|
|
| 676 | 685 |
|
| 677 | 686 |
# Execute query |
| 678 | 687 |
$opt{statement} = 'insert';
|
| 679 | 688 |
$opt{cleanup} = \@timestamp_cleanup;
|
| 680 |
- $self->execute($sql, $param, %opt); |
|
| 689 |
+ $self->execute($sql, $params, %opt); |
|
| 681 | 690 |
} |
| 682 | 691 |
|
| 683 | 692 |
sub insert_timestamp {
|