Skip to content

Commit

Permalink
Added db-specific drivers. Not used yet, but they will be.
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Sep 28, 2009
1 parent 8a19d6c commit e263902
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/DBIx/Connection.pm
Expand Up @@ -4,6 +4,7 @@ use 5.6.2;
use strict;
use warnings;
use DBI '1.605';
use DBIx::Connection::Driver;

our $VERSION = '0.10';

Expand Down Expand Up @@ -48,9 +49,27 @@ sub _connect {
# transaction in progress by definition
$self->{_depth} = $dbh->{AutoCommit} ? 0 : 1;

# Grab the driver.
$self->_set_driver;

return $self->{_dbh} = $dbh;
}

sub _set_driver {
my $self = shift;
my $driver = 'DBIx::Connection::Driver::' . do {
if (my $dbh = $self->{_dbh}) {
$dbh->{Driver}{Name};
} else {
(DBI->parse_dsn( $self->{_args}[0]))[1];
}
};
eval "require $driver";
$driver = 'DBIx::Connection::Driver' if $@;
$self->{_driver} = $driver->new;
}


sub connect { shift->new(@_)->dbh }

sub dbh {
Expand Down
57 changes: 57 additions & 0 deletions lib/DBIx/Connection/Driver.pm
@@ -0,0 +1,57 @@
package DBIx::Connection::Driver;

use strict;
use warnings;

DRIVERS: {
my %DRIVERS;

sub new {
my $class = shift;
return $DRIVERS{$class} ||= bless {} => $class;
}
}

1;
__END__
=head1 Name
DBIx::Connection::Driver - Database-specific connection interface
=begin comment
=head3 C<new>
=end comment
=head1 Authors
This module was written and is maintained by:
=over
=item David E. Wheeler <david@kineticode.com>
=back
It is based on code written by:
=over
=item Brandon Black <blblack@gmail.com>
=item Matt S. Trout <mst@shadowcatsystems.co.uk>
=item Alex Pavlovic <alex.pavlovic@taskforce-1.com>
=back
=head1 Copyright and License
Copyright (c) 2009 David E. Wheeler. Some Rights Reserved.
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
49 changes: 49 additions & 0 deletions lib/DBIx/Connection/Driver/Pg.pm
@@ -0,0 +1,49 @@
package DBIx::Connection::Driver::Pg;

use strict;
use warnings;
use base 'DBIx::Connection::Driver';

1;
__END__
=head1 Name
DBIx::Connection::Driver - Database-specific connection interface
=begin comment
=head3 C<new>
=end comment
=head1 Authors
This module was written and is maintained by:
=over
=item David E. Wheeler <david@kineticode.com>
=back
It is based on code written by:
=over
=item Brandon Black <blblack@gmail.com>
=item Matt S. Trout <mst@shadowcatsystems.co.uk>
=item Alex Pavlovic <alex.pavlovic@taskforce-1.com>
=back
=head1 Copyright and License
Copyright (c) 2009 David E. Wheeler. Some Rights Reserved.
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
2 changes: 0 additions & 2 deletions t/base.t
Expand Up @@ -5,13 +5,11 @@ use warnings;
use Test::More tests => 94;
#use Test::More 'no_plan';
use Test::MockModule;
use Carp;

my $CLASS;
BEGIN {
$CLASS = 'DBIx::Connection';
use_ok $CLASS or die;
$SIG{__WARN__} = \&Carp::cluck;
}

# Try the basics.
Expand Down
35 changes: 35 additions & 0 deletions t/driver.t
@@ -0,0 +1,35 @@
#!/usr/bin/env perl -w

use strict;
use warnings;
use Test::More tests => 16;
#use Test::More 'no_plan';

my $CLASS;
BEGIN {
$CLASS = 'DBIx::Connection::Driver';
use_ok $CLASS or die;
use_ok 'DBIx::Connection' or die;
use_ok "$CLASS\::Pg" or die;
}

# Make sure it's a singleton.
ok my $dr = $CLASS->new, 'Create a new driver';
isa_ok $dr, $CLASS;
is $CLASS->new, $dr, 'It should be a singleton';

# Subclass should have a different singleton.
ok my $pg = "$CLASS\::Pg"->new, 'Get a Pg driver';
isa_ok $pg, "$CLASS\::Pg";
isa_ok $pg, $CLASS;
isnt $pg, $dr, 'It should be a different object';
is "$CLASS\::Pg"->new, $pg, 'But it should be a singleton';

ok my $conn = DBIx::Connection->new( 'dbi:ExampleP:dummy', '', '' ),
'Construct example connection';
is $conn->_set_driver, $dr, 'It should have the driver';

ok $conn = DBIx::Connection->new('dbi:Pg:dbname=try', '', '' ),
'Construct a Pg connection';
isa_ok $conn->_set_driver, 'DBIx::Connection::Driver::Pg';
is $conn->{_driver}, $pg, 'It should be the Pg singleton';

0 comments on commit e263902

Please sign in to comment.