8 use DBIx::Class::Optional::Dependencies ();
9 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mssql_odbc')
10 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mssql_odbc');
15 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
17 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
18 unless ($dsn && $user);
21 my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
22 ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
25 DBICTest::Schema->load_classes('ArtistGUID');
26 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
29 no warnings 'redefine';
30 my $connect_count = 0;
31 my $orig_connect = \&DBI::connect;
32 local *DBI::connect = sub { $connect_count++; goto &$orig_connect };
34 $schema->storage->ensure_connected;
36 is( $connect_count, 1, 'only one connection made');
39 isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
42 my $schema2 = $schema->connect (@{$schema->storage->connect_info});
43 ok (! $schema2->storage->connected, 'a re-connected cloned schema starts unconnected');
45 $schema->storage->_dbh->disconnect;
48 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
53 { opts => { on_connect_call => 'use_mars' } },
54 use_dynamic_cursors =>
55 { opts => { on_connect_call => 'use_dynamic_cursors' },
56 required => $schema->storage->_using_freetds ? 0 : 1,
59 { opts => { on_connect_call => 'use_server_cursors' } },
61 { opts => {}, required => 1 },
64 for my $opts_name (keys %opts) {
66 my $opts = $opts{$opts_name}{opts};
67 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
70 $schema->storage->ensure_connected
73 if ($opts{$opts_name}{required}) {
74 die "on_connect_call option '$opts_name' is not functional: $_";
78 "on_connect_call option '$opts_name' not functional in this configuration: $_",
84 $schema->storage->dbh_do (sub {
85 my ($storage, $dbh) = @_;
86 eval { $dbh->do("DROP TABLE artist") };
89 artistid INT IDENTITY NOT NULL,
91 rank INT NOT NULL DEFAULT '13',
92 charfield CHAR(10) NULL,
99 $schema->resultset('Artist')->search({ name => 'foo' })->delete;
101 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
103 ok(($new->artistid||0) > 0, "Auto-PK worked for $opts_name");
105 # Test multiple active statements
107 skip 'not a multiple active statements configuration', 1
108 if $opts_name eq 'plain';
110 $schema->storage->ensure_connected;
114 no warnings 'redefine';
115 local *DBI::connect = sub { die "NO RECONNECTS!!!" };
117 my $artist_rs = $schema->resultset('Artist');
121 $artist_rs->create({ name => "Artist$_" }) for (1..3);
123 my $forward = $artist_rs->search({},
124 { order_by => { -asc => 'artistid' } });
125 my $backward = $artist_rs->search({},
126 { order_by => { -desc => 'artistid' } });
129 [qw/Artist1 Artist3/], [qw/Artist2 Artist2/], [qw/Artist3 Artist1/]
133 while (my $forward_row = $forward->next) {
134 my $backward_row = $backward->next;
135 push @result, [$forward_row->name, $backward_row->name];
138 is_deeply \@result, \@map, "multiple active statements in $opts_name";
142 is($artist_rs->count, 0, '$dbh still viable');
143 } "Multiple active statements survive $opts_name";
149 $schema->storage->dbh_do (sub {
150 my ($storage, $dbh) = @_;
151 eval { $dbh->do("DROP TABLE owners") };
152 eval { $dbh->do("DROP TABLE books") };
155 id INT IDENTITY (1, 1) NOT NULL,
162 CREATE TABLE owners (
163 id INT IDENTITY (1, 1) NOT NULL,
170 # start a new connection, make sure rebless works
171 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
172 $schema->populate ('Owners', [
185 [qw/12 face_to_face/],
190 }, 'populate with PKs supplied ok' );
194 # start a new connection, make sure rebless works
195 # test an insert with a supplied identity, followed by one without
196 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
199 $schema->resultset ('Owners')->create ({ id => $id, name => "troglodoogle $id" });
200 $schema->resultset ('Owners')->create ({ name => "troglodoogle " . ($id + 1) });
202 }, 'create with/without PKs ok' );
204 is ($schema->resultset ('Owners')->count, 19, 'owner rows really in db' );
207 # start a new connection, make sure rebless works
208 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
209 $schema->populate ('BooksInLibrary', [
210 [qw/source owner title /],
211 [qw/Library 1 secrets0/],
212 [qw/Library 1 secrets1/],
213 [qw/Eatery 1 secrets2/],
214 [qw/Library 2 secrets3/],
215 [qw/Library 3 secrets4/],
216 [qw/Eatery 3 secrets5/],
217 [qw/Library 4 secrets6/],
218 [qw/Library 5 secrets7/],
219 [qw/Eatery 5 secrets8/],
220 [qw/Library 6 secrets9/],
221 [qw/Library 7 secrets10/],
222 [qw/Eatery 7 secrets11/],
223 [qw/Library 8 secrets12/],
225 }, 'populate without PKs supplied ok' );
228 # test simple, complex LIMIT and limited prefetch support, with both dialects and quote combinations (if possible)
231 ($schema->storage->_server_info->{normalized_dbms_version} || 0 ) >= 9
236 for my $quoted (0, 1) {
238 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
239 limit_dialect => $dialect,
242 ? ( quote_char => [ qw/ [ ] / ], name_sep => '.' )
247 my $test_type = "Dialect:$dialect Quoted:$quoted";
249 # basic limit support
251 my $art_rs = $schema->resultset ('Artist');
253 $art_rs->create({ name => 'Artist ' . $_ }) for (1..6);
255 my $it = $schema->resultset('Artist')->search( {}, {
258 order_by => 'artistid',
261 is( $it->count, 3, "$test_type: LIMIT count ok" );
263 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
264 if $dialect eq 'Top';
266 is( $it->next->name, 'Artist 4', "$test_type: iterator->next ok" );
268 is( $it->next->name, 'Artist 6', "$test_type: iterator->next ok" );
269 is( $it->next, undef, "$test_type: next past end of resultset ok" );
272 # plain ordered subqueries throw
274 $schema->resultset('Owners')->search ({}, { order_by => 'name' })->as_query
275 }, qr/ordered subselect encountered/, "$test_type: Ordered Subselect detection throws ok");
277 # make sure ordered subselects *somewhat* work
279 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
280 my $sealed_owners = $owners->as_subselect_rs;
283 [ map { $_->name } ($sealed_owners->all) ],
284 [ map { $_->name } ($owners->all) ],
285 "$test_type: Sort preserved from within a subquery",
289 # still even with lost order of IN, we should be getting correct
292 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
293 my $corelated_owners = $owners->result_source->resultset->search (
295 id => { -in => $owners->get_column('id')->as_query },
298 order_by => 'name' #reorder because of what is shown above
303 join ("\x00", map { $_->name } ($corelated_owners->all) ),
304 join ("\x00", map { $_->name } ($owners->all) ),
305 "$test_type: With an outer order_by, everything still matches",
309 # make sure right-join-side single-prefetch ordering limit works
311 my $rs = $schema->resultset ('BooksInLibrary')->search (
313 'owner.name' => { '!=', 'woggle' },
317 order_by => 'owner.name',
320 # this is the order in which they should come from the above query
321 my @owner_names = qw/boggle fISMBoC fREW fRIOUX fROOH fRUE wiggle wiggle/;
323 is ($rs->all, 8, "$test_type: Correct amount of objects from right-sorted joined resultset");
325 [map { $_->owner->name } ($rs->all) ],
327 "$test_type: Prefetched rows were properly ordered"
330 my $limited_rs = $rs->search ({}, {rows => 6, offset => 2, unsafe_subselect_ok => 1});
331 is ($limited_rs->count, 6, "$test_type: Correct count of limited right-sorted joined resultset");
332 is ($limited_rs->count_rs->next, 6, "$test_type: Correct count_rs of limited right-sorted joined resultset");
334 $schema->is_executed_querycount( sub {
336 [map { $_->owner->name } ($limited_rs->all) ],
337 [@owner_names[2 .. 7]],
338 "$test_type: Prefetch-limited rows were properly ordered"
340 }, 1, "$test_type: Only one query with prefetch" );
343 [map { $_->name } ($limited_rs->search_related ('owner')->all) ],
344 [@owner_names[2 .. 7]],
345 "$test_type: Rows are still properly ordered after search_related",
349 # try a ->has_many direction with duplicates
350 my $owners = $schema->resultset ('Owners')->search (
352 'books.id' => { '!=', undef },
353 'me.name' => { '!=', 'somebogusstring' },
357 order_by => [ { -asc => \['name + ?', [ test => 'xxx' ]] }, 'me.id' ], # test bindvar propagation
358 group_by => [ map { "me.$_" } $schema->source('Owners')->columns ], # the literal order_by requires an explicit group_by
359 rows => 3, # 8 results total
360 unsafe_subselect_ok => 1,
364 is ($owners->page(1)->all, 3, "$test_type: has_many prefetch returns correct number of rows");
365 is ($owners->page(1)->count, 3, "$test_type: has-many prefetch returns correct count");
367 is ($owners->page(3)->count, 2, "$test_type: has-many prefetch returns correct count");
369 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
370 if $dialect eq 'Top';
371 is ($owners->page(3)->all, 2, "$test_type: has_many prefetch returns correct number of rows");
372 is ($owners->page(3)->count_rs->next, 2, "$test_type: has-many prefetch returns correct count_rs");
376 # try a ->belongs_to direction (no select collapse, group_by should work)
377 my $books = $schema->resultset ('BooksInLibrary')->search (
379 'owner.name' => [qw/wiggle woggle/],
383 having => \['1 = ?', [ test => 1 ] ], #test having propagation
385 rows => 2, # 3 results total
386 order_by => [{ -desc => 'me.owner' }, 'me.id'],
387 unsafe_subselect_ok => 1,
391 is ($books->page(1)->all, 2, "$test_type: Prefetched grouped search returns correct number of rows");
392 is ($books->page(1)->count, 2, "$test_type: Prefetched grouped search returns correct count");
394 is ($books->page(2)->count, 1, "$test_type: Prefetched grouped search returns correct count");
396 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
397 if $dialect eq 'Top';
398 is ($books->page(2)->all, 1, "$test_type: Prefetched grouped search returns correct number of rows");
399 is ($books->page(2)->count_rs->next, 1, "$test_type: Prefetched grouped search returns correct count_rs");
407 $schema->storage->dbh_do (sub {
408 my ($storage, $dbh) = @_;
409 eval { $dbh->do("DROP TABLE artist_guid") };
411 CREATE TABLE artist_guid (
412 artistid UNIQUEIDENTIFIER NOT NULL,
414 rank INT NOT NULL DEFAULT '13',
415 charfield CHAR(10) NULL,
416 a_guid UNIQUEIDENTIFIER,
417 primary key(artistid)
422 # start disconnected to make sure insert works on an un-reblessed storage
423 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
427 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
428 } 'created a row with a GUID';
431 eval { $row->artistid },
432 'row has GUID PK col populated',
437 eval { $row->a_guid },
438 'row has a GUID col with auto_nextval populated',
442 my $row_from_db = $schema->resultset('ArtistGUID')
443 ->search({ name => 'mtfnpy' })->first;
445 is $row_from_db->artistid, $row->artistid,
446 'PK GUID round trip';
448 is $row_from_db->a_guid, $row->a_guid,
449 'NON-PK GUID round trip';
454 $schema->storage->dbh_do (sub {
455 my ($storage, $dbh) = @_;
456 eval { $dbh->do("DROP TABLE money_test") };
458 CREATE TABLE money_test (
459 id INT IDENTITY PRIMARY KEY,
466 my $freetds_and_dynamic_cursors = 1
467 if $opts_name eq 'use_dynamic_cursors' &&
468 $schema->storage->_using_freetds;
471 'these tests fail on freetds with dynamic cursors for some reason'
472 if $freetds_and_dynamic_cursors;
473 local $ENV{DBIC_NULLABLE_KEY_NOWARN} = 1
474 if $freetds_and_dynamic_cursors;
476 my $rs = $schema->resultset('Money');
480 $row = $rs->create({ amount => 100 });
481 } 'inserted a money value';
483 cmp_ok ((try { $rs->find($row->id)->amount })||0, '==', 100,
484 'money value round-trip');
487 $row->update({ amount => 200 });
488 } 'updated a money value';
490 cmp_ok ((try { $rs->find($row->id)->amount })||0, '==', 200,
491 'updated money value round-trip');
494 $row->update({ amount => undef });
495 } 'updated a money value to NULL';
497 is try { $rs->find($row->id)->amount }, undef,
498 'updated money value to NULL round-trip';
508 if (my $dbh = eval { $schema->storage->_dbh }) {
509 eval { $dbh->do("DROP TABLE $_") }
510 for qw/artist artist_guid money_test books owners/;