| 1 | package DBIx::Class::Schema::Slave; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base qw/ DBIx::Class /; | ||
| 6 | use Clone qw//; | ||
| 7 | |||
| 8 | our $VERSION = '0.02400'; | ||
| 9 | |||
| 10 | __PACKAGE__->mk_classdata( slave_moniker => '::Slave' ); | ||
| 11 | __PACKAGE__->mk_classdata('slave_schema'); | ||
| 12 | __PACKAGE__->mk_classdata('slave_connect_info' => [] ); | ||
| 13 | |||
| 14 | ## TODO remove next major release | ||
| 15 | sub slave_connection { | ||
| 16 | my $self = shift; | ||
| 17 | warn "DBIx::Class::Schema::Slave::slave_connection is changed to " . | ||
| 18 | "DBIx::Clas::Schema::Slave::slave_schema. " . | ||
| 19 | "This message will be removed next major release."; | ||
| 20 | return $self->slave_schema; | ||
| 21 | } | ||
| 22 | |||
| 23 | ## TODO remove next major release | ||
| 24 | sub connect_slave { | ||
| 25 | my $self = shift; | ||
| 26 | warn "DBIx::Class::Schema::Slave::connect_slave was changed to " . | ||
| 27 | "DBIx::Class::Schema::Slave::slave_connect. " . | ||
| 28 | "This message will be removed next major release."; | ||
| 29 | return $self->slave_connect( @_ ); | ||
| 30 | } | ||
| 31 | |||
| 32 | =head1 NAME | ||
| 33 | |||
| 34 | DBIx::Class::Schema::Slave - L<DBIx::Class::Schema> for slave B<(EXPERIMENTAL)> | ||
| 35 | |||
| 36 | =head1 CAUTION | ||
| 37 | |||
| 38 | DIBx::Class::Schema::Slave is B<EXPERIMENTAL> and B<DO NOT> use. | ||
| 39 | Please check L<DBIx::Class::Storage::DBI::Replicated> or L<DBIx::Class::Storage::DBI::Replication>. | ||
| 40 | DBIx::Class::Schema::Slave will be deleted. | ||
| 41 | |||
| 42 | =head1 SYNOPSIS | ||
| 43 | |||
| 44 | # In your MyApp::Schema (DBIx::Class::Schema based) | ||
| 45 | package MyApp::Schema; | ||
| 46 | |||
| 47 | use base 'DBIx::Class::Schema'; | ||
| 48 | |||
| 49 | __PACKAGE__->load_components( qw/ Schema::Slave / ); | ||
| 50 | __PACKAGE__->slave_moniker('::Slave'); | ||
| 51 | __PACKAGE__->slave_connect_info( [ | ||
| 52 | [ 'dbi:mysql:database:hostname=host', 'username', 'passsword', { ... } ], | ||
| 53 | [ 'dbi:mysql:database:hostname=host', 'username', 'passsword', { ... } ], | ||
| 54 | [ 'dbi:mysql:database:hostname=host', 'username', 'passsword', { ... } ], | ||
| 55 | ..., | ||
| 56 | ] ); | ||
| 57 | |||
| 58 | # As it is now, DBIx::Class::Schema::Slave works out with DBIx::Class::Schema::Loader. | ||
| 59 | # If you use DBIx::Class::Schema::Loader based MyApp::Schema, maybe it is just like below. | ||
| 60 | |||
| 61 | # In your MyApp::Schema (DBIx::Class::Schema::Loader based) | ||
| 62 | package MyApp::Schema; | ||
| 63 | |||
| 64 | use base 'DBIx::Class::Schema::Loader'; | ||
| 65 | |||
| 66 | __PACKAGE__->load_components( qw/ Schema::Slave / ); | ||
| 67 | __PACKAGE__->slave_moniker('::Slave'); | ||
| 68 | __PACKAGE__->slave_connect_info( [ | ||
| 69 | [ 'dbi:mysql:database:hostname=host', 'username', 'passsword', { ... } ], | ||
| 70 | [ 'dbi:mysql:database:hostname=host', 'username', 'passsword', { ... } ], | ||
| 71 | [ 'dbi:mysql:database:hostname=host', 'username', 'passsword', { ... } ], | ||
| 72 | ..., | ||
| 73 | ] ); | ||
| 74 | __PACKAGE__->loader_options( | ||
| 75 | relationships => 1, | ||
| 76 | components => [ qw/ | ||
| 77 | ... | ||
| 78 | ... | ||
| 79 | ... | ||
| 80 | Row::Slave # DO NOT forget to specify | ||
| 81 | Core | ||
| 82 | / ], | ||
| 83 | ); | ||
| 84 | |||
| 85 | # Somewhere in your code | ||
| 86 | use MyApp::Schema; | ||
| 87 | |||
| 88 | # First, connect to master | ||
| 89 | my $schema = MyApp::Schema->connect( @master_connect_info ); | ||
| 90 | |||
| 91 | # Retrieving from master | ||
| 92 | my $master = $schema->resultset('Track')->find( $id ); | ||
| 93 | |||
| 94 | # Retrieving from slave | ||
| 95 | my $slave = $schema->resultset('Track::Slave')->find( $id ); | ||
| 96 | |||
| 97 | See L<DBIx::Class::Schema>. | ||
| 98 | |||
| 99 | =head1 DESCRIPTION | ||
| 100 | |||
| 101 | DBIx::Class::Schema::Slave is L<DBIx::Class::Schema> for slave. | ||
| 102 | DBIx::Class::Schema::Slave creates C<result_source> classes for slave automatically, | ||
| 103 | and connects slave datasources as you like (or at rondom). | ||
| 104 | You can retrieve rows from either master or slave in the same way L<DBIx::Class::Schema> provies | ||
| 105 | but you can neither add nor remove rows from slave. | ||
| 106 | |||
| 107 | =head1 SETTIN UP DBIx::Class::Schema::Slave | ||
| 108 | |||
| 109 | =head2 Setting it up manually | ||
| 110 | |||
| 111 | First, load DBIx::Class::Schema::Slave as component in your MyApp::Schema. | ||
| 112 | |||
| 113 | # In your MyApp::Schema | ||
| 114 | package MyApp::Schema; | ||
| 115 | |||
| 116 | use base 'DBIx::Class::Schema'; | ||
| 117 | |||
| 118 | __PACKAGE__->load_components( qw/ Schema::Slave / ); | ||
| 119 | |||
| 120 | Set L</slave_moniker> as you like. If you do not specify, C<::Slave> is set. | ||
| 121 | |||
| 122 | __PACKAGE__->slave_moniker('::Slave'); | ||
| 123 | |||
| 124 | Set L</slave_connect_info> as C<ARRAYREF> of C<ARRAYREF>. | ||
| 125 | |||
| 126 | __PACKAGE__->slave_connect_info( [ | ||
| 127 | [ 'dbi:mysql:database:hostname=host', 'user', 'passsword', { ... } ], | ||
| 128 | [ 'dbi:mysql:database:hostname=host', 'user', 'passsword', { ... } ], | ||
| 129 | [ 'dbi:mysql:database:hostname=host', 'user', 'passsword', { ... } ], | ||
| 130 | ..., | ||
| 131 | ] ); | ||
| 132 | |||
| 133 | Next, you have MyApp::Schema::Artist, MyApp::Schema::Album, MyApp::Schema::Track, load these C<result_source> classes. | ||
| 134 | |||
| 135 | __PACKAGE__->load_classes( qw/ Artist Album Track / ); | ||
| 136 | |||
| 137 | In running L</register_source>, DBIx::Class::Schema::Slave creates slave C<result_source> classes | ||
| 138 | MyApp::Schema::Artist::Slave, MyApp::Schema::Album::Slave and MyApp::Schema::Track::Slave automatically. | ||
| 139 | If you set C<::MySlave> to L</slave_moniker>, it creates | ||
| 140 | MyApp::Schema::Artist::MySlave, MyApp::Schema::Album::MySlave and MyApp::Schema::Track::MySlave. | ||
| 141 | |||
| 142 | # MyApp::Schema::Artist wouldn't be loaded | ||
| 143 | # MyApp::Schema::Artist::Slave wouldn't be created | ||
| 144 | __PACKAGE__->load_classes( qw/ Album Track / ); | ||
| 145 | |||
| 146 | I recommend every C<result_source> classes to be loaded. | ||
| 147 | |||
| 148 | # Every result_source classes are loaded | ||
| 149 | __PACKAGE__->load_classes; | ||
| 150 | |||
| 151 | Next, load L<DBIx::Class::Row::Slave> as component in your C<result_source> classes. | ||
| 152 | |||
| 153 | # In your MyApp::Schema::Artist; | ||
| 154 | package MyApp::Schema::Artist; | ||
| 155 | |||
| 156 | use base 'DBIx::Class'; | ||
| 157 | |||
| 158 | __PACKEAGE__->load_components( qw/ ... Row::Slave Core / ); | ||
| 159 | |||
| 160 | =head2 Using L<DBIx::Class::Schema::Loader> | ||
| 161 | |||
| 162 | As it is now, DBIx::Class::Schema::Slave B<WORKS OUT> with L<DBIx::Class::Schema::Loader>. | ||
| 163 | First, load DBIx::Class::Schema::Slave as component in your MyApp::Schema. | ||
| 164 | |||
| 165 | # In your MyApp::Schema | ||
| 166 | package MyApp::Schema; | ||
| 167 | |||
| 168 | use base 'DBIx::Class::Schema::Loader'; | ||
| 169 | |||
| 170 | __PACKAGE__->load_components( qw/ Schema::Slave / ); | ||
| 171 | |||
| 172 | Set L</slave_moniker> as you like. If you do not specify, C<::Slave> is set. | ||
| 173 | |||
| 174 | __PACKAGE__->slave_moniker('::Slave'); | ||
| 175 | |||
| 176 | Set L</slave_connect_info> as C<ARRAYREF> of C<ARRAYREF>. | ||
| 177 | |||
| 178 | __PACKAGE__->slave_connect_info( [ | ||
| 179 | [ 'dbi:mysql:database:hostname=host', 'user', 'passsword', { ... } ], | ||
| 180 | [ 'dbi:mysql:database:hostname=host', 'user', 'passsword', { ... } ], | ||
| 181 | [ 'dbi:mysql:database:hostname=host', 'user', 'passsword', { ... } ], | ||
| 182 | ..., | ||
| 183 | ] ); | ||
| 184 | |||
| 185 | Call L<DBIx::Class::Schema::Loader/loader_options>. B<DO NOT> forget to specify L<DBIx::Class::Row::Slave> as component. | ||
| 186 | |||
| 187 | __PACKAGE__->loader_options( | ||
| 188 | relationships => 1, | ||
| 189 | components => [ qw/ | ||
| 190 | ... | ||
| 191 | Row::Slave # DO NOT forget to load | ||
| 192 | Core | ||
| 193 | / ], | ||
| 194 | ); | ||
| 195 | |||
| 196 | =head2 Connecting (Create Schema instance) | ||
| 197 | |||
| 198 | To connect your Schema, provive C<connect_info> not for slave but for master. | ||
| 199 | |||
| 200 | my $schema = MyApp::Schema->connect( @master_connect_info ); | ||
| 201 | |||
| 202 | =head2 Retrieving | ||
| 203 | |||
| 204 | Retrieving from master, you don't have to care about anything. | ||
| 205 | |||
| 206 | my $album_master = $schema->resultset('Album')->find( $id ); | ||
| 207 | my $itr_album_master = $schema->resultset('Album')->search( { ... }, { ... } ); | ||
| 208 | |||
| 209 | Retrieving from slave, set slave moniker to L</resultset>. | ||
| 210 | |||
| 211 | my $track_slave = $schema->resultset('Album::Slave')->find( $id ); | ||
| 212 | my $itr_track_slave = $schema->resultset('Album::Slave')->search( { ... }, { ... } ); | ||
| 213 | |||
| 214 | =head2 Adding and removing rows | ||
| 215 | |||
| 216 | You can either create a new row or remove some rows from master. But you can neither create a new row nor remove some rows from slave. | ||
| 217 | |||
| 218 | # These complete normally | ||
| 219 | my $track = $schema->resultset('Track')->create( { | ||
| 220 | created_on => $dt->now || undef, | ||
| 221 | modified_on => $dt->now || undef, | ||
| 222 | album_id => $album->id || undef, | ||
| 223 | title => $title || undef, | ||
| 224 | time => $time || undef, | ||
| 225 | } ); | ||
| 226 | $track->title('WORLD\'S END SUPERNOVA'); | ||
| 227 | $track->update; | ||
| 228 | $track->delete; | ||
| 229 | |||
| 230 | # You got an error! | ||
| 231 | # DBIx::Class::ResultSet::create(): Can't insert via result source "Track::Slave". This is slave connection. | ||
| 232 | my $track = $schema->resultset('Track::Slave')->create( { | ||
| 233 | created_on => $dt->now || undef, | ||
| 234 | modified_on => $dt->now || undef, | ||
| 235 | album_id => $album->id || undef, | ||
| 236 | title => $title || undef, | ||
| 237 | time => $time || undef, | ||
| 238 | } ); | ||
| 239 | |||
| 240 | $track->title('TEAM ROCK'); | ||
| 241 | # You got an error! | ||
| 242 | # DBIx::Class::Row::Slave::update(): Can't update via result source "Track::Slave". This is slave connection. | ||
| 243 | $track->update; | ||
| 244 | |||
| 245 | # And, you got an error! | ||
| 246 | # DBIx::Class::Row::Slave::delete(): Can't delete via result source "Track::Slave". This is slave connection. | ||
| 247 | $track->delete; | ||
| 248 | |||
| 249 | B<DO NOT> call L<DBIx::Class::ResultSet/"update_all">, L<DBIx::Class::ResultSet/"delete_all">, L<DBIx::Class::ResultSet/"populate"> and L<DBIx::Class::ResultSet/"update_or_create"> via slave C<result_source>s. | ||
| 250 | Also you B<SHOULD NOT> call L<DBIx::Class::ResultSet/"find_or_new">, L<DBIx::Class::ResultSet/"find_or_create"> via slave C<result_source>s. | ||
| 251 | |||
| 252 | =head1 CLASS DATA | ||
| 253 | |||
| 254 | =head2 slave_moniker | ||
| 255 | |||
| 256 | Moniker suffix for slave. C<::Slave> default. | ||
| 257 | |||
| 258 | # In your MyApp::Schema | ||
| 259 | __PACKAGE__->slave_moniker('::Slave'); | ||
| 260 | |||
| 261 | B<IMPORTANT:> | ||
| 262 | If you have already MyApp::Schema::Artist::Slave, B<DO NOT> set C<::Slave> to C<slave_moniker>. | ||
| 263 | Set C<::SlaveFor> or something else. | ||
| 264 | |||
| 265 | =head2 slave_connect_info | ||
| 266 | |||
| 267 | C<connect_info>s C<ARRAYREF> of C<ARRAYREF> for slave. | ||
| 268 | |||
| 269 | # In your MyApp::Schema | ||
| 270 | __PACKAGE__->slave_connect_info( [ | ||
| 271 | [ 'dbi:mysql:database:hostname=host', 'username', 'passsword', { ... } ], | ||
| 272 | [ 'dbi:mysql:database:hostname=host', 'username', 'passsword', { ... } ], | ||
| 273 | [ 'dbi:mysql:database:hostname=host', 'username', 'passsword', { ... } ], | ||
| 274 | ..., | ||
| 275 | ] ); | ||
| 276 | |||
| 277 | =head2 slave_schema | ||
| 278 | |||
| 279 | Schema for slave. You can get this by L</slave>. | ||
| 280 | |||
| 281 | =head1 METHODS | ||
| 282 | |||
| 283 | =head2 register_source | ||
| 284 | |||
| 285 | =over 4 | ||
| 286 | |||
| 287 | =item Arguments: $moniker, $result_source | ||
| 288 | |||
| 289 | =item Return Value: none | ||
| 290 | |||
| 291 | =back | ||
| 292 | |||
| 293 | Registers the L<DBIx::Class::ResultSource> in the schema with the given moniker | ||
| 294 | and re-maps C<class_mappings> and C<source_registrations>. | ||
| 295 | |||
| 296 | # Re-mapped class_mappings | ||
| 297 | class_mappings => { | ||
| 298 | MyApp::Schema::Artist => 'Artist', | ||
| 299 | MyApp::Schema::Artist::Slave => 'Artist::Slave', | ||
| 300 | MyApp::Schema::Album => 'Album', | ||
| 301 | MyApp::Schema::Album::Slave => 'Album::Slave', | ||
| 302 | MyApp::Schema::Track => 'Track', | ||
| 303 | MyApp::Schema::Track::Slave => 'Track::Slave', | ||
| 304 | } | ||
| 305 | |||
| 306 | # Re-mapped source_registrations | ||
| 307 | source_registrations => { | ||
| 308 | MyApp::Schema::Artist => { | ||
| 309 | bless( { | ||
| 310 | ..., | ||
| 311 | ..., | ||
| 312 | ..., | ||
| 313 | }, DBIx::Class::ResultSource::Table ) | ||
| 314 | }, | ||
| 315 | MyApp::Schema::Artist::Slave => { | ||
| 316 | bless( { | ||
| 317 | ..., | ||
| 318 | ..., | ||
| 319 | ..., | ||
| 320 | }, DBIx::Class::ResultSource::Table ) | ||
| 321 | }, | ||
| 322 | ..., | ||
| 323 | ..., | ||
| 324 | ..., | ||
| 325 | MyApp::Schema::Track::Slave => { | ||
| 326 | bless( { | ||
| 327 | ..., | ||
| 328 | ..., | ||
| 329 | ..., | ||
| 330 | }, DBIx::Class::ResultSource::Table ) | ||
| 331 | }, | ||
| 332 | } | ||
| 333 | |||
| 334 | See L<DBIx::Class::Schema/"register_source">. | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | sub register_source { | ||
| 339 | my ( $self, $moniker, $source ) = @_; | ||
| 340 | |||
| 341 | unless ( $self->is_slave( $moniker ) ) { | ||
| 342 | my $s_moniker = $moniker . $self->slave_moniker; | ||
| 343 | my $s_source = $source->new( $self->_clone_source( $source ) ); | ||
| 344 | $self->next::method( $s_moniker, $s_source ); | ||
| 345 | } | ||
| 346 | |||
| 347 | $self->next::method( $moniker, $source ); | ||
| 348 | } | ||
| 349 | |||
| 350 | sub _clone_source { | ||
| 351 | my ( $self, $source ) = @_; | ||
| 352 | |||
| 353 | my $s_moniker = $self->slave_moniker; | ||
| 354 | my $c_source = Clone::clone( $source ); | ||
| 355 | $self->_slave_relationships( $c_source ); | ||
| 356 | |||
| 357 | if ( ref $self ) { | ||
| 358 | no strict 'refs'; | ||
| 359 | no warnings 'redefine'; | ||
| 360 | local *Class::C3::reinitialize = sub {}; | ||
| 361 | my $s_result_class = $c_source->result_class . $s_moniker; | ||
| 362 | ## Set VERSION to create pseudo namespace. | ||
| 363 | local ${"${s_result_class}::VERSION"} ||= 1; | ||
| 364 | $self->inject_base( $s_result_class => $source->result_class ); | ||
| 365 | $c_source->result_class( $s_result_class ); | ||
| 366 | } | ||
| 367 | |||
| 368 | return $c_source; | ||
| 369 | } | ||
| 370 | |||
| 371 | sub _slave_relationships { | ||
| 372 | my ( $self, $source ) = @_; | ||
| 373 | |||
| 374 | my $s_moniker = $self->slave_moniker; | ||
| 375 | my %rels = %{$source->_relationships}; | ||
| 376 | return unless %rels; | ||
| 377 | |||
| 378 | foreach my $rel ( keys %rels ) { | ||
| 379 | $rels{$rel}->{source} = $rels{$rel}->{source} . $s_moniker | ||
| 380 | unless $self->is_slave( $rels{$rel}->{source} ); | ||
| 381 | $rels{$rel}->{class} = $rels{$rel}->{class} . $s_moniker | ||
| 382 | unless $self->is_slave( $rels{$rel}->{class} ); | ||
| 383 | } | ||
| 384 | |||
| 385 | $source->_relationships( \%rels ); | ||
| 386 | } | ||
| 387 | |||
| 388 | =head2 resultset | ||
| 389 | |||
| 390 | =over 4 | ||
| 391 | |||
| 392 | =item Arguments: $moniker | ||
| 393 | |||
| 394 | =item Return Value: $result_set | ||
| 395 | |||
| 396 | =back | ||
| 397 | |||
| 398 | If C<$moniker> is slave moniker, this method returns C<$result_set> for slave. | ||
| 399 | See L<DBIx::Class::Schema/"resultset">. | ||
| 400 | |||
| 401 | my $master_rs = $schema->resultset('Artist'); | ||
| 402 | my $slave_rs = $schema->resultset('Artist::Slave'); | ||
| 403 | |||
| 404 | =cut | ||
| 405 | |||
| 406 | sub resultset { | ||
| 407 | my ( $self, $moniker ) = @_; | ||
| 408 | |||
| 409 | if ( $self->is_slave( $moniker ) ) { | ||
| 410 | ## connect slave | ||
| 411 | if ( $self->slave ) { | ||
| 412 | ## TODO re-select per not ->resultset('Foo::Slave'), but request. | ||
| 413 | $self->slave->storage->connect_info( $self->_select_connect_info ); | ||
| 414 | } else { | ||
| 415 | $self->slave_connect( @{$self->_select_connect_info} ); | ||
| 416 | } | ||
| 417 | ## TODO more tidily | ||
| 418 | $self->slave->storage->debug( $self->storage->debug ); | ||
| 419 | $self->slave->storage->debugobj( $self->storage->debugobj ); | ||
| 420 | $self->slave->next::method( $moniker ); | ||
| 421 | } else { | ||
| 422 | ## connect master | ||
| 423 | $self->next::method( $moniker ); | ||
| 424 | } | ||
| 425 | } | ||
| 426 | |||
| 427 | =head2 sources | ||
| 428 | |||
| 429 | =over 4 | ||
| 430 | |||
| 431 | =item Argunemts: none | ||
| 432 | |||
| 433 | =item Return Value: @sources | ||
| 434 | |||
| 435 | =back | ||
| 436 | |||
| 437 | This method returns the sorted alphabetically source monikers of all source registrations on this schema. | ||
| 438 | See L<DBIx::Class::Schema/"sources">. | ||
| 439 | |||
| 440 | # Returns all sources including slave sources | ||
| 441 | my @all_sources = $schema->sources; | ||
| 442 | |||
| 443 | =cut | ||
| 444 | |||
| 445 | sub sources { | ||
| 446 | my $self = shift; | ||
| 447 | |||
| 448 | $self->next::method( @_ ); | ||
| 449 | return sort( { $b cmp $a } keys( %{$self->source_registrations} ) ); | ||
| 450 | } | ||
| 451 | |||
| 452 | =head2 master_sources | ||
| 453 | |||
| 454 | =over 4 | ||
| 455 | |||
| 456 | =item Argunemts: none | ||
| 457 | |||
| 458 | =item Return Value: @sources | ||
| 459 | |||
| 460 | =back | ||
| 461 | |||
| 462 | This method returns the sorted alphabetically master source monikers of all source registrations on this schema. | ||
| 463 | |||
| 464 | my @master_sources = $schema->master_sources; | ||
| 465 | |||
| 466 | =cut | ||
| 467 | |||
| 468 | sub master_sources { grep { !$_[0]->is_slave( $_ ) } $_[0]->sources } | ||
| 469 | |||
| 470 | =head2 slave_sources | ||
| 471 | |||
| 472 | =over 4 | ||
| 473 | |||
| 474 | =item Argunemts: none | ||
| 475 | |||
| 476 | =item Return Value: @sources | ||
| 477 | |||
| 478 | =back | ||
| 479 | |||
| 480 | This method returns the sorted alphabetically slave source monikers of all source registrations on this schema. | ||
| 481 | |||
| 482 | my @slave_sources = $schema->slave_sources; | ||
| 483 | |||
| 484 | =cut | ||
| 485 | |||
| 486 | sub slave_sources { grep { $_[0]->is_slave( $_ ) } $_[0]->sources } | ||
| 487 | |||
| 488 | =head2 slave_connect | ||
| 489 | |||
| 490 | =over 4 | ||
| 491 | |||
| 492 | =item Arguments: @info | ||
| 493 | |||
| 494 | =item Return Value: $slave_schema | ||
| 495 | |||
| 496 | =back | ||
| 497 | |||
| 498 | This method creates slave connection, and store it in C<slave_schema>. You can get this by L</slave>. | ||
| 499 | Usualy, you don't have to call it directry. | ||
| 500 | |||
| 501 | =cut | ||
| 502 | |||
| 503 | sub slave_connect { $_[0]->slave_schema( shift->connect( @_ ) ) } | ||
| 504 | |||
| 505 | =head2 slave | ||
| 506 | |||
| 507 | Getter for L</slave_schema>. You can get schema for slave if it stored in L</slave_schema>. | ||
| 508 | |||
| 509 | my $slave_schema = $schema->slave; | ||
| 510 | |||
| 511 | =cut | ||
| 512 | |||
| 513 | sub slave { shift->slave_connection } | ||
| 514 | |||
| 515 | =head2 select_connect_info | ||
| 516 | |||
| 517 | =over 4 | ||
| 518 | |||
| 519 | =item Argunemts: none | ||
| 520 | |||
| 521 | =item Return Value: $connect_info | ||
| 522 | |||
| 523 | =back | ||
| 524 | |||
| 525 | You can define this method in your schema class as you like. This method has to return C<$connect_info> as C<ARRAYREF>. | ||
| 526 | If L</select_connect_info> returns C<undef>, undef value or not C<ARRAYREF>, L</_select_connect_info> will be called, | ||
| 527 | and return C<$connect_info> at random from L</slave_connect_info>. | ||
| 528 | |||
| 529 | # In your MyApp::Schame | ||
| 530 | sub select_connect_info { | ||
| 531 | my $self = shift; | ||
| 532 | |||
| 533 | my @connect_info = @{$self->slave_connect_info}; | ||
| 534 | my $connect_info; | ||
| 535 | # Some algorithm to select connect_info here | ||
| 536 | |||
| 537 | return $connect_info; | ||
| 538 | } | ||
| 539 | |||
| 540 | =cut | ||
| 541 | |||
| 542 | sub select_connect_info {} | ||
| 543 | |||
| 544 | =head2 is_slave | ||
| 545 | |||
| 546 | =over 4 | ||
| 547 | |||
| 548 | =item Arguments: $string | ||
| 549 | |||
| 550 | =item Return Value: 1 or 0 | ||
| 551 | |||
| 552 | =back | ||
| 553 | |||
| 554 | This method returns 1 if C<$string> (moniker, class name and so on) is slave stuff, otherwise returns 0. | ||
| 555 | |||
| 556 | __PACKAGE__->slave_moniker('::Slave'); | ||
| 557 | |||
| 558 | # Returns 0 | ||
| 559 | $self->is_slave('Artist'); | ||
| 560 | |||
| 561 | # Returns 1 | ||
| 562 | $self->is_slave('Artist::Slave'); | ||
| 563 | |||
| 564 | # Returns 1 | ||
| 565 | $self->is_slave('MyApp::Model::DBIC::MyApp::Artist::Slave'); | ||
| 566 | |||
| 567 | __PACKAGE__->slave_moniker('::SlaveFor'); | ||
| 568 | |||
| 569 | # Returns 0 | ||
| 570 | $self->is_slave('Artist'); | ||
| 571 | |||
| 572 | # Returns 1 | ||
| 573 | $self->is_slave('Artist::SlaveFor'); | ||
| 574 | |||
| 575 | # Returns 1 | ||
| 576 | $self->is_slave('MyApp::Model::DBIC::MyApp::Artist::SlaveFor'); | ||
| 577 | |||
| 578 | =cut | ||
| 579 | |||
| 580 | sub is_slave { | ||
| 581 | my ( $self, $string ) = @_; | ||
| 582 | |||
| 583 | my $match = $self->slave_moniker; | ||
| 584 | return $string =~ m/$match$/o ? 1 : 0; | ||
| 585 | } | ||
| 586 | |||
| 587 | =head1 INTERNAL METHOD | ||
| 588 | |||
| 589 | =head2 _select_connect_info | ||
| 590 | |||
| 591 | =over 4 | ||
| 592 | |||
| 593 | =item Return Value: $connect_info | ||
| 594 | |||
| 595 | =back | ||
| 596 | |||
| 597 | Internal method. This method returns C<$connect_info> for slave as C<ARRAYREF>. | ||
| 598 | Usually, you don't have to call it directry. | ||
| 599 | If you select C<$connect_info> as you like, define L</select_connect_info> in your schema class. | ||
| 600 | See L</select_connect_info> for more information. | ||
| 601 | |||
| 602 | =cut | ||
| 603 | |||
| 604 | sub _select_connect_info { | ||
| 605 | my $self = shift; | ||
| 606 | |||
| 607 | my $info = ( $self->can('select_connect_info') | ||
| 608 | && $self->select_connect_info | ||
| 609 | && ref $self->select_connect_info eq 'ARRAY' ) | ||
| 610 | ? $self->select_connect_info | ||
| 611 | : $self->slave_connect_info->[ rand @{$self->slave_connect_info} ]; | ||
| 612 | |||
| 613 | # warn "Select slave_connect_info $info"; | ||
| 614 | return $info; | ||
| 615 | } | ||
| 616 | |||
| 617 | # sub _dbd_multi { | ||
| 618 | # my $self = shift; | ||
| 619 | |||
| 620 | # my @connect_info = @{$self->slave_connect_info}; | ||
| 621 | # my $dbd_multi_opt = ref $connect_info[-1] eq 'HASH' ? | ||
| 622 | # pop @connect_info : {}; | ||
| 623 | # $dbd_multi_opt->{limit_dialect} = $self->storage->sql_maker->limit_dialect | ||
| 624 | # unless defined $dbd_multi_opt->{limit_dialect}; | ||
| 625 | |||
| 626 | # @connect_info = map { | ||
| 627 | # my $dsn = ref $_->[0] ? $_->[0] : $_; | ||
| 628 | # my $pri = ( ref $_->[-1] && $_->[-1]->{priority} ) ? | ||
| 629 | # $_->[-1]->{priority} : 10; | ||
| 630 | # $pri => $dsn; | ||
| 631 | # } @connect_info; | ||
| 632 | |||
| 633 | # return [ 'dbi:Multi:', undef, undef, | ||
| 634 | # { dsns => \@connect_info, %$dbd_multi_opt } ]; | ||
| 635 | # } | ||
| 636 | |||
| 637 | =head1 AUTHOR | ||
| 638 | |||
| 639 | travail C<travail@cabane.no-ip.org> | ||
| 640 | |||
| 641 | =head1 COPYRIGHT | ||
| 642 | |||
| 643 | This program is free software; you can redistribute | ||
| 644 | it and/or modify it under the same terms as Perl itself. | ||
| 645 | |||
| 646 | The full text of the license can be found in the | ||
| 647 | LICENSE file included with this module. | ||
| 648 | |||
| 649 | =cut | ||
| 650 | |||
| 651 | 1; |