From: Rafael Kitover Date: Wed, 14 Apr 2010 15:04:00 +0000 (+0000) Subject: support $ENV{DBI_DSN} and $ENV{DBI_DRIVER} (patch from Possum) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c1807ed502a52187c5f7654346ddf5135616576f;p=dbsrgits%2FDBIx-Class-Historic.git support $ENV{DBI_DSN} and $ENV{DBI_DRIVER} (patch from Possum) --- diff --git a/Changes b/Changes index d71bbe2..470c644 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ Revision history for DBIx::Class + - support connecting using $ENV{DBI_DSN} and $ENV{DBI_DRIVER} - current_source_alias method on ResultSet objects to determine the alias to use in programatically assembled search()es (originally added in 0.08100 but unmentioned) diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index 5c5a647..1a50606 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -384,6 +384,8 @@ wreis: Wallace Reis zamolxes: Bogdan Lucaciu +Possum: Daniel LeWarne + =head1 COPYRIGHT Copyright (c) 2005 - 2010 the DBIx::Class L and L diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index f24a9e1..e048b62 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -998,8 +998,9 @@ sub _determine_driver { # try to use dsn to not require being connected, the driver may still # force a connection in _rebless to determine version # (dsn may not be supplied at all if all we do is make a mock-schema) - my $dsn = $self->_dbi_connect_info->[0] || ''; + my $dsn = $self->_dbi_connect_info->[0] || $ENV{DBI_DSN} || ''; ($driver) = $dsn =~ /dbi:([^:]+):/i; + $driver ||= $ENV{DBI_DRIVER}; } } diff --git a/t/storage/dbi_env.t b/t/storage/dbi_env.t new file mode 100644 index 0000000..f5275c3 --- /dev/null +++ b/t/storage/dbi_env.t @@ -0,0 +1,90 @@ +#!/usr/bin/perl +use strict; +use warnings; +use lib qw(t/lib); +use DBICTest; +use Test::More; +use Test::Exception; + +BEGIN { delete @ENV{qw(DBI_DSN DBI_DRIVER)} } + +my $schema; + +DBICTest->init_schema(sqlite_use_file => 1); + +my $dbname = DBICTest->_sqlite_dbname(sqlite_use_file => 1); + +sub count_sheep { + my $schema = shift; + scalar $schema->resultset('Artist')->search( { name => "Exploding Sheep" } ) + ->all; +} + +$schema = DBICTest::Schema->connect("dbi::$dbname"); +throws_ok { count_sheep($schema) } qr{I can't work out what driver to use}, + 'Driver in DSN empty'; +isa_ok $schema->storage, 'DBIx::Class::Storage::DBI'; + +$schema = DBICTest::Schema->connect("dbi:Test_NonExistant_DBD:$dbname"); +throws_ok { count_sheep($schema) } + qr{Can't locate DBD/Test_NonExistant_DBD\.pm in \@INC}, + "Driver class doesn't exist"; +isa_ok $schema->storage, 'DBIx::Class::Storage::DBI'; + +$ENV{DBI_DSN} = "dbi::$dbname"; +$schema = DBICTest::Schema->connect; +throws_ok { count_sheep($schema) } qr{I can't work out what driver to use}, + "Driver class not defined in DBI_DSN either."; +isa_ok $schema->storage, 'DBIx::Class::Storage::DBI'; + +$ENV{DBI_DSN} = "dbi:Test_NonExistant_DBD2:$dbname"; +$schema = DBICTest::Schema->connect; +throws_ok { count_sheep($schema) } + qr{Can't locate DBD/Test_NonExistant_DBD2\.pm in \@INC}, + "Driver class defined in DBI_DSN doesn't exist"; +isa_ok $schema->storage, 'DBIx::Class::Storage::DBI'; + +$ENV{DBI_DSN} = "dbi::$dbname"; +$ENV{DBI_DRIVER} = 'Test_NonExistant_DBD3'; +$schema = DBICTest::Schema->connect; +throws_ok { count_sheep($schema) } + qr{Can't locate DBD/Test_NonExistant_DBD3\.pm in \@INC}, + "Driver class defined in DBI_DRIVER doesn't exist"; +isa_ok $schema->storage, 'DBIx::Class::Storage::DBI'; + +$ENV{DBI_DSN} = "dbi:Test_NonExistant_DBD4:$dbname"; +$schema = DBICTest::Schema->connect; +throws_ok { count_sheep($schema) } +qr{Can't locate DBD/Test_NonExistant_DBD4\.pm in \@INC}, + "Driver class defined in DBI_DSN doesn't exist"; +isa_ok $schema->storage, 'DBIx::Class::Storage::DBI'; + +delete @ENV{qw(DBI_DSN DBI_DRIVER)}; + +$schema = DBICTest::Schema->connect("dbi:SQLite:$dbname"); +lives_ok { count_sheep($schema) } 'SQLite passed to connect_info'; +isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite'; + +$ENV{DBI_DRIVER} = 'SQLite'; +$schema = DBICTest::Schema->connect("dbi::$dbname"); +lives_ok { count_sheep($schema) } 'SQLite in DBI_DRIVER'; +isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite'; + +undef $ENV{DBI_DRIVER}; +$ENV{DBI_DSN} = "dbi:SQLite:$dbname"; +$schema = DBICTest::Schema->connect; +lives_ok { count_sheep($schema) } 'SQLite in DBI_DSN'; +isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite'; + +$ENV{DBI_DRIVER} = 'SQLite'; +$schema = DBICTest::Schema->connect; +lives_ok { count_sheep($schema) } 'SQLite in DBI_DSN (and DBI_DRIVER)'; +isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite'; + +$ENV{DBI_DSN} = "dbi::$dbname"; +$ENV{DBI_DRIVER} = 'SQLite'; +$schema = DBICTest::Schema->connect; +lives_ok { count_sheep($schema) } 'SQLite in DBI_DRIVER (not DBI_DSN)'; +isa_ok $schema->storage, 'DBIx::Class::Storage::DBI::SQLite'; + +done_testing;