converted replicant to a role so that we can apply it after ensure_connected properly...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Replicated / Pool.pm
index 3bf1ac8..5bf61e6 100644 (file)
@@ -36,13 +36,12 @@ has 'replicant_type' => (
     is=>'ro',
     isa=>'ClassName',
     required=>1,
-    default=>'DBIx::Class::Storage::DBI::Replicated::Replicant',
+    default=>'DBIx::Class::Storage::DBI',
     handles=>{
        'create_replicant' => 'new',
     }, 
 );
 
-
 =head2 replicants
 
 A hashref of replicant, with the key being the dsn and the value returning the
@@ -85,7 +84,7 @@ removes the replicant under $key from the pool
 has 'replicants' => (
     is=>'rw',
     metaclass => 'Collection::Hash',
-    isa=>'HashRef[DBIx::Class::Storage::DBI::Replicated::Replicant]',
+    isa=>'HashRef[DBIx::Class::Storage::DBI]',
     default=>sub {{}},
     provides  => {
                'set' => 'set_replicant',
@@ -96,12 +95,11 @@ has 'replicants' => (
        },
 );
 
-
 =head1 METHODS
 
 This class defines the following methods.
 
-=head2 create_replicants (Array[$connect_info])
+=head2 connect_replicants ($schema, Array[$connect_info])
 
 Given an array of $dsn suitable for connected to a database, create an
 L<DBIx::Class::Storage::DBI::Replicated::Replicant> object and store it in the
@@ -109,14 +107,21 @@ L</replicants> attribute.
 
 =cut
 
-sub create_replicants {
+use Data::Dump qw/dump/; 
+
+sub connect_replicants {
        my $self = shift @_;
+       my $schema = shift @_;
        
        my @newly_created = ();
        foreach my $connect_info (@_) {
-               my $replicant = $self->create_replicant;
-               $replicant->connect_info($connect_info);
+               
+               my $replicant = $self->create_replicant($schema);
+               $replicant->connect_info($connect_info);        
                $replicant->ensure_connected;
+               
+               DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
+               
                my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/);
                $self->set_replicant( $key => $replicant);      
                push @newly_created, $replicant;
@@ -125,7 +130,6 @@ sub create_replicants {
        return @newly_created;
 }
 
-
 =head2 connected_replicants
 
 Returns true if there are connected replicants.  Actually is overloaded to
@@ -149,6 +153,21 @@ sub connected_replicants {
        } $self->all_replicants );
 }
 
+=head2 active_replicants
+
+This is an array of replicants that are considered to be active in the pool.
+This does not check to see if they are connected, but if they are not, DBIC
+should automatically reconnect them for us when we hit them with a query.
+
+=cut
+
+sub active_replicants {
+    my $self = shift @_;
+    return ( grep {$_} map {
+        $_->active ? $_:0
+    } $self->all_replicants );
+}
+
 =head2 all_replicants
 
 Just a simple array of all the replicant storages.  No particular order to the
@@ -161,7 +180,6 @@ sub all_replicants {
        return values %{$self->replicants};
 }
 
-
 =head1 AUTHOR
 
 John Napiorkowski <john.napiorkowski@takkle.com>
@@ -172,5 +190,4 @@ You may distribute this code under the same terms as Perl itself.
 
 =cut
 
-
-1;
\ No newline at end of file
+1;