From: Marc Mims Date: Tue, 12 Aug 2008 17:36:14 +0000 (+0000) Subject: ODBC fixes: superfluous connection in rebless; undef PK on first insert in MSSQL X-Git-Tag: v0.08240~378 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class.git;a=commitdiff_plain;h=2eebd801e7300ecc24a68c0062f35aa72775908f ODBC fixes: superfluous connection in rebless; undef PK on first insert in MSSQL --- diff --git a/Changes b/Changes index c7a1ae8..d00c6ed 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,6 @@ Revision history for DBIx::Class + - Fixed superfluous connection in ODBC::_rebless + - Fixed undef PK for first insert in ODBC::Microsoft_SQL_Server 0.08099_04 2008-07-24 01:00:00 - Functionality to storage to enable a sub to be run without FK checks diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 24f177c..806cef8 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -1151,12 +1151,12 @@ sub insert { my $ident = $source->from; my $bind_attributes = $self->source_bind_attributes($source); + $self->ensure_connected; foreach my $col ( $source->columns ) { if ( !defined $to_insert->{$col} ) { my $col_info = $source->column_info($col); if ( $col_info->{auto_nextval} ) { - $self->ensure_connected; $to_insert->{$col} = $self->_sequence_fetch( 'nextval', $col_info->{sequence} || $self->_dbh_get_autoinc_seq($self->dbh, $source) ); } } diff --git a/lib/DBIx/Class/Storage/DBI/ODBC.pm b/lib/DBIx/Class/Storage/DBI/ODBC.pm index d7b4509..16e198e 100644 --- a/lib/DBIx/Class/Storage/DBI/ODBC.pm +++ b/lib/DBIx/Class/Storage/DBI/ODBC.pm @@ -7,8 +7,7 @@ use base qw/DBIx::Class::Storage::DBI/; sub _rebless { my ($self) = @_; - my $dbh = $self->dbh; - my $dbtype = eval { $dbh->get_info(17) }; + my $dbtype = eval { $self->_dbh->get_info(17) }; unless ( $@ ) { # Translate the backend name into a perl identifier $dbtype =~ s/\W/_/gi; diff --git a/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm b/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm index 1b0c5d8..3c18a3c 100644 --- a/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm +++ b/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm @@ -14,14 +14,14 @@ sub _prep_for_execute { return ($sql, $bind); } -sub insert { - my ($self, $source, $to_insert) = @_; +sub _execute { + my $self = shift; + my ($op) = @_; - my $bind_attributes = $self->source_bind_attributes($source); - my (undef, $sth) = $self->_execute( 'insert' => [], $source, $bind_attributes, $to_insert); - $self->{_scope_identity} = $sth->fetchrow_array; + my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_); + $self->{_scope_identity} = $sth->fetchrow_array if $op eq 'insert'; - return $to_insert; + return wantarray ? ($rv, $sth, @bind) : $rv; } sub last_insert_id { shift->{_scope_identity} } @@ -74,8 +74,6 @@ onto each INSERT to accommodate that requirement. =head1 METHODS -=head2 insert - =head2 last_insert_id =head2 sqlt_type diff --git a/t/746mssql.t b/t/746mssql.t index 52b5357..9d46eea 100644 --- a/t/746mssql.t +++ b/t/746mssql.t @@ -17,7 +17,7 @@ my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1}); $schema->storage->ensure_connected; isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' ); -my $dbh = $schema->storage->dbh; +my $dbh = $schema->storage->_dbh; eval { $dbh->do("DROP TABLE artist") }; @@ -25,12 +25,15 @@ eval { $dbh->do("DROP TABLE artist") }; CREATE TABLE artist ( artistid INT IDENTITY NOT NULL, name VARCHAR(255), - charfield CHAR(10), + charfield CHAR(10) NULL, primary key(artistid) ) my %seen_id; +# fresh $schema so we start unconnected +$schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1}); + # test primary key handling my $new = $schema->resultset('Artist')->create({ name => 'foo' }); ok($new->artistid > 0, "Auto-PK worked"); @@ -58,6 +61,7 @@ is( $it->next, undef, "next past end of resultset ok" ); # clean up our mess END { + $dbh = eval { $schema->storage->_dbh }; $dbh->do('DROP TABLE artist') if $dbh; }