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';
10 use DBIx::Class::_Util 'sigwarn_silencer';
14 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
16 my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
17 ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
22 my $testdb_supports_placeholders = DBICTest::Schema->connect($dsn, $user, $pass)
24 ->_supports_typeless_placeholders;
26 $testdb_supports_placeholders ? 'DBI::Sybase::Microsoft_SQL_Server' : (),
27 '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 local $ENV{DBIC_MSSQL_FREETDS_LOWVER_NOWARN} = 1; # disable nobindvars warning
41 $schema->storage->ensure_connected;
43 if ($storage_type =~ /NoBindVars\z/) {
44 is $schema->storage->disable_sth_caching, 1,
45 'prepare_cached disabled for NoBindVars';
48 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
51 skip 'This version of DBD::Sybase segfaults on disconnect', 1 if DBD::Sybase->VERSION < 1.08;
53 # start disconnected to test _ping
54 $schema->storage->_dbh->disconnect;
57 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
61 my $dbh = $schema->storage->dbh;
63 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
65 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
68 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
69 $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);");
70 # Just to test compat shim, Auto is in Core
71 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
74 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
75 ok($new->artistid, "Auto-PK worked");
79 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
82 my $it = $schema->resultset('Artist')->search( { },
85 order_by => 'artistid'
89 # Test ? in data don't get treated as placeholders
90 my $cd = $schema->resultset('CD')->create( {
92 title => 'Does this break things?',
95 ok($cd->id, 'Not treating ? in data as placeholders');
97 is( $it->count, 3, "LIMIT count ok" );
98 ok( $it->next->name, "iterator->next ok" );
101 is( $it->next, undef, "next past end of resultset ok" );
103 # test MONEY column support
104 $schema->storage->dbh_do (sub {
105 my ($storage, $dbh) = @_;
106 eval { $dbh->do("DROP TABLE money_test") };
108 CREATE TABLE money_test (
109 id INT IDENTITY PRIMARY KEY,
115 my $rs = $schema->resultset('Money');
116 weaken(my $rs_cp = $rs); # nested closure refcounting is an utter mess in perl
120 $row = $rs->create({ amount => 100 });
121 } 'inserted a money value';
123 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
126 $row->update({ amount => 200 });
127 } 'updated a money value';
129 cmp_ok $rs->find($row->id)->amount, '==', 200,
130 'updated money value round-trip';
133 $row->update({ amount => undef });
134 } 'updated a money value to NULL';
136 is $rs->find($row->id)->amount,
137 undef, 'updated money value to NULL round-trip';
141 # test simple transaction with commit
143 $schema->txn_do(sub {
144 $rs_cp->create({ amount => 300 });
146 } 'simple transaction';
148 cmp_ok $rs->first->amount, '==', 300, 'committed';
155 $schema->txn_do(sub {
156 $rs_cp->create({ amount => 700 });
159 } qr/mtfnpy/, 'simple failed txn';
161 is $rs->first, undef, 'rolled back';
166 # test multiple active statements
168 $rs->create({ amount => 800 + $_ }) for 1..3;
171 [ 'Artist 1', '801.00' ],
172 [ 'Artist 2', '802.00' ],
173 [ 'Artist 3', '803.00' ]
176 my $artist_rs = $schema->resultset('Artist')->search({
177 name => { -like => 'Artist %' }
182 while (my $money_row = $rs->next) {
183 my $artist_row = $artist_rs->next;
185 is_deeply [ $artist_row->name, $money_row->amount ], $map[$i++],
186 'multiple active statements';
193 no_transaction => sub { shift->() },
194 txn_do => sub { my $code = shift; $schema->txn_do(sub { $code->() } ) },
195 txn_begin => sub { $schema->txn_begin; shift->(); $schema->txn_commit },
196 txn_guard => sub { my $g = $schema->txn_scope_guard; shift->(); $g->commit },
199 # test transaction handling on a disconnected handle
200 for my $wrapper (keys %$wrappers) {
203 # a reconnect should trigger on next action
204 $schema->storage->_get_dbh->disconnect;
208 $wrappers->{$wrapper}->( sub {
209 $rs_cp->create({ amount => 900 + $_ }) for 1..3;
212 } "transaction on disconnected handle with $wrapper wrapper";
215 # test transaction handling on a disconnected handle with multiple active
217 for my $wrapper (keys %$wrappers) {
218 $schema->storage->disconnect;
221 $rs->create({ amount => 1000 + $_ }) for (1..3);
223 my $artist_rs = $schema->resultset('Artist')->search({
224 name => { -like => 'Artist %' }
229 my $map = [ ['Artist 1', '1002.00'], ['Artist 2', '1003.00'] ];
231 weaken(my $a_rs_cp = $artist_rs);
233 $wrapper ne 'no_transaction'
236 local $TODO = 'Transaction handling with multiple active statements will '
237 .'need eager cursor support.',
239 local local $SIG{__WARN__} = sigwarn_silencer qr/disconnect invalidates .+? active statement/
245 $wrappers->{$wrapper}->( sub {
246 while (my $money = $rs_cp->next) {
247 my $artist = $a_rs_cp->next;
248 push @results, [ $artist->name, $money->amount ];
252 is_deeply \@results, $map;
253 } "transactions with multiple active statement with $wrapper wrapper";
256 # test RNO detection when version detection fails
258 my $storage = $schema->storage;
259 my $version = $storage->_server_info->{normalized_dbms_version};
261 skip 'could not detect SQL Server version', 1 if not defined $version;
263 my $have_rno = $version >= 9 ? 1 : 0;
265 local $storage->{_dbh_details}{info} = {}; # delete cache
268 ($storage->sql_limit_dialect eq 'RowNumberOver') ? 1 : 0;
270 ok (($have_rno == $rno_detected),
271 'row_number() over support detected correctly');
275 my $schema = DBICTest::Schema->clone;
276 $schema->connection($dsn, $user, $pass);
278 like $schema->storage->sql_maker->{limit_dialect},
279 qr/^(?:Top|RowNumberOver)\z/,
280 'sql_maker is correct on unconnected schema';
284 # test op-induced autoconnect
287 my $schema = DBICTest::Schema->clone;
288 $schema->connection($dsn, $user, $pass);
290 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
291 is ($artist->id, 1, 'Artist retrieved successfully');
292 }, 'Query-induced autoconnect works');
296 local $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK} = 1;
297 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass, { AutoCommit => 0 });
299 my $rs = $schema2->resultset('Money');
302 $schema2->txn_commit;
304 is $rs->count, 0, 'initially empty'
305 || diag ('Found row with amount ' . $_->amount) for $rs->all;
307 $rs->create({ amount => 3000 });
308 $schema2->txn_rollback;
310 is $rs->count, 0, 'rolled back in AutoCommit=0'
311 || diag ('Found row with amount ' . $_->amount) for $rs->all;
313 $rs->create({ amount => 4000 });
314 $schema2->txn_commit;
316 cmp_ok $rs->first->amount, '==', 4000, 'committed in AutoCommit=0';
323 if (my $dbh = eval { $schema->storage->dbh }) {
324 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
325 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
326 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");