... | ... |
@@ -229,38 +229,12 @@ sub dbh { |
229 | 229 |
} |
230 | 230 |
} |
231 | 231 |
|
232 |
-sub _where_clause_and_param { |
|
233 |
- my ($self, $where, $param) = @_; |
|
234 |
- |
|
235 |
- $where ||= {}; |
|
236 |
- $param ||= {}; |
|
237 |
- my $w = {}; |
|
238 |
- my $where_clause = ''; |
|
239 |
- if (ref $where eq 'ARRAY' && !ref $where->[0]) { |
|
240 |
- $w->{clause} = "where " . $where->[0]; |
|
241 |
- $w->{param} = $where->[1]; |
|
242 |
- } |
|
243 |
- elsif (ref $where) { |
|
244 |
- $where = $self->_where_to_obj($where); |
|
245 |
- $w->{param} = keys %$param |
|
246 |
- ? $self->merge_param($param, $where->param) |
|
247 |
- : $where->param; |
|
248 |
- $w->{clause} = $where->to_string; |
|
249 |
- } |
|
250 |
- elsif ($where) { |
|
251 |
- $w->{clause} = "where $where"; |
|
252 |
- $w->{param} = $param; |
|
253 |
- } |
|
254 |
- |
|
255 |
- return $w; |
|
256 |
-} |
|
257 |
- |
|
258 | 232 |
sub delete { |
259 | 233 |
my ($self, %opt) = @_; |
260 | 234 |
warn "delete method where_param option is DEPRECATED!" |
261 | 235 |
if $opt{where_param}; |
262 | 236 |
|
263 |
- # Don't allow delete all row |
|
237 |
+ # Don't allow delete all rows |
|
264 | 238 |
croak qq{delete method where or id option must be specified } . _subname |
265 | 239 |
if !$opt{where} && !defined $opt{id} && !$opt{allow_delete_all}; |
266 | 240 |
|
... | ... |
@@ -628,42 +602,6 @@ sub insert { |
628 | 602 |
return $self->execute($sql, $param, %opt); |
629 | 603 |
} |
630 | 604 |
|
631 |
-sub update_or_insert { |
|
632 |
- my $self = shift; |
|
633 |
- |
|
634 |
- # Arguments |
|
635 |
- my $param = shift; |
|
636 |
- my %opt = @_; |
|
637 |
- my $id = $opt{id}; |
|
638 |
- my $primary_key = $opt{primary_key}; |
|
639 |
- $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY'; |
|
640 |
- croak "update_or_insert method need primary_key option " . |
|
641 |
- "when id is specified" . _subname |
|
642 |
- if defined $id && !defined $primary_key; |
|
643 |
- my $table = $opt{table}; |
|
644 |
- croak qq{"table" option must be specified } . _subname |
|
645 |
- unless defined $table; |
|
646 |
- my $select_option = $opt{select_option}; |
|
647 |
- |
|
648 |
- my $rows = $self->select(table => $table, id => $id, |
|
649 |
- primary_key => $primary_key, %$select_option)->all; |
|
650 |
- |
|
651 |
- croak "selected row count must be one or zero" . _subname |
|
652 |
- if @$rows > 1; |
|
653 |
- |
|
654 |
- my $row = $rows->[0]; |
|
655 |
- my @options = (table => $table); |
|
656 |
- push @options, id => $id, primary_key => $primary_key if defined $id; |
|
657 |
- push @options, %opt; |
|
658 |
- |
|
659 |
- if ($row) { |
|
660 |
- return $self->update($param, @options); |
|
661 |
- } |
|
662 |
- else { |
|
663 |
- return $self->insert($param, @options); |
|
664 |
- } |
|
665 |
-} |
|
666 |
- |
|
667 | 605 |
sub insert_timestamp { |
668 | 606 |
my $self = shift; |
669 | 607 |
|
... | ... |
@@ -1085,11 +1023,6 @@ sub type_rule { |
1085 | 1023 |
return $self->{type_rule} || {}; |
1086 | 1024 |
} |
1087 | 1025 |
|
1088 |
-sub _create_where { |
|
1089 |
- my $self = shift; |
|
1090 |
- |
|
1091 |
-} |
|
1092 |
- |
|
1093 | 1026 |
sub update { |
1094 | 1027 |
my $self = shift; |
1095 | 1028 |
|
... | ... |
@@ -1103,6 +1036,10 @@ sub update { |
1103 | 1036 |
my $where = $opt{where} || {}; |
1104 | 1037 |
my $where_param = $opt{where_param} || {}; |
1105 | 1038 |
|
1039 |
+ # Don't allow update all rows |
|
1040 |
+ croak qq{update method where option must be specified } . _subname |
|
1041 |
+ if !$opt{where} && !defined $opt{id} && !$opt{allow_update_all}; |
|
1042 |
+ |
|
1106 | 1043 |
# Timestamp |
1107 | 1044 |
if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) { |
1108 | 1045 |
my $columns = $update_timestamp->[0]; |
... | ... |
@@ -1133,8 +1070,6 @@ sub update { |
1133 | 1070 |
$where_clause = $where->to_string; |
1134 | 1071 |
} |
1135 | 1072 |
elsif ($where) { $where_clause = "where $where" } |
1136 |
- croak qq{"where" must be specified } . _subname |
|
1137 |
- if "$where_clause" eq '' && !$opt{allow_update_all}; |
|
1138 | 1073 |
|
1139 | 1074 |
# Merge where parameter to parameter |
1140 | 1075 |
$param = $self->merge_param($param, $where_param) if keys %$where_param; |
... | ... |
@@ -1150,6 +1085,42 @@ sub update { |
1150 | 1085 |
|
1151 | 1086 |
sub update_all { shift->update(allow_update_all => 1, @_) }; |
1152 | 1087 |
|
1088 |
+sub update_or_insert { |
|
1089 |
+ my $self = shift; |
|
1090 |
+ |
|
1091 |
+ # Arguments |
|
1092 |
+ my $param = shift; |
|
1093 |
+ my %opt = @_; |
|
1094 |
+ my $id = $opt{id}; |
|
1095 |
+ my $primary_key = $opt{primary_key}; |
|
1096 |
+ $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY'; |
|
1097 |
+ croak "update_or_insert method need primary_key option " . |
|
1098 |
+ "when id is specified" . _subname |
|
1099 |
+ if defined $id && !defined $primary_key; |
|
1100 |
+ my $table = $opt{table}; |
|
1101 |
+ croak qq{"table" option must be specified } . _subname |
|
1102 |
+ unless defined $table; |
|
1103 |
+ my $select_option = $opt{select_option}; |
|
1104 |
+ |
|
1105 |
+ my $rows = $self->select(table => $table, id => $id, |
|
1106 |
+ primary_key => $primary_key, %$select_option)->all; |
|
1107 |
+ |
|
1108 |
+ croak "selected row count must be one or zero" . _subname |
|
1109 |
+ if @$rows > 1; |
|
1110 |
+ |
|
1111 |
+ my $row = $rows->[0]; |
|
1112 |
+ my @opt = (table => $table); |
|
1113 |
+ push @opt, id => $id, primary_key => $primary_key if defined $id; |
|
1114 |
+ push @opt, %opt; |
|
1115 |
+ |
|
1116 |
+ if ($row) { |
|
1117 |
+ return $self->update($param, @opt); |
|
1118 |
+ } |
|
1119 |
+ else { |
|
1120 |
+ return $self->insert($param, @opt); |
|
1121 |
+ } |
|
1122 |
+} |
|
1123 |
+ |
|
1153 | 1124 |
sub update_timestamp { |
1154 | 1125 |
my $self = shift; |
1155 | 1126 |
|
... | ... |
@@ -1598,6 +1569,32 @@ sub _where_to_obj { |
1598 | 1569 |
return $obj; |
1599 | 1570 |
} |
1600 | 1571 |
|
1572 |
+sub _where_clause_and_param { |
|
1573 |
+ my ($self, $where, $param) = @_; |
|
1574 |
+ |
|
1575 |
+ $where ||= {}; |
|
1576 |
+ $param ||= {}; |
|
1577 |
+ my $w = {}; |
|
1578 |
+ my $where_clause = ''; |
|
1579 |
+ if (ref $where eq 'ARRAY' && !ref $where->[0]) { |
|
1580 |
+ $w->{clause} = "where " . $where->[0]; |
|
1581 |
+ $w->{param} = $where->[1]; |
|
1582 |
+ } |
|
1583 |
+ elsif (ref $where) { |
|
1584 |
+ $where = $self->_where_to_obj($where); |
|
1585 |
+ $w->{param} = keys %$param |
|
1586 |
+ ? $self->merge_param($param, $where->param) |
|
1587 |
+ : $where->param; |
|
1588 |
+ $w->{clause} = $where->to_string; |
|
1589 |
+ } |
|
1590 |
+ elsif ($where) { |
|
1591 |
+ $w->{clause} = "where $where"; |
|
1592 |
+ $w->{param} = $param; |
|
1593 |
+ } |
|
1594 |
+ |
|
1595 |
+ return $w; |
|
1596 |
+} |
|
1597 |
+ |
|
1601 | 1598 |
sub _apply_filter { |
1602 | 1599 |
my ($self, $table, @cinfos) = @_; |
1603 | 1600 |
|
... | ... |
@@ -660,7 +660,7 @@ $dbi->update(table => $table1, param => {$key1 => 3}, where => $where); |
660 | 660 |
$result = $dbi->select(table => $table1); |
661 | 661 |
is_deeply($result->all, [{$key1 => 3, $key2 => 2}], 'update() where'); |
662 | 662 |
|
663 |
-eval{$dbi->update(table => $table1, param => {';' => 1})}; |
|
663 |
+eval{$dbi->update(table => $table1, param => {';' => 1}, where => {$key1 => 1})}; |
|
664 | 664 |
like($@, qr/safety/); |
665 | 665 |
|
666 | 666 |
eval{$dbi->update(table => $table1, param => {$key1 => 1}, where => {';' => 1})}; |