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'
21 my $testdb_supports_placeholders = DBICTest::Schema->connect($dsn, $user, $pass)
23 ->_supports_typeless_placeholders;
25 $testdb_supports_placeholders ? 'DBI::Sybase::Microsoft_SQL_Server' : (),
26 'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
30 for my $storage_type (@test_storages) {
31 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
33 if ($storage_type =~ /NoBindVars\z/) {
34 # since we want to use the nobindvar - disable the capability so the
35 # rebless happens to the correct class
36 $schema->storage->_use_typeless_placeholders (0);
39 $schema->storage->ensure_connected;
40 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
43 skip 'This version of DBD::Sybase segfaults on disconnect', 1 if DBD::Sybase->VERSION < 1.08;
45 # start disconnected to test _ping
46 $schema->storage->_dbh->disconnect;
49 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
53 my $dbh = $schema->storage->dbh;
55 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
57 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
60 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
61 $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);");
62 # Just to test compat shim, Auto is in Core
63 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
66 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
67 ok($new->artistid, "Auto-PK worked");
71 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
74 my $it = $schema->resultset('Artist')->search( { },
77 order_by => 'artistid'
81 # Test ? in data don't get treated as placeholders
82 my $cd = $schema->resultset('CD')->create( {
84 title => 'Does this break things?',
87 ok($cd->id, 'Not treating ? in data as placeholders');
89 is( $it->count, 3, "LIMIT count ok" );
90 ok( $it->next->name, "iterator->next ok" );
93 is( $it->next, undef, "next past end of resultset ok" );
95 # test MONEY column support
96 $schema->storage->dbh_do (sub {
97 my ($storage, $dbh) = @_;
98 eval { $dbh->do("DROP TABLE money_test") };
100 CREATE TABLE money_test (
101 id INT IDENTITY PRIMARY KEY,
108 my $rs = $schema->resultset('Money');
112 $row = $rs->create({ amount => 100 });
113 } 'inserted a money value';
115 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
118 $row->update({ amount => 200 });
119 } 'updated a money value';
121 cmp_ok $rs->find($row->id)->amount, '==', 200,
122 'updated money value round-trip';
125 $row->update({ amount => undef });
126 } 'updated a money value to NULL';
128 is $rs->find($row->id)->amount,
129 undef, 'updated money value to NULL round-trip';
131 $rs->create({ amount => 300 }) for (1..3);
133 # test multiple active statements
135 my $artist_rs = $schema->resultset('Artist');
136 while (my $row = $rs->next) {
137 my $artist = $artist_rs->next;
140 } 'multiple active statements';
144 # test simple transaction with commit
146 $schema->txn_do(sub {
147 $rs->create({ amount => 400 });
149 } 'simple transaction';
151 cmp_ok $rs->first->amount, '==', 400, 'committed';
158 $schema->txn_do(sub {
159 $rs->create({ amount => 400 });
162 } qr/mtfnpy/, 'simple failed txn';
164 is $rs->first, undef, 'rolled back';
167 # test RNO detection when version detection fails
169 my $storage = $schema->storage;
170 my $version = $storage->_server_info->{normalized_dbms_version};
172 skip 'could not detect SQL Server version', 1 if not defined $version;
174 my $have_rno = $version >= 9 ? 1 : 0;
176 local $storage->{_dbh_details}{info} = {}; # delete cache
179 ($storage->sql_limit_dialect eq 'RowNumberOver') ? 1 : 0;
181 ok (($have_rno == $rno_detected),
182 'row_number() over support detected correctly');
186 my $schema = DBICTest::Schema->clone;
187 $schema->connection($dsn, $user, $pass);
189 like $schema->storage->sql_maker->{limit_dialect},
190 qr/^(?:Top|RowNumberOver)\z/,
191 'sql_maker is correct on unconnected schema';
195 # test op-induced autoconnect
198 my $schema = DBICTest::Schema->clone;
199 $schema->connection($dsn, $user, $pass);
201 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
202 is ($artist->id, 1, 'Artist retrieved successfully');
203 }, 'Query-induced autoconnect works');
209 if (my $dbh = eval { $schema->storage->dbh }) {
210 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
211 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
212 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");