... | ... |
@@ -1,13 +1,14 @@ |
1 | 1 |
package DBIx::Custom; |
2 |
-use base 'Object::Simple'; |
|
3 | 2 |
|
4 | 3 |
use strict; |
5 | 4 |
use warnings; |
6 | 5 |
|
6 |
+use base 'Object::Simple'; |
|
7 | 7 |
use Carp 'croak'; |
8 | 8 |
use DBI; |
9 | 9 |
use DBIx::Custom::Result; |
10 | 10 |
use DBIx::Custom::SQL::Template; |
11 |
+use DBIx::Custom::Transaction; |
|
11 | 12 |
|
12 | 13 |
__PACKAGE__->attr('dbh'); |
13 | 14 |
|
... | ... |
@@ -356,55 +357,7 @@ sub _build_bind_values { |
356 | 357 |
return \@bind_values; |
357 | 358 |
} |
358 | 359 |
|
359 |
-sub run_transaction { |
|
360 |
- my ($self, $transaction) = @_; |
|
361 |
- |
|
362 |
- # Check auto commit |
|
363 |
- croak("AutoCommit must be true before transaction start") |
|
364 |
- unless $self->_auto_commit; |
|
365 |
- |
|
366 |
- # Auto commit off |
|
367 |
- $self->_auto_commit(0); |
|
368 |
- |
|
369 |
- # Run transaction |
|
370 |
- eval {$transaction->($self)}; |
|
371 |
- |
|
372 |
- # Tranzaction error |
|
373 |
- my $transaction_error = $@; |
|
374 |
- |
|
375 |
- # Tranzaction is failed. |
|
376 |
- if ($transaction_error) { |
|
377 |
- # Rollback |
|
378 |
- eval{$self->dbh->rollback}; |
|
379 |
- |
|
380 |
- # Rollback error |
|
381 |
- my $rollback_error = $@; |
|
382 |
- |
|
383 |
- # Auto commit on |
|
384 |
- $self->_auto_commit(1); |
|
385 |
- |
|
386 |
- if ($rollback_error) { |
|
387 |
- # Rollback is failed |
|
388 |
- croak("${transaction_error}Rollback is failed : $rollback_error"); |
|
389 |
- } |
|
390 |
- else { |
|
391 |
- # Rollback is success |
|
392 |
- croak("${transaction_error}Rollback is success"); |
|
393 |
- } |
|
394 |
- } |
|
395 |
- # Tranzaction is success |
|
396 |
- else { |
|
397 |
- # Commit |
|
398 |
- eval{$self->dbh->commit}; |
|
399 |
- my $commit_error = $@; |
|
400 |
- |
|
401 |
- # Auto commit on |
|
402 |
- $self->_auto_commit(1); |
|
403 |
- |
|
404 |
- # Commit is failed |
|
405 |
- croak($commit_error) if $commit_error; |
|
406 |
- } |
|
407 |
-} |
|
360 |
+sub transaction { DBIx::Custom::Transaction->new(dbi => shift) } |
|
408 | 361 |
|
409 | 362 |
sub last_insert_id { |
410 | 363 |
my $self = shift; |
... | ... |
@@ -726,11 +679,11 @@ DBIx::Custom - Customizable DBI |
726 | 679 |
|
727 | 680 |
=head1 VERSION |
728 | 681 |
|
729 |
-Version 0.0906 |
|
682 |
+Version 0.1001 |
|
730 | 683 |
|
731 | 684 |
=cut |
732 | 685 |
|
733 |
-our $VERSION = '0.0906'; |
|
686 |
+our $VERSION = '0.1001'; |
|
734 | 687 |
|
735 | 688 |
=head1 SYNOPSYS |
736 | 689 |
|
... | ... |
@@ -1038,11 +991,11 @@ Return value of query method is L<DBIx::Custom::Result> object |
1038 | 991 |
|
1039 | 992 |
See also L<DBIx::Custom::Result>. |
1040 | 993 |
|
1041 |
-=head2 run_transaction |
|
994 |
+=head2 transaction |
|
1042 | 995 |
|
1043 |
-Run transaction |
|
996 |
+Get L<DBIx::Custom::Transaction> object, and you run a transaction. |
|
1044 | 997 |
|
1045 |
- $dbi->run_transaction(sub { |
|
998 |
+ $dbi->transaction->run(sub { |
|
1046 | 999 |
my $dbi = shift; |
1047 | 1000 |
|
1048 | 1001 |
# do something |
... | ... |
@@ -0,0 +1,106 @@ |
1 |
+package DBIx::Custom::Transaction; |
|
2 |
+ |
|
3 |
+use strict; |
|
4 |
+use warnings; |
|
5 |
+ |
|
6 |
+use base 'Object::Simple'; |
|
7 |
+use Carp 'croak'; |
|
8 |
+ |
|
9 |
+__PACKAGE__->attr('dbi'); |
|
10 |
+ |
|
11 |
+sub run { |
|
12 |
+ my ($self, $transaction) = @_; |
|
13 |
+ |
|
14 |
+ # DBIx::Custom object |
|
15 |
+ my $dbi = $self->dbi; |
|
16 |
+ |
|
17 |
+ # Shorcut |
|
18 |
+ return unless $dbi; |
|
19 |
+ |
|
20 |
+ # Check auto commit |
|
21 |
+ croak("AutoCommit must be true before transaction start") |
|
22 |
+ unless $dbi->_auto_commit; |
|
23 |
+ |
|
24 |
+ # Auto commit off |
|
25 |
+ $dbi->_auto_commit(0); |
|
26 |
+ |
|
27 |
+ # Run transaction |
|
28 |
+ eval {$transaction->()}; |
|
29 |
+ |
|
30 |
+ # Tranzaction error |
|
31 |
+ my $transaction_error = $@; |
|
32 |
+ |
|
33 |
+ # Tranzaction is failed. |
|
34 |
+ if ($transaction_error) { |
|
35 |
+ # Rollback |
|
36 |
+ eval{$dbi->dbh->rollback}; |
|
37 |
+ |
|
38 |
+ # Rollback error |
|
39 |
+ my $rollback_error = $@; |
|
40 |
+ |
|
41 |
+ # Auto commit on |
|
42 |
+ $dbi->_auto_commit(1); |
|
43 |
+ |
|
44 |
+ if ($rollback_error) { |
|
45 |
+ # Rollback is failed |
|
46 |
+ croak("${transaction_error}Rollback is failed : $rollback_error"); |
|
47 |
+ } |
|
48 |
+ else { |
|
49 |
+ # Rollback is success |
|
50 |
+ croak("${transaction_error}Rollback is success"); |
|
51 |
+ } |
|
52 |
+ } |
|
53 |
+ # Tranzaction is success |
|
54 |
+ else { |
|
55 |
+ # Commit |
|
56 |
+ eval{$dbi->dbh->commit}; |
|
57 |
+ my $commit_error = $@; |
|
58 |
+ |
|
59 |
+ # Auto commit on |
|
60 |
+ $dbi->_auto_commit(1); |
|
61 |
+ |
|
62 |
+ # Commit is failed |
|
63 |
+ croak($commit_error) if $commit_error; |
|
64 |
+ } |
|
65 |
+} |
|
66 |
+ |
|
67 |
+1; |
|
68 |
+ |
|
69 |
+=head1 NAME |
|
70 |
+ |
|
71 |
+DBIx::Custom::TransactionScope - Transaction scope |
|
72 |
+ |
|
73 |
+=head1 SYNOPSYS |
|
74 |
+ |
|
75 |
+ use DBIx::Custom::SQLite; |
|
76 |
+ |
|
77 |
+ # New |
|
78 |
+ my $dbi = DBIx::Custom::SQLite->new(user => 'taro', $password => 'kliej&@K', |
|
79 |
+ database => 'sample'); |
|
80 |
+ |
|
81 |
+ # Connect memory database |
|
82 |
+ my $dbi->connect_memory; |
|
83 |
+ |
|
84 |
+=head1 ATTRIBUTES |
|
85 |
+ |
|
86 |
+=head2 dbi |
|
87 |
+ |
|
88 |
+=head1 METHODS |
|
89 |
+ |
|
90 |
+=head2 run |
|
91 |
+ |
|
92 |
+=head1 AUTHOR |
|
93 |
+ |
|
94 |
+Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
95 |
+ |
|
96 |
+Github L<http://github.com/yuki-kimoto> |
|
97 |
+ |
|
98 |
+I develope this module L<http://github.com/yuki-kimoto/DBIx-Custom> |
|
99 |
+ |
|
100 |
+=head1 Copyright & lisence |
|
101 |
+ |
|
102 |
+Copyright 2009 Yuki Kimoto, all rights reserved. |
|
103 |
+ |
|
104 |
+This program is free software; you can redistribute it and/or modify it |
|
105 |
+under the same terms as Perl itself. |
|
106 |
+ |
... | ... |
@@ -432,11 +432,10 @@ is_deeply($rows, [{key1 => 6, key2 => 6, key3 => 6, key4 => 6, key5 => 5}, |
432 | 432 |
{key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : update tag #update with table name dot"); |
433 | 433 |
|
434 | 434 |
|
435 |
-test 'run_tansaction'; |
|
435 |
+test 'transaction'; |
|
436 | 436 |
$dbi->do($DROP_TABLE->{0}); |
437 | 437 |
$dbi->do($CREATE_TABLE->{0}); |
438 |
-$dbi->run_transaction(sub { |
|
439 |
- my $dbi = shift; |
|
438 |
+$dbi->transaction->run(sub { |
|
440 | 439 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
441 | 440 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2}); |
442 | 441 |
$dbi->query($insert_tmpl, {key1 => 3, key2 => 4}); |
... | ... |
@@ -449,8 +448,7 @@ $dbi->do($DROP_TABLE->{0}); |
449 | 448 |
$dbi->do($CREATE_TABLE->{0}); |
450 | 449 |
$dbi->dbh->{RaiseError} = 0; |
451 | 450 |
eval{ |
452 |
- $dbi->run_transaction(sub { |
|
453 |
- my $dbi = shift; |
|
451 |
+ $dbi->transaction->run(sub { |
|
454 | 452 |
$insert_tmpl = 'insert into table1 {insert key1 key2}'; |
455 | 453 |
$dbi->query($insert_tmpl, {key1 => 1, key2 => 2}); |
456 | 454 |
die "Fatal Error"; |
... | ... |
@@ -464,9 +462,10 @@ $rows = $result->fetch_hash_all; |
464 | 462 |
is_deeply($rows, [], "$test : rollback"); |
465 | 463 |
|
466 | 464 |
|
465 |
+ |
|
467 | 466 |
test 'Error case'; |
468 | 467 |
$dbi = DBIx::Custom->new; |
469 |
-eval{$dbi->run_transaction}; |
|
468 |
+eval{$dbi->transaction->run}; |
|
470 | 469 |
like($@, qr/Not yet connect to database/, "$test : Yet Connected"); |
471 | 470 |
|
472 | 471 |
$dbi = DBIx::Custom->new(data_source => 'dbi:SQLit'); |
... | ... |
@@ -476,9 +475,9 @@ ok($@, "$test : connect error"); |
476 | 475 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
477 | 476 |
$dbi->connect; |
478 | 477 |
$dbi->dbh->{AutoCommit} = 0; |
479 |
-eval{$dbi->run_transaction()}; |
|
478 |
+eval{$dbi->transaction->run}; |
|
480 | 479 |
like($@, qr/AutoCommit must be true before transaction start/, |
481 |
- "$test : run_transaction auto commit is false"); |
|
480 |
+ "$test : transaction auto commit is false"); |
|
482 | 481 |
|
483 | 482 |
$dbi = DBIx::Custom->new($NEW_ARGS->{0}); |
484 | 483 |
$sql = 'laksjdf'; |