Port ::Replicated from Moose to Moo+Type::Tiny
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Replicated.pm
index 648072c..852c816 100644 (file)
@@ -1,26 +1,31 @@
 package DBIx::Class::Storage::DBI::Replicated;
 
+use warnings;
+use strict;
+
 BEGIN {
-  use DBIx::Class;
-  die('The following modules are required for Replication ' . DBIx::Class::Optional::Dependencies->req_missing_for ('replicated') . "\n" )
-    unless DBIx::Class::Optional::Dependencies->req_ok_for ('replicated');
+  require DBIx::Class::Optional::Dependencies;
+  if ( my $missing = DBIx::Class::Optional::Dependencies->req_missing_for('replicated') ) {
+    die "The following modules are required for Replicated storage support: $missing\n";
+  }
 }
 
-use Moose;
+use Moo;
 use DBIx::Class::Storage::DBI;
 use DBIx::Class::Storage::DBI::Replicated::Pool;
 use DBIx::Class::Storage::DBI::Replicated::Balancer;
 use DBIx::Class::Storage::DBI::Replicated::Types qw/BalancerClassNamePart DBICSchema DBICStorageDBI/;
-use MooseX::Types::Moose qw/ClassName HashRef Object/;
-use Scalar::Util 'reftype';
+use Types::Standard qw/ClassName HashRef Object/;
+use Type::Utils qw(class_type role_type);
+use Scalar::Util qw(reftype blessed);
+use Sub::Name qw(subname);
 use Hash::Merge;
 use List::Util qw/min max reduce/;
 use Context::Preserve 'preserve_context';
+use Class::Inspector;
 use Try::Tiny;
 
-use namespace::clean -except => 'meta';
-
-=encoding utf8
+use namespace::clean;
 
 =head1 NAME
 
@@ -37,7 +42,7 @@ also define your arguments, such as which balancer you want and any arguments
 that the Pool object should get.
 
   my $schema = Schema::Class->clone;
-  $schema->storage_type( ['::DBI::Replicated', {balancer=>'::Random'}] );
+  $schema->storage_type(['::DBI::Replicated', { balancer_type => '::Random' }]);
   $schema->connection(...);
 
 Next, you need to add in the Replicants.  Basically this is an array of
@@ -201,8 +206,11 @@ container class for one or more replicated databases.
 
 has 'pool' => (
   is=>'ro',
-  isa=>'DBIx::Class::Storage::DBI::Replicated::Pool',
-  lazy_build=>1,
+  isa=>class_type('DBIx::Class::Storage::DBI::Replicated::Pool'),
+  lazy=>1,
+  builder=>1,
+  predicate=>1,
+  clearer=>1,
   handles=>[qw/
     connect_replicants
     replicants
@@ -219,8 +227,11 @@ is a class that takes a pool (L<DBIx::Class::Storage::DBI::Replicated::Pool>)
 
 has 'balancer' => (
   is=>'rw',
-  isa=>'DBIx::Class::Storage::DBI::Replicated::Balancer',
-  lazy_build=>1,
+  isa=>role_type('DBIx::Class::Storage::DBI::Replicated::Balancer'),
+  lazy=>1,
+  builder=>1,
+  predicate=>1,
+  clearer=>1,
   handles=>[qw/auto_validate_every/],
 );
 
@@ -237,7 +248,10 @@ pool of databases that is allowed to handle write traffic.
 has 'master' => (
   is=> 'ro',
   isa=>DBICStorageDBI,
-  lazy_build=>1,
+  lazy=>1,
+  builder=>1,
+  predicate=>1,
+  clearer=>1,
 );
 
 =head1 ATTRIBUTES IMPLEMENTING THE DBIx::Storage::DBI INTERFACE
@@ -265,7 +279,6 @@ my $method_dispatch = {
     build_datetime_parser
     last_insert_id
     insert
-    insert_bulk
     update
     delete
     dbh
@@ -310,15 +323,18 @@ my $method_dispatch = {
     _parse_connect_do
     savepoints
     _sql_maker_opts
+    _use_multicolumn_in
     _conn_pid
     _dbh_autocommit
     _native_data_type
     _get_dbh
     sql_maker_class
+    insert_bulk
+    _insert_bulk
     _execute
     _do_query
     _dbh_execute
-  /, Class::MOP::Class->initialize('DBIx::Class::Storage::DBIHacks')->get_method_list ],
+  /, @{Class::Inspector->functions('DBIx::Class::Storage::DBIHacks')} ],
   reader => [qw/
     select
     select_single
@@ -362,10 +378,9 @@ my $method_dispatch = {
     _bind_sth_params
   /,(
     # the capability framework
-    # not sure if CMOP->initialize does evil things to DBIC::S::DBI, fix if a problem
     grep
-      { $_ =~ /^ _ (?: use | supports | determine_supports ) _ /x }
-      ( Class::MOP::Class->initialize('DBIx::Class::Storage::DBI')->get_all_method_names )
+      { $_ =~ /^ _ (?: use | supports | determine_supports ) _ /x and $_ ne '_use_multicolumn_in' }
+      @{Class::Inspector->functions('DBIx::Class::Storage::DBI')}
   )],
 };
 
@@ -396,28 +411,32 @@ if (DBIx::Class::_ENV_::DBICTEST) {
 }
 
 for my $method (@{$method_dispatch->{unimplemented}}) {
-  __PACKAGE__->meta->add_method($method, sub {
+  no strict 'refs';
+  *{$method} = subname $method, sub {
     my $self = shift;
     $self->throw_exception("$method() must not be called on ".(blessed $self).' objects');
-  });
+  };
 }
 
 =head2 read_handler
 
-Defines an object that implements the read side of L<BIx::Class::Storage::DBI>.
+Defines an object that implements the read side of L<DBIx::Class::Storage::DBI>.
 
 =cut
 
 has 'read_handler' => (
   is=>'rw',
   isa=>Object,
-  lazy_build=>1,
+  lazy=>1,
+  builder=>1,
+  predicate=>1,
+  clearer=>1,
   handles=>$method_dispatch->{reader},
 );
 
 =head2 write_handler
 
-Defines an object that implements the write side of L<BIx::Class::Storage::DBI>,
+Defines an object that implements the write side of L<DBIx::Class::Storage::DBI>,
 as well as methods that don't write or read that can be called on only one
 storage, methods that return a C<$dbh>, and any methods that don't make sense to
 run on a replicant.
@@ -427,7 +446,10 @@ run on a replicant.
 has 'write_handler' => (
   is=>'ro',
   isa=>Object,
-  lazy_build=>1,
+  lazy=>1,
+  builder=>1,
+  predicate=>1,
+  clearer=>1,
   handles=>$method_dispatch->{writer},
 );
 
@@ -494,9 +516,11 @@ around connect_info => sub {
     # Make sure master is blessed into the correct class and apply role to it.
     my $master = $self->master;
     $master->_determine_driver;
-    Moose::Meta::Class->initialize(ref $master);
 
-    DBIx::Class::Storage::DBI::Replicated::WithDSN->meta->apply($master);
+    Moo::Role->apply_roles_to_object(
+      $master,
+      'DBIx::Class::Storage::DBI::Replicated::WithDSN',
+    );
 
     # link pool back to master
     $self->pool->master($master);
@@ -589,7 +613,8 @@ sub _build_read_handler {
 =head2 around: connect_replicants
 
 All calls to connect_replicants needs to have an existing $schema tacked onto
-top of the args, since L<DBIx::Storage::DBI> needs it, and any C<connect_info>
+top of the args, since L<DBIx::Class::Storage::DBI> needs it, and any
+L<connect_info|DBIx::Class::Storage::DBI/connect_info>
 options merged with the master, with replicant opts having higher priority.
 
 =cut
@@ -1080,7 +1105,8 @@ sub _get_server_version {
 Due to the fact that replicants can lag behind a master, you must take care to
 make sure you use one of the methods to force read queries to a master should
 you need realtime data integrity.  For example, if you insert a row, and then
-immediately re-read it from the database (say, by doing $result->discard_changes)
+immediately re-read it from the database (say, by doing
+L<< $result->discard_changes|DBIx::Class::Row/discard_changes >>)
 or you insert a row and then immediately build a query that expects that row
 to be an item, you should force the master to handle reads.  Otherwise, due to
 the lag, there is no certainty your data will be in the expected state.
@@ -1112,21 +1138,17 @@ using the Schema clone method.
   ## $new_schema will use only the Master storage for all reads/writes while
   ## the $schema object will use replicated storage.
 
-=head1 AUTHOR
+=head1 FURTHER QUESTIONS?
 
-  John Napiorkowski <john.napiorkowski@takkle.com>
+Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
 
-Based on code originated by:
+=head1 COPYRIGHT AND LICENSE
 
-  Norbert Csongrádi <bert@cpan.org>
-  Peter Siklósi <einon@einon.hu>
-
-=head1 LICENSE
-
-You may distribute this code under the same terms as Perl itself.
+This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
+by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
+redistribute it and/or modify it under the same terms as the
+L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
 
 =cut
 
-__PACKAGE__->meta->make_immutable;
-
 1;