Showing 4 changed files with 45 additions and 59 deletions
+2
Changes
... ...
@@ -1,4 +1,6 @@
1 1
 0.1672
2
+    - removed EXPERIMENTAL Prefork server safe implementation, my implementation is very buggy.
3
+    - added EXPERIMETNAL connector() attribute.
2 4
     - change retern value to array refrence of EXPERIMENTAL replace()
3 5
 0,1671
4 6
     - added environment variable DBIX_CUSTOM_DEBUG
+34 -34
lib/DBIx/Custom.pm
... ...
@@ -24,7 +24,7 @@ use constant DEBUG => $ENV{DBIX_CUSTOM_DEBUG} || 0;
24 24
 our @COMMON_ARGS = qw/table query filter type/;
25 25
 
26 26
 __PACKAGE__->attr(
27
-    [qw/data_source password pid user/],
27
+    [qw/connector data_source password pid user/],
28 28
     cache => 0,
29 29
     cache_method => sub {
30 30
         sub {
... ...
@@ -74,7 +74,7 @@ sub AUTOLOAD {
74 74
     if (my $method = $self->{_methods}->{$mname}) {
75 75
         return $self->$method(@_)
76 76
     }
77
-    elsif (my $dbh_method = $self->dbh->can($mname)) {
77
+    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
78 78
         $self->dbh->$dbh_method(@_);
79 79
     }
80 80
     else {
... ...
@@ -166,11 +166,8 @@ sub column {
166 166
 sub connect {
167 167
     my $self = ref $_[0] ? shift : shift->new(@_);;
168 168
     
169
-    # Connect and get database handle
170
-    my $dbh = $self->_connect;
171
-    
172
-    # Set database handle
173
-    $self->dbh($dbh);
169
+    # Connect
170
+    $self->dbh;
174 171
     
175 172
     # Set process ID
176 173
     $self->pid($$);
... ...
@@ -240,30 +237,15 @@ sub create_query {
240 237
 sub dbh {
241 238
     my $self = shift;
242 239
     
243
-    # Set
244
-    if (@_) {
245
-        $self->{dbh} = $_[0];
246
-        return $self;
247
-    }
248
-    
249
-    # Get
250
-    else {
251
-        my $pid = $$;
252
-        
253
-        # Get database handle
254
-        if ($self->pid eq $pid) {
255
-            return $self->{dbh};
256
-        }
257
-        
258
-        # Create new database handle in child process
259
-        else {
260
-            croak "Process is forked in transaction"
261
-              unless $self->{dbh}->{AutoCommit};
262
-            $self->pid($pid);
263
-            $self->{dbh}->{InactiveDestroy} = 1;
264
-            return $self->{dbh} = $self->_connect;
265
-        }
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;
266 246
     }
247
+
248
+    return $self->{dbh} ||= $self->_connect;
267 249
 }
268 250
 
269 251
 our %DELETE_ARGS
... ...
@@ -1541,6 +1523,26 @@ L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki>
1541 1523
 
1542 1524
 =head1 ATTRIBUTES
1543 1525
 
1526
+=head2 C<connector> EXPERIMENTAL
1527
+
1528
+    my $connector = $dbi->connector;
1529
+    $dbi          = $dbi->connector(DBIx::Connector->new(...));
1530
+
1531
+Connection manager object. if connector is set, you can get C<dbh()>
1532
+from connection manager. conection manager object must have dbh() mehtod.
1533
+
1534
+This is L<DBIx::Connector> example. Please pass
1535
+C<default_dbi_option> to L<DBIx::Connector>.
1536
+
1537
+    my $connector = DBIx::Connector->new(
1538
+        "dbi:mysql:database=$DATABASE",
1539
+        $USER,
1540
+        $PASSWORD,
1541
+        DBIx::Custom->new->default_dbi_option
1542
+    );
1543
+    
1544
+    my $dbi = DBIx::Custom->new(connector => $connector);
1545
+
1544 1546
 =head2 C<data_source>
1545 1547
 
1546 1548
     my $data_source = $dbi->data_source;
... ...
@@ -1732,11 +1734,9 @@ instead of other methods, such as C<insert>, C<update>.
1732 1734
 =head2 C<dbh>
1733 1735
 
1734 1736
     my $dbh = $dbi->dbh;
1735
-    $dbi    = $dbi->dbh($dbh);
1736
-
1737
-Get and set database handle of L<DBI>.
1738 1737
 
1739
-If process is spawn by forking, new connection is created automatically.
1738
+Get L<DBI> database handle. if C<connector> is set, you can get
1739
+database handle from C<connector>.
1740 1740
 
1741 1741
 =head2 C<each_column>
1742 1742
 
+1 -1
lib/DBIx/Custom/Model.pm
... ...
@@ -36,7 +36,7 @@ sub AUTOLOAD {
36 36
     elsif (my $dbi_method = $self->dbi->can($mname)) {
37 37
         $self->dbi->$dbi_method(@_);
38 38
     }
39
-    elsif (my $dbh_method = $self->dbi->dbh->can($mname)) {
39
+    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
40 40
         $self->dbi->dbh->$dbh_method(@_);
41 41
     }
42 42
     else {
+8 -24
t/dbix-custom-mysql-private.t
... ...
@@ -97,12 +97,16 @@ is_deeply($rows, [{key1 => 1, key2 => 2}]);
97 97
 $dbi->delete_all(table => 'table1');
98 98
 
99 99
 test 'fork';
100
+use DBIx::Connector;
100 101
 {
101
-    $dbi = DBIx::Custom->connect(
102
-        data_source => "dbi:mysql:database=$DATABASE",
103
-        user => $USER,
104
-        password => $PASSWORD
102
+    my $connector = DBIx::Connector->new(
103
+        "dbi:mysql:database=$DATABASE",
104
+        $USER,
105
+        $PASSWORD,
106
+        DBIx::Custom->new->default_dbi_option
105 107
     );
108
+    
109
+    $dbi = DBIx::Custom->new(connector => $connector);
106 110
     $dbi->delete_all(table => 'table1');
107 111
     $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
108 112
     die "Can't fork" unless defined (my $pid = fork);
... ...
@@ -119,23 +123,3 @@ test 'fork';
119 123
     }
120 124
 }
121 125
 
122
-test 'fork in transaction';
123
-{
124
-    $dbi = DBIx::Custom->connect(
125
-        data_source => "dbi:mysql:database=$DATABASE",
126
-        user => $USER,
127
-        password => $PASSWORD
128
-    );
129
-    
130
-    $dbi->begin_work;
131
-    die "Can't fork" unless defined (my $pid = fork);
132
-    
133
-    if ($pid) {
134
-        # Parent
135
-    }
136
-    else {
137
-        # Child
138
-        eval {$dbi->select(table => 'table1') };
139
-        die "Not OK" unless $@ =~ /transaction/;
140
-    }
141
-}