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: $_",
88 $schema->storage->dbh_do (sub {
89 my ($storage, $dbh) = @_;
90 eval { $dbh->do("DROP TABLE artist") };
93 artistid INT IDENTITY NOT NULL,
95 rank INT NOT NULL DEFAULT '13',
96 charfield CHAR(10) NULL,
103 $schema->resultset('Artist')->search({ name => 'foo' })->delete;
105 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
107 ok(($new->artistid||0) > 0, "Auto-PK worked for $opts_name");
109 # Test multiple active statements
111 skip 'not a multiple active statements configuration', 1
112 if $opts_name eq 'plain';
114 my $artist_rs = $schema->resultset('Artist');
118 $artist_rs->create({ name => "Artist$_" }) for (1..3);
120 my $forward = $artist_rs->search({},
121 { order_by => { -asc => 'artistid' } });
122 my $backward = $artist_rs->search({},
123 { order_by => { -desc => 'artistid' } });
126 [qw/Artist1 Artist3/], [qw/Artist2 Artist2/], [qw/Artist3 Artist1/]
130 while (my $forward_row = $forward->next) {
131 my $backward_row = $backward->next;
132 push @result, [$forward_row->name, $backward_row->name];
135 is_deeply \@result, \@map, "multiple active statements in $opts_name";
139 is($artist_rs->count, 0, '$dbh still viable');
145 $schema->storage->dbh_do (sub {
146 my ($storage, $dbh) = @_;
147 eval { $dbh->do("DROP TABLE owners") };
148 eval { $dbh->do("DROP TABLE books") };
151 id INT IDENTITY (1, 1) NOT NULL,
158 CREATE TABLE owners (
159 id INT IDENTITY (1, 1) NOT NULL,
166 # start a new connection, make sure rebless works
167 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
168 $schema->populate ('Owners', [
181 [qw/12 face_to_face/],
186 }, 'populate with PKs supplied ok' );
190 # start a new connection, make sure rebless works
191 # test an insert with a supplied identity, followed by one without
192 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
195 $schema->resultset ('Owners')->create ({ id => $id, name => "troglodoogle $id" });
196 $schema->resultset ('Owners')->create ({ name => "troglodoogle " . ($id + 1) });
198 }, 'create with/without PKs ok' );
200 is ($schema->resultset ('Owners')->count, 19, 'owner rows really in db' );
203 # start a new connection, make sure rebless works
204 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
205 $schema->populate ('BooksInLibrary', [
206 [qw/source owner title /],
207 [qw/Library 1 secrets0/],
208 [qw/Library 1 secrets1/],
209 [qw/Eatery 1 secrets2/],
210 [qw/Library 2 secrets3/],
211 [qw/Library 3 secrets4/],
212 [qw/Eatery 3 secrets5/],
213 [qw/Library 4 secrets6/],
214 [qw/Library 5 secrets7/],
215 [qw/Eatery 5 secrets8/],
216 [qw/Library 6 secrets9/],
217 [qw/Library 7 secrets10/],
218 [qw/Eatery 7 secrets11/],
219 [qw/Library 8 secrets12/],
221 }, 'populate without PKs supplied ok' );
224 # test simple, complex LIMIT and limited prefetch support, with both dialects and quote combinations (if possible)
227 ($schema->storage->_server_info->{normalized_dbms_version} || 0 ) >= 9
232 for my $quoted (0, 1) {
234 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
235 limit_dialect => $dialect,
238 ? ( quote_char => [ qw/ [ ] / ], name_sep => '.' )
243 my $test_type = "Dialect:$dialect Quoted:$quoted";
245 # basic limit support
247 my $art_rs = $schema->resultset ('Artist');
249 $art_rs->create({ name => 'Artist ' . $_ }) for (1..6);
251 my $it = $schema->resultset('Artist')->search( {}, {
254 order_by => 'artistid',
257 is( $it->count, 3, "$test_type: LIMIT count ok" );
259 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
260 if $dialect eq 'Top';
262 is( $it->next->name, 'Artist 4', "$test_type: iterator->next ok" );
264 is( $it->next->name, 'Artist 6', "$test_type: iterator->next ok" );
265 is( $it->next, undef, "$test_type: next past end of resultset ok" );
268 # plain ordered subqueries throw
270 $schema->resultset('Owners')->search ({}, { order_by => 'name' })->as_query
271 }, qr/ordered subselect encountered/, "$test_type: Ordered Subselect detection throws ok");
273 # make sure ordered subselects *somewhat* work
275 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
276 my $sealed_owners = $owners->as_subselect_rs;
279 [ map { $_->name } ($sealed_owners->all) ],
280 [ map { $_->name } ($owners->all) ],
281 "$test_type: Sort preserved from within a subquery",
285 # still even with lost order of IN, we should be getting correct
288 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
289 my $corelated_owners = $owners->result_source->resultset->search (
291 id => { -in => $owners->get_column('id')->as_query },
294 order_by => 'name' #reorder because of what is shown above
299 join ("\x00", map { $_->name } ($corelated_owners->all) ),
300 join ("\x00", map { $_->name } ($owners->all) ),
301 "$test_type: With an outer order_by, everything still matches",
305 # make sure right-join-side single-prefetch ordering limit works
307 my $rs = $schema->resultset ('BooksInLibrary')->search (
309 'owner.name' => { '!=', 'woggle' },
313 order_by => 'owner.name',
316 # this is the order in which they should come from the above query
317 my @owner_names = qw/boggle fISMBoC fREW fRIOUX fROOH fRUE wiggle wiggle/;
319 is ($rs->all, 8, "$test_type: Correct amount of objects from right-sorted joined resultset");
321 [map { $_->owner->name } ($rs->all) ],
323 "$test_type: Prefetched rows were properly ordered"
326 my $limited_rs = $rs->search ({}, {rows => 6, offset => 2, unsafe_subselect_ok => 1});
327 is ($limited_rs->count, 6, "$test_type: Correct count of limited right-sorted joined resultset");
328 is ($limited_rs->count_rs->next, 6, "$test_type: Correct count_rs of limited right-sorted joined resultset");
331 my $orig_debug = $schema->storage->debug;
332 $schema->storage->debugcb(sub { $queries++; });
333 $schema->storage->debug(1);
336 [map { $_->owner->name } ($limited_rs->all) ],
337 [@owner_names[2 .. 7]],
338 "$test_type: Prefetch-limited rows were properly ordered"
340 is ($queries, 1, "$test_type: Only one query with prefetch");
342 $schema->storage->debugcb(undef);
343 $schema->storage->debug($orig_debug);
346 [map { $_->name } ($limited_rs->search_related ('owner')->all) ],
347 [@owner_names[2 .. 7]],
348 "$test_type: Rows are still properly ordered after search_related",
352 # try a ->has_many direction with duplicates
353 my $owners = $schema->resultset ('Owners')->search (
355 'books.id' => { '!=', undef },
356 'me.name' => { '!=', 'somebogusstring' },
360 order_by => [ { -asc => \['name + ?', [ test => 'xxx' ]] }, 'me.id' ], # test bindvar propagation
361 group_by => [ map { "me.$_" } $schema->source('Owners')->columns ], # the literal order_by requires an explicit group_by
362 rows => 3, # 8 results total
363 unsafe_subselect_ok => 1,
367 my ($sql, @bind) = @${$owners->page(3)->as_query};
371 ($dialect eq 'Top' ? [ { dbic_colname => 'test' } => 'xxx' ] : ()), # the extra re-order bind
372 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.name' }
373 => 'somebogusstring' ],
374 [ { dbic_colname => 'test' }
376 ($dialect ne 'Top' ? ( [ $OFFSET => 7 ], [ $TOTAL => 9 ] ) : ()), # parameterised RNO
377 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.name' }
378 => 'somebogusstring' ],
379 [ { dbic_colname => 'test' }
384 is ($owners->page(1)->all, 3, "$test_type: has_many prefetch returns correct number of rows");
385 is ($owners->page(1)->count, 3, "$test_type: has-many prefetch returns correct count");
387 is ($owners->page(3)->count, 2, "$test_type: has-many prefetch returns correct count");
389 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
390 if $dialect eq 'Top';
391 is ($owners->page(3)->all, 2, "$test_type: has_many prefetch returns correct number of rows");
392 is ($owners->page(3)->count_rs->next, 2, "$test_type: has-many prefetch returns correct count_rs");
396 # try a ->belongs_to direction (no select collapse, group_by should work)
397 my $books = $schema->resultset ('BooksInLibrary')->search (
399 'owner.name' => [qw/wiggle woggle/],
403 having => \['1 = ?', [ test => 1 ] ], #test having propagation
405 rows => 2, # 3 results total
406 order_by => [{ -desc => 'me.owner' }, 'me.id'],
407 unsafe_subselect_ok => 1,
411 ($sql, @bind) = @${$books->page(3)->as_query};
416 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
418 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
420 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' }
422 [ { dbic_colname => 'test' }
426 $dialect ne 'Top' ? ( [ $OFFSET => 5 ], [ $TOTAL => 6 ] ) : (),
428 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
430 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'owner.name' }
432 [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' }
437 is ($books->page(1)->all, 2, "$test_type: Prefetched grouped search returns correct number of rows");
438 is ($books->page(1)->count, 2, "$test_type: Prefetched grouped search returns correct count");
440 is ($books->page(2)->count, 1, "$test_type: Prefetched grouped search returns correct count");
442 local $TODO = "Top-limit does not work when your limit ends up past the resultset"
443 if $dialect eq 'Top';
444 is ($books->page(2)->all, 1, "$test_type: Prefetched grouped search returns correct number of rows");
445 is ($books->page(2)->count_rs->next, 1, "$test_type: Prefetched grouped search returns correct count_rs");
453 $schema->storage->dbh_do (sub {
454 my ($storage, $dbh) = @_;
455 eval { $dbh->do("DROP TABLE artist_guid") };
457 CREATE TABLE artist_guid (
458 artistid UNIQUEIDENTIFIER NOT NULL,
460 rank INT NOT NULL DEFAULT '13',
461 charfield CHAR(10) NULL,
462 a_guid UNIQUEIDENTIFIER,
463 primary key(artistid)
468 # start disconnected to make sure insert works on an un-reblessed storage
469 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
473 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
474 } 'created a row with a GUID';
477 eval { $row->artistid },
478 'row has GUID PK col populated',
483 eval { $row->a_guid },
484 'row has a GUID col with auto_nextval populated',
488 my $row_from_db = $schema->resultset('ArtistGUID')
489 ->search({ name => 'mtfnpy' })->first;
491 is $row_from_db->artistid, $row->artistid,
492 'PK GUID round trip';
494 is $row_from_db->a_guid, $row->a_guid,
495 'NON-PK GUID round trip';
500 $schema->storage->dbh_do (sub {
501 my ($storage, $dbh) = @_;
502 eval { $dbh->do("DROP TABLE money_test") };
504 CREATE TABLE money_test (
505 id INT IDENTITY PRIMARY KEY,
512 my $freetds_and_dynamic_cursors = 1
513 if $opts_name eq 'use_dynamic_cursors' &&
514 $schema->storage->using_freetds;
517 'these tests fail on freetds with dynamic cursors for some reason'
518 if $freetds_and_dynamic_cursors;
519 local $ENV{DBIC_NULLABLE_KEY_NOWARN} = 1
520 if $freetds_and_dynamic_cursors;
522 my $rs = $schema->resultset('Money');
526 $row = $rs->create({ amount => 100 });
527 } 'inserted a money value';
529 cmp_ok ((try { $rs->find($row->id)->amount })||0, '==', 100,
530 'money value round-trip');
533 $row->update({ amount => 200 });
534 } 'updated a money value';
536 cmp_ok ((try { $rs->find($row->id)->amount })||0, '==', 200,
537 'updated money value round-trip');
540 $row->update({ amount => undef });
541 } 'updated a money value to NULL';
543 is try { $rs->find($row->id)->amount }, undef,
544 'updated money value to NULL round-trip';
554 if (my $dbh = eval { $schema->storage->_dbh }) {
555 eval { $dbh->do("DROP TABLE $_") }
556 for qw/artist artist_guid money_test books owners/;