querycounter role, test for that and a new schema hierarchy for additional Moose...
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Storage / DBI / Replication.pm
index bc3a881..6ec74e9 100644 (file)
@@ -11,7 +11,7 @@ __PACKAGE__->mk_accessors( qw/read_source write_source/ );
 
 =head1 NAME
 
-DBIx::Class::Storage::DBI::Replication - Replicated database support
+DBIx::Class::Storage::DBI::Replication - EXPERIMENTAL Replicated database support
 
 =head1 SYNOPSIS
 
@@ -21,26 +21,36 @@ DBIx::Class::Storage::DBI::Replication - Replicated database support
                     [ "dbi:mysql:database=test;hostname=master", "username", "password", { AutoCommit => 1 } ], # master
                     [ "dbi:mysql:database=test;hostname=slave1", "username", "password", { priority => 10 } ],  # slave1
                     [ "dbi:mysql:database=test;hostname=slave2", "username", "password", { priority => 10 } ],  # slave2
+                    [ $dbh, '','', {priority=>10}], # add in a preexisting database handle
+                    [ sub {  DBI->connect }, '', '', {priority=>10}], # DBD::Multi will call this coderef for connects
                     <...>,
                     { limit_dialect => 'LimitXY' } # If needed, see below
                    ] );
 
 =head1 DESCRIPTION
 
-This class implements replicated data store for DBI. Currently you can define one master and numerous slave database
-connections. All write-type queries (INSERT, UPDATE, DELETE and even LAST_INSERT_ID) are routed to master database,
-all read-type queries (SELECTs) go to the slave database.
+Warning: This class is marked EXPERIMENTAL. It works for the authors but does
+not currently have automated tests so your mileage may vary.
 
-For every slave database you can define a priority value, which controls data source usage pattern. It uses
-L<DBD::Multi>, so first the lower priority data sources used (if they have the same priority, the are used
-randomized), than if all low priority data sources fail, higher ones tried in order.
+This class implements replicated data store for DBI. Currently you can define
+one master and numerous slave database connections. All write-type queries
+(INSERT, UPDATE, DELETE and even LAST_INSERT_ID) are routed to master
+database, all read-type queries (SELECTs) go to the slave database.
+
+For every slave database you can define a priority value, which controls data
+source usage pattern. It uses L<DBD::Multi>, so first the lower priority data
+sources used (if they have the same priority, the are used randomized), than
+if all low priority data sources fail, higher ones tried in order.
 
 =head1 CONFIGURATION
 
 =head2 Limit dialect
 
-If you use LIMIT in your queries (effectively, if you use SQL::Abstract::Limit), do not forget to set up limit_dialect (perldoc SQL::Abstract::Limit) by passing it as an option in the (optional) hash reference to connect_info.
-DBIC can not set it up automatically, since it can not guess DBD::Multi connection types.
+If you use LIMIT in your queries (effectively, if you use
+SQL::Abstract::Limit), do not forget to set up limit_dialect (perldoc
+SQL::Abstract::Limit) by passing it as an option in the (optional) hash
+reference to connect_info.  DBIC can not set it up automatically, since it can
+not guess DBD::Multi connection types.
 
 =cut
 
@@ -75,14 +85,24 @@ sub connect_info {
     $global_options = ref $info->[-1] eq 'HASH' ? pop( @$info ) : {};
     if( ref( $options = $info->[0]->[-1] ) eq 'HASH' ) {
        # Local options present in dsn, merge them with global options
-       map { $global_options->{$_} = $options->{$_} } keys %$options;
-       pop @{$info->[0]};
+        map { $global_options->{$_} = $options->{$_} } keys %$options;
+        pop @{$info->[0]};
     }
 
-    # We need to copy-pass $global_options, since connect_info clears it while processing options
-    $self->write_source->connect_info( [ @{$info->[0]}, { %$global_options } ] );
-
-    @dsns = map { ($_->[3]->{priority} || 10) => $_ } @{$info}[1..@$info-1];
+    # We need to copy-pass $global_options, since connect_info clears it while
+    # processing options
+    $self->write_source->connect_info( @{$info->[0]}, { %$global_options } );
+
+       ## allow either a DSN string or an already connect $dbh.  Just remember if
+       ## you use the $dbh option then DBD::Multi has no idea how to reconnect in
+       ## the event of a failure.
+       
+    @dsns = map {
+        ## if the first element in the arrayhash is a ref, make that the value
+        my $db = ref $_->[0] ? $_->[0] : $_;
+        ($_->[3]->{priority} || 10) => $db;
+    } @{$info->[0]}[1..@{$info->[0]}-1];
+    
     $global_options->{dsns} = \@dsns;
 
     $self->read_source->connect_info( [ 'dbi:Multi:', undef, undef, { %$global_options } ] );