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);
17 DBICTest::Schema->load_classes('ArtistGUID');
18 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
21 no warnings 'redefine';
22 my $connect_count = 0;
23 my $orig_connect = \&DBI::connect;
24 local *DBI::connect = sub { $connect_count++; goto &$orig_connect };
26 $schema->storage->ensure_connected;
28 is( $connect_count, 1, 'only one connection made');
31 isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
33 $schema->storage->dbh_do (sub {
34 my ($storage, $dbh) = @_;
35 eval { $dbh->do("DROP TABLE artist") };
38 artistid INT IDENTITY NOT NULL,
40 rank INT NOT NULL DEFAULT '13',
41 charfield CHAR(10) NULL,
50 { on_connect_call => 'use_dynamic_cursors' },
55 # test Auto-PK with different options
56 for my $opts (@opts) {
58 $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
61 $schema->storage->ensure_connected
63 if ($@ =~ /dynamic cursors/) {
65 'Dynamic Cursors not functional, tds_version 8.0 or greater required if using'.
69 $schema->resultset('Artist')->search({ name => 'foo' })->delete;
71 $new = $schema->resultset('Artist')->create({ name => 'foo' });
73 ok($new->artistid > 0, "Auto-PK worked");
77 $seen_id{$new->artistid}++;
81 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
82 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
83 $seen_id{$new->artistid}++;
86 my $it = $schema->resultset('Artist')->search( {}, {
88 order_by => 'artistid',
91 is( $it->count, 3, "LIMIT count ok" );
92 is( $it->next->name, "foo", "iterator->next ok" );
94 is( $it->next->name, "Artist 2", "iterator->next ok" );
95 is( $it->next, undef, "next past end of resultset ok" );
99 $schema->storage->dbh_do (sub {
100 my ($storage, $dbh) = @_;
101 eval { $dbh->do("DROP TABLE artist") };
103 CREATE TABLE artist (
104 artistid UNIQUEIDENTIFIER NOT NULL,
106 rank INT NOT NULL DEFAULT '13',
107 charfield CHAR(10) NULL,
108 a_guid UNIQUEIDENTIFIER,
109 primary key(artistid)
114 # start disconnected to make sure insert works on an un-reblessed storage
115 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
119 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
120 } 'created a row with a GUID';
123 eval { $row->artistid },
124 'row has GUID PK col populated',
129 eval { $row->a_guid },
130 'row has a GUID col with auto_nextval populated',
134 my $row_from_db = $schema->resultset('ArtistGUID')
135 ->search({ name => 'mtfnpy' })->first;
137 is $row_from_db->artistid, $row->artistid,
138 'PK GUID round trip';
140 is $row_from_db->a_guid, $row->a_guid,
141 'NON-PK GUID round trip';
144 $schema->storage->dbh_do (sub {
145 my ($storage, $dbh) = @_;
146 eval { $dbh->do("DROP TABLE money_test") };
149 CREATE TABLE money_test (
150 id INT IDENTITY PRIMARY KEY,
158 my $rs = $schema->resultset('Money');
161 $row = $rs->create({ amount => 100 });
162 } 'inserted a money value';
164 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
167 $row->update({ amount => 200 });
168 } 'updated a money value';
170 cmp_ok $rs->find($row->id)->amount, '==', 200,
171 'updated money value round-trip';
174 $row->update({ amount => undef });
175 } 'updated a money value to NULL';
177 is $rs->find($row->id)->amount, undef,'updated money value to NULL round-trip';
179 $schema->storage->dbh_do (sub {
180 my ($storage, $dbh) = @_;
181 eval { $dbh->do("DROP TABLE Owners") };
182 eval { $dbh->do("DROP TABLE Books") };
185 id INT IDENTITY (1, 1) NOT NULL,
192 CREATE TABLE Owners (
193 id INT IDENTITY (1, 1) NOT NULL,
201 # start a new connection, make sure rebless works
202 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
203 $schema->populate ('Owners', [
216 [qw/12 face_to_face/],
221 }, 'populate with PKs supplied ok' );
224 # start a new connection, make sure rebless works
225 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
226 $schema->populate ('BooksInLibrary', [
227 [qw/source owner title /],
228 [qw/Library 1 secrets0/],
229 [qw/Library 1 secrets1/],
230 [qw/Eatery 1 secrets2/],
231 [qw/Library 2 secrets3/],
232 [qw/Library 3 secrets4/],
233 [qw/Eatery 3 secrets5/],
234 [qw/Library 4 secrets6/],
235 [qw/Library 5 secrets7/],
236 [qw/Eatery 5 secrets8/],
237 [qw/Library 6 secrets9/],
238 [qw/Library 7 secrets10/],
239 [qw/Eatery 7 secrets11/],
240 [qw/Library 8 secrets12/],
242 }, 'populate without PKs supplied ok' );
245 # try a prefetch on tables with identically named columns
248 # set quote char - make sure things work while quoted
249 $schema->storage->_sql_maker->{quote_char} = [qw/[ ]/];
250 $schema->storage->_sql_maker->{name_sep} = '.';
253 # try a ->has_many direction
254 my $owners = $schema->resultset ('Owners')->search ({
255 'books.id' => { '!=', undef }
259 rows => 3, # 8 results total
262 is ($owners->page(1)->all, 3, 'has_many prefetch returns correct number of rows');
263 is ($owners->page(1)->count, 3, 'has-many prefetch returns correct count');
266 local $TODO = 'limit past end of resultset problem';
267 is ($owners->page(3)->all, 2, 'has_many prefetch returns correct number of rows');
268 is ($owners->page(3)->count, 2, 'has-many prefetch returns correct count');
269 is ($owners->page(3)->count_rs->next, 2, 'has-many prefetch returns correct count_rs');
271 # make sure count does not become overly complex
273 $owners->page(3)->count_rs->as_query,
277 SELECT TOP 3 [me].[id]
279 LEFT JOIN [books] [books] ON [books].[owner] = [me].[id]
280 WHERE ( [books].[id] IS NOT NULL )
282 ORDER BY [me].[id] DESC
289 # try a ->belongs_to direction (no select collapse, group_by should work)
290 my $books = $schema->resultset ('BooksInLibrary')->search ({
291 'owner.name' => [qw/wiggle woggle/],
295 rows => 2, # 3 results total
296 order_by => { -desc => 'owner' },
297 # there is no sane way to order by the right side of a grouped prefetch currently :(
298 #order_by => { -desc => 'owner.name' },
302 is ($books->page(1)->all, 2, 'Prefetched grouped search returns correct number of rows');
303 is ($books->page(1)->count, 2, 'Prefetched grouped search returns correct count');
306 local $TODO = 'limit past end of resultset problem';
307 is ($books->page(2)->all, 1, 'Prefetched grouped search returns correct number of rows');
308 is ($books->page(2)->count, 1, 'Prefetched grouped search returns correct count');
309 is ($books->page(2)->count_rs->next, 1, 'Prefetched grouped search returns correct count_rs');
311 # make sure count does not become overly complex (FIXME - the distinct-induced group_by is incorrect)
313 $books->page(2)->count_rs->as_query,
317 SELECT TOP 2 [me].[id]
319 JOIN [owners] [owner] ON [owner].[id] = [me].[owner]
320 WHERE ( ( ( [owner].[name] = ? OR [owner].[name] = ? ) AND [source] = ? ) )
321 GROUP BY [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price]
322 ORDER BY [me].[id] DESC
326 [ 'owner.name' => 'wiggle' ],
327 [ 'owner.name' => 'woggle' ],
328 [ 'source' => 'Library' ],
337 if (my $dbh = eval { $schema->storage->_dbh }) {
338 eval { $dbh->do("DROP TABLE $_") }
339 for qw/artist money_test Books Owners/;