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;
13 use DBIx::Class::Optional::Dependencies ();
17 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mssql_sybase')
18 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mssql_sybase');
20 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
22 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
26 my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
27 ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
32 my $testdb_supports_placeholders = DBICTest::Schema->connect($dsn, $user, $pass)
34 ->_supports_typeless_placeholders;
36 $testdb_supports_placeholders ? 'DBI::Sybase::Microsoft_SQL_Server' : (),
37 'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
40 for my $storage_type (@test_storages) {
41 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
43 if ($storage_type =~ /NoBindVars\z/) {
44 # since we want to use the nobindvar - disable the capability so the
45 # rebless happens to the correct class
46 $schema->storage->_use_typeless_placeholders (0);
49 local $ENV{DBIC_MSSQL_FREETDS_LOWVER_NOWARN} = 1; # disable nobindvars warning
51 $schema->storage->ensure_connected;
53 if ($storage_type =~ /NoBindVars\z/) {
54 is $schema->storage->disable_sth_caching, 1,
55 'prepare_cached disabled for NoBindVars';
58 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
61 skip 'This version of DBD::Sybase segfaults on disconnect', 1 if DBD::Sybase->VERSION < 1.08;
63 # start disconnected to test _ping
64 $schema->storage->_dbh->disconnect;
67 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
71 my $dbh = $schema->storage->dbh;
73 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
75 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
78 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
79 $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);");
80 # Just to test compat shim, Auto is in Core
81 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
84 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
85 ok($new->artistid, "Auto-PK worked");
89 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
92 my $it = $schema->resultset('Artist')->search( { },
95 order_by => 'artistid'
99 # Test ? in data don't get treated as placeholders
100 my $cd = $schema->resultset('CD')->create( {
102 title => 'Does this break things?',
105 ok($cd->id, 'Not treating ? in data as placeholders');
107 is( $it->count, 3, "LIMIT count ok" );
108 ok( $it->next->name, "iterator->next ok" );
111 is( $it->next, undef, "next past end of resultset ok" );
113 # test MONEY column support
114 $schema->storage->dbh_do (sub {
115 my ($storage, $dbh) = @_;
116 eval { $dbh->do("DROP TABLE money_test") };
118 CREATE TABLE money_test (
119 id INT IDENTITY PRIMARY KEY,
125 my $rs = $schema->resultset('Money');
129 $row = $rs->create({ amount => 100 });
130 } 'inserted a money value';
132 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
135 $row->update({ amount => 200 });
136 } 'updated a money value';
138 cmp_ok $rs->find($row->id)->amount, '==', 200,
139 'updated money value round-trip';
142 $row->update({ amount => undef });
143 } 'updated a money value to NULL';
145 is $rs->find($row->id)->amount,
146 undef, 'updated money value to NULL round-trip';
150 # test simple transaction with commit
152 $schema->txn_do(sub {
153 $rs->create({ amount => 300 });
155 } 'simple transaction';
157 cmp_ok $rs->first->amount, '==', 300, 'committed';
164 $schema->txn_do(sub {
165 $rs->create({ amount => 700 });
168 } qr/mtfnpy/, 'simple failed txn';
170 is $rs->first, undef, 'rolled back';
175 # test multiple active statements
177 $rs->create({ amount => 800 + $_ }) for 1..3;
180 [ 'Artist 1', '801.00' ],
181 [ 'Artist 2', '802.00' ],
182 [ 'Artist 3', '803.00' ]
185 my $artist_rs = $schema->resultset('Artist')->search({
186 name => { -like => 'Artist %' }
191 while (my $money_row = $rs->next) {
192 my $artist_row = $artist_rs->next;
194 is_deeply [ $artist_row->name, $money_row->amount ], $map[$i++],
195 'multiple active statements';
201 # test transaction handling on a disconnected handle
203 no_transaction => sub { shift->() },
204 txn_do => sub { my $code = shift; $schema->txn_do(sub { $code->() } ) },
205 txn_begin => sub { $schema->txn_begin; shift->(); $schema->txn_commit },
206 txn_guard => sub { my $g = $schema->txn_scope_guard; shift->(); $g->commit },
208 for my $wrapper (keys %$wrappers) {
211 # a reconnect should trigger on next action
212 $schema->storage->_get_dbh->disconnect;
215 $wrappers->{$wrapper}->( sub {
216 $rs->create({ amount => 900 + $_ }) for 1..3;
219 } "transaction on disconnected handle with $wrapper wrapper";
223 local $TODO = 'Transaction handling with multiple active statements will '
224 .'need eager cursor support.';
226 # test transaction handling on a disconnected handle with multiple active
229 no_transaction => sub { shift->() },
230 txn_do => sub { my $code = shift; $schema->txn_do(sub { $code->() } ) },
231 txn_begin => sub { $schema->txn_begin; shift->(); $schema->txn_commit },
232 txn_guard => sub { my $g = $schema->txn_scope_guard; shift->(); $g->commit },
234 for my $wrapper (keys %$wrappers) {
237 $rs->create({ amount => 1000 + $_ }) for (1..3);
239 my $artist_rs = $schema->resultset('Artist')->search({
240 name => { -like => 'Artist %' }
245 my $map = [ ['Artist 1', '1002.00'], ['Artist 2', '1003.00'] ];
250 $wrappers->{$wrapper}->( sub {
251 while (my $money = $rs->next) {
252 my $artist = $artist_rs->next;
253 push @results, [ $artist->name, $money->amount ];
257 is_deeply \@results, $map;
258 } "transactions with multiple active statement with $wrapper wrapper";
262 # test RNO detection when version detection fails
264 my $storage = $schema->storage;
265 my $version = $storage->_server_info->{normalized_dbms_version};
267 skip 'could not detect SQL Server version', 1 if not defined $version;
269 my $have_rno = $version >= 9 ? 1 : 0;
271 local $storage->{_dbh_details}{info} = {}; # delete cache
274 ($storage->sql_limit_dialect eq 'RowNumberOver') ? 1 : 0;
276 ok (($have_rno == $rno_detected),
277 'row_number() over support detected correctly');
281 my $schema = DBICTest::Schema->clone;
282 $schema->connection($dsn, $user, $pass);
284 like $schema->storage->sql_maker->{limit_dialect},
285 qr/^(?:Top|RowNumberOver)\z/,
286 'sql_maker is correct on unconnected schema';
290 # test op-induced autoconnect
293 my $schema = DBICTest::Schema->clone;
294 $schema->connection($dsn, $user, $pass);
296 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
297 is ($artist->id, 1, 'Artist retrieved successfully');
298 }, 'Query-induced autoconnect works');
302 local $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK} = 1;
303 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 0 });
305 my $rs = $schema2->resultset('Money');
308 $schema2->txn_commit;
310 is $rs->count, 0, 'initially empty'
311 || diag ('Found row with amount ' . $_->amount) for $rs->all;
313 $rs->create({ amount => 3000 });
314 $schema2->txn_rollback;
316 is $rs->count, 0, 'rolled back in AutoCommit=0'
317 || diag ('Found row with amount ' . $_->amount) for $rs->all;
319 $rs->create({ amount => 4000 });
320 $schema2->txn_commit;
322 cmp_ok $rs->first->amount, '==', 4000, 'committed in AutoCommit=0';
329 if (my $dbh = eval { $schema->storage->dbh }) {
330 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
331 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
332 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");