Commit | Line | Data |
70350518 |
1 | use strict; |
bbdda281 |
2 | use warnings; |
70350518 |
3 | |
4 | use Test::More; |
8c52ffd3 |
5 | use Test::Exception; |
65d35121 |
6 | use Scalar::Util 'weaken'; |
199fbc45 |
7 | use DBIx::Class::Optional::Dependencies (); |
70350518 |
8 | use lib qw(t/lib); |
9 | use DBICTest; |
eef2ff6c |
10 | |
11 | my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/}; |
12 | |
70350518 |
13 | plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test' |
eef2ff6c |
14 | unless ($dsn); |
15 | |
49b3a264 |
16 | |
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'); |
19 | |
77c7628c |
20 | { |
2c2bc4e5 |
21 | my $srv_ver = DBICTest->connect_schema($dsn, $user, $pass)->storage->_server_info->{dbms_version}; |
77c7628c |
22 | ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') ); |
23 | } |
24 | |
b30f1a32 |
25 | my $schema; |
26 | |
2c2bc4e5 |
27 | my $testdb_supports_placeholders = DBICTest->connect_schema($dsn, $user, $pass) |
bbdda281 |
28 | ->storage |
29 | ->_supports_typeless_placeholders; |
30 | my @test_storages = ( |
31 | $testdb_supports_placeholders ? 'DBI::Sybase::Microsoft_SQL_Server' : (), |
c29ce317 |
32 | 'DBI::Sybase::Microsoft_SQL_Server::NoBindVars', |
7379eb67 |
33 | ); |
8c52ffd3 |
34 | |
bbdda281 |
35 | for my $storage_type (@test_storages) { |
2c2bc4e5 |
36 | $schema = DBICTest->connect_schema($dsn, $user, $pass); |
eef2ff6c |
37 | |
bbdda281 |
38 | if ($storage_type =~ /NoBindVars\z/) { |
39 | # since we want to use the nobindvar - disable the capability so the |
40 | # rebless happens to the correct class |
41 | $schema->storage->_use_typeless_placeholders (0); |
7379eb67 |
42 | } |
43 | |
c1e5a9ac |
44 | local $ENV{DBIC_MSSQL_FREETDS_LOWVER_NOWARN} = 1; # disable nobindvars warning |
45 | |
bbdda281 |
46 | $schema->storage->ensure_connected; |
b30f1a32 |
47 | |
48 | if ($storage_type =~ /NoBindVars\z/) { |
49 | is $schema->storage->disable_sth_caching, 1, |
50 | 'prepare_cached disabled for NoBindVars'; |
51 | } |
52 | |
c29ce317 |
53 | isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type"); |
7379eb67 |
54 | |
99083752 |
55 | SKIP: { |
56 | skip 'This version of DBD::Sybase segfaults on disconnect', 1 if DBD::Sybase->VERSION < 1.08; |
7379eb67 |
57 | |
99083752 |
58 | # start disconnected to test _ping |
59 | $schema->storage->_dbh->disconnect; |
60 | |
61 | lives_ok { |
62 | $schema->storage->dbh_do(sub { $_[1]->do('select 1') }) |
63 | } '_ping works'; |
64 | } |
ecdf1ac8 |
65 | |
66 | my $dbh = $schema->storage->dbh; |
7379eb67 |
67 | |
68 | $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL |
69 | DROP TABLE artist"); |
70 | $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL |
71 | DROP TABLE cd"); |
72 | |
73 | $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);"); |
74 | $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 |
75 | # Just to test compat shim, Auto is in Core |
7379eb67 |
76 | $schema->class('Artist')->load_components('PK::Auto::MSSQL'); |
eef2ff6c |
77 | |
78 | # Test PK |
7379eb67 |
79 | my $new = $schema->resultset('Artist')->create( { name => 'foo' } ); |
80 | ok($new->artistid, "Auto-PK worked"); |
eef2ff6c |
81 | |
82 | # Test LIMIT |
7379eb67 |
83 | for (1..6) { |
84 | $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } ); |
85 | } |
eef2ff6c |
86 | |
7379eb67 |
87 | my $it = $schema->resultset('Artist')->search( { }, |
88 | { rows => 3, |
89 | offset => 2, |
90 | order_by => 'artistid' |
91 | } |
92 | ); |
eef2ff6c |
93 | |
b4474f31 |
94 | # Test ? in data don't get treated as placeholders |
7379eb67 |
95 | my $cd = $schema->resultset('CD')->create( { |
96 | artist => 1, |
97 | title => 'Does this break things?', |
98 | year => 2007, |
99 | } ); |
100 | ok($cd->id, 'Not treating ? in data as placeholders'); |
101 | |
102 | is( $it->count, 3, "LIMIT count ok" ); |
103 | ok( $it->next->name, "iterator->next ok" ); |
104 | $it->next; |
105 | $it->next; |
106 | is( $it->next, undef, "next past end of resultset ok" ); |
eef2ff6c |
107 | |
5064f5c3 |
108 | # test MONEY column support |
7379eb67 |
109 | $schema->storage->dbh_do (sub { |
110 | my ($storage, $dbh) = @_; |
111 | eval { $dbh->do("DROP TABLE money_test") }; |
112 | $dbh->do(<<'SQL'); |
7379eb67 |
113 | CREATE TABLE money_test ( |
114 | id INT IDENTITY PRIMARY KEY, |
115 | amount MONEY NULL |
116 | ) |
5064f5c3 |
117 | SQL |
b30f1a32 |
118 | }); |
5064f5c3 |
119 | |
65d35121 |
120 | my $rs = $schema->resultset('Money'); |
121 | weaken(my $rs_cp = $rs); # nested closure refcounting is an utter mess in perl |
5064f5c3 |
122 | |
7379eb67 |
123 | my $row; |
124 | lives_ok { |
125 | $row = $rs->create({ amount => 100 }); |
126 | } 'inserted a money value'; |
5064f5c3 |
127 | |
a33d2444 |
128 | cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip'; |
5064f5c3 |
129 | |
7379eb67 |
130 | lives_ok { |
131 | $row->update({ amount => 200 }); |
132 | } 'updated a money value'; |
5064f5c3 |
133 | |
a33d2444 |
134 | cmp_ok $rs->find($row->id)->amount, '==', 200, |
135 | 'updated money value round-trip'; |
5064f5c3 |
136 | |
7379eb67 |
137 | lives_ok { |
138 | $row->update({ amount => undef }); |
139 | } 'updated a money value to NULL'; |
5064f5c3 |
140 | |
c8365716 |
141 | is $rs->find($row->id)->amount, |
142 | undef, 'updated money value to NULL round-trip'; |
a467a0c9 |
143 | |
b90d7eba |
144 | $rs->delete; |
145 | |
146 | # test simple transaction with commit |
147 | lives_ok { |
148 | $schema->txn_do(sub { |
65d35121 |
149 | $rs_cp->create({ amount => 300 }); |
b90d7eba |
150 | }); |
151 | } 'simple transaction'; |
152 | |
b30f1a32 |
153 | cmp_ok $rs->first->amount, '==', 300, 'committed'; |
b90d7eba |
154 | |
b30f1a32 |
155 | $rs->reset; |
b90d7eba |
156 | $rs->delete; |
157 | |
158 | # test rollback |
159 | throws_ok { |
160 | $schema->txn_do(sub { |
65d35121 |
161 | $rs_cp->create({ amount => 700 }); |
b90d7eba |
162 | die 'mtfnpy'; |
163 | }); |
164 | } qr/mtfnpy/, 'simple failed txn'; |
165 | |
166 | is $rs->first, undef, 'rolled back'; |
b30f1a32 |
167 | |
b90d7eba |
168 | $rs->reset; |
b30f1a32 |
169 | $rs->delete; |
170 | |
171 | # test multiple active statements |
172 | { |
173 | $rs->create({ amount => 800 + $_ }) for 1..3; |
174 | |
175 | my @map = ( |
176 | [ 'Artist 1', '801.00' ], |
177 | [ 'Artist 2', '802.00' ], |
178 | [ 'Artist 3', '803.00' ] |
179 | ); |
180 | |
181 | my $artist_rs = $schema->resultset('Artist')->search({ |
182 | name => { -like => 'Artist %' } |
183 | });; |
184 | |
185 | my $i = 0; |
186 | |
187 | while (my $money_row = $rs->next) { |
188 | my $artist_row = $artist_rs->next; |
189 | |
190 | is_deeply [ $artist_row->name, $money_row->amount ], $map[$i++], |
191 | 'multiple active statements'; |
192 | } |
193 | $rs->reset; |
194 | $rs->delete; |
195 | } |
196 | |
c1e5a9ac |
197 | my $wrappers = { |
198 | no_transaction => sub { shift->() }, |
199 | txn_do => sub { my $code = shift; $schema->txn_do(sub { $code->() } ) }, |
200 | txn_begin => sub { $schema->txn_begin; shift->(); $schema->txn_commit }, |
201 | txn_guard => sub { my $g = $schema->txn_scope_guard; shift->(); $g->commit }, |
202 | }; |
7fc9679d |
203 | |
204 | # test transaction handling on a disconnected handle |
c1e5a9ac |
205 | for my $wrapper (keys %$wrappers) { |
206 | $rs->delete; |
207 | |
208 | # a reconnect should trigger on next action |
209 | $schema->storage->_get_dbh->disconnect; |
210 | |
65d35121 |
211 | |
c1e5a9ac |
212 | lives_and { |
213 | $wrappers->{$wrapper}->( sub { |
65d35121 |
214 | $rs_cp->create({ amount => 900 + $_ }) for 1..3; |
c1e5a9ac |
215 | }); |
216 | is $rs->count, 3; |
217 | } "transaction on disconnected handle with $wrapper wrapper"; |
218 | } |
219 | |
7fc9679d |
220 | # test transaction handling on a disconnected handle with multiple active |
221 | # statements |
222 | for my $wrapper (keys %$wrappers) { |
223 | $schema->storage->disconnect; |
224 | $rs->delete; |
225 | $rs->reset; |
226 | $rs->create({ amount => 1000 + $_ }) for (1..3); |
227 | |
228 | my $artist_rs = $schema->resultset('Artist')->search({ |
229 | name => { -like => 'Artist %' } |
230 | });; |
231 | |
232 | $rs->next; |
233 | |
234 | my $map = [ ['Artist 1', '1002.00'], ['Artist 2', '1003.00'] ]; |
235 | |
236 | weaken(my $a_rs_cp = $artist_rs); |
237 | |
c1e5a9ac |
238 | local $TODO = 'Transaction handling with multiple active statements will ' |
7fc9679d |
239 | .'need eager cursor support.' |
240 | unless $wrapper eq 'no_transaction'; |
241 | |
242 | lives_and { |
243 | my @results; |
244 | |
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 ]; |
249 | }; |
250 | }); |
251 | |
252 | is_deeply \@results, $map; |
253 | } "transactions with multiple active statement with $wrapper wrapper"; |
c1e5a9ac |
254 | } |
a218ef4e |
255 | |
256 | # test RNO detection when version detection fails |
257 | SKIP: { |
258 | my $storage = $schema->storage; |
259 | my $version = $storage->_server_info->{normalized_dbms_version}; |
99083752 |
260 | |
261 | skip 'could not detect SQL Server version', 1 if not defined $version; |
a218ef4e |
262 | |
263 | my $have_rno = $version >= 9 ? 1 : 0; |
264 | |
bbdda281 |
265 | local $storage->{_dbh_details}{info} = {}; # delete cache |
4282b6f8 |
266 | |
a218ef4e |
267 | my $rno_detected = |
6a247f33 |
268 | ($storage->sql_limit_dialect eq 'RowNumberOver') ? 1 : 0; |
a218ef4e |
269 | |
233c3a46 |
270 | ok (($have_rno == $rno_detected), |
a218ef4e |
271 | 'row_number() over support detected correctly'); |
272 | } |
0a064375 |
273 | |
274 | { |
2c2bc4e5 |
275 | my $schema = DBICTest->connect_schema($dsn, $user, $pass); |
0a064375 |
276 | |
277 | like $schema->storage->sql_maker->{limit_dialect}, |
278 | qr/^(?:Top|RowNumberOver)\z/, |
279 | 'sql_maker is correct on unconnected schema'; |
280 | } |
7379eb67 |
281 | } |
5064f5c3 |
282 | |
559ae74c |
283 | # test op-induced autoconnect |
284 | lives_ok (sub { |
285 | |
2c2bc4e5 |
286 | my $schema = DBICTest->connect_schema($dsn, $user, $pass); |
559ae74c |
287 | |
288 | my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next; |
289 | is ($artist->id, 1, 'Artist retrieved successfully'); |
290 | }, 'Query-induced autoconnect works'); |
291 | |
c1e5a9ac |
292 | # test AutoCommit=0 |
293 | { |
294 | local $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK} = 1; |
2c2bc4e5 |
295 | my $schema2 = DBICTest->connect_schema($dsn, $user, $pass, { AutoCommit => 0 }); |
c1e5a9ac |
296 | |
297 | my $rs = $schema2->resultset('Money'); |
298 | |
299 | $rs->delete; |
300 | $schema2->txn_commit; |
301 | |
302 | is $rs->count, 0, 'initially empty' |
303 | || diag ('Found row with amount ' . $_->amount) for $rs->all; |
304 | |
305 | $rs->create({ amount => 3000 }); |
306 | $schema2->txn_rollback; |
307 | |
308 | is $rs->count, 0, 'rolled back in AutoCommit=0' |
309 | || diag ('Found row with amount ' . $_->amount) for $rs->all; |
310 | |
311 | $rs->create({ amount => 4000 }); |
312 | $schema2->txn_commit; |
313 | |
314 | cmp_ok $rs->first->amount, '==', 4000, 'committed in AutoCommit=0'; |
315 | } |
316 | |
1a789a72 |
317 | done_testing; |
318 | |
3ff5b740 |
319 | # clean up our mess |
320 | END { |
7379eb67 |
321 | if (my $dbh = eval { $schema->storage->dbh }) { |
322 | $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist"); |
323 | $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd"); |
324 | $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test"); |
325 | } |
65d35121 |
326 | |
327 | undef $schema; |
3ff5b740 |
328 | } |