... | ... |
@@ -387,12 +387,28 @@ sub drop_table { |
387 | 387 |
return $self->do($sql); |
388 | 388 |
} |
389 | 389 |
|
390 |
+our %VALID_INSERT_ARGS = map { $_ => 1 } qw/append query_edit_cb/; |
|
391 |
+ |
|
390 | 392 |
sub insert { |
391 |
- my $self = shift; |
|
392 |
- my $table = shift || ''; |
|
393 |
- my $insert_params = shift || {}; |
|
394 |
- my $append_statement = shift unless ref $_[0]; |
|
395 |
- my $query_edit_cb = shift; |
|
393 |
+ my ($self, $table, $insert_params, $args) = @_; |
|
394 |
+ |
|
395 |
+ # Table |
|
396 |
+ $table ||= ''; |
|
397 |
+ |
|
398 |
+ # Insert params |
|
399 |
+ $insert_params ||= {}; |
|
400 |
+ |
|
401 |
+ # Arguments |
|
402 |
+ $args ||= {}; |
|
403 |
+ |
|
404 |
+ # Check arguments |
|
405 |
+ foreach my $name (keys %$args) { |
|
406 |
+ croak "\"$name\" is invalid name" |
|
407 |
+ unless $VALID_INSERT_ARGS{$name}; |
|
408 |
+ } |
|
409 |
+ |
|
410 |
+ my $append_statement = $args->{append} || ''; |
|
411 |
+ my $query_edit_cb = $args->{query_edit_cb}; |
|
396 | 412 |
|
397 | 413 |
# Insert keys |
398 | 414 |
my @insert_keys = keys %$insert_params; |
... | ... |
@@ -438,31 +438,35 @@ $rows = $result->fetch_hash_all; |
438 | 438 |
is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic"); |
439 | 439 |
|
440 | 440 |
$dbi->do('delete from table1'); |
441 |
-$dbi->insert('table1', {key1 => 1, key2 => 2}, sub { |
|
442 |
- my $query = shift; |
|
443 |
- $query->bind_filter(sub { |
|
444 |
- my ($value, $table, $column, $dbi) = @_; |
|
445 |
- if ($column eq 'key1') { |
|
446 |
- return $value * 3; |
|
441 |
+$dbi->insert('table1', {key1 => 1, key2 => 2}, |
|
442 |
+ { |
|
443 |
+ query_edit_cb => sub { |
|
444 |
+ my $query = shift; |
|
445 |
+ $query->bind_filter(sub { |
|
446 |
+ my ($value, $table, $column, $dbi) = @_; |
|
447 |
+ if ($column eq 'key1') { |
|
448 |
+ return $value * 3; |
|
449 |
+ } |
|
450 |
+ return $value; |
|
451 |
+ }); |
|
447 | 452 |
} |
448 |
- return $value; |
|
449 |
- }); |
|
450 |
-}); |
|
453 |
+ } |
|
454 |
+); |
|
451 | 455 |
$result = $dbi->query($SELECT_TMPLS->{0}); |
452 | 456 |
$rows = $result->fetch_hash_all; |
453 | 457 |
is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback"); |
454 | 458 |
|
455 |
-$dbi->insert('table1', {key1 => 1, key2 => 2}, ' ', sub { |
|
459 |
+$dbi->insert('table1', {key1 => 1, key2 => 2}, {append => ' ', query_edit_cb => sub { |
|
456 | 460 |
my $query = shift; |
457 | 461 |
like($query->sql, qr/insert into table1 \(.+\) values \(\?, \?\) ;/, |
458 | 462 |
"$test: append statement"); |
459 |
-}); |
|
463 |
+}}); |
|
460 | 464 |
|
461 | 465 |
test 'insert error'; |
462 | 466 |
eval{$dbi->insert('table1')}; |
463 | 467 |
like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed"); |
464 | 468 |
|
465 |
-eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, '', 'aaa')}; |
|
469 |
+eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, {append => '', query_edit_cb => 'aaa'})}; |
|
466 | 470 |
like($@, qr/Query edit callback must be code reference/, "$test : query edit callback not code ref"); |
467 | 471 |
|
468 | 472 |
|