Extend tests for MARs under MSSQL over DBD::Sybase
[dbsrgits/DBIx-Class.git] / t / 74mssql.t
CommitLineData
70350518 1use strict;
bbdda281 2use warnings;
70350518 3
46e3af47 4# use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
5BEGIN {
6 if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
7 unshift @INC, $_ for split /:/, $lib_dirs;
8 }
9}
10
70350518 11use Test::More;
8c52ffd3 12use Test::Exception;
70350518 13use lib qw(t/lib);
14use DBICTest;
eef2ff6c 15
16my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
17
70350518 18plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
eef2ff6c 19 unless ($dsn);
20
77c7628c 21{
22 my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
23 ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
24}
25
b30f1a32 26my $schema;
27
bbdda281 28my $testdb_supports_placeholders = DBICTest::Schema->connect($dsn, $user, $pass)
29 ->storage
30 ->_supports_typeless_placeholders;
31my @test_storages = (
32 $testdb_supports_placeholders ? 'DBI::Sybase::Microsoft_SQL_Server' : (),
c29ce317 33 'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
7379eb67 34);
8c52ffd3 35
bbdda281 36for my $storage_type (@test_storages) {
37 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
eef2ff6c 38
bbdda281 39 if ($storage_type =~ /NoBindVars\z/) {
40 # since we want to use the nobindvar - disable the capability so the
41 # rebless happens to the correct class
42 $schema->storage->_use_typeless_placeholders (0);
7379eb67 43 }
44
bbdda281 45 $schema->storage->ensure_connected;
b30f1a32 46
47 if ($storage_type =~ /NoBindVars\z/) {
48 is $schema->storage->disable_sth_caching, 1,
49 'prepare_cached disabled for NoBindVars';
50 }
51
c29ce317 52 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
7379eb67 53
99083752 54 SKIP: {
55 skip 'This version of DBD::Sybase segfaults on disconnect', 1 if DBD::Sybase->VERSION < 1.08;
7379eb67 56
99083752 57 # start disconnected to test _ping
58 $schema->storage->_dbh->disconnect;
59
60 lives_ok {
61 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
62 } '_ping works';
63 }
ecdf1ac8 64
65 my $dbh = $schema->storage->dbh;
7379eb67 66
67 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
68 DROP TABLE artist");
69 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
70 DROP TABLE cd");
71
72 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
73 $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);");
3ff5b740 74# Just to test compat shim, Auto is in Core
7379eb67 75 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
eef2ff6c 76
77# Test PK
7379eb67 78 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
79 ok($new->artistid, "Auto-PK worked");
eef2ff6c 80
81# Test LIMIT
7379eb67 82 for (1..6) {
83 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
84 }
eef2ff6c 85
7379eb67 86 my $it = $schema->resultset('Artist')->search( { },
87 { rows => 3,
88 offset => 2,
89 order_by => 'artistid'
90 }
91 );
eef2ff6c 92
b4474f31 93# Test ? in data don't get treated as placeholders
7379eb67 94 my $cd = $schema->resultset('CD')->create( {
95 artist => 1,
96 title => 'Does this break things?',
97 year => 2007,
98 } );
99 ok($cd->id, 'Not treating ? in data as placeholders');
100
101 is( $it->count, 3, "LIMIT count ok" );
102 ok( $it->next->name, "iterator->next ok" );
103 $it->next;
104 $it->next;
105 is( $it->next, undef, "next past end of resultset ok" );
eef2ff6c 106
5064f5c3 107# test MONEY column support
7379eb67 108 $schema->storage->dbh_do (sub {
109 my ($storage, $dbh) = @_;
110 eval { $dbh->do("DROP TABLE money_test") };
111 $dbh->do(<<'SQL');
7379eb67 112 CREATE TABLE money_test (
113 id INT IDENTITY PRIMARY KEY,
114 amount MONEY NULL
115 )
5064f5c3 116SQL
b30f1a32 117 });
5064f5c3 118
b30f1a32 119 my $rs = $schema->resultset('Money');
5064f5c3 120
7379eb67 121 my $row;
122 lives_ok {
123 $row = $rs->create({ amount => 100 });
124 } 'inserted a money value';
5064f5c3 125
a33d2444 126 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
5064f5c3 127
7379eb67 128 lives_ok {
129 $row->update({ amount => 200 });
130 } 'updated a money value';
5064f5c3 131
a33d2444 132 cmp_ok $rs->find($row->id)->amount, '==', 200,
133 'updated money value round-trip';
5064f5c3 134
7379eb67 135 lives_ok {
136 $row->update({ amount => undef });
137 } 'updated a money value to NULL';
5064f5c3 138
c8365716 139 is $rs->find($row->id)->amount,
140 undef, 'updated money value to NULL round-trip';
a467a0c9 141
b90d7eba 142 $rs->delete;
143
144 # test simple transaction with commit
145 lives_ok {
146 $schema->txn_do(sub {
b30f1a32 147 $rs->create({ amount => 300 });
b90d7eba 148 });
149 } 'simple transaction';
150
b30f1a32 151 cmp_ok $rs->first->amount, '==', 300, 'committed';
b90d7eba 152
b30f1a32 153 $rs->reset;
b90d7eba 154 $rs->delete;
155
156 # test rollback
157 throws_ok {
158 $schema->txn_do(sub {
b30f1a32 159 $rs->create({ amount => 700 });
b90d7eba 160 die 'mtfnpy';
161 });
162 } qr/mtfnpy/, 'simple failed txn';
163
164 is $rs->first, undef, 'rolled back';
b30f1a32 165
b90d7eba 166 $rs->reset;
b30f1a32 167 $rs->delete;
168
169 # test multiple active statements
170 {
171 $rs->create({ amount => 800 + $_ }) for 1..3;
172
173 my @map = (
174 [ 'Artist 1', '801.00' ],
175 [ 'Artist 2', '802.00' ],
176 [ 'Artist 3', '803.00' ]
177 );
178
179 my $artist_rs = $schema->resultset('Artist')->search({
180 name => { -like => 'Artist %' }
181 });;
182
183 my $i = 0;
184
185 while (my $money_row = $rs->next) {
186 my $artist_row = $artist_rs->next;
187
188 is_deeply [ $artist_row->name, $money_row->amount ], $map[$i++],
189 'multiple active statements';
190 }
191 $rs->reset;
192 $rs->delete;
193 }
194
a218ef4e 195
196 # test RNO detection when version detection fails
197 SKIP: {
198 my $storage = $schema->storage;
199 my $version = $storage->_server_info->{normalized_dbms_version};
99083752 200
201 skip 'could not detect SQL Server version', 1 if not defined $version;
a218ef4e 202
203 my $have_rno = $version >= 9 ? 1 : 0;
204
bbdda281 205 local $storage->{_dbh_details}{info} = {}; # delete cache
4282b6f8 206
a218ef4e 207 my $rno_detected =
6a247f33 208 ($storage->sql_limit_dialect eq 'RowNumberOver') ? 1 : 0;
a218ef4e 209
233c3a46 210 ok (($have_rno == $rno_detected),
a218ef4e 211 'row_number() over support detected correctly');
212 }
0a064375 213
214 {
215 my $schema = DBICTest::Schema->clone;
216 $schema->connection($dsn, $user, $pass);
217
218 like $schema->storage->sql_maker->{limit_dialect},
219 qr/^(?:Top|RowNumberOver)\z/,
220 'sql_maker is correct on unconnected schema';
221 }
7379eb67 222}
5064f5c3 223
559ae74c 224# test op-induced autoconnect
225lives_ok (sub {
226
227 my $schema = DBICTest::Schema->clone;
228 $schema->connection($dsn, $user, $pass);
229
230 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
231 is ($artist->id, 1, 'Artist retrieved successfully');
232}, 'Query-induced autoconnect works');
233
1a789a72 234done_testing;
235
3ff5b740 236# clean up our mess
237END {
7379eb67 238 if (my $dbh = eval { $schema->storage->dbh }) {
239 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
240 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
241 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");
242 }
3ff5b740 243}