1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2 use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_mssql_sybase';
9 use Scalar::Util 'weaken';
13 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
15 my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
16 ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
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',
29 for my $storage_type (@test_storages) {
30 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
32 if ($storage_type =~ /NoBindVars\z/) {
33 # since we want to use the nobindvar - disable the capability so the
34 # rebless happens to the correct class
35 $schema->storage->_use_typeless_placeholders (0);
38 local $ENV{DBIC_MSSQL_FREETDS_LOWVER_NOWARN} = 1; # disable nobindvars warning
40 $schema->storage->ensure_connected;
42 if ($storage_type =~ /NoBindVars\z/) {
43 is $schema->storage->disable_sth_caching, 1,
44 'prepare_cached disabled for NoBindVars';
47 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
50 skip 'This version of DBD::Sybase segfaults on disconnect', 1 if DBD::Sybase->VERSION < 1.08;
52 # start disconnected to test _ping
53 $schema->storage->_dbh->disconnect;
56 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
60 my $dbh = $schema->storage->dbh;
62 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
64 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
67 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
68 $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);");
69 # Just to test compat shim, Auto is in Core
70 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
73 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
74 ok($new->artistid, "Auto-PK worked");
78 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
81 my $it = $schema->resultset('Artist')->search( { },
84 order_by => 'artistid'
88 # Test ? in data don't get treated as placeholders
89 my $cd = $schema->resultset('CD')->create( {
91 title => 'Does this break things?',
94 ok($cd->id, 'Not treating ? in data as placeholders');
96 is( $it->count, 3, "LIMIT count ok" );
97 ok( $it->next->name, "iterator->next ok" );
100 is( $it->next, undef, "next past end of resultset ok" );
102 # test MONEY column support
103 $schema->storage->dbh_do (sub {
104 my ($storage, $dbh) = @_;
105 eval { $dbh->do("DROP TABLE money_test") };
107 CREATE TABLE money_test (
108 id INT IDENTITY PRIMARY KEY,
114 my $rs = $schema->resultset('Money');
115 weaken(my $rs_cp = $rs); # nested closure refcounting is an utter mess in perl
119 $row = $rs->create({ amount => 100 });
120 } 'inserted a money value';
122 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
125 $row->update({ amount => 200 });
126 } 'updated a money value';
128 cmp_ok $rs->find($row->id)->amount, '==', 200,
129 'updated money value round-trip';
132 $row->update({ amount => undef });
133 } 'updated a money value to NULL';
135 is $rs->find($row->id)->amount,
136 undef, 'updated money value to NULL round-trip';
140 # test simple transaction with commit
142 $schema->txn_do(sub {
143 $rs_cp->create({ amount => 300 });
145 } 'simple transaction';
147 cmp_ok $rs->first->amount, '==', 300, 'committed';
154 $schema->txn_do(sub {
155 $rs_cp->create({ amount => 700 });
158 } qr/mtfnpy/, 'simple failed txn';
160 is $rs->first, undef, 'rolled back';
165 # test multiple active statements
167 $rs->create({ amount => 800 + $_ }) for 1..3;
170 [ 'Artist 1', '801.00' ],
171 [ 'Artist 2', '802.00' ],
172 [ 'Artist 3', '803.00' ]
175 my $artist_rs = $schema->resultset('Artist')->search({
176 name => { -like => 'Artist %' }
181 while (my $money_row = $rs->next) {
182 my $artist_row = $artist_rs->next;
184 is_deeply [ $artist_row->name, $money_row->amount ], $map[$i++],
185 'multiple active statements';
192 no_transaction => sub { shift->() },
193 txn_do => sub { my $code = shift; $schema->txn_do(sub { $code->() } ) },
194 txn_begin => sub { $schema->txn_begin; shift->(); $schema->txn_commit },
195 txn_guard => sub { my $g = $schema->txn_scope_guard; shift->(); $g->commit },
198 # test transaction handling on a disconnected handle
199 for my $wrapper (keys %$wrappers) {
202 # a reconnect should trigger on next action
203 $schema->storage->_get_dbh->disconnect;
207 $wrappers->{$wrapper}->( sub {
208 $rs_cp->create({ amount => 900 + $_ }) for 1..3;
211 } "transaction on disconnected handle with $wrapper wrapper";
214 # test transaction handling on a disconnected handle with multiple active
216 for my $wrapper (keys %$wrappers) {
217 $schema->storage->disconnect;
220 $rs->create({ amount => 1000 + $_ }) for (1..3);
222 my $artist_rs = $schema->resultset('Artist')->search({
223 name => { -like => 'Artist %' }
228 my $map = [ ['Artist 1', '1002.00'], ['Artist 2', '1003.00'] ];
230 weaken(my $a_rs_cp = $artist_rs);
232 local $TODO = 'Transaction handling with multiple active statements will '
233 .'need eager cursor support.'
234 unless $wrapper eq 'no_transaction';
239 $wrappers->{$wrapper}->( sub {
240 while (my $money = $rs_cp->next) {
241 my $artist = $a_rs_cp->next;
242 push @results, [ $artist->name, $money->amount ];
246 is_deeply \@results, $map;
247 } "transactions with multiple active statement with $wrapper wrapper";
250 # test RNO detection when version detection fails
252 my $storage = $schema->storage;
253 my $version = $storage->_server_info->{normalized_dbms_version};
255 skip 'could not detect SQL Server version', 1 if not defined $version;
257 my $have_rno = $version >= 9 ? 1 : 0;
259 local $storage->{_dbh_details}{info} = {}; # delete cache
262 ($storage->sql_limit_dialect eq 'RowNumberOver') ? 1 : 0;
264 ok (($have_rno == $rno_detected),
265 'row_number() over support detected correctly');
269 my $schema = DBICTest::Schema->clone;
270 $schema->connection($dsn, $user, $pass);
272 like $schema->storage->sql_maker->{limit_dialect},
273 qr/^(?:Top|RowNumberOver)\z/,
274 'sql_maker is correct on unconnected schema';
278 # test op-induced autoconnect
281 my $schema = DBICTest::Schema->clone;
282 $schema->connection($dsn, $user, $pass);
284 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
285 is ($artist->id, 1, 'Artist retrieved successfully');
286 }, 'Query-induced autoconnect works');
290 local $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK} = 1;
291 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 0 });
293 my $rs = $schema2->resultset('Money');
296 $schema2->txn_commit;
298 is $rs->count, 0, 'initially empty'
299 || diag ('Found row with amount ' . $_->amount) for $rs->all;
301 $rs->create({ amount => 3000 });
302 $schema2->txn_rollback;
304 is $rs->count, 0, 'rolled back in AutoCommit=0'
305 || diag ('Found row with amount ' . $_->amount) for $rs->all;
307 $rs->create({ amount => 4000 });
308 $schema2->txn_commit;
310 cmp_ok $rs->first->amount, '==', 4000, 'committed in AutoCommit=0';
317 if (my $dbh = eval { $schema->storage->dbh }) {
318 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
319 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
320 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");