8 use DBIC::SqlMakerTest;
11 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
13 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
14 unless ($dsn && $user);
17 my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
18 ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
21 DBICTest::Schema->load_classes('ArtistGUID');
22 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
25 no warnings 'redefine';
26 my $connect_count = 0;
27 my $orig_connect = \&DBI::connect;
28 local *DBI::connect = sub { $connect_count++; goto &$orig_connect };
30 $schema->storage->ensure_connected;
32 is( $connect_count, 1, 'only one connection made');
35 isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
38 my $schema2 = $schema->connect ($schema->storage->connect_info);
39 ok (! $schema2->storage->connected, 'a re-connected cloned schema starts unconnected');
42 $schema->storage->_dbh->disconnect;
45 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
50 { on_connect_call => 'use_mars' },
51 use_dynamic_cursors =>
52 { on_connect_call => 'use_dynamic_cursors' },
54 { on_connect_call => 'use_server_cursors' },
59 for my $opts_name (keys %opts) {
61 my $opts = $opts{$opts_name};
62 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
65 $schema->storage->ensure_connected
68 skip "$opts_name not functional in this configuration: $_", 1;
71 $schema->storage->dbh_do (sub {
72 my ($storage, $dbh) = @_;
73 eval { $dbh->do("DROP TABLE artist") };
76 artistid INT IDENTITY NOT NULL,
78 rank INT NOT NULL DEFAULT '13',
79 charfield CHAR(10) NULL,
86 $schema->resultset('Artist')->search({ name => 'foo' })->delete;
88 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
90 ok(($new->artistid||0) > 0, "Auto-PK worked for $opts_name");
92 # Test multiple active statements
94 skip 'not a multiple active statements configuration', 1
95 if $opts_name eq 'plain';
97 my $artist_rs = $schema->resultset('Artist');
101 $artist_rs->create({ name => "Artist$_" }) for (1..3);
103 my $forward = $artist_rs->search({},
104 { order_by => { -asc => 'artistid' } });
105 my $backward = $artist_rs->search({},
106 { order_by => { -desc => 'artistid' } });
109 [qw/Artist1 Artist3/], [qw/Artist2 Artist2/], [qw/Artist3 Artist1/]
113 while (my $forward_row = $forward->next) {
114 my $backward_row = $backward->next;
115 push @result, [$forward_row->name, $backward_row->name];
118 is_deeply \@result, \@map, "multiple active statements in $opts_name";
126 $schema->storage->dbh_do (sub {
127 my ($storage, $dbh) = @_;
128 eval { $dbh->do("DROP TABLE owners") };
129 eval { $dbh->do("DROP TABLE books") };
132 id INT IDENTITY (1, 1) NOT NULL,
139 CREATE TABLE owners (
140 id INT IDENTITY (1, 1) NOT NULL,
147 # start a new connection, make sure rebless works
148 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
149 $schema->populate ('Owners', [
162 [qw/12 face_to_face/],
167 }, 'populate with PKs supplied ok' );
171 # start a new connection, make sure rebless works
172 # test an insert with a supplied identity, followed by one without
173 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
176 $schema->resultset ('Owners')->create ({ id => $id, name => "troglodoogle $id" });
177 $schema->resultset ('Owners')->create ({ name => "troglodoogle " . ($id + 1) });
179 }, 'create with/without PKs ok' );
181 is ($schema->resultset ('Owners')->count, 19, 'owner rows really in db' );
184 # start a new connection, make sure rebless works
185 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
186 $schema->populate ('BooksInLibrary', [
187 [qw/source owner title /],
188 [qw/Library 1 secrets0/],
189 [qw/Library 1 secrets1/],
190 [qw/Eatery 1 secrets2/],
191 [qw/Library 2 secrets3/],
192 [qw/Library 3 secrets4/],
193 [qw/Eatery 3 secrets5/],
194 [qw/Library 4 secrets6/],
195 [qw/Library 5 secrets7/],
196 [qw/Eatery 5 secrets8/],
197 [qw/Library 6 secrets9/],
198 [qw/Library 7 secrets10/],
199 [qw/Eatery 7 secrets11/],
200 [qw/Library 8 secrets12/],
202 }, 'populate without PKs supplied ok' );
205 # test simple, complex LIMIT and limited prefetch support, with both dialects and quote combinations (if possible)
208 ($schema->storage->_server_info->{normalized_dbms_version} || 0 ) >= 9
213 for my $quoted (0, 1) {
215 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
216 limit_dialect => $dialect,
219 ? ( quote_char => [ qw/ [ ] / ], name_sep => '.' )
224 my $test_type = "Dialect:$dialect Quoted:$quoted";
226 # basic limit support
228 my $art_rs = $schema->resultset ('Artist');
230 $art_rs->create({ name => 'Artist ' . $_ }) for (1..6);
232 my $it = $schema->resultset('Artist')->search( {}, {
235 order_by => 'artistid',
238 is( $it->count, 3, "$test_type: LIMIT count ok" );
240 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
241 if $dialect eq 'Top';
243 is( $it->next->name, 'Artist 4', "$test_type: iterator->next ok" );
245 is( $it->next->name, 'Artist 6', "$test_type: iterator->next ok" );
246 is( $it->next, undef, "$test_type: next past end of resultset ok" );
249 # plain ordered subqueries throw
251 $schema->resultset('Owners')->search ({}, { order_by => 'name' })->as_query
252 }, qr/ordered subselect encountered/, "$test_type: Ordered Subselect detection throws ok");
254 # make sure ordered subselects *somewhat* work
256 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
257 my $sealed_owners = $owners->as_subselect_rs;
260 [ map { $_->name } ($sealed_owners->all) ],
261 [ map { $_->name } ($owners->all) ],
262 "$test_type: Sort preserved from within a subquery",
267 my $book_owner_ids = $schema->resultset ('BooksInLibrary')->search ({}, {
272 order_by => 'owner.name',
273 unsafe_subselect_ok => 1
274 })->get_column ('owner');
276 my @ids = $book_owner_ids->all;
278 is (@ids, 6, 'Limit works');
280 my $book_owners = $schema->resultset ('Owners')->search ({
281 id => { -in => $book_owner_ids->as_query }
285 local $TODO = "Correlated limited IN subqueries will probably never preserve order";
288 [ map { $_->id } ($book_owners->all) ],
289 [ $book_owner_ids->all ],
290 "$test_type: Sort is preserved across IN subqueries",
295 # still even with lost order of IN, we should be getting correct
298 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
299 my $corelated_owners = $owners->result_source->resultset->search (
301 id => { -in => $owners->get_column('id')->as_query },
304 order_by => 'name' #reorder because of what is shown above
309 join ("\x00", map { $_->name } ($corelated_owners->all) ),
310 join ("\x00", map { $_->name } ($owners->all) ),
311 "$test_type: With an outer order_by, everything still matches",
315 # make sure right-join-side single-prefetch ordering limit works
317 my $rs = $schema->resultset ('BooksInLibrary')->search (
319 'owner.name' => { '!=', 'woggle' },
323 order_by => 'owner.name',
326 # this is the order in which they should come from the above query
327 my @owner_names = qw/boggle fISMBoC fREW fRIOUX fROOH fRUE wiggle wiggle/;
329 is ($rs->all, 8, "$test_type: Correct amount of objects from right-sorted joined resultset");
331 [map { $_->owner->name } ($rs->all) ],
333 "$test_type: Prefetched rows were properly ordered"
336 my $limited_rs = $rs->search ({}, {rows => 6, offset => 2, unsafe_subselect_ok => 1});
337 is ($limited_rs->count, 6, "$test_type: Correct count of limited right-sorted joined resultset");
338 is ($limited_rs->count_rs->next, 6, "$test_type: Correct count_rs of limited right-sorted joined resultset");
341 my $orig_debug = $schema->storage->debug;
342 $schema->storage->debugcb(sub { $queries++; });
343 $schema->storage->debug(1);
346 [map { $_->owner->name } ($limited_rs->all) ],
347 [@owner_names[2 .. 7]],
348 "$test_type: Prefetch-limited rows were properly ordered"
350 is ($queries, 1, "$test_type: Only one query with prefetch");
352 $schema->storage->debugcb(undef);
353 $schema->storage->debug($orig_debug);
356 [map { $_->name } ($limited_rs->search_related ('owner')->all) ],
357 [@owner_names[2 .. 7]],
358 "$test_type: Rows are still properly ordered after search_related",
362 # try a ->has_many direction with duplicates
363 my $owners = $schema->resultset ('Owners')->search (
365 'books.id' => { '!=', undef },
366 'me.name' => { '!=', 'somebogusstring' },
370 order_by => { -asc => \['name + ?', [ test => 'xxx' ]] }, # test bindvar propagation
371 group_by => [ map { "me.$_" } $schema->source('Owners')->columns ], # the literal order_by requires an explicit group_by
372 rows => 3, # 8 results total
373 unsafe_subselect_ok => 1,
377 my ($sql, @bind) = @${$owners->page(3)->as_query};
381 $dialect eq 'Top' ? [ test => 'xxx' ] : (), # the extra re-order bind
382 ([ 'me.name' => 'somebogusstring' ], [ test => 'xxx' ]) x 2 # double because of the prefetch subq
386 is ($owners->page(1)->all, 3, "$test_type: has_many prefetch returns correct number of rows");
387 is ($owners->page(1)->count, 3, "$test_type: has-many prefetch returns correct count");
389 is ($owners->page(3)->count, 2, "$test_type: has-many prefetch returns correct count");
391 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
392 if $dialect eq 'Top';
393 is ($owners->page(3)->all, 2, "$test_type: has_many prefetch returns correct number of rows");
394 is ($owners->page(3)->count_rs->next, 2, "$test_type: has-many prefetch returns correct count_rs");
398 # try a ->belongs_to direction (no select collapse, group_by should work)
399 my $books = $schema->resultset ('BooksInLibrary')->search (
401 'owner.name' => [qw/wiggle woggle/],
405 having => \['1 = ?', [ test => 1 ] ], #test having propagation
407 rows => 2, # 3 results total
408 order_by => { -desc => 'me.owner' },
409 unsafe_subselect_ok => 1,
413 ($sql, @bind) = @${$books->page(3)->as_query};
418 [ 'owner.name' => 'wiggle' ], [ 'owner.name' => 'woggle' ], [ source => 'Library' ], [ test => '1' ],
420 [ 'owner.name' => 'wiggle' ], [ 'owner.name' => 'woggle' ], [ source => 'Library' ],
424 is ($books->page(1)->all, 2, "$test_type: Prefetched grouped search returns correct number of rows");
425 is ($books->page(1)->count, 2, "$test_type: Prefetched grouped search returns correct count");
427 is ($books->page(2)->count, 1, "$test_type: Prefetched grouped search returns correct count");
429 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
430 if $dialect eq 'Top';
431 is ($books->page(2)->all, 1, "$test_type: Prefetched grouped search returns correct number of rows");
432 is ($books->page(2)->count_rs->next, 1, "$test_type: Prefetched grouped search returns correct count_rs");
440 $schema->storage->dbh_do (sub {
441 my ($storage, $dbh) = @_;
442 eval { $dbh->do("DROP TABLE artist_guid") };
444 CREATE TABLE artist_guid (
445 artistid UNIQUEIDENTIFIER NOT NULL,
447 rank INT NOT NULL DEFAULT '13',
448 charfield CHAR(10) NULL,
449 a_guid UNIQUEIDENTIFIER,
450 primary key(artistid)
455 # start disconnected to make sure insert works on an un-reblessed storage
456 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
460 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
461 } 'created a row with a GUID';
464 eval { $row->artistid },
465 'row has GUID PK col populated',
470 eval { $row->a_guid },
471 'row has a GUID col with auto_nextval populated',
475 my $row_from_db = $schema->resultset('ArtistGUID')
476 ->search({ name => 'mtfnpy' })->first;
478 is $row_from_db->artistid, $row->artistid,
479 'PK GUID round trip';
481 is $row_from_db->a_guid, $row->a_guid,
482 'NON-PK GUID round trip';
487 $schema->storage->dbh_do (sub {
488 my ($storage, $dbh) = @_;
489 eval { $dbh->do("DROP TABLE money_test") };
491 CREATE TABLE money_test (
492 id INT IDENTITY PRIMARY KEY,
498 my $rs = $schema->resultset('Money');
502 $row = $rs->create({ amount => 100 });
503 } 'inserted a money value';
505 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
508 $row->update({ amount => 200 });
509 } 'updated a money value';
511 cmp_ok $rs->find($row->id)->amount, '==', 200,
512 'updated money value round-trip';
515 $row->update({ amount => undef });
516 } 'updated a money value to NULL';
518 is $rs->find($row->id)->amount, undef,'updated money value to NULL round-trip';
527 if (my $dbh = eval { $schema->storage->_dbh }) {
528 eval { $dbh->do("DROP TABLE $_") }
529 for qw/artist artist_guid money_test books owners/;