... | ... |
@@ -1088,24 +1088,12 @@ sub update { |
1088 | 1088 |
my $self = shift; |
1089 | 1089 |
|
1090 | 1090 |
# Arguments |
1091 |
- my $param; |
|
1092 |
- $param = shift if @_ % 2; |
|
1091 |
+ my $param = @_ % 2 ? shift : undef; |
|
1093 | 1092 |
my %opt = @_; |
1094 |
- my $table = $opt{table} || ''; |
|
1095 |
- croak qq{"table" option must be specified } . _subname |
|
1096 |
- unless $table; |
|
1097 |
- my $p = $opt{param} || {}; |
|
1098 |
- $param ||= $p; |
|
1093 |
+ warn "update param option is DEPRECATED!" if $opt{param}; |
|
1094 |
+ $param ||= $opt{param} || {}; |
|
1099 | 1095 |
my $where = $opt{where} || {}; |
1100 | 1096 |
my $where_param = $opt{where_param} || {}; |
1101 |
- my $allow_update_all = $opt{allow_update_all}; |
|
1102 |
- my $id = $opt{id}; |
|
1103 |
- my $primary_key = $opt{primary_key}; |
|
1104 |
- croak "update method primary_key option " . |
|
1105 |
- "must be specified when id is specified " . _subname |
|
1106 |
- if defined $id && !defined $primary_key; |
|
1107 |
- $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY'; |
|
1108 |
- my $prefix = $opt{prefix}; |
|
1109 | 1097 |
|
1110 | 1098 |
# Timestamp |
1111 | 1099 |
if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) { |
... | ... |
@@ -1116,12 +1104,12 @@ sub update { |
1116 | 1104 |
$param->{$_} = $value for @$columns; |
1117 | 1105 |
} |
1118 | 1106 |
|
1119 |
- # Update clause |
|
1107 |
+ # Assign clause |
|
1120 | 1108 |
my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}}); |
1121 | 1109 |
|
1122 | 1110 |
# Where |
1123 |
- $where = $self->_id_to_param($id, $primary_key, $table) |
|
1124 |
- if defined $id; |
|
1111 |
+ $where = $self->_id_to_param($opt{id}, $opt{primary_key}, $opt{table}) |
|
1112 |
+ if defined $opt{id}; |
|
1125 | 1113 |
my $where_clause = ''; |
1126 | 1114 |
if (ref $where eq 'ARRAY' && !ref $where->[0]) { |
1127 | 1115 |
$where_clause = "where " . $where->[0]; |
... | ... |
@@ -1138,16 +1126,15 @@ sub update { |
1138 | 1126 |
} |
1139 | 1127 |
elsif ($where) { $where_clause = "where $where" } |
1140 | 1128 |
croak qq{"where" must be specified } . _subname |
1141 |
- if "$where_clause" eq '' && !$allow_update_all; |
|
1129 |
+ if "$where_clause" eq '' && !$opt{allow_update_all}; |
|
1142 | 1130 |
|
1143 | 1131 |
# Merge param |
1144 | 1132 |
$param = $self->merge_param($param, $where_param) if keys %$where_param; |
1145 | 1133 |
|
1146 | 1134 |
# Update statement |
1147 |
- my $sql; |
|
1148 |
- $sql .= "update "; |
|
1149 |
- $sql .= "$prefix " if defined $prefix; |
|
1150 |
- $sql .= $self->_q($table) . " set $assign_clause $where_clause "; |
|
1135 |
+ my $sql = "update "; |
|
1136 |
+ $sql .= "$opt{prefix} " if defined $opt{prefix}; |
|
1137 |
+ $sql .= $self->_q($opt{table}) . " set $assign_clause $where_clause "; |
|
1151 | 1138 |
|
1152 | 1139 |
# Execute query |
1153 | 1140 |
return $self->execute($sql, $param, %opt); |
... | ... |
@@ -1664,8 +1664,6 @@ is($dbi->{_tags}->{b}->(), 2); |
1664 | 1664 |
|
1665 | 1665 |
test 'table not specify exception'; |
1666 | 1666 |
$dbi = DBIx::Custom->connect; |
1667 |
-eval {$dbi->update}; |
|
1668 |
-like($@, qr/table/); |
|
1669 | 1667 |
eval {$dbi->delete}; |
1670 | 1668 |
like($@, qr/table/); |
1671 | 1669 |
eval {$dbi->select}; |
... | ... |
@@ -51,8 +51,8 @@ test 'prefix'; |
51 | 51 |
$dbi = DBIx::Custom->connect; |
52 | 52 |
eval { $dbi->execute('drop table table1') }; |
53 | 53 |
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));'); |
54 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
55 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}, prefix => 'or replace'); |
|
54 |
+$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
55 |
+$dbi->insert({key1 => 1, key2 => 4}, table => 'table1', prefix => 'or replace'); |
|
56 | 56 |
$result = $dbi->execute('select * from table1;'); |
57 | 57 |
$rows = $result->all; |
58 | 58 |
is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
... | ... |
@@ -60,8 +60,8 @@ is_deeply($rows, [{key1 => 1, key2 => 4}], "basic"); |
60 | 60 |
$dbi = DBIx::Custom->connect; |
61 | 61 |
eval { $dbi->execute('drop table table1') }; |
62 | 62 |
$dbi->execute('create table table1 (key1 varchar, key2 varchar, primary key(key1));'); |
63 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
64 |
-$dbi->update(table => 'table1', param => {key2 => 4}, |
|
63 |
+$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
64 |
+$dbi->update({key2 => 4}, table => 'table1', |
|
65 | 65 |
where => {key1 => 1}, prefix => 'or replace'); |
66 | 66 |
$result = $dbi->execute('select * from table1;'); |
67 | 67 |
$rows = $result->all; |
... | ... |
@@ -74,7 +74,7 @@ $dbi->quote('"'); |
74 | 74 |
eval { $dbi->execute("drop table ${q}table$p") }; |
75 | 75 |
$dbi->execute($create_table_reserved); |
76 | 76 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
77 |
-$dbi->insert(table => 'table', param => {select => 1}); |
|
77 |
+$dbi->insert({select => 1}, table => 'table'); |
|
78 | 78 |
$dbi->delete(table => 'table', where => {select => 1}); |
79 | 79 |
$result = $dbi->execute("select * from ${q}table$p"); |
80 | 80 |
$rows = $result->all; |
... | ... |
@@ -154,7 +154,7 @@ $dbi = DBIx::Custom->connect( |
154 | 154 |
$binary = pack("I3", 1, 2, 3); |
155 | 155 |
eval { $dbi->execute('drop table table1') }; |
156 | 156 |
$dbi->execute('create table table1(key1, key2)'); |
157 |
-$dbi->insert(table => 'table1', param => {key1 => $binary, key2 => 'あ'}, type => [key1 => DBI::SQL_BLOB]); |
|
157 |
+$dbi->insert({key1 => $binary, key2 => 'あ'}, table => 'table1', type => [key1 => DBI::SQL_BLOB]); |
|
158 | 158 |
$result = $dbi->select(table => 'table1'); |
159 | 159 |
$row = $result->one; |
160 | 160 |
is_deeply($row, {key1 => $binary, key2 => 'あ'}, "basic"); |
... | ... |
@@ -180,8 +180,8 @@ is($result->one->{key1}, 'A'); |
180 | 180 |
test 'select limit'; |
181 | 181 |
eval { $dbi->execute('drop table table1') }; |
182 | 182 |
$dbi->execute($create_table1); |
183 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
184 |
-$dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4}); |
|
183 |
+$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
184 |
+$dbi->insert({key1 => 3, key2 => 4}, table => 'table1'); |
|
185 | 185 |
$rows = $dbi->select(table => 'table1', append => "order by key1 desc limit 1")->all; |
186 | 186 |
is_deeply($rows, [{key1 => 3, key2 => 4}], "append statement"); |
187 | 187 |
|
... | ... |
@@ -198,8 +198,8 @@ $dbi->apply_filter('table3', |
198 | 198 |
name => {in => sub { uc $_[0] } } |
199 | 199 |
); |
200 | 200 |
|
201 |
-$dbi->insert(table => 'table2', param => {id => 1, name => 'a', table3_id => 2}); |
|
202 |
-$dbi->insert(table => 'table3', param => {id => 2, name => 'b'}); |
|
201 |
+$dbi->insert({id => 1, name => 'a', table3_id => 2}, table => 'table2'); |
|
202 |
+$dbi->insert({id => 2, name => 'b'}, table => 'table3'); |
|
203 | 203 |
|
204 | 204 |
$result = $dbi->select( |
205 | 205 |
table => ['table2', 'table3'], relation => {'table2.table3_id' => 'table3.id'}, |
... | ... |
@@ -226,8 +226,8 @@ $dbi->reserved_word_quote('"'); |
226 | 226 |
$dbi->execute($create_table_reserved); |
227 | 227 |
$dbi->apply_filter('table', select => {out => sub { $_[0] * 2}}); |
228 | 228 |
$dbi->apply_filter('table', update => {out => sub { $_[0] * 3}}); |
229 |
-$dbi->insert(table => 'table', param => {select => 1}); |
|
230 |
-$dbi->update(table => 'table', where => {'table.select' => 1}, param => {update => 2}); |
|
229 |
+$dbi->insert({select => 1}, table => 'table'); |
|
230 |
+$dbi->update({update => 2}, table => 'table', where => {'table.select' => 1}); |
|
231 | 231 |
$result = $dbi->execute("select * from ${q}table$p"); |
232 | 232 |
$rows = $result->all; |
233 | 233 |
is_deeply($rows, [{select => 2, update => 6}], "reserved word"); |
... | ... |
@@ -236,9 +236,9 @@ test 'limit tag'; |
236 | 236 |
$dbi = DBIx::Custom->connect; |
237 | 237 |
eval { $dbi->execute('drop table table1') }; |
238 | 238 |
$dbi->execute($create_table1); |
239 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
240 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 4}); |
|
241 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 6}); |
|
239 |
+$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
240 |
+$dbi->insert({key1 => 1, key2 => 4}, table => 'table1'); |
|
241 |
+$dbi->insert({key1 => 1, key2 => 6}, table => 'table1'); |
|
242 | 242 |
$dbi->register_tag( |
243 | 243 |
limit => sub { |
244 | 244 |
my ($count, $offset) = @_; |
... | ... |
@@ -275,9 +275,9 @@ eval { $dbi->execute("drop table table1") }; |
275 | 275 |
eval { $dbi->execute("drop table table2") }; |
276 | 276 |
$dbi->execute($create_table1); |
277 | 277 |
$dbi->execute("create table table2 (key1, key3)"); |
278 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
279 |
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
280 |
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 1}); |
|
278 |
+$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
279 |
+$dbi->insert({key1 => 1, key3 => 4}, table => 'table2'); |
|
280 |
+$dbi->insert({key1 => 1, key3 => 1}, table => 'table2'); |
|
281 | 281 |
$result = $dbi->select( |
282 | 282 |
table => 'table1', |
283 | 283 |
column => [{table2 => ['key3']}], |
... | ... |
@@ -292,9 +292,9 @@ eval { $dbi->execute("drop table table1") }; |
292 | 292 |
eval { $dbi->execute("drop table table2") }; |
293 | 293 |
$dbi->execute($create_table1); |
294 | 294 |
$dbi->execute("create table table2 (key1, key3)"); |
295 |
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
296 |
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 4}); |
|
297 |
-$dbi->insert(table => 'table2', param => {key1 => 1, key3 => 1}); |
|
295 |
+$dbi->insert({key1 => 1, key2 => 2}, table => 'table1'); |
|
296 |
+$dbi->insert({key1 => 1, key3 => 4}, table => 'table2'); |
|
297 |
+$dbi->insert({key1 => 1, key3 => 1}, table => 'table2'); |
|
298 | 298 |
$result = $dbi->select( |
299 | 299 |
table => 'table1', |
300 | 300 |
column => [{table2 => ['key3']}], |