From: Rafael Kitover Date: Sat, 23 May 2009 14:44:59 +0000 (+0000) Subject: Sybase autopk, and a test, no limit support yet X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a964a928b10f79f18e4e3f5dbf2380174a0f7ca2;p=dbsrgits%2FDBIx-Class-Historic.git Sybase autopk, and a test, no limit support yet --- diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index c3de659..90d41b2 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -105,8 +105,10 @@ sub _find_syntax { # RowNumberOver is still needed here (should be part of SQLA) leave the # code in place my $dbhname = blessed($syntax) ? $syntax->{Driver}{Name} : $syntax; - if(ref($self) && $dbhname && $dbhname eq 'DB2') { - return 'RowNumberOver'; + if(ref($self) && $dbhname) { + if ($dbhname eq 'DB2') { + return 'RowNumberOver'; + } } $self->{_cached_syntax} ||= $self->SUPER::_find_syntax($syntax); diff --git a/lib/DBIx/Class/Storage/DBI/Sybase.pm b/lib/DBIx/Class/Storage/DBI/Sybase.pm index 670e55c..1a63bc8 100644 --- a/lib/DBIx/Class/Storage/DBI/Sybase.pm +++ b/lib/DBIx/Class/Storage/DBI/Sybase.pm @@ -19,6 +19,12 @@ sub _rebless { } } +sub _dbh_last_insert_id { + my $self = shift; + my $sth = $self->_dbh->prepare_cached('select @@identity'); + ($self->_dbh->selectrow_array($sth))[0]; +} + 1; =head1 NAME diff --git a/t/746sybase.t b/t/746sybase.t new file mode 100644 index 0000000..f09862f --- /dev/null +++ b/t/746sybase.t @@ -0,0 +1,82 @@ +use strict; +use warnings; + +use Test::More; +use lib qw(t/lib); +use DBICTest; + +my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/}; + +plan skip_all => 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' + unless ($dsn && $user); + +plan tests => 12; + +my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {AutoCommit => 1}); + +$schema->storage->ensure_connected; +isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::Sybase' ); + +$schema->storage->dbh_do (sub { + my ($storage, $dbh) = @_; + eval { $dbh->do("DROP TABLE artist") }; + $dbh->do(<<'SQL'); + +CREATE TABLE artist ( + artistid INT IDENTITY NOT NULL, + name VARCHAR(100), + rank INT DEFAULT 13 NOT NULL, + charfield CHAR(10) NULL, + primary key(artistid) +) + +SQL + +}); + +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"); + +$seen_id{$new->artistid}++; + +# test LIMIT support +for (1..6) { + $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ }); + is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" ); + $seen_id{$new->artistid}++; +} + +my $it; + +$it = $schema->resultset('Artist')->search( {}, { + rows => 3, + order_by => 'artistid', +}); + +TODO: { + local $TODO = 'Sybase is very very fucked in the limit department'; + + is( $it->count, 3, "LIMIT count ok" ); +} + +# The iterator still works correctly with rows => 3, even though the sql is +# fucked, very interesting. + +is( $it->next->name, "foo", "iterator->next ok" ); +$it->next; +is( $it->next->name, "Artist 2", "iterator->next ok" ); +is( $it->next, undef, "next past end of resultset ok" ); + + +# clean up our mess +END { + my $dbh = eval { $schema->storage->_dbh }; + $dbh->do('DROP TABLE artist') if $dbh; +} +