X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FReplication.pm;h=cd13b935f1b373ddd3d17412ef40787afa1fecde;hb=cd6d847fc1c74901aa534def86cf10ac9b3adbe3;hp=76e86e29d12e555878a1c7567312ab25051a8e24;hpb=82aa2963a9fdea79abb2fb42cf970c4d3ee0f942;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/Storage/DBI/Replication.pm b/lib/DBIx/Class/Storage/DBI/Replication.pm index 76e86e2..cd13b93 100644 --- a/lib/DBIx/Class/Storage/DBI/Replication.pm +++ b/lib/DBIx/Class/Storage/DBI/Replication.pm @@ -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 @@ -27,20 +27,28 @@ DBIx::Class::Storage::DBI::Replication - Replicated database support =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, 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, 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 @@ -68,12 +76,25 @@ sub all_sources { sub connect_info { my( $self, $source_info ) = @_; - my $last_info = ref $source_info->[-1] eq 'HASH' ? pop( @$source_info ) : undef; + 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 } ] ); - $self->write_source->connect_info( $source_info->[0], $last_info ); + @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 } ], $last_info ); + $self->read_source->connect_info( [ 'dbi:Multi:', undef, undef, { %$global_options } ] ); } sub select { @@ -110,22 +131,12 @@ sub build_datetime_parser { shift->read_source->build_datetime_parser( @_ ); } -sub limit_dialect { - my $self = shift; - $self->$_->limit_dialect( @_ ) for( $self->all_sources ); -} -sub quote_char { - my $self = shift; - $self->$_->quote_char( @_ ) for( $self->all_sources ); -} -sub name_sep { - my $self = shift; - $self->$_->quote_char( @_ ) for( $self->all_sources ); -} -sub disconnect { - my $self = shift; - $self->$_->disconnect( @_ ) for( $self->all_sources ); -} +sub limit_dialect { $_->limit_dialect( @_ ) for( shift->all_sources ) } +sub quote_char { $_->quote_char( @_ ) for( shift->all_sources ) } +sub name_sep { $_->quote_char( @_ ) for( shift->all_sources ) } +sub disconnect { $_->disconnect( @_ ) for( shift->all_sources ) } +sub set_schema { $_->set_schema( @_ ) for( shift->all_sources ) } + sub DESTROY { my $self = shift;