Generalized the loading of subclasses for specfic ODBC backends.
Marc Mims [Mon, 8 May 2006 17:41:54 +0000 (17:41 +0000)]
lib/DBIx/Class/Storage/DBI.pm
lib/DBIx/Class/Storage/DBI/ODBC.pm [new file with mode: 0644]
lib/DBIx/Class/Storage/DBI/ODBC/DB2_400_SQL.pm [moved from lib/DBIx/Class/Storage/DBI/ODBC400.pm with 79% similarity]

index 196fdc9..aad0353 100644 (file)
@@ -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 (file)
index 0000000..f33100c
--- /dev/null
@@ -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<< <marc@sssonline.com> >>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
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 (file)
@@ -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@questright.com> >>
+Marc Mims C<< <marc@sssonline.com> >>
 
 Based on DBIx::Class::Storage::DBI::DB2 by Jess Robinson.