8 use DBIC::SqlMakerTest;
10 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
12 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
13 unless ($dsn && $user);
15 DBICTest::Schema->load_classes('ArtistGUID');
16 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
19 no warnings 'redefine';
20 my $connect_count = 0;
21 my $orig_connect = \&DBI::connect;
22 local *DBI::connect = sub { $connect_count++; goto &$orig_connect };
24 $schema->storage->ensure_connected;
26 is( $connect_count, 1, 'only one connection made');
29 isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
32 my $schema2 = $schema->connect ($schema->storage->connect_info);
33 ok (! $schema2->storage->connected, 'a re-connected cloned schema starts unconnected');
36 $schema->storage->_dbh->disconnect;
39 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
42 $schema->storage->dbh_do (sub {
43 my ($storage, $dbh) = @_;
44 eval { $dbh->do("DROP TABLE artist") };
47 artistid INT IDENTITY NOT NULL,
49 rank INT NOT NULL DEFAULT '13',
50 charfield CHAR(10) NULL,
59 { on_connect_call => 'use_dynamic_cursors' },
64 # test Auto-PK with different options
65 for my $opts (@opts) {
67 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
70 $schema->storage->ensure_connected
72 if ($@ =~ /dynamic cursors/) {
74 'Dynamic Cursors not functional, tds_version 8.0 or greater required if using'.
78 $schema->resultset('Artist')->search({ name => 'foo' })->delete;
80 $new = $schema->resultset('Artist')->create({ name => 'foo' });
82 ok($new->artistid > 0, "Auto-PK worked");
86 $seen_id{$new->artistid}++;
90 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
91 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
92 $seen_id{$new->artistid}++;
95 my $it = $schema->resultset('Artist')->search( {}, {
97 order_by => 'artistid',
100 is( $it->count, 3, "LIMIT count ok" );
101 is( $it->next->name, "foo", "iterator->next ok" );
103 is( $it->next->name, "Artist 2", "iterator->next ok" );
104 is( $it->next, undef, "next past end of resultset ok" );
108 $schema->storage->dbh_do (sub {
109 my ($storage, $dbh) = @_;
110 eval { $dbh->do("DROP TABLE artist") };
112 CREATE TABLE artist (
113 artistid UNIQUEIDENTIFIER NOT NULL,
115 rank INT NOT NULL DEFAULT '13',
116 charfield CHAR(10) NULL,
117 a_guid UNIQUEIDENTIFIER,
118 primary key(artistid)
123 # start disconnected to make sure insert works on an un-reblessed storage
124 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
128 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
129 } 'created a row with a GUID';
132 eval { $row->artistid },
133 'row has GUID PK col populated',
138 eval { $row->a_guid },
139 'row has a GUID col with auto_nextval populated',
143 my $row_from_db = $schema->resultset('ArtistGUID')
144 ->search({ name => 'mtfnpy' })->first;
146 is $row_from_db->artistid, $row->artistid,
147 'PK GUID round trip';
149 is $row_from_db->a_guid, $row->a_guid,
150 'NON-PK GUID round trip';
153 $schema->storage->dbh_do (sub {
154 my ($storage, $dbh) = @_;
155 eval { $dbh->do("DROP TABLE money_test") };
157 CREATE TABLE money_test (
158 id INT IDENTITY PRIMARY KEY,
164 my $rs = $schema->resultset('Money');
167 $row = $rs->create({ amount => 100 });
168 } 'inserted a money value';
170 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
173 $row->update({ amount => 200 });
174 } 'updated a money value';
176 cmp_ok $rs->find($row->id)->amount, '==', 200,
177 'updated money value round-trip';
180 $row->update({ amount => undef });
181 } 'updated a money value to NULL';
183 is $rs->find($row->id)->amount, undef,'updated money value to NULL round-trip';
185 $schema->storage->dbh_do (sub {
186 my ($storage, $dbh) = @_;
187 eval { $dbh->do("DROP TABLE owners") };
188 eval { $dbh->do("DROP TABLE books") };
191 id INT IDENTITY (1, 1) NOT NULL,
198 CREATE TABLE owners (
199 id INT IDENTITY (1, 1) NOT NULL,
207 # start a new connection, make sure rebless works
208 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
209 $schema->populate ('Owners', [
222 [qw/12 face_to_face/],
227 }, 'populate with PKs supplied ok' );
231 # start a new connection, make sure rebless works
232 # test an insert with a supplied identity, followed by one without
233 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
236 $schema->resultset ('Owners')->create ({ id => $id, name => "troglodoogle $id" });
237 $schema->resultset ('Owners')->create ({ name => "troglodoogle " . ($id + 1) });
239 }, 'create with/without PKs ok' );
241 is ($schema->resultset ('Owners')->count, 19, 'owner rows really in db' );
244 # start a new connection, make sure rebless works
245 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
246 $schema->populate ('BooksInLibrary', [
247 [qw/source owner title /],
248 [qw/Library 1 secrets0/],
249 [qw/Library 1 secrets1/],
250 [qw/Eatery 1 secrets2/],
251 [qw/Library 2 secrets3/],
252 [qw/Library 3 secrets4/],
253 [qw/Eatery 3 secrets5/],
254 [qw/Library 4 secrets6/],
255 [qw/Library 5 secrets7/],
256 [qw/Eatery 5 secrets8/],
257 [qw/Library 6 secrets9/],
258 [qw/Library 7 secrets10/],
259 [qw/Eatery 7 secrets11/],
260 [qw/Library 8 secrets12/],
262 }, 'populate without PKs supplied ok' );
264 # plain ordered subqueries throw
266 $schema->resultset('Owners')->search ({}, { order_by => 'name' })->as_query
267 }, qr/ordered subselect encountered/, 'Ordered Subselect detection throws ok');
269 # make sure ordered subselects *somewhat* work
271 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
273 my $al = $owners->current_source_alias;
274 my $sealed_owners = $owners->result_source->resultset->search (
280 -source_handle => $owners->result_source->handle,
281 $al => $owners->as_query,
287 [ map { $_->name } ($sealed_owners->all) ],
288 [ map { $_->name } ($owners->all) ],
289 'Sort preserved from within a subquery',
294 local $TODO = "This porbably will never work, but it isn't critical either afaik";
296 my $book_owner_ids = $schema->resultset ('BooksInLibrary')
297 ->search ({}, { join => 'owner', distinct => 1, order_by => 'owner.name', unsafe_subselect_ok => 1 })
298 ->get_column ('owner');
300 my $book_owners = $schema->resultset ('Owners')->search ({
301 id => { -in => $book_owner_ids->as_query }
305 [ map { $_->id } ($book_owners->all) ],
306 [ $book_owner_ids->all ],
307 'Sort is preserved across IN subqueries',
311 # This is known not to work - thus the negative test
313 my $owners = $schema->resultset ('Owners')->search ({}, { order_by => 'name', offset => 2, rows => 3, unsafe_subselect_ok => 1 });
314 my $corelated_owners = $owners->result_source->resultset->search (
316 id => { -in => $owners->get_column('id')->as_query },
319 order_by => 'name' #reorder because of what is shown above
324 join ("\x00", map { $_->name } ($corelated_owners->all) ),
326 join ("\x00", map { $_->name } ($owners->all) ),
327 'Sadly sort not preserved from within a corelated subquery',
331 join ("\x00", sort map { $_->name } ($corelated_owners->all) ),
333 join ("\x00", sort map { $_->name } ($owners->all) ),
334 'Which in fact gives a completely wrong dataset',
339 # make sure right-join-side single-prefetch ordering limit works
341 my $rs = $schema->resultset ('BooksInLibrary')->search (
343 'owner.name' => { '!=', 'woggle' },
347 order_by => 'owner.name',
350 # this is the order in which they should come from the above query
351 my @owner_names = qw/boggle fISMBoC fREW fRIOUX fROOH fRUE wiggle wiggle/;
353 is ($rs->all, 8, 'Correct amount of objects from right-sorted joined resultset');
355 [map { $_->owner->name } ($rs->all) ],
357 'Rows were properly ordered'
360 my $limited_rs = $rs->search ({}, {rows => 7, offset => 2, unsafe_subselect_ok => 1});
361 is ($limited_rs->count, 6, 'Correct count of limited right-sorted joined resultset');
362 is ($limited_rs->count_rs->next, 6, 'Correct count_rs of limited right-sorted joined resultset');
365 $schema->storage->debugcb(sub { $queries++; });
366 $schema->storage->debug(1);
369 [map { $_->owner->name } ($limited_rs->all) ],
370 [@owner_names[2 .. 7]],
371 'Limited rows were properly ordered'
373 is ($queries, 1, 'Only one query with prefetch');
375 $schema->storage->debugcb(undef);
376 $schema->storage->debug(0);
380 [map { $_->name } ($limited_rs->search_related ('owner')->all) ],
381 [@owner_names[2 .. 7]],
382 'Rows are still properly ordered after search_related'
388 # try a prefetch on tables with identically named columns
391 # set quote char - make sure things work while quoted
392 $schema->storage->_sql_maker->{quote_char} = [qw/[ ]/];
393 $schema->storage->_sql_maker->{name_sep} = '.';
396 # try a ->has_many direction
397 my $owners = $schema->resultset ('Owners')->search (
399 'books.id' => { '!=', undef },
400 'me.name' => { '!=', 'somebogusstring' },
404 order_by => { -asc => \['name + ?', [ test => 'xxx' ]] }, # test bindvar propagation
405 rows => 3, # 8 results total
406 unsafe_subselect_ok => 1,
410 my ($sql, @bind) = @${$owners->page(3)->as_query};
413 [ ([ 'me.name' => 'somebogusstring' ], [ test => 'xxx' ]) x 2 ], # double because of the prefetch subq
416 is ($owners->page(1)->all, 3, 'has_many prefetch returns correct number of rows');
417 is ($owners->page(1)->count, 3, 'has-many prefetch returns correct count');
419 is ($owners->page(3)->all, 2, 'has_many prefetch returns correct number of rows');
420 is ($owners->page(3)->count, 2, 'has-many prefetch returns correct count');
421 is ($owners->page(3)->count_rs->next, 2, 'has-many prefetch returns correct count_rs');
424 # try a ->belongs_to direction (no select collapse, group_by should work)
425 my $books = $schema->resultset ('BooksInLibrary')->search (
427 'owner.name' => [qw/wiggle woggle/],
431 having => \['1 = ?', [ test => 1 ] ], #test having propagation
433 rows => 2, # 3 results total
434 order_by => { -desc => 'me.owner' },
435 unsafe_subselect_ok => 1,
439 ($sql, @bind) = @${$books->page(3)->as_query};
444 [ 'owner.name' => 'wiggle' ], [ 'owner.name' => 'woggle' ], [ source => 'Library' ], [ test => '1' ],
446 [ 'owner.name' => 'wiggle' ], [ 'owner.name' => 'woggle' ], [ source => 'Library' ],
450 is ($books->page(1)->all, 2, 'Prefetched grouped search returns correct number of rows');
451 is ($books->page(1)->count, 2, 'Prefetched grouped search returns correct count');
453 is ($books->page(2)->all, 1, 'Prefetched grouped search returns correct number of rows');
454 is ($books->page(2)->count, 1, 'Prefetched grouped search returns correct count');
455 is ($books->page(2)->count_rs->next, 1, 'Prefetched grouped search returns correct count_rs');
462 if (my $dbh = eval { $schema->storage->_dbh }) {
463 eval { $dbh->do("DROP TABLE $_") }
464 for qw/artist money_test books owners/;