Scope-Container-DBI

 view release on metacpan or  search on metacpan

lib/Scope/Container/DBI.pm  view on Meta::CPAN

    scope_container($key, shift);
}

# stolen from Mouse::PurePerl
sub is_class_loaded {
    my $class = shift;

    return 0 if ref($class) || !defined($class) || !length($class);

    # walk the symbol table tree to avoid autovififying
    # \*{${main::}{"Foo::"}{"Bar::"}} == \*main::Foo::Bar::

    my $pack = \%::;

    foreach my $part (split('::', $class)) {
        $part .= '::';
        return 0 if !exists $pack->{$part};

        my $entry = \$pack->{$part};
        return 0 if ref($entry) ne 'GLOB';
        $pack = *{$entry}{HASH};
    }

    return 0 if !%{$pack};

    # check for $VERSION or @ISA
    return 1 if exists $pack->{VERSION}
             && defined *{$pack->{VERSION}}{SCALAR} && defined ${ $pack->{VERSION} };
    return 1 if exists $pack->{ISA}
             && defined *{$pack->{ISA}}{ARRAY} && @{ $pack->{ISA} } != 0;

    # check for any method
    foreach my $name( keys %{$pack} ) {
        my $entry = \$pack->{$name};
        return 1 if ref($entry) ne 'GLOB' || defined *{$entry}{CODE};
    }

    # fail
    return 0;
}


1;
__END__

=head1 NAME

Scope::Container::DBI - DB connection manager with Scope::Container

=head1 SYNOPSIS

  use Scope::Container::DBI;
  use Scope::Container;

  FOO: {
      my $contaier = start_scope_container();

      # first connect
      my $dbh = Scope::Container::DBI->connect(
          'dbi:mysql:mydb;host=myhost', 'myuser', 'mypasswd',
          { RaiseError => 1, mysql_connect_timeout => 4, mysql_enable_utf8 => 1 }
      );

      # same dsn, user/pass, and attributes, reuse connection
      my $dbh2 = Scope::Container::DBI->connect(
          'dbi:mysql:mydb;host=myhost', 'myuser', 'mypasswd',
          { RaiseError => 1, mysql_connect_timeout => 4, mysql_enable_utf8 => 1 }
      );

      #disconnect
  }

  BAR: {
      my $contaier = start_scope_container();

      # connect randomly
      my $dbh = Scope::Container::DBI->connect(
          ['dbi:mysql:mydb;host=myslave01', 'myuser', 'mypasswd', {..}],
          ['dbi:mysql:mydb;host=myslave02', 'myuser', 'mypasswd', {..}],
          ['dbi:mysql:mydb;host=myslave03', 'myuser', 'mypasswd', {..}],
      );

      # reuse randomly connected 
      my $dbh2 = Scope::Container::DBI->connect(
          ['dbi:mysql:mydb;host=myslave01', 'myuser', 'mypasswd', {..}],
          ['dbi:mysql:mydb;host=myslave02', 'myuser', 'mypasswd', {..}],
          ['dbi:mysql:mydb;host=myslave03', 'myuser', 'mypasswd', {..}],
      );

  }

=head1 DESCRIPTION

Scope::Container::DBI is DB connection manager that uses Scope::Container. 
You can control DB connection within any scope.

=head1 METHOD

=over 4

=item $dbh = Scope::Container::DBI->connect();

connect to databases and cache connections.

  $dbh = Scope::Container::DBI->connect($dsn,$user,$password,$attr);

You can give multiple dsn with arrayref, Scope::Container::DBI chooses database randomly.
 
  $dbh = Scope::Container::DBI->connect(
      [$dsn,$user,$password,$attr],
      [$dsn,$user,$password,$attr],
      [$dsn,$user,$password,$attr]
  );

=back


=head1 ADDITIONAL ATTRIBUTES

=over 4

=item ScopeContainerConnectRetry

number of connection retry, if failed connection.

  my $dbh = Scope::Container::DBI->connect(
      'dbi:mysql:mydb;host=myhost', 'myuser', 'mypasswd',



( run in 0.400 second using v1.01-cache-2.11-cpan-5511b514fd6 )