| ... | ... |
@@ -1,3 +1,5 @@ |
| 1 |
+0.1673 |
|
| 2 |
+ - fixed dbh() method bug. |
|
| 1 | 3 |
0.1672 |
| 2 | 4 |
- removed EXPERIMENTAL Prefork server safe implementation, my implementation is very buggy. |
| 3 | 5 |
- added EXPERIMETNAL connector() attribute. |
| ... | ... |
@@ -1,6 +1,6 @@ |
| 1 | 1 |
package DBIx::Custom; |
| 2 | 2 |
|
| 3 |
-our $VERSION = '0.1671'; |
|
| 3 |
+our $VERSION = '0.1673'; |
|
| 4 | 4 |
|
| 5 | 5 |
use 5.008001; |
| 6 | 6 |
use strict; |
| ... | ... |
@@ -237,15 +237,25 @@ sub create_query {
|
| 237 | 237 |
sub dbh {
|
| 238 | 238 |
my $self = shift; |
| 239 | 239 |
|
| 240 |
- # From Connction manager |
|
| 241 |
- if (my $connector = $self->connector) {
|
|
| 242 |
- croak "connector must have dbh() method" |
|
| 243 |
- unless ref $connector && $connector->can('dbh');
|
|
| 244 |
- |
|
| 245 |
- return $connector->dbh; |
|
| 240 |
+ # Set |
|
| 241 |
+ if (@_) {
|
|
| 242 |
+ $self->{dbh} = $_[0];
|
|
| 243 |
+ |
|
| 244 |
+ return $self; |
|
| 245 |
+ } |
|
| 246 |
+ |
|
| 247 |
+ # Get |
|
| 248 |
+ else {
|
|
| 249 |
+ # From Connction manager |
|
| 250 |
+ if (my $connector = $self->connector) {
|
|
| 251 |
+ croak "connector must have dbh() method" |
|
| 252 |
+ unless ref $connector && $connector->can('dbh');
|
|
| 253 |
+ |
|
| 254 |
+ return $self->{dbh} = $connector->dbh;
|
|
| 255 |
+ } |
|
| 256 |
+ |
|
| 257 |
+ return $self->{dbh} ||= $self->_connect;
|
|
| 246 | 258 |
} |
| 247 |
- |
|
| 248 |
- return $self->{dbh} ||= $self->_connect;
|
|
| 249 | 259 |
} |
| 250 | 260 |
|
| 251 | 261 |
our %DELETE_ARGS |
| ... | ... |
@@ -2,6 +2,8 @@ use Test::More; |
| 2 | 2 |
use strict; |
| 3 | 3 |
use warnings; |
| 4 | 4 |
|
| 5 |
+use DBIx::Connector; |
|
| 6 |
+ |
|
| 5 | 7 |
# user password database |
| 6 | 8 |
our ($USER, $PASSWORD, $DATABASE) = connect_info(); |
| 7 | 9 |
|
| ... | ... |
@@ -33,11 +35,6 @@ my $rows; |
| 33 | 35 |
my $result; |
| 34 | 36 |
|
| 35 | 37 |
# Constant varialbes for test |
| 36 |
-my $CREATE_TABLE = {
|
|
| 37 |
- 0 => 'create table table1 (key1 char(255), key2 char(255));' |
|
| 38 |
-}; |
|
| 39 |
- |
|
| 40 |
- |
|
| 41 | 38 |
use DBIx::Custom::MySQL; |
| 42 | 39 |
|
| 43 | 40 |
test 'connect'; |
| ... | ... |
@@ -96,8 +93,58 @@ $rows = $dbi->select( |
| 96 | 93 |
is_deeply($rows, [{key1 => 1, key2 => 2}]);
|
| 97 | 94 |
$dbi->delete_all(table => 'table1'); |
| 98 | 95 |
|
| 96 |
+test 'dbh'; |
|
| 97 |
+{
|
|
| 98 |
+ my $connector = DBIx::Connector->new( |
|
| 99 |
+ "dbi:mysql:database=$DATABASE", |
|
| 100 |
+ $USER, |
|
| 101 |
+ $PASSWORD, |
|
| 102 |
+ DBIx::Custom->new->default_dbi_option |
|
| 103 |
+ ); |
|
| 104 |
+ |
|
| 105 |
+ my $dbi = DBIx::Custom->connect(connector => $connector); |
|
| 106 |
+ $dbi->delete_all(table => 'table1'); |
|
| 107 |
+ $dbi->do('insert into table1 (key1, key2) values (1, 2)');
|
|
| 108 |
+ is($dbi->select(table => 'table1')->fetch_hash_first->{key1}, 1);
|
|
| 109 |
+ |
|
| 110 |
+ $dbi = DBIx::Custom->new; |
|
| 111 |
+ $dbi->dbh('a');
|
|
| 112 |
+ is($dbi->{dbh}, 'a');
|
|
| 113 |
+} |
|
| 114 |
+ |
|
| 115 |
+test 'transaction'; |
|
| 116 |
+test 'dbh'; |
|
| 117 |
+{
|
|
| 118 |
+ my $connector = DBIx::Connector->new( |
|
| 119 |
+ "dbi:mysql:database=$DATABASE", |
|
| 120 |
+ $USER, |
|
| 121 |
+ $PASSWORD, |
|
| 122 |
+ DBIx::Custom->new->default_dbi_option |
|
| 123 |
+ ); |
|
| 124 |
+ |
|
| 125 |
+ my $dbi = DBIx::Custom->connect(connector => $connector); |
|
| 126 |
+ $dbi->delete_all(table => 'table1'); |
|
| 127 |
+ |
|
| 128 |
+ $dbi->connector->txn(sub {
|
|
| 129 |
+ $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 130 |
+ $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 131 |
+ }); |
|
| 132 |
+ is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
| 133 |
+ [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}]);
|
|
| 134 |
+ |
|
| 135 |
+ $dbi->delete_all(table => 'table1'); |
|
| 136 |
+ eval {
|
|
| 137 |
+ $dbi->connector->txn(sub {
|
|
| 138 |
+ $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
|
| 139 |
+ die "Error"; |
|
| 140 |
+ $dbi->insert(table => 'table1', param => {key1 => 3, key2 => 4});
|
|
| 141 |
+ }); |
|
| 142 |
+ }; |
|
| 143 |
+ is_deeply($dbi->select(table => 'table1')->fetch_hash_all, |
|
| 144 |
+ []); |
|
| 145 |
+} |
|
| 146 |
+ |
|
| 99 | 147 |
test 'fork'; |
| 100 |
-use DBIx::Connector; |
|
| 101 | 148 |
{
|
| 102 | 149 |
my $connector = DBIx::Connector->new( |
| 103 | 150 |
"dbi:mysql:database=$DATABASE", |
| ... | ... |
@@ -106,7 +153,7 @@ use DBIx::Connector; |
| 106 | 153 |
DBIx::Custom->new->default_dbi_option |
| 107 | 154 |
); |
| 108 | 155 |
|
| 109 |
- $dbi = DBIx::Custom->new(connector => $connector); |
|
| 156 |
+ my $dbi = DBIx::Custom->new(connector => $connector); |
|
| 110 | 157 |
$dbi->delete_all(table => 'table1'); |
| 111 | 158 |
$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
|
| 112 | 159 |
die "Can't fork" unless defined (my $pid = fork); |