... | ... |
@@ -431,14 +431,14 @@ sub last_insert_id { |
431 | 431 |
|
432 | 432 |
# Insert |
433 | 433 |
sub insert { |
434 |
- my ($self, $table, $insert_params, $edit_query_cb) = @_; |
|
434 |
+ my ($self, $table, $insert_params, $query_edit_cb) = @_; |
|
435 | 435 |
$insert_params ||= {}; |
436 | 436 |
|
437 | 437 |
# Insert keys |
438 | 438 |
my @insert_keys = keys %$insert_params; |
439 | 439 |
|
440 | 440 |
# Not exists insert keys |
441 |
- croak("Insert key must be specified") |
|
441 |
+ croak("key-value pairs must be specified to 'insert' second argument") |
|
442 | 442 |
unless @insert_keys; |
443 | 443 |
|
444 | 444 |
# Templte for insert |
... | ... |
@@ -447,12 +447,12 @@ sub insert { |
447 | 447 |
# Create query |
448 | 448 |
my $query = $self->create_query($template); |
449 | 449 |
|
450 |
- # Edit query callback must be code reference |
|
451 |
- croak("Edit query callback must be code reference") |
|
452 |
- if $edit_query_cb && !ref $edit_query_cb eq 'CODE'; |
|
450 |
+ # Query edit callback must be code reference |
|
451 |
+ croak("Query edit callback must be code reference") |
|
452 |
+ if $query_edit_cb && ref $query_edit_cb ne 'CODE'; |
|
453 | 453 |
|
454 |
- # Edit query if need |
|
455 |
- $edit_query_cb->($query) if ref $edit_query_cb eq 'CODE'; |
|
454 |
+ # Query edit if need |
|
455 |
+ $query_edit_cb->($query) if $query_edit_cb; |
|
456 | 456 |
|
457 | 457 |
# Execute query |
458 | 458 |
my $ret_val = $self->execute($query, $insert_params); |
... | ... |
@@ -462,7 +462,7 @@ sub insert { |
462 | 462 |
|
463 | 463 |
sub update { |
464 | 464 |
my ($self, $table, $update_params, |
465 |
- $where_params, $edit_query_cb, $options) = @_; |
|
465 |
+ $where_params, $query_edit_cb, $options) = @_; |
|
466 | 466 |
|
467 | 467 |
$update_params ||= {}; |
468 | 468 |
$where_params ||= {}; |
... | ... |
@@ -497,12 +497,12 @@ sub update { |
497 | 497 |
# Create query |
498 | 498 |
my $query = $self->create_query($template); |
499 | 499 |
|
500 |
- # Edit query callback must be code reference |
|
501 |
- croak("Edit query callback must be code reference") |
|
502 |
- if $edit_query_cb && !ref $edit_query_cb eq 'CODE'; |
|
500 |
+ # Query edit callback must be code reference |
|
501 |
+ croak("Query edit callback must be code reference") |
|
502 |
+ if $query_edit_cb && ref $query_edit_cb ne 'CODE'; |
|
503 | 503 |
|
504 |
- # Edit query if need |
|
505 |
- $edit_query_cb->($query) if $edit_query_cb; |
|
504 |
+ # Query edit if need |
|
505 |
+ $query_edit_cb->($query) if $query_edit_cb; |
|
506 | 506 |
|
507 | 507 |
# Rearrange parammeters |
508 | 508 |
my $params = {'#update' => $update_params, %$where_params}; |
... | ... |
@@ -515,15 +515,15 @@ sub update { |
515 | 515 |
|
516 | 516 |
# Update all rows |
517 | 517 |
sub update_all { |
518 |
- my ($self, $table, $update_params, $edit_query_cb) = @_; |
|
518 |
+ my ($self, $table, $update_params, $query_edit_cb) = @_; |
|
519 | 519 |
|
520 |
- return $self->update($table, $update_params, {}, $edit_query_cb, |
|
520 |
+ return $self->update($table, $update_params, {}, $query_edit_cb, |
|
521 | 521 |
{allow_update_all => 1}); |
522 | 522 |
} |
523 | 523 |
|
524 | 524 |
# Delete |
525 | 525 |
sub delete { |
526 |
- my ($self, $table, $where_params, $edit_query_cb, $options) = @_; |
|
526 |
+ my ($self, $table, $where_params, $query_edit_cb, $options) = @_; |
|
527 | 527 |
$where_params ||= {}; |
528 | 528 |
|
529 | 529 |
# Where keys |
... | ... |
@@ -546,12 +546,12 @@ sub delete { |
546 | 546 |
# Create query |
547 | 547 |
my $query = $self->create_query($template); |
548 | 548 |
|
549 |
- # Edit query callback must be code reference |
|
550 |
- croak("Edit query callback must be code reference") |
|
551 |
- if $edit_query_cb && !ref $edit_query_cb eq 'CODE'; |
|
549 |
+ # Query edit callback must be code reference |
|
550 |
+ croak("Query edit callback must be code reference") |
|
551 |
+ if $query_edit_cb && ref $query_edit_cb ne 'CODE'; |
|
552 | 552 |
|
553 |
- # Edit query if need |
|
554 |
- $edit_query_cb->($query) if $edit_query_cb; |
|
553 |
+ # Query edit if need |
|
554 |
+ $query_edit_cb->($query) if $query_edit_cb; |
|
555 | 555 |
|
556 | 556 |
# Execute query |
557 | 557 |
my $ret_val = $self->execute($query, $where_params); |
... | ... |
@@ -561,8 +561,8 @@ sub delete { |
561 | 561 |
|
562 | 562 |
# Delete all rows |
563 | 563 |
sub delete_all { |
564 |
- my ($self, $table, $edit_query_cb) = @_; |
|
565 |
- return $self->delete($table, {}, $edit_query_cb, {allow_delete_all => 1}); |
|
564 |
+ my ($self, $table, $query_edit_cb) = @_; |
|
565 |
+ return $self->delete($table, {}, $query_edit_cb, {allow_delete_all => 1}); |
|
566 | 566 |
} |
567 | 567 |
|
568 | 568 |
sub _query_caches : ClassAttr { type => 'hash', |
... | ... |
@@ -20,20 +20,25 @@ sub test { |
20 | 20 |
|
21 | 21 |
|
22 | 22 |
|
23 |
-# Varialbes for test |
|
24 |
-our $CREATE_TABLE = { |
|
23 |
+# Constant varialbes for test |
|
24 |
+my $CREATE_TABLE = { |
|
25 | 25 |
0 => 'create table table1 (key1 char(255), key2 char(255));', |
26 | 26 |
1 => 'create table table1 (key1 char(255), key2 char(255), key3 char(255), key4 char(255), key5 char(255));' |
27 | 27 |
}; |
28 | 28 |
|
29 |
-our $SELECT_TMPL = { |
|
29 |
+my $SELECT_TMPL = { |
|
30 | 30 |
0 => 'select * from table1;' |
31 | 31 |
}; |
32 | 32 |
|
33 |
-our $DROP_TABLE = { |
|
33 |
+my $DROP_TABLE = { |
|
34 | 34 |
0 => 'drop table table1' |
35 | 35 |
}; |
36 | 36 |
|
37 |
+my $NEW_ARGS = { |
|
38 |
+ 0 => {data_source => 'dbi:SQLite:dbname=:memory:'} |
|
39 |
+}; |
|
40 |
+ |
|
41 |
+# Variables for test |
|
37 | 42 |
my $dbi; |
38 | 43 |
my $sth; |
39 | 44 |
my $tmpl; |
... | ... |
@@ -53,21 +58,21 @@ my $ret_val; |
53 | 58 |
|
54 | 59 |
|
55 | 60 |
test 'disconnect'; |
56 |
-$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
61 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
57 | 62 |
$dbi->connect; |
58 | 63 |
$dbi->disconnect; |
59 | 64 |
ok(!$dbi->dbh, $test); |
60 | 65 |
|
61 | 66 |
|
62 | 67 |
test 'connected'; |
63 |
-$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
68 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
64 | 69 |
ok(!$dbi->connected, "$test : not connected"); |
65 | 70 |
$dbi->connect; |
66 | 71 |
ok($dbi->connected, "$test : connected"); |
67 | 72 |
|
68 | 73 |
|
69 | 74 |
test 'preapare'; |
70 |
-$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
75 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
71 | 76 |
$sth = $dbi->prepare($CREATE_TABLE->{0}); |
72 | 77 |
ok($sth, "$test : auto connect"); |
73 | 78 |
$sth->execute; |
... | ... |
@@ -76,7 +81,7 @@ ok($sth, "$test : basic"); |
76 | 81 |
|
77 | 82 |
|
78 | 83 |
test 'do'; |
79 |
-$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
84 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
80 | 85 |
$ret_val = $dbi->do($CREATE_TABLE->{0}); |
81 | 86 |
ok(defined $ret_val, "$test : auto connect"); |
82 | 87 |
$ret_val = $dbi->do($DROP_TABLE->{0}); |
... | ... |
@@ -84,7 +89,7 @@ ok(defined $ret_val, "$test : basic"); |
84 | 89 |
|
85 | 90 |
|
86 | 91 |
# Prepare table |
87 |
-$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
92 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
88 | 93 |
$dbi->connect; |
89 | 94 |
$dbi->do($CREATE_TABLE->{0}); |
90 | 95 |
$sth = $dbi->prepare("insert into table1 (key1, key2) values (?, ?);"); |
... | ... |
@@ -436,31 +441,61 @@ $dbi = DBI::Custom->new(data_source => 'dbi:SQLit'); |
436 | 441 |
eval{$dbi->connect;}; |
437 | 442 |
ok($@, "$test : connect error"); |
438 | 443 |
|
439 |
-$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
444 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
440 | 445 |
$dbi->connect; |
441 | 446 |
$dbi->dbh->{AutoCommit} = 0; |
442 | 447 |
eval{$dbi->run_tranzaction()}; |
443 | 448 |
like($@, qr/AutoCommit must be true before tranzaction start/, |
444 | 449 |
"$test : run_tranzaction auto commit is false"); |
445 | 450 |
|
446 |
-$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
451 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
447 | 452 |
$sql = 'laksjdf'; |
448 | 453 |
eval{$dbi->prepare($sql)}; |
449 | 454 |
like($@, qr/$sql/, "$test : prepare fail"); |
450 | 455 |
|
451 |
-$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
456 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
452 | 457 |
$sql = 'laksjdf'; |
453 | 458 |
eval{$dbi->do($sql, qw/1 2 3/)}; |
454 | 459 |
like($@, qr/$sql/, "$test : do fail"); |
455 | 460 |
|
456 |
-$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
461 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
457 | 462 |
eval{$dbi->create_query("{p }")}; |
458 | 463 |
ok($@, "$test : create_query invalid SQL template"); |
459 | 464 |
|
460 |
-$dbi = DBI::Custom->new(data_source => 'dbi:SQLite:dbname=:memory:'); |
|
465 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
461 | 466 |
$dbi->do($CREATE_TABLE->{0}); |
462 | 467 |
$query = $dbi->create_query("select * from table1 where {= key1}"); |
463 | 468 |
eval{$dbi->execute($query, {key2 => 1})}; |
464 | 469 |
like($@, qr/Corresponding key is not found in your parameters/, |
465 | 470 |
"$test : execute corresponding key not found"); |
466 | 471 |
|
472 |
+ |
|
473 |
+test 'insert'; |
|
474 |
+$dbi = DBI::Custom->new($NEW_ARGS->{0}); |
|
475 |
+$dbi->do($CREATE_TABLE->{0}); |
|
476 |
+$dbi->insert('table1', {key1 => 1, key2 => 2}); |
|
477 |
+$dbi->insert('table1', {key1 => 3, key2 => 4}); |
|
478 |
+$result = $dbi->execute($SELECT_TMPL->{0}); |
|
479 |
+$rows = $result->fetch_all_hash; |
|
480 |
+is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic"); |
|
481 |
+ |
|
482 |
+$dbi->do('delete from table1'); |
|
483 |
+$dbi->insert('table1', {key1 => 1, key2 => 2}, sub { |
|
484 |
+ my $query = shift; |
|
485 |
+ $query->bind_filter(sub { |
|
486 |
+ my ($key, $value) = @_; |
|
487 |
+ if ($key eq 'key1') { |
|
488 |
+ return $value * 3; |
|
489 |
+ } |
|
490 |
+ return $value; |
|
491 |
+ }); |
|
492 |
+}); |
|
493 |
+$result = $dbi->execute($SELECT_TMPL->{0}); |
|
494 |
+$rows = $result->fetch_all_hash; |
|
495 |
+is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback"); |
|
496 |
+ |
|
497 |
+eval{$dbi->insert('table1')}; |
|
498 |
+like($@, qr/key-value pairs must be specified to 'insert' second argument/, "$test : insert key-value not specifed"); |
|
499 |
+ |
|
500 |
+eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, 'aaa')}; |
|
501 |
+like($@, qr/Query edit callback must be code reference/, "$test : query edit callback not code ref"); |