4 # use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
6 if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
7 unshift @INC, $_ for split /:/, $lib_dirs;
16 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
18 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
22 'DBI::Sybase::Microsoft_SQL_Server',
23 'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
28 my $NUMBER_OF_TESTS_IN_BLOCK = 18;
29 for my $storage_type (@storage_types) {
32 $schema = DBICTest::Schema->clone;
34 $schema->connection($dsn, $user, $pass);
36 if ($storage_idx != 0) { # autodetect
37 no warnings 'redefine';
38 local *DBIx::Class::Storage::DBI::_typeless_placeholders_supported =
40 # $schema->storage_type("::$storage_type");
41 $schema->storage->ensure_connected;
44 $schema->storage->ensure_connected;
47 if ($storage_idx == 0 && ref($schema->storage) =~ /NoBindVars\z/) {
48 my $tb = Test::More->builder;
49 $tb->skip('no placeholders') for 1..$NUMBER_OF_TESTS_IN_BLOCK;
53 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
56 skip 'This version of DBD::Sybase segfaults on disconnect', 1 if DBD::Sybase->VERSION < 1.08;
58 # start disconnected to test _ping
59 $schema->storage->_dbh->disconnect;
62 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
66 my $dbh = $schema->storage->dbh;
68 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
70 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
73 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
74 $dbh->do("CREATE TABLE cd (cdid INT IDENTITY PRIMARY KEY, artist INT, title VARCHAR(100), year VARCHAR(100), genreid INT NULL, single_track INT NULL);");
75 # Just to test compat shim, Auto is in Core
76 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
79 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
80 ok($new->artistid, "Auto-PK worked");
84 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
87 my $it = $schema->resultset('Artist')->search( { },
90 order_by => 'artistid'
94 # Test ? in data don't get treated as placeholders
95 my $cd = $schema->resultset('CD')->create( {
97 title => 'Does this break things?',
100 ok($cd->id, 'Not treating ? in data as placeholders');
102 is( $it->count, 3, "LIMIT count ok" );
103 ok( $it->next->name, "iterator->next ok" );
106 is( $it->next, undef, "next past end of resultset ok" );
108 # test MONEY column support
109 $schema->storage->dbh_do (sub {
110 my ($storage, $dbh) = @_;
111 eval { $dbh->do("DROP TABLE money_test") };
113 CREATE TABLE money_test (
114 id INT IDENTITY PRIMARY KEY,
121 my $rs = $schema->resultset('Money');
125 $row = $rs->create({ amount => 100 });
126 } 'inserted a money value';
128 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
131 $row->update({ amount => 200 });
132 } 'updated a money value';
134 cmp_ok $rs->find($row->id)->amount, '==', 200,
135 'updated money value round-trip';
138 $row->update({ amount => undef });
139 } 'updated a money value to NULL';
141 is $rs->find($row->id)->amount,
142 undef, 'updated money value to NULL round-trip';
144 $rs->create({ amount => 300 }) for (1..3);
146 # test multiple active statements
148 my $artist_rs = $schema->resultset('Artist');
149 while (my $row = $rs->next) {
150 my $artist = $artist_rs->next;
153 } 'multiple active statements';
157 # test simple transaction with commit
159 $schema->txn_do(sub {
160 $rs->create({ amount => 400 });
162 } 'simple transaction';
164 cmp_ok $rs->first->amount, '==', 400, 'committed';
171 $schema->txn_do(sub {
172 $rs->create({ amount => 400 });
175 } qr/mtfnpy/, 'simple failed txn';
177 is $rs->first, undef, 'rolled back';
180 # test RNO detection when version detection fails
182 my $storage = $schema->storage;
183 my $version = $storage->_server_info->{normalized_dbms_version};
185 skip 'could not detect SQL Server version', 1 if not defined $version;
187 my $have_rno = $version >= 9 ? 1 : 0;
189 local $storage->{_sql_maker} = undef;
190 local $storage->{_sql_maker_opts} = undef;
192 local $storage->{_server_info_hash} = { %{ $storage->_server_info_hash } }; # clone
193 delete @{$storage->{_server_info_hash}}{qw/dbms_version normalized_dbms_version/};
198 ($storage->{_sql_maker_opts}{limit_dialect} eq 'RowNumberOver') ? 1 : 0;
200 ok (($have_rno == $rno_detected),
201 'row_number() over support detected correctly');
205 my $schema = DBICTest::Schema->clone;
206 $schema->connection($dsn, $user, $pass);
208 like $schema->storage->sql_maker->{limit_dialect},
209 qr/^(?:Top|RowNumberOver)\z/,
210 'sql_maker is correct on unconnected schema';
214 # test op-induced autoconnect
217 my $schema = DBICTest::Schema->clone;
218 $schema->connection($dsn, $user, $pass);
220 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
221 is ($artist->id, 1, 'Artist retrieved successfully');
222 }, 'Query-induced autoconnect works');
228 if (my $dbh = eval { $schema->storage->dbh }) {
229 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
230 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
231 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");