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 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
20 no warnings 'redefine';
21 my $connect_count = 0;
22 my $orig_connect = \&DBI::connect;
23 local *DBI::connect = sub { $connect_count++; goto &$orig_connect };
25 $schema->storage->ensure_connected;
27 is( $connect_count, 1, 'only one connection made');
30 isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
32 $schema->storage->dbh_do (sub {
33 my ($storage, $dbh) = @_;
34 eval { $dbh->do("DROP TABLE artist") };
38 artistid INT IDENTITY NOT NULL,
40 rank INT NOT NULL DEFAULT '13',
41 charfield CHAR(10) NULL,
51 # fresh $schema so we start unconnected
52 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
54 # test primary key handling
55 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
56 ok($new->artistid > 0, "Auto-PK worked");
58 $seen_id{$new->artistid}++;
62 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
63 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
64 $seen_id{$new->artistid}++;
67 my $it = $schema->resultset('Artist')->search( {}, {
69 order_by => 'artistid',
72 is( $it->count, 3, "LIMIT count ok" );
73 is( $it->next->name, "foo", "iterator->next ok" );
75 is( $it->next->name, "Artist 2", "iterator->next ok" );
76 is( $it->next, undef, "next past end of resultset ok" );
79 $schema->storage->dbh_do (sub {
80 my ($storage, $dbh) = @_;
81 eval { $dbh->do("DROP TABLE money_test") };
84 CREATE TABLE money_test (
85 id INT IDENTITY PRIMARY KEY,
93 my $rs = $schema->resultset('Money');
97 $row = $rs->create({ amount => 100 });
98 } 'inserted a money value';
100 is $rs->find($row->id)->amount, '100.00', 'money value round-trip';
103 $row->update({ amount => 200 });
104 } 'updated a money value';
106 is $rs->find($row->id)->amount, '200.00', 'updated money value round-trip';
109 $row->update({ amount => undef });
110 } 'updated a money value to NULL';
112 is $rs->find($row->id)->amount, undef,'updated money value to NULL round-trip';
114 $schema->storage->dbh_do (sub {
115 my ($storage, $dbh) = @_;
116 eval { $dbh->do("DROP TABLE Owners") };
117 eval { $dbh->do("DROP TABLE Books") };
122 id INT IDENTITY (1, 1) NOT NULL,
129 CREATE TABLE Owners (
130 id INT IDENTITY (1, 1) NOT NULL,
139 $schema->populate ('Owners', [
152 [qw/12 face_to_face/],
157 }, 'populate with PKs supplied ok' );
160 $schema->populate ('BooksInLibrary', [
161 [qw/source owner title /],
162 [qw/Library 1 secrets0/],
163 [qw/Library 1 secrets1/],
164 [qw/Eatery 1 secrets2/],
165 [qw/Library 2 secrets3/],
166 [qw/Library 3 secrets4/],
167 [qw/Eatery 3 secrets5/],
168 [qw/Library 4 secrets6/],
169 [qw/Library 5 secrets7/],
170 [qw/Eatery 5 secrets8/],
171 [qw/Library 6 secrets9/],
172 [qw/Library 7 secrets10/],
173 [qw/Eatery 7 secrets11/],
174 [qw/Library 8 secrets12/],
176 }, 'populate without PKs supplied ok' );
179 # try a prefetch on tables with identically named columns
182 # set quote char - make sure things work while quoted
183 $schema->storage->_sql_maker->{quote_char} = [qw/[ ]/];
184 $schema->storage->_sql_maker->{name_sep} = '.';
187 # try a ->has_many direction
188 my $owners = $schema->resultset ('Owners')->search ({
189 'books.id' => { '!=', undef }
193 rows => 3, # 8 results total
196 is ($owners->page(1)->all, 3, 'has_many prefetch returns correct number of rows');
197 is ($owners->page(1)->count, 3, 'has-many prefetch returns correct count');
200 local $TODO = 'limit past end of resultset problem';
201 is ($owners->page(3)->all, 2, 'has_many prefetch returns correct number of rows');
202 is ($owners->page(3)->count, 2, 'has-many prefetch returns correct count');
203 is ($owners->page(3)->count_rs->next, 2, 'has-many prefetch returns correct count_rs');
205 # make sure count does not become overly complex FIXME
207 $owners->page(3)->count_rs->as_query,
211 SELECT TOP 3 [me].[id]
213 LEFT JOIN [books] [books] ON [books].[owner] = [me].[id]
214 WHERE ( [books].[id] IS NOT NULL )
216 ORDER BY [me].[id] DESC
223 # try a ->belongs_to direction (no select collapse, group_by should work)
224 my $books = $schema->resultset ('BooksInLibrary')->search ({
225 'owner.name' => [qw/wiggle woggle/],
229 rows => 2, # 3 results total
230 order_by => { -desc => 'owner' },
231 # there is no sane way to order by the right side of a grouped prefetch currently :(
232 #order_by => { -desc => 'owner.name' },
236 is ($books->page(1)->all, 2, 'Prefetched grouped search returns correct number of rows');
237 is ($books->page(1)->count, 2, 'Prefetched grouped search returns correct count');
240 local $TODO = 'limit past end of resultset problem';
241 is ($books->page(2)->all, 1, 'Prefetched grouped search returns correct number of rows');
242 is ($books->page(2)->count, 1, 'Prefetched grouped search returns correct count');
243 is ($books->page(2)->count_rs->next, 1, 'Prefetched grouped search returns correct count_rs');
245 # make sure count does not become overly complex FIXME
247 $books->page(2)->count_rs->as_query,
251 SELECT TOP 2 [me].[id]
253 JOIN [owners] [owner] ON [owner].[id] = [me].[owner]
254 WHERE ( ( ( [owner].[name] = ? OR [owner].[name] = ? ) AND [source] = ? ) )
255 GROUP BY [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price], [owner].[id], [owner].[name]
256 ORDER BY [me].[id] DESC
260 [ 'owner.name' => 'wiggle' ],
261 [ 'owner.name' => 'woggle' ],
262 [ 'source' => 'Library' ],
271 if (my $dbh = eval { $schema->storage->_dbh }) {
272 $dbh->do('DROP TABLE artist');
273 $dbh->do('DROP TABLE money_test');
274 $dbh->do('DROP TABLE Books');
275 $dbh->do('DROP TABLE Owners');