7 use DBIx::Class::Optional::Dependencies ();
11 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mssql_ado')
12 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mssql_ado');
14 # Example DSN (from frew):
15 # dbi:ADO:PROVIDER=sqlncli10;SERVER=tcp:172.24.2.10;MARS Connection=True;Initial Catalog=CIS;UID=cis_web;PWD=...;DataTypeCompatibility=80;
17 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ADO_${_}" } qw/DSN USER PASS/};
19 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ADO_DSN}, _USER and _PASS to run this test'
20 unless ($dsn && $user);
22 DBICTest::Schema->load_classes(qw/VaryingMAX ArtistGUID/);
24 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
25 $binstr{'large'} = $binstr{'small'} x 1024;
27 my $maxloblen = length $binstr{'large'};
29 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
31 LongReadLen => $maxloblen,
34 $schema->storage->ensure_connected;
36 isa_ok($schema->storage, 'DBIx::Class::Storage::DBI::ADO::Microsoft_SQL_Server');
38 my $ver = $schema->storage->_server_info->{normalized_dbms_version};
40 ok $ver, 'can introspect DBMS version';
43 is $schema->storage->sql_limit_dialect, ($ver >= 9 ? 'RowNumberOver' : 'Top'),
44 'correct limit dialect detected';
46 $schema->storage->dbh_do (sub {
47 my ($storage, $dbh) = @_;
48 try { local $^W = 0; $dbh->do("DROP TABLE artist") };
51 artistid INT IDENTITY NOT NULL,
53 rank INT NOT NULL DEFAULT '13',
54 charfield CHAR(10) NULL,
60 $schema->storage->dbh_do (sub {
61 my ($storage, $dbh) = @_;
62 try { local $^W = 0; $dbh->do("DROP TABLE artist_guid") };
64 CREATE TABLE artist_guid (
65 artistid UNIQUEIDENTIFIER NOT NULL,
68 charfield CHAR(10) NULL,
69 a_guid UNIQUEIDENTIFIER,
75 my $have_max = $ver >= 9; # 2005 and greater
77 $schema->storage->dbh_do (sub {
78 my ($storage, $dbh) = @_;
79 try { local $^W = 0; $dbh->do("DROP TABLE varying_max_test") };
81 CREATE TABLE varying_max_test (
82 id INT IDENTITY NOT NULL,
84 varchar_max VARCHAR(MAX),
85 nvarchar_max NVARCHAR(MAX),
86 varbinary_max VARBINARY(MAX),
96 my $ars = $schema->resultset('Artist');
98 my $new = $ars->create({ name => 'foo' });
99 ok($new->artistid > 0, 'Auto-PK worked');
101 # make sure select works
102 my $found = $schema->resultset('Artist')->search({ name => 'foo' })->first;
103 is $found->artistid, $new->artistid, 'search works';
105 # test large column list in select
106 $found = $schema->resultset('Artist')->search({ name => 'foo' }, {
107 select => ['artistid', 'name', map \"'foo' foo_$_", 0..50],
108 as => ['artistid', 'name', map "foo_$_", 0..50],
110 is $found->artistid, $new->artistid, 'select with big column list';
111 is $found->get_column('foo_50'), 'foo', 'last item in big column list';
113 # create a few more rows
115 $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
118 # test multiple active cursors
119 my $rs1 = $schema->resultset('Artist')->search({}, { order_by => 'artistid' });
120 my $rs2 = $schema->resultset('Artist')->search({}, { order_by => 'name' });
123 ok try { $rs2->next }, 'multiple active cursors';
126 # test bug where ADO blows up if the first bindparam is shorter than the second
127 is $schema->resultset('Artist')->search({ artistid => 2 })->first->name,
131 is $schema->resultset('Artist')->search({ artistid => 13 })->first->name,
135 # test explicit key spec
136 $new = $ars->create ({ name => 'bar', artistid => 66 });
137 is($new->artistid, 66, 'Explicit PK worked');
138 $new->discard_changes;
139 is($new->artistid, 66, 'Explicit PK assigned');
141 # test basic transactions
142 $schema->txn_do(sub {
143 $ars->create({ name => 'transaction_commit' });
145 ok($ars->search({ name => 'transaction_commit' })->first,
146 'transaction committed');
147 $ars->search({ name => 'transaction_commit' })->delete,
149 $schema->txn_do(sub {
150 $ars->create({ name => 'transaction_rollback' });
153 } qr/rolling back/, 'rollback executed';
154 is $ars->search({ name => 'transaction_rollback' })->first, undef,
155 'transaction rolled back';
157 # test two-phase commit and inner transaction rollback from nested transactions
158 $schema->txn_do(sub {
159 $ars->create({ name => 'in_outer_transaction' });
160 $schema->txn_do(sub {
161 $ars->create({ name => 'in_inner_transaction' });
163 ok($ars->search({ name => 'in_inner_transaction' })->first,
164 'commit from inner transaction visible in outer transaction');
166 $schema->txn_do(sub {
167 $ars->create({ name => 'in_inner_transaction_rolling_back' });
168 die 'rolling back inner transaction';
170 } qr/rolling back inner transaction/, 'inner transaction rollback executed';
172 ok($ars->search({ name => 'in_outer_transaction' })->first,
173 'commit from outer transaction');
174 ok($ars->search({ name => 'in_inner_transaction' })->first,
175 'commit from inner transaction');
176 is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
178 'rollback from inner transaction';
179 $ars->search({ name => 'in_outer_transaction' })->delete;
180 $ars->search({ name => 'in_inner_transaction' })->delete;
186 push @pop, { name => "Artist_$_" };
188 $ars->populate (\@pop);
191 # test populate with explicit key
195 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
197 $ars->populate (\@pop);
200 # count what we did so far
201 is ($ars->count, 18, 'Simple count works');
204 my $current_artistid = $ars->search({}, {
205 select => [ { max => 'artistid' } ], as => ['artistid']
209 lives_ok { $row = $ars->create({}) }
210 'empty insert works';
212 $row->discard_changes;
214 is $row->artistid, $current_artistid+1,
215 'empty insert generated correct PK';
217 # test that autoinc column still works after empty insert
218 $row = $ars->create({ name => 'after_empty_insert' });
220 is $row->artistid, $current_artistid+2,
221 'autoincrement column functional aftear empty insert';
223 my $rs = $schema->resultset('VaryingMAX');
225 foreach my $size (qw/small large/) {
226 my $orig_debug = $schema->storage->debug;
228 $schema->storage->debug(0) if $size eq 'large';
230 my $str = $binstr{$size};
234 varchar_max => $str, nvarchar_max => $str, varbinary_max => $str
236 } "created $size VARXXX(MAX) LOBs";
239 $row->discard_changes;
240 } 're-selected just-inserted LOBs';
242 cmp_ok try { $row->varchar_max }, 'eq', $str, 'VARCHAR(MAX) matches';
243 cmp_ok try { $row->nvarchar_max }, 'eq', $str, 'NVARCHAR(MAX) matches';
244 cmp_ok try { $row->varbinary_max }, 'eq', $str, 'VARBINARY(MAX) matches';
246 $schema->storage->debug($orig_debug);
251 try { local $^W = 0; $schema->storage->dbh->do('DROP TABLE bindtype_test') };
252 $schema->storage->dbh->do(qq[
253 CREATE TABLE bindtype_test
255 id INT IDENTITY NOT NULL PRIMARY KEY,
261 ],{ RaiseError => 1, PrintError => 1 });
263 $rs = $schema->resultset('BindType');
266 foreach my $type (qw( blob clob a_memo )) {
267 foreach my $size (qw( small large )) {
270 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
271 "inserted $size $type without dying" or next;
273 my $from_db = eval { $rs->find($id)->$type } || '';
276 ok($from_db eq $binstr{$size}, "verified inserted $size $type" )
279 join '', map sprintf('%02X', ord), split //, shift
281 diag 'Got: ', "\n", substr($hexdump->($from_db),0,255), '...',
282 substr($hexdump->($from_db),-255);
283 diag 'Size: ', length($from_db);
284 diag 'Expected Size: ', length($binstr{$size});
285 diag 'Expected: ', "\n",
286 substr($hexdump->($binstr{$size}), 0, 255),
287 "...", substr($hexdump->($binstr{$size}),-255);
293 $rs->search({ id => 0 })->update({ blob => $binstr{small} });
294 } 'updated IMAGE to small binstr without dying';
297 $rs->search({ id => 0 })->update({ blob => $binstr{large} });
298 } 'updated IMAGE to large binstr without dying';
302 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
303 } 'created a row with a GUID';
306 eval { $row->artistid },
307 'row has GUID PK col populated',
311 my $guid = try { $row->artistid }||'';
313 ok(($guid !~ /^{.*?}\z/), 'GUID not enclosed in braces')
314 or diag "GUID is: $guid";
317 eval { $row->a_guid },
318 'row has a GUID col with auto_nextval populated',
322 my $row_from_db = $schema->resultset('ArtistGUID')
323 ->search({ name => 'mtfnpy' })->first;
325 is try { $row_from_db->artistid }, try { $row->artistid },
326 'PK GUID round trip (via ->search->next)';
328 is try { $row_from_db->a_guid }, try { $row->a_guid },
329 'NON-PK GUID round trip (via ->search->next)';
331 $row_from_db = try { $schema->resultset('ArtistGUID')
332 ->find($row->artistid) };
334 is try { $row_from_db->artistid }, try { $row->artistid },
335 'PK GUID round trip (via ->find)';
337 is try { $row_from_db->a_guid }, try { $row->a_guid },
338 'NON-PK GUID round trip (via ->find)';
340 ($row_from_db) = $schema->resultset('ArtistGUID')
341 ->search({ name => 'mtfnpy' })->all;
343 is try { $row_from_db->artistid }, try { $row->artistid },
344 'PK GUID round trip (via ->search->all)';
346 is try { $row_from_db->a_guid }, try { $row->a_guid },
347 'NON-PK GUID round trip (via ->search->all)';
350 $row = $schema->resultset('ArtistGUID')->create({
351 artistid => '70171270-4822-4450-81DF-921F99BA3C06',
352 name => 'explicit_guid',
354 } 'created a row with explicit PK GUID';
356 is try { $row->artistid }, '70171270-4822-4450-81DF-921F99BA3C06',
357 'row has correct PK GUID';
360 $row->update({ artistid => '70171270-4822-4450-81DF-921F99BA3C07' });
361 } "updated row's PK GUID";
363 is try { $row->artistid }, '70171270-4822-4450-81DF-921F99BA3C07',
364 'row has correct PK GUID';
371 $schema->resultset('ArtistGUID')->populate([{
372 artistid => '70171270-4822-4450-81DF-921F99BA3C06',
373 name => 'explicit_guid',
375 } 'created a row with explicit PK GUID via ->populate in void context';
381 local $SIG{__WARN__} = sub {};
382 if (my $dbh = try { $schema->storage->_dbh }) {
383 (try { $dbh->do("DROP TABLE $_") })
384 for qw/artist artist_guid varying_max_test bindtype_test/;