1 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_mssql_sybase';
8 use Scalar::Util 'weaken';
12 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
14 my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
15 ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
20 my $testdb_supports_placeholders = DBICTest::Schema->connect($dsn, $user, $pass)
22 ->_supports_typeless_placeholders;
24 $testdb_supports_placeholders ? 'DBI::Sybase::Microsoft_SQL_Server' : (),
25 'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
28 for my $storage_type (@test_storages) {
29 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
31 if ($storage_type =~ /NoBindVars\z/) {
32 # since we want to use the nobindvar - disable the capability so the
33 # rebless happens to the correct class
34 $schema->storage->_use_typeless_placeholders (0);
37 local $ENV{DBIC_MSSQL_FREETDS_LOWVER_NOWARN} = 1; # disable nobindvars warning
39 $schema->storage->ensure_connected;
41 if ($storage_type =~ /NoBindVars\z/) {
42 is $schema->storage->disable_sth_caching, 1,
43 'prepare_cached disabled for NoBindVars';
46 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
49 skip 'This version of DBD::Sybase segfaults on disconnect', 1 if DBD::Sybase->VERSION < 1.08;
51 # start disconnected to test _ping
52 $schema->storage->_dbh->disconnect;
55 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
59 my $dbh = $schema->storage->dbh;
61 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
63 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
66 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
67 $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);");
68 # Just to test compat shim, Auto is in Core
69 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
72 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
73 ok($new->artistid, "Auto-PK worked");
77 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
80 my $it = $schema->resultset('Artist')->search( { },
83 order_by => 'artistid'
87 # Test ? in data don't get treated as placeholders
88 my $cd = $schema->resultset('CD')->create( {
90 title => 'Does this break things?',
93 ok($cd->id, 'Not treating ? in data as placeholders');
95 is( $it->count, 3, "LIMIT count ok" );
96 ok( $it->next->name, "iterator->next ok" );
99 is( $it->next, undef, "next past end of resultset ok" );
101 # test MONEY column support
102 $schema->storage->dbh_do (sub {
103 my ($storage, $dbh) = @_;
104 eval { $dbh->do("DROP TABLE money_test") };
106 CREATE TABLE money_test (
107 id INT IDENTITY PRIMARY KEY,
113 my $rs = $schema->resultset('Money');
114 weaken(my $rs_cp = $rs); # nested closure refcounting is an utter mess in perl
118 $row = $rs->create({ amount => 100 });
119 } 'inserted a money value';
121 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
124 $row->update({ amount => 200 });
125 } 'updated a money value';
127 cmp_ok $rs->find($row->id)->amount, '==', 200,
128 'updated money value round-trip';
131 $row->update({ amount => undef });
132 } 'updated a money value to NULL';
134 is $rs->find($row->id)->amount,
135 undef, 'updated money value to NULL round-trip';
139 # test simple transaction with commit
141 $schema->txn_do(sub {
142 $rs_cp->create({ amount => 300 });
144 } 'simple transaction';
146 cmp_ok $rs->first->amount, '==', 300, 'committed';
153 $schema->txn_do(sub {
154 $rs_cp->create({ amount => 700 });
157 } qr/mtfnpy/, 'simple failed txn';
159 is $rs->first, undef, 'rolled back';
164 # test multiple active statements
166 $rs->create({ amount => 800 + $_ }) for 1..3;
169 [ 'Artist 1', '801.00' ],
170 [ 'Artist 2', '802.00' ],
171 [ 'Artist 3', '803.00' ]
174 my $artist_rs = $schema->resultset('Artist')->search({
175 name => { -like => 'Artist %' }
180 while (my $money_row = $rs->next) {
181 my $artist_row = $artist_rs->next;
183 is_deeply [ $artist_row->name, $money_row->amount ], $map[$i++],
184 'multiple active statements';
191 no_transaction => sub { shift->() },
192 txn_do => sub { my $code = shift; $schema->txn_do(sub { $code->() } ) },
193 txn_begin => sub { $schema->txn_begin; shift->(); $schema->txn_commit },
194 txn_guard => sub { my $g = $schema->txn_scope_guard; shift->(); $g->commit },
197 # test transaction handling on a disconnected handle
198 for my $wrapper (keys %$wrappers) {
201 # a reconnect should trigger on next action
202 $schema->storage->_get_dbh->disconnect;
206 $wrappers->{$wrapper}->( sub {
207 $rs_cp->create({ amount => 900 + $_ }) for 1..3;
210 } "transaction on disconnected handle with $wrapper wrapper";
213 # test transaction handling on a disconnected handle with multiple active
215 for my $wrapper (keys %$wrappers) {
216 $schema->storage->disconnect;
219 $rs->create({ amount => 1000 + $_ }) for (1..3);
221 my $artist_rs = $schema->resultset('Artist')->search({
222 name => { -like => 'Artist %' }
227 my $map = [ ['Artist 1', '1002.00'], ['Artist 2', '1003.00'] ];
229 weaken(my $a_rs_cp = $artist_rs);
231 local $TODO = 'Transaction handling with multiple active statements will '
232 .'need eager cursor support.'
233 unless $wrapper eq 'no_transaction';
238 $wrappers->{$wrapper}->( sub {
239 while (my $money = $rs_cp->next) {
240 my $artist = $a_rs_cp->next;
241 push @results, [ $artist->name, $money->amount ];
245 is_deeply \@results, $map;
246 } "transactions with multiple active statement with $wrapper wrapper";
249 # test RNO detection when version detection fails
251 my $storage = $schema->storage;
252 my $version = $storage->_server_info->{normalized_dbms_version};
254 skip 'could not detect SQL Server version', 1 if not defined $version;
256 my $have_rno = $version >= 9 ? 1 : 0;
258 local $storage->{_dbh_details}{info} = {}; # delete cache
261 ($storage->sql_limit_dialect eq 'RowNumberOver') ? 1 : 0;
263 ok (($have_rno == $rno_detected),
264 'row_number() over support detected correctly');
268 my $schema = DBICTest::Schema->clone;
269 $schema->connection($dsn, $user, $pass);
271 like $schema->storage->sql_maker->{limit_dialect},
272 qr/^(?:Top|RowNumberOver)\z/,
273 'sql_maker is correct on unconnected schema';
277 # test op-induced autoconnect
280 my $schema = DBICTest::Schema->clone;
281 $schema->connection($dsn, $user, $pass);
283 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
284 is ($artist->id, 1, 'Artist retrieved successfully');
285 }, 'Query-induced autoconnect works');
289 local $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK} = 1;
290 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 0 });
292 my $rs = $schema2->resultset('Money');
295 $schema2->txn_commit;
297 is $rs->count, 0, 'initially empty'
298 || diag ('Found row with amount ' . $_->amount) for $rs->all;
300 $rs->create({ amount => 3000 });
301 $schema2->txn_rollback;
303 is $rs->count, 0, 'rolled back in AutoCommit=0'
304 || diag ('Found row with amount ' . $_->amount) for $rs->all;
306 $rs->create({ amount => 4000 });
307 $schema2->txn_commit;
309 cmp_ok $rs->first->amount, '==', 4000, 'committed in AutoCommit=0';
316 if (my $dbh = eval { $schema->storage->dbh }) {
317 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
318 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
319 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");