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' );
31 $schema->storage->dbh_do (sub {
32 my ($storage, $dbh) = @_;
33 eval { $dbh->do("DROP TABLE artist") };
36 artistid INT IDENTITY NOT NULL,
38 rank INT NOT NULL DEFAULT '13',
39 charfield CHAR(10) NULL,
48 { on_connect_call => 'use_dynamic_cursors' },
53 # test Auto-PK with different options
54 for my $opts (@opts) {
56 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
59 $schema->storage->ensure_connected
61 if ($@ =~ /dynamic cursors/) {
63 'Dynamic Cursors not functional, tds_version 8.0 or greater required if using'.
67 $schema->resultset('Artist')->search({ name => 'foo' })->delete;
69 $new = $schema->resultset('Artist')->create({ name => 'foo' });
71 ok($new->artistid > 0, "Auto-PK worked");
75 $seen_id{$new->artistid}++;
79 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
80 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
81 $seen_id{$new->artistid}++;
84 my $it = $schema->resultset('Artist')->search( {}, {
86 order_by => 'artistid',
89 is( $it->count, 3, "LIMIT count ok" );
90 is( $it->next->name, "foo", "iterator->next ok" );
92 is( $it->next->name, "Artist 2", "iterator->next ok" );
93 is( $it->next, undef, "next past end of resultset ok" );
97 $schema->storage->dbh_do (sub {
98 my ($storage, $dbh) = @_;
99 eval { $dbh->do("DROP TABLE artist") };
101 CREATE TABLE artist (
102 artistid UNIQUEIDENTIFIER NOT NULL,
104 rank INT NOT NULL DEFAULT '13',
105 charfield CHAR(10) NULL,
106 a_guid UNIQUEIDENTIFIER,
107 primary key(artistid)
112 # start disconnected to make sure insert works on an un-reblessed storage
113 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
117 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
118 } 'created a row with a GUID';
121 eval { $row->artistid },
122 'row has GUID PK col populated',
127 eval { $row->a_guid },
128 'row has a GUID col with auto_nextval populated',
132 my $row_from_db = $schema->resultset('ArtistGUID')
133 ->search({ name => 'mtfnpy' })->first;
135 is $row_from_db->artistid, $row->artistid,
136 'PK GUID round trip';
138 is $row_from_db->a_guid, $row->a_guid,
139 'NON-PK GUID round trip';
142 $schema->storage->dbh_do (sub {
143 my ($storage, $dbh) = @_;
144 eval { $dbh->do("DROP TABLE money_test") };
146 CREATE TABLE money_test (
147 id INT IDENTITY PRIMARY KEY,
153 my $rs = $schema->resultset('Money');
156 $row = $rs->create({ amount => 100 });
157 } 'inserted a money value';
159 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
162 $row->update({ amount => 200 });
163 } 'updated a money value';
165 cmp_ok $rs->find($row->id)->amount, '==', 200,
166 'updated money value round-trip';
169 $row->update({ amount => undef });
170 } 'updated a money value to NULL';
172 is $rs->find($row->id)->amount, undef,'updated money value to NULL round-trip';
174 $schema->storage->dbh_do (sub {
175 my ($storage, $dbh) = @_;
176 eval { $dbh->do("DROP TABLE Owners") };
177 eval { $dbh->do("DROP TABLE Books") };
180 id INT IDENTITY (1, 1) NOT NULL,
187 CREATE TABLE Owners (
188 id INT IDENTITY (1, 1) NOT NULL,
196 # start a new connection, make sure rebless works
197 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
198 $schema->populate ('Owners', [
211 [qw/12 face_to_face/],
216 }, 'populate with PKs supplied ok' );
219 # start a new connection, make sure rebless works
220 # test an insert with a supplied identity, followed by one without
221 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
224 $schema->resultset ('Owners')->create ({ id => $id, name => "troglodoogle $id" });
225 $schema->resultset ('Owners')->create ({ name => "troglodoogle " . ($id + 1) });
227 }, 'create with/without PKs ok' );
229 is ($schema->resultset ('Owners')->count, 19, 'owner rows really in db' );
232 # start a new connection, make sure rebless works
233 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
234 $schema->populate ('BooksInLibrary', [
235 [qw/source owner title /],
236 [qw/Library 1 secrets0/],
237 [qw/Library 1 secrets1/],
238 [qw/Eatery 1 secrets2/],
239 [qw/Library 2 secrets3/],
240 [qw/Library 3 secrets4/],
241 [qw/Eatery 3 secrets5/],
242 [qw/Library 4 secrets6/],
243 [qw/Library 5 secrets7/],
244 [qw/Eatery 5 secrets8/],
245 [qw/Library 6 secrets9/],
246 [qw/Library 7 secrets10/],
247 [qw/Eatery 7 secrets11/],
248 [qw/Library 8 secrets12/],
250 }, 'populate without PKs supplied ok' );
252 # make sure ordered subselects work
254 my $book_owner_ids = $schema->resultset ('BooksInLibrary')
255 ->search ({}, { join => 'owner', distinct => 1, order_by => { -desc => 'owner'} })
256 ->get_column ('owner');
258 my $owners = $schema->resultset ('Owners')->search ({
259 id => { -in => $book_owner_ids->as_query }
262 is ($owners->count, 8, 'Correct amount of book owners');
263 is ($owners->all, 8, 'Correct amount of book owner objects');
267 # try a prefetch on tables with identically named columns
270 # set quote char - make sure things work while quoted
271 $schema->storage->_sql_maker->{quote_char} = [qw/[ ]/];
272 $schema->storage->_sql_maker->{name_sep} = '.';
275 # try a ->has_many direction
276 my $owners = $schema->resultset ('Owners')->search (
278 'books.id' => { '!=', undef },
279 'me.name' => { '!=', 'somebogusstring' },
283 order_by => { -asc => \['name + ?', [ test => 'xxx' ]] }, # test bindvar propagation
284 rows => 3, # 8 results total
288 my ($sql, @bind) = @${$owners->page(3)->as_query};
291 [ ([ 'me.name' => 'somebogusstring' ], [ test => 'xxx' ]) x 2 ], # double because of the prefetch subq
294 is ($owners->page(1)->all, 3, 'has_many prefetch returns correct number of rows');
295 is ($owners->page(1)->count, 3, 'has-many prefetch returns correct count');
297 is ($owners->page(3)->all, 2, 'has_many prefetch returns correct number of rows');
298 is ($owners->page(3)->count, 2, 'has-many prefetch returns correct count');
299 is ($owners->page(3)->count_rs->next, 2, 'has-many prefetch returns correct count_rs');
302 # try a ->belongs_to direction (no select collapse, group_by should work)
303 my $books = $schema->resultset ('BooksInLibrary')->search (
305 'owner.name' => [qw/wiggle woggle/],
309 having => \['1 = ?', [ test => 1 ] ], #test having propagation
311 rows => 2, # 3 results total
312 order_by => { -desc => 'owner' },
313 # there is no sane way to order by the right side of a grouped prefetch currently :(
314 #order_by => { -desc => 'owner.name' },
318 ($sql, @bind) = @${$books->page(3)->as_query};
323 [ 'owner.name' => 'wiggle' ], [ 'owner.name' => 'woggle' ], [ source => 'Library' ], [ test => '1' ],
325 [ 'owner.name' => 'wiggle' ], [ 'owner.name' => 'woggle' ], [ source => 'Library' ],
329 is ($books->page(1)->all, 2, 'Prefetched grouped search returns correct number of rows');
330 is ($books->page(1)->count, 2, 'Prefetched grouped search returns correct count');
332 is ($books->page(2)->all, 1, 'Prefetched grouped search returns correct number of rows');
333 is ($books->page(2)->count, 1, 'Prefetched grouped search returns correct count');
334 is ($books->page(2)->count_rs->next, 1, 'Prefetched grouped search returns correct count_rs');
341 if (my $dbh = eval { $schema->storage->_dbh }) {
342 eval { $dbh->do("DROP TABLE $_") }
343 for qw/artist money_test Books Owners/;