Skip to content

Commit

Permalink
support subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
kazeburo committed Apr 7, 2011
1 parent 9ff5902 commit 6942d3f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/Scope/Container/DBI.pm
Expand Up @@ -10,6 +10,7 @@ use Carp;
use DBI;

our $VERSION = '0.01';
our $DBI_CLASS = 'DBI';

sub connect {
my $class = shift;
Expand Down Expand Up @@ -49,9 +50,9 @@ sub connect {
my $dbh = do {
if ($INC{'Apache/DBI.pm'} && $ENV{MOD_PERL}) {
local $DBI::connect_via = 'connect'; # Disable Apache::DBI.
DBI->connect( $dsn, $user, $pass, $attr );
$DBI_CLASS->connect( $dsn, $user, $pass, $attr );
} else {
DBI->connect( $dsn, $user, $pass, $attr );
$DBI_CLASS->connect( $dsn, $user, $pass, $attr );
}
};
croak($DBI::errstr) if !$dbh;
Expand Down Expand Up @@ -194,6 +195,21 @@ Scope::Container::DBI doesn't have callback function, but you can set callbacks
},
});
=item USING DBI SUBCLASSES
There is two way of using DBI subclass with Scope::Container::DBI. One is DBI's RootClass attribute, other is $Scope::Container::DBI::DBI_CLASS.
# use RootClass
my $dbh = Scope::Container::DBI->connect($dsn, $username, $password, {
RootClass => 'MySubDBI',
});
# use $Scope::Container::DBI::DBI_CLASS
local $Scope::Container::DBI::DBI_CLASS = 'MySubDBI';
my $dbh = Scope::Container::DBI->connect($dsn, $username, $password);
=back
=head1 AUTHOR
Expand Down
23 changes: 23 additions & 0 deletions t/02_subclass.t
@@ -0,0 +1,23 @@
use strict;

package MySubDBI;

use base qw/DBI/;

package MySubDBI::db;
our @ISA = qw(DBI::db);

package MySubDBI::st;
our @ISA = qw(DBI::st);

package main;

use Test::More;
use Scope::Container::DBI;

local $Scope::Container::DBI::DBI_CLASS = 'MySubDBI';
my $dbh = Scope::Container::DBI->connect("dbi:SQLite::memory:","","");
ok($dbh);
is ref($dbh), 'MySubDBI::db';

done_testing;
1 change: 1 addition & 0 deletions xt/01_podspell.t
Expand Up @@ -12,4 +12,5 @@ Scope::Container::DBI
DBI's
InactiveDestroy
dsn
RootClass

0 comments on commit 6942d3f

Please sign in to comment.