From: Marc Mims Date: Mon, 8 May 2006 17:41:54 +0000 (+0000) Subject: Generalized the loading of subclasses for specfic ODBC backends. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2a57124dc95b1f1b8b2c2e6003a42b5777c462e4;p=dbsrgits%2FDBIx-Class-Historic.git Generalized the loading of subclasses for specfic ODBC backends. --- diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 196fdc9..aad0353 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -421,14 +421,11 @@ sub _populate_dbh { my ($self) = @_; my @info = @{$self->_connect_info || []}; $self->_dbh($self->_connect(@info)); - my $dbh = $self->_dbh; - my $driver = $dbh->{Driver}->{Name}; - if ( $driver eq 'ODBC' and $dbh->get_info(17) =~ m{^DB2/400} ) { - $driver = 'ODBC400'; - } + my $driver = $self->_dbh->{Driver}->{Name}; eval "require DBIx::Class::Storage::DBI::${driver}"; unless ($@) { bless $self, "DBIx::Class::Storage::DBI::${driver}"; + $self->_rebless() if $self->can('_rebless'); } # if on-connect sql statements are given execute them foreach my $sql_statement (@{$self->on_connect_do || []}) { diff --git a/lib/DBIx/Class/Storage/DBI/ODBC.pm b/lib/DBIx/Class/Storage/DBI/ODBC.pm new file mode 100644 index 0000000..f33100c --- /dev/null +++ b/lib/DBIx/Class/Storage/DBI/ODBC.pm @@ -0,0 +1,48 @@ +package DBIx::Class::Storage::DBI::ODBC; +use strict; +use warnings; + +use base qw/DBIx::Class::Storage::DBI/; + +sub _rebless { + my ($self) = @_; + + my $dbh = $self->_dbh; + my $dbtype = eval { $dbh->get_info(17) }; + unless ( $@ ) { + # Translate the backend name into a perl identifier + $dbtype =~ s/\W/_/gi; + my $class = "DBIx::Class::Storage::DBI::ODBC::${dbtype}"; + eval "require $class"; + bless $self, $class unless $@; + } +} + + +1; + +=head1 NAME + +DBIx::Class::Storage::DBI::ODBC - Base class for ODBC drivers + +=head1 SYNOPSIS + + # In your table classes + __PACKAGE__->load_components(qw/Core/); + + +=head1 DESCRIPTION + +This class simply provides a mechanism for discovering and loading a sub-class +for a specific ODBC backend. It should be transparent to the user. + + +=head1 AUTHORS + +Marc Mims C<< >> + +=head1 LICENSE + +You may distribute this code under the same terms as Perl itself. + +=cut diff --git a/lib/DBIx/Class/Storage/DBI/ODBC400.pm b/lib/DBIx/Class/Storage/DBI/ODBC/DB2_400_SQL.pm similarity index 79% rename from lib/DBIx/Class/Storage/DBI/ODBC400.pm rename to lib/DBIx/Class/Storage/DBI/ODBC/DB2_400_SQL.pm index 7fdd1f8..cba274a 100644 --- a/lib/DBIx/Class/Storage/DBI/ODBC400.pm +++ b/lib/DBIx/Class/Storage/DBI/ODBC/DB2_400_SQL.pm @@ -1,8 +1,8 @@ -package DBIx::Class::Storage::DBI::ODBC400; +package DBIx::Class::Storage::DBI::ODBC::DB2_400_SQL; use strict; use warnings; -use base qw/DBIx::Class::Storage::DBI/; +use base qw/DBIx::Class::Storage::DBI::ODBC/; sub last_insert_id { @@ -27,7 +27,7 @@ sub last_insert_id =head1 NAME -DBIx::Class::Storage::DBI::ODBC400 - Automatic primary key class for DB2/400 +DBIx::Class::Storage::DBI::ODBC::DB2_400_SQL - Automatic primary key class for DB2/400 over ODBC =head1 SYNOPSIS @@ -44,7 +44,7 @@ This class implements autoincrements for DB2/400 over ODBC. =head1 AUTHORS -Marc Mims C<< >> +Marc Mims C<< >> Based on DBIx::Class::Storage::DBI::DB2 by Jess Robinson.