X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FReplication.pm;h=d588373e5bad6076ee5bba270a84e84a38a6e4c8;hb=1c32fcf0404c47e7cd43de68c9a93792b0d1598a;hp=42c3170d7c7ea464fafec2ef5200b8817ce3fd46;hpb=dbc6d8547307886d46632f23c0c49078dfb6c231;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI/Replication.pm b/lib/DBIx/Class/Storage/DBI/Replication.pm index 42c3170..d588373 100644 --- a/lib/DBIx/Class/Storage/DBI/Replication.pm +++ b/lib/DBIx/Class/Storage/DBI/Replication.pm @@ -21,7 +21,8 @@ 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 - <...> + <...>, + { limit_dialect => 'LimitXY' } # If needed, see below ] ); =head1 DESCRIPTION @@ -34,6 +35,13 @@ For every slave database you can define a priority value, which controls data so L, 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. + =cut sub new { @@ -60,10 +68,24 @@ sub all_sources { sub connect_info { my( $self, $source_info ) = @_; - $self->write_source->connect_info( $source_info->[0] ); + my( $info, $global_options, $options, @dsns ); + + $info = [ @$source_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]}; + } + + # 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]; + $global_options->{dsns} = \@dsns; - my @dsns = map { ($_->[3]->{priority} || 10) => $_ } @{$source_info}[1..@$source_info-1]; - $self->read_source->connect_info( [ 'dbi:Multi:', undef, undef, { dsns => \@dsns } ] ); + $self->read_source->connect_info( [ 'dbi:Multi:', undef, undef, { %$global_options } ] ); } sub select {