8 use DBIC::SqlMakerTest;
11 use DBIx::Class::SQLMaker::LimitDialects;
12 my $OFFSET = DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype;
13 my $TOTAL = DBIx::Class::SQLMaker::LimitDialects->__total_bindtype;
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');
46 $schema->storage->_dbh->disconnect;
49 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
54 { opts => { on_connect_call => 'use_mars' } },
55 use_dynamic_cursors =>
56 { opts => { on_connect_call => 'use_dynamic_cursors' }, required => 1 },
58 { opts => { on_connect_call => 'use_server_cursors' } },
60 { opts => {}, required => 1 },
63 for my $opts_name (keys %opts) {
65 my $opts = $opts{$opts_name}{opts};
66 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
69 $schema->storage->ensure_connected
72 if ($opts{$opts_name}{required}) {
73 BAIL_OUT "on_connect_call option '$opts_name' is not functional: $_";
77 "on_connect_call option '$opts_name' not functional in this configuration: $_",
82 $schema->storage->dbh_do (sub {
83 my ($storage, $dbh) = @_;
84 eval { $dbh->do("DROP TABLE artist") };
87 artistid INT IDENTITY NOT NULL,
89 rank INT NOT NULL DEFAULT '13',
90 charfield CHAR(10) NULL,
97 $schema->resultset('Artist')->search({ name => 'foo' })->delete;
99 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
101 ok(($new->artistid||0) > 0, "Auto-PK worked for $opts_name");
103 # Test multiple active statements
105 skip 'not a multiple active statements configuration', 1
106 if $opts_name eq 'plain';
108 my $artist_rs = $schema->resultset('Artist');
112 $artist_rs->create({ name => "Artist$_" }) for (1..3);
114 my $forward = $artist_rs->search({},
115 { order_by => { -asc => 'artistid' } });
116 my $backward = $artist_rs->search({},
117 { order_by => { -desc => 'artistid' } });
120 [qw/Artist1 Artist3/], [qw/Artist2 Artist2/], [qw/Artist3 Artist1/]
124 while (my $forward_row = $forward->next) {
125 my $backward_row = $backward->next;
126 push @result, [$forward_row->name, $backward_row->name];
129 is_deeply \@result, \@map, "multiple active statements in $opts_name";
137 $schema->storage->dbh_do (sub {
138 my ($storage, $dbh) = @_;
139 eval { $dbh->do("DROP TABLE owners") };
140 eval { $dbh->do("DROP TABLE books") };
143 id INT IDENTITY (1, 1) NOT NULL,
150 CREATE TABLE owners (
151 id INT IDENTITY (1, 1) NOT NULL,
158 # start a new connection, make sure rebless works
159 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
160 $schema->populate ('Owners', [
173 [qw/12 face_to_face/],
178 }, 'populate with PKs supplied ok' );
182 # start a new connection, make sure rebless works
183 # test an insert with a supplied identity, followed by one without
184 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
187 $schema->resultset ('Owners')->create ({ id => $id, name => "troglodoogle $id" });
188 $schema->resultset ('Owners')->create ({ name => "troglodoogle " . ($id + 1) });
190 }, 'create with/without PKs ok' );
192 is ($schema->resultset ('Owners')->count, 19, 'owner rows really in db' );
195 # start a new connection, make sure rebless works
196 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
197 $schema->populate ('BooksInLibrary', [
198 [qw/source owner title /],
199 [qw/Library 1 secrets0/],
200 [qw/Library 1 secrets1/],
201 [qw/Eatery 1 secrets2/],
202 [qw/Library 2 secrets3/],
203 [qw/Library 3 secrets4/],
204 [qw/Eatery 3 secrets5/],
205 [qw/Library 4 secrets6/],
206 [qw/Library 5 secrets7/],
207 [qw/Eatery 5 secrets8/],
208 [qw/Library 6 secrets9/],
209 [qw/Library 7 secrets10/],
210 [qw/Eatery 7 secrets11/],
211 [qw/Library 8 secrets12/],
213 }, 'populate without PKs supplied ok' );
216 # test simple, complex LIMIT and limited prefetch support, with both dialects and quote combinations (if possible)
219 ($schema->storage->_server_info->{normalized_dbms_version} || 0 ) >= 9
224 for my $quoted (0, 1) {
226 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
227 limit_dialect => $dialect,
230 ? ( quote_char => [ qw/ [ ] / ], name_sep => '.' )
235 my $test_type = "Dialect:$dialect Quoted:$quoted";
237 # basic limit support
239 my $art_rs = $schema->resultset ('Artist');
241 $art_rs->create({ name => 'Artist ' . $_ }) for (1..6);
243 my $it = $schema->resultset('Artist')->search( {}, {
246 order_by => 'artistid',
249 is( $it->count, 3, "$test_type: LIMIT count ok" );
251 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
252 if $dialect eq 'Top';
254 is( $it->next->name, 'Artist 4', "$test_type: iterator->next ok" );
256 is( $it->next->name, 'Artist 6', "$test_type: iterator->next ok" );
257 is( $it->next, undef, "$test_type: next past end of resultset ok" );
260 # plain ordered subqueries throw
262 $schema->resultset('Owners')->search ({}, { order_by => 'name' })->as_query
263 }, qr/ordered subselect encountered/, "$test_type: Ordered Subselect detection throws ok");
265 # make sure ordered subselects *somewhat* work
267 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
268 my $sealed_owners = $owners->as_subselect_rs;
271 [ map { $_->name } ($sealed_owners->all) ],
272 [ map { $_->name } ($owners->all) ],
273 "$test_type: Sort preserved from within a subquery",
278 my $book_owner_ids = $schema->resultset ('BooksInLibrary')->search ({}, {
283 order_by => 'owner.name',
284 unsafe_subselect_ok => 1
285 })->get_column ('owner');
287 my @ids = $book_owner_ids->all;
289 is (@ids, 6, 'Limit works');
291 my $book_owners = $schema->resultset ('Owners')->search ({
292 id => { -in => $book_owner_ids->as_query }
296 local $TODO = "Correlated limited IN subqueries will probably never preserve order";
299 [ map { $_->id } ($book_owners->all) ],
300 [ $book_owner_ids->all ],
301 "$test_type: Sort is preserved across IN subqueries",
306 # still even with lost order of IN, we should be getting correct
309 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
310 my $corelated_owners = $owners->result_source->resultset->search (
312 id => { -in => $owners->get_column('id')->as_query },
315 order_by => 'name' #reorder because of what is shown above
320 join ("\x00", map { $_->name } ($corelated_owners->all) ),
321 join ("\x00", map { $_->name } ($owners->all) ),
322 "$test_type: With an outer order_by, everything still matches",
326 # make sure right-join-side single-prefetch ordering limit works
328 my $rs = $schema->resultset ('BooksInLibrary')->search (
330 'owner.name' => { '!=', 'woggle' },
334 order_by => 'owner.name',
337 # this is the order in which they should come from the above query
338 my @owner_names = qw/boggle fISMBoC fREW fRIOUX fROOH fRUE wiggle wiggle/;
340 is ($rs->all, 8, "$test_type: Correct amount of objects from right-sorted joined resultset");
342 [map { $_->owner->name } ($rs->all) ],
344 "$test_type: Prefetched rows were properly ordered"
347 my $limited_rs = $rs->search ({}, {rows => 6, offset => 2, unsafe_subselect_ok => 1});
348 is ($limited_rs->count, 6, "$test_type: Correct count of limited right-sorted joined resultset");
349 is ($limited_rs->count_rs->next, 6, "$test_type: Correct count_rs of limited right-sorted joined resultset");
352 my $orig_debug = $schema->storage->debug;
353 $schema->storage->debugcb(sub { $queries++; });
354 $schema->storage->debug(1);
357 [map { $_->owner->name } ($limited_rs->all) ],
358 [@owner_names[2 .. 7]],
359 "$test_type: Prefetch-limited rows were properly ordered"
361 is ($queries, 1, "$test_type: Only one query with prefetch");
363 $schema->storage->debugcb(undef);
364 $schema->storage->debug($orig_debug);
367 [map { $_->name } ($limited_rs->search_related ('owner')->all) ],
368 [@owner_names[2 .. 7]],
369 "$test_type: Rows are still properly ordered after search_related",
373 # try a ->has_many direction with duplicates
374 my $owners = $schema->resultset ('Owners')->search (
376 'books.id' => { '!=', undef },
377 'me.name' => { '!=', 'somebogusstring' },
381 order_by => { -asc => \['name + ?', [ test => 'xxx' ]] }, # test bindvar propagation
382 group_by => [ map { "me.$_" } $schema->source('Owners')->columns ], # the literal order_by requires an explicit group_by
383 rows => 3, # 8 results total
384 unsafe_subselect_ok => 1,
388 my ($sql, @bind) = @${$owners->page(3)->as_query};
392 ($dialect eq 'Top' ? [ { dbic_colname => 'test' } => 'xxx' ] : ()), # the extra re-order bind
393 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.name' }
394 => 'somebogusstring' ],
395 [ { dbic_colname => 'test' }
397 ($dialect ne 'Top' ? ( [ $OFFSET => 7 ], [ $TOTAL => 9 ] ) : ()), # parameterised RNO
398 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.name' }
399 => 'somebogusstring' ],
400 [ { dbic_colname => 'test' }
405 is ($owners->page(1)->all, 3, "$test_type: has_many prefetch returns correct number of rows");
406 is ($owners->page(1)->count, 3, "$test_type: has-many prefetch returns correct count");
408 is ($owners->page(3)->count, 2, "$test_type: has-many prefetch returns correct count");
410 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
411 if $dialect eq 'Top';
412 is ($owners->page(3)->all, 2, "$test_type: has_many prefetch returns correct number of rows");
413 is ($owners->page(3)->count_rs->next, 2, "$test_type: has-many prefetch returns correct count_rs");
417 # try a ->belongs_to direction (no select collapse, group_by should work)
418 my $books = $schema->resultset ('BooksInLibrary')->search (
420 'owner.name' => [qw/wiggle woggle/],
424 having => \['1 = ?', [ test => 1 ] ], #test having propagation
426 rows => 2, # 3 results total
427 order_by => { -desc => 'me.owner' },
428 unsafe_subselect_ok => 1,
432 ($sql, @bind) = @${$books->page(3)->as_query};
437 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
439 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
441 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' }
443 [ { dbic_colname => 'test' }
447 $dialect ne 'Top' ? ( [ $OFFSET => 5 ], [ $TOTAL => 6 ] ) : (),
449 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
451 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
453 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' }
458 is ($books->page(1)->all, 2, "$test_type: Prefetched grouped search returns correct number of rows");
459 is ($books->page(1)->count, 2, "$test_type: Prefetched grouped search returns correct count");
461 is ($books->page(2)->count, 1, "$test_type: Prefetched grouped search returns correct count");
463 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
464 if $dialect eq 'Top';
465 is ($books->page(2)->all, 1, "$test_type: Prefetched grouped search returns correct number of rows");
466 is ($books->page(2)->count_rs->next, 1, "$test_type: Prefetched grouped search returns correct count_rs");
474 $schema->storage->dbh_do (sub {
475 my ($storage, $dbh) = @_;
476 eval { $dbh->do("DROP TABLE artist_guid") };
478 CREATE TABLE artist_guid (
479 artistid UNIQUEIDENTIFIER NOT NULL,
481 rank INT NOT NULL DEFAULT '13',
482 charfield CHAR(10) NULL,
483 a_guid UNIQUEIDENTIFIER,
484 primary key(artistid)
489 # start disconnected to make sure insert works on an un-reblessed storage
490 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
494 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
495 } 'created a row with a GUID';
498 eval { $row->artistid },
499 'row has GUID PK col populated',
504 eval { $row->a_guid },
505 'row has a GUID col with auto_nextval populated',
509 my $row_from_db = $schema->resultset('ArtistGUID')
510 ->search({ name => 'mtfnpy' })->first;
512 is $row_from_db->artistid, $row->artistid,
513 'PK GUID round trip';
515 is $row_from_db->a_guid, $row->a_guid,
516 'NON-PK GUID round trip';
521 $schema->storage->dbh_do (sub {
522 my ($storage, $dbh) = @_;
523 eval { $dbh->do("DROP TABLE money_test") };
525 CREATE TABLE money_test (
526 id INT IDENTITY PRIMARY KEY,
534 'these tests fail on freetds with dynamic cursors for some reason'
535 if $opts_name eq 'use_dynamic_cursors'
536 && $schema->storage->using_freetds;
538 my $rs = $schema->resultset('Money');
542 $row = $rs->create({ amount => 100 });
543 } 'inserted a money value';
545 cmp_ok ((try { $rs->find($row->id)->amount })||0, '==', 100,
546 'money value round-trip');
549 $row->update({ amount => 200 });
550 } 'updated a money value';
552 cmp_ok ((try { $rs->find($row->id)->amount })||0, '==', 200,
553 'updated money value round-trip');
556 $row->update({ amount => undef });
557 } 'updated a money value to NULL';
559 is try { $rs->find($row->id)->amount }, undef,
560 'updated money value to NULL round-trip';
570 if (my $dbh = eval { $schema->storage->_dbh }) {
571 eval { $dbh->do("DROP TABLE $_") }
572 for qw/artist artist_guid money_test books owners/;