7 use DBIx::Class::SQLMaker::LimitDialects;
8 use DBIx::Class::Optional::Dependencies ();
11 use DBIC::SqlMakerTest;
13 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_rdbms_mssql_odbc')
14 unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_rdbms_mssql_odbc');
16 my $OFFSET = DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype;
17 my $TOTAL = DBIx::Class::SQLMaker::LimitDialects->__total_bindtype;
19 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
21 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
22 unless ($dsn && $user);
25 my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
26 ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
29 DBICTest::Schema->load_classes('ArtistGUID');
30 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
33 no warnings 'redefine';
34 my $connect_count = 0;
35 my $orig_connect = \&DBI::connect;
36 local *DBI::connect = sub { $connect_count++; goto &$orig_connect };
38 $schema->storage->ensure_connected;
40 is( $connect_count, 1, 'only one connection made');
43 isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
46 my $schema2 = $schema->connect ($schema->storage->connect_info);
47 ok (! $schema2->storage->connected, 'a re-connected cloned schema starts unconnected');
50 $schema->storage->_dbh->disconnect;
53 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
58 { opts => { on_connect_call => 'use_mars' } },
59 use_dynamic_cursors =>
60 { opts => { on_connect_call => 'use_dynamic_cursors' },
61 required => $schema->storage->_using_freetds ? 0 : 1,
64 { opts => { on_connect_call => 'use_server_cursors' } },
66 { opts => {}, required => 1 },
69 for my $opts_name (keys %opts) {
71 my $opts = $opts{$opts_name}{opts};
72 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
75 $schema->storage->ensure_connected
78 if ($opts{$opts_name}{required}) {
79 BAIL_OUT "on_connect_call option '$opts_name' is not functional: $_";
83 "on_connect_call option '$opts_name' not functional in this configuration: $_",
89 $schema->storage->dbh_do (sub {
90 my ($storage, $dbh) = @_;
91 eval { $dbh->do("DROP TABLE artist") };
94 artistid INT IDENTITY NOT NULL,
96 rank INT NOT NULL DEFAULT '13',
97 charfield CHAR(10) NULL,
104 $schema->resultset('Artist')->search({ name => 'foo' })->delete;
106 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
108 ok(($new->artistid||0) > 0, "Auto-PK worked for $opts_name");
110 # Test multiple active statements
112 skip 'not a multiple active statements configuration', 1
113 if $opts_name eq 'plain';
115 $schema->storage->ensure_connected;
119 no warnings 'redefine';
120 local *DBI::connect = sub { die "NO RECONNECTS!!!" };
122 my $artist_rs = $schema->resultset('Artist');
126 $artist_rs->create({ name => "Artist$_" }) for (1..3);
128 my $forward = $artist_rs->search({},
129 { order_by => { -asc => 'artistid' } });
130 my $backward = $artist_rs->search({},
131 { order_by => { -desc => 'artistid' } });
134 [qw/Artist1 Artist3/], [qw/Artist2 Artist2/], [qw/Artist3 Artist1/]
138 while (my $forward_row = $forward->next) {
139 my $backward_row = $backward->next;
140 push @result, [$forward_row->name, $backward_row->name];
143 is_deeply \@result, \@map, "multiple active statements in $opts_name";
147 is($artist_rs->count, 0, '$dbh still viable');
148 } "Multiple active statements survive $opts_name";
154 $schema->storage->dbh_do (sub {
155 my ($storage, $dbh) = @_;
156 eval { $dbh->do("DROP TABLE owners") };
157 eval { $dbh->do("DROP TABLE books") };
160 id INT IDENTITY (1, 1) NOT NULL,
167 CREATE TABLE owners (
168 id INT IDENTITY (1, 1) NOT NULL,
175 # start a new connection, make sure rebless works
176 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
177 $schema->populate ('Owners', [
190 [qw/12 face_to_face/],
195 }, 'populate with PKs supplied ok' );
199 # start a new connection, make sure rebless works
200 # test an insert with a supplied identity, followed by one without
201 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
204 $schema->resultset ('Owners')->create ({ id => $id, name => "troglodoogle $id" });
205 $schema->resultset ('Owners')->create ({ name => "troglodoogle " . ($id + 1) });
207 }, 'create with/without PKs ok' );
209 is ($schema->resultset ('Owners')->count, 19, 'owner rows really in db' );
212 # start a new connection, make sure rebless works
213 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
214 $schema->populate ('BooksInLibrary', [
215 [qw/source owner title /],
216 [qw/Library 1 secrets0/],
217 [qw/Library 1 secrets1/],
218 [qw/Eatery 1 secrets2/],
219 [qw/Library 2 secrets3/],
220 [qw/Library 3 secrets4/],
221 [qw/Eatery 3 secrets5/],
222 [qw/Library 4 secrets6/],
223 [qw/Library 5 secrets7/],
224 [qw/Eatery 5 secrets8/],
225 [qw/Library 6 secrets9/],
226 [qw/Library 7 secrets10/],
227 [qw/Eatery 7 secrets11/],
228 [qw/Library 8 secrets12/],
230 }, 'populate without PKs supplied ok' );
233 # test simple, complex LIMIT and limited prefetch support, with both dialects and quote combinations (if possible)
236 ($schema->storage->_server_info->{normalized_dbms_version} || 0 ) >= 9
241 for my $quoted (0, 1) {
243 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
244 limit_dialect => $dialect,
247 ? ( quote_char => [ qw/ [ ] / ], name_sep => '.' )
252 my $test_type = "Dialect:$dialect Quoted:$quoted";
254 # basic limit support
256 my $art_rs = $schema->resultset ('Artist');
258 $art_rs->create({ name => 'Artist ' . $_ }) for (1..6);
260 my $it = $schema->resultset('Artist')->search( {}, {
263 order_by => 'artistid',
266 is( $it->count, 3, "$test_type: LIMIT count ok" );
268 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
269 if $dialect eq 'Top';
271 is( $it->next->name, 'Artist 4', "$test_type: iterator->next ok" );
273 is( $it->next->name, 'Artist 6', "$test_type: iterator->next ok" );
274 is( $it->next, undef, "$test_type: next past end of resultset ok" );
277 # plain ordered subqueries throw
279 $schema->resultset('Owners')->search ({}, { order_by => 'name' })->as_query
280 }, qr/ordered subselect encountered/, "$test_type: Ordered Subselect detection throws ok");
282 # make sure ordered subselects *somewhat* work
284 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
285 my $sealed_owners = $owners->as_subselect_rs;
288 [ map { $_->name } ($sealed_owners->all) ],
289 [ map { $_->name } ($owners->all) ],
290 "$test_type: Sort preserved from within a subquery",
294 # still even with lost order of IN, we should be getting correct
297 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
298 my $corelated_owners = $owners->result_source->resultset->search (
300 id => { -in => $owners->get_column('id')->as_query },
303 order_by => 'name' #reorder because of what is shown above
308 join ("\x00", map { $_->name } ($corelated_owners->all) ),
309 join ("\x00", map { $_->name } ($owners->all) ),
310 "$test_type: With an outer order_by, everything still matches",
314 # make sure right-join-side single-prefetch ordering limit works
316 my $rs = $schema->resultset ('BooksInLibrary')->search (
318 'owner.name' => { '!=', 'woggle' },
322 order_by => 'owner.name',
325 # this is the order in which they should come from the above query
326 my @owner_names = qw/boggle fISMBoC fREW fRIOUX fROOH fRUE wiggle wiggle/;
328 is ($rs->all, 8, "$test_type: Correct amount of objects from right-sorted joined resultset");
330 [map { $_->owner->name } ($rs->all) ],
332 "$test_type: Prefetched rows were properly ordered"
335 my $limited_rs = $rs->search ({}, {rows => 6, offset => 2, unsafe_subselect_ok => 1});
336 is ($limited_rs->count, 6, "$test_type: Correct count of limited right-sorted joined resultset");
337 is ($limited_rs->count_rs->next, 6, "$test_type: Correct count_rs of limited right-sorted joined resultset");
340 my $orig_debug = $schema->storage->debug;
341 $schema->storage->debugcb(sub { $queries++; });
342 $schema->storage->debug(1);
345 [map { $_->owner->name } ($limited_rs->all) ],
346 [@owner_names[2 .. 7]],
347 "$test_type: Prefetch-limited rows were properly ordered"
349 is ($queries, 1, "$test_type: Only one query with prefetch");
351 $schema->storage->debugcb(undef);
352 $schema->storage->debug($orig_debug);
355 [map { $_->name } ($limited_rs->search_related ('owner')->all) ],
356 [@owner_names[2 .. 7]],
357 "$test_type: Rows are still properly ordered after search_related",
361 # try a ->has_many direction with duplicates
362 my $owners = $schema->resultset ('Owners')->search (
364 'books.id' => { '!=', undef },
365 'me.name' => { '!=', 'somebogusstring' },
369 order_by => [ { -asc => \['name + ?', [ test => 'xxx' ]] }, 'me.id' ], # test bindvar propagation
370 group_by => [ map { "me.$_" } $schema->source('Owners')->columns ], # the literal order_by requires an explicit group_by
371 rows => 3, # 8 results total
372 unsafe_subselect_ok => 1,
376 my ($sql, @bind) = @${$owners->page(3)->as_query};
380 ($dialect eq 'Top' ? [ { dbic_colname => 'test' } => 'xxx' ] : ()), # the extra re-order bind
381 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.name' }
382 => 'somebogusstring' ],
383 [ { dbic_colname => 'test' }
385 ($dialect ne 'Top' ? ( [ $OFFSET => 7 ], [ $TOTAL => 9 ] ) : ()), # parameterised RNO
386 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.name' }
387 => 'somebogusstring' ],
388 [ { dbic_colname => 'test' }
393 is ($owners->page(1)->all, 3, "$test_type: has_many prefetch returns correct number of rows");
394 is ($owners->page(1)->count, 3, "$test_type: has-many prefetch returns correct count");
396 is ($owners->page(3)->count, 2, "$test_type: has-many prefetch returns correct count");
398 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
399 if $dialect eq 'Top';
400 is ($owners->page(3)->all, 2, "$test_type: has_many prefetch returns correct number of rows");
401 is ($owners->page(3)->count_rs->next, 2, "$test_type: has-many prefetch returns correct count_rs");
405 # try a ->belongs_to direction (no select collapse, group_by should work)
406 my $books = $schema->resultset ('BooksInLibrary')->search (
408 'owner.name' => [qw/wiggle woggle/],
412 having => \['1 = ?', [ test => 1 ] ], #test having propagation
414 rows => 2, # 3 results total
415 order_by => [{ -desc => 'me.owner' }, 'me.id'],
416 unsafe_subselect_ok => 1,
420 ($sql, @bind) = @${$books->page(3)->as_query};
425 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
427 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
429 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' }
431 [ { dbic_colname => 'test' }
435 $dialect ne 'Top' ? ( [ $OFFSET => 5 ], [ $TOTAL => 6 ] ) : (),
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' }
446 is ($books->page(1)->all, 2, "$test_type: Prefetched grouped search returns correct number of rows");
447 is ($books->page(1)->count, 2, "$test_type: Prefetched grouped search returns correct count");
449 is ($books->page(2)->count, 1, "$test_type: Prefetched grouped search returns correct count");
451 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
452 if $dialect eq 'Top';
453 is ($books->page(2)->all, 1, "$test_type: Prefetched grouped search returns correct number of rows");
454 is ($books->page(2)->count_rs->next, 1, "$test_type: Prefetched grouped search returns correct count_rs");
462 $schema->storage->dbh_do (sub {
463 my ($storage, $dbh) = @_;
464 eval { $dbh->do("DROP TABLE artist_guid") };
466 CREATE TABLE artist_guid (
467 artistid UNIQUEIDENTIFIER NOT NULL,
469 rank INT NOT NULL DEFAULT '13',
470 charfield CHAR(10) NULL,
471 a_guid UNIQUEIDENTIFIER,
472 primary key(artistid)
477 # start disconnected to make sure insert works on an un-reblessed storage
478 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
482 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
483 } 'created a row with a GUID';
486 eval { $row->artistid },
487 'row has GUID PK col populated',
492 eval { $row->a_guid },
493 'row has a GUID col with auto_nextval populated',
497 my $row_from_db = $schema->resultset('ArtistGUID')
498 ->search({ name => 'mtfnpy' })->first;
500 is $row_from_db->artistid, $row->artistid,
501 'PK GUID round trip';
503 is $row_from_db->a_guid, $row->a_guid,
504 'NON-PK GUID round trip';
509 $schema->storage->dbh_do (sub {
510 my ($storage, $dbh) = @_;
511 eval { $dbh->do("DROP TABLE money_test") };
513 CREATE TABLE money_test (
514 id INT IDENTITY PRIMARY KEY,
521 my $freetds_and_dynamic_cursors = 1
522 if $opts_name eq 'use_dynamic_cursors' &&
523 $schema->storage->_using_freetds;
526 'these tests fail on freetds with dynamic cursors for some reason'
527 if $freetds_and_dynamic_cursors;
528 local $ENV{DBIC_NULLABLE_KEY_NOWARN} = 1
529 if $freetds_and_dynamic_cursors;
531 my $rs = $schema->resultset('Money');
535 $row = $rs->create({ amount => 100 });
536 } 'inserted a money value';
538 cmp_ok ((try { $rs->find($row->id)->amount })||0, '==', 100,
539 'money value round-trip');
542 $row->update({ amount => 200 });
543 } 'updated a money value';
545 cmp_ok ((try { $rs->find($row->id)->amount })||0, '==', 200,
546 'updated money value round-trip');
549 $row->update({ amount => undef });
550 } 'updated a money value to NULL';
552 is try { $rs->find($row->id)->amount }, undef,
553 'updated money value to NULL round-trip';
563 if (my $dbh = eval { $schema->storage->_dbh }) {
564 eval { $dbh->do("DROP TABLE $_") }
565 for qw/artist artist_guid money_test books owners/;