Backout sybase changes
[dbsrgits/DBIx-Class.git] / t / 746mssql.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8 use DBIC::SqlMakerTest;
9
10 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
11
12 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
13   unless ($dsn && $user);
14
15 DBICTest::Schema->load_classes('ArtistGUID');
16 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
17
18 {
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 };
23
24   $schema->storage->ensure_connected;
25
26   is( $connect_count, 1, 'only one connection made');
27 }
28
29 isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
30
31 $schema->storage->dbh_do (sub {
32     my ($storage, $dbh) = @_;
33     eval { $dbh->do("DROP TABLE artist") };
34     $dbh->do(<<'SQL');
35 CREATE TABLE artist (
36    artistid INT IDENTITY NOT NULL,
37    name VARCHAR(100),
38    rank INT NOT NULL DEFAULT '13',
39    charfield CHAR(10) NULL,
40    primary key(artistid)
41 )
42 SQL
43 });
44
45 my %seen_id;
46
47 my @opts = (
48   { on_connect_call => 'use_dynamic_cursors' },
49   {},
50 );
51 my $new;
52
53 # test Auto-PK with different options
54 for my $opts (@opts) {
55   SKIP: {
56     $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opts);
57
58     eval {
59       $schema->storage->ensure_connected
60     };
61     if ($@ =~ /dynamic cursors/) {
62       skip
63 'Dynamic Cursors not functional, tds_version 8.0 or greater required if using'.
64 ' FreeTDS', 1;
65     }
66
67     $schema->resultset('Artist')->search({ name => 'foo' })->delete;
68
69     $new = $schema->resultset('Artist')->create({ name => 'foo' });
70
71     ok($new->artistid > 0, "Auto-PK worked");
72   }
73 }
74
75 $seen_id{$new->artistid}++;
76
77 # test LIMIT support
78 for (1..6) {
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}++;
82 }
83
84 my $it = $schema->resultset('Artist')->search( {}, {
85     rows => 3,
86     order_by => 'artistid',
87 });
88
89 is( $it->count, 3, "LIMIT count ok" );
90 is( $it->next->name, "foo", "iterator->next ok" );
91 $it->next;
92 is( $it->next->name, "Artist 2", "iterator->next ok" );
93 is( $it->next, undef, "next past end of resultset ok" );
94
95 # test GUID columns
96
97 $schema->storage->dbh_do (sub {
98     my ($storage, $dbh) = @_;
99     eval { $dbh->do("DROP TABLE artist") };
100     $dbh->do(<<'SQL');
101 CREATE TABLE artist (
102    artistid UNIQUEIDENTIFIER NOT NULL,
103    name VARCHAR(100),
104    rank INT NOT NULL DEFAULT '13',
105    charfield CHAR(10) NULL,
106    a_guid UNIQUEIDENTIFIER,
107    primary key(artistid)
108 )
109 SQL
110 });
111
112 # start disconnected to make sure insert works on an un-reblessed storage
113 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
114
115 my $row;
116 lives_ok {
117   $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
118 } 'created a row with a GUID';
119
120 ok(
121   eval { $row->artistid },
122   'row has GUID PK col populated',
123 );
124 diag $@ if $@;
125
126 ok(
127   eval { $row->a_guid },
128   'row has a GUID col with auto_nextval populated',
129 );
130 diag $@ if $@;
131
132 my $row_from_db = $schema->resultset('ArtistGUID')
133   ->search({ name => 'mtfnpy' })->first;
134
135 is $row_from_db->artistid, $row->artistid,
136   'PK GUID round trip';
137
138 is $row_from_db->a_guid, $row->a_guid,
139   'NON-PK GUID round trip';
140
141 # test MONEY type
142 $schema->storage->dbh_do (sub {
143     my ($storage, $dbh) = @_;
144     eval { $dbh->do("DROP TABLE money_test") };
145     $dbh->do(<<'SQL');
146
147 CREATE TABLE money_test (
148    id INT IDENTITY PRIMARY KEY,
149    amount MONEY NULL
150 )
151
152 SQL
153
154 });
155
156 my $rs = $schema->resultset('Money');
157
158 lives_ok {
159   $row = $rs->create({ amount => 100 });
160 } 'inserted a money value';
161
162 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
163
164 lives_ok {
165   $row->update({ amount => 200 });
166 } 'updated a money value';
167
168 cmp_ok $rs->find($row->id)->amount, '==', 200,
169   'updated money value round-trip';
170
171 lives_ok {
172   $row->update({ amount => undef });
173 } 'updated a money value to NULL';
174
175 is $rs->find($row->id)->amount, undef,'updated money value to NULL round-trip';
176
177 $schema->storage->dbh_do (sub {
178     my ($storage, $dbh) = @_;
179     eval { $dbh->do("DROP TABLE Owners") };
180     eval { $dbh->do("DROP TABLE Books") };
181     $dbh->do(<<'SQL');
182 CREATE TABLE Books (
183    id INT IDENTITY (1, 1) NOT NULL,
184    source VARCHAR(100),
185    owner INT,
186    title VARCHAR(10),
187    price INT NULL
188 )
189
190 CREATE TABLE Owners (
191    id INT IDENTITY (1, 1) NOT NULL,
192    name VARCHAR(100),
193 )
194 SQL
195
196 });
197
198 lives_ok ( sub {
199   # start a new connection, make sure rebless works
200   my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
201   $schema->populate ('Owners', [
202     [qw/id  name  /],
203     [qw/1   wiggle/],
204     [qw/2   woggle/],
205     [qw/3   boggle/],
206     [qw/4   fREW/],
207     [qw/5   fRIOUX/],
208     [qw/6   fROOH/],
209     [qw/7   fRUE/],
210     [qw/8   fISMBoC/],
211     [qw/9   station/],
212     [qw/10   mirror/],
213     [qw/11   dimly/],
214     [qw/12   face_to_face/],
215     [qw/13   icarus/],
216     [qw/14   dream/],
217     [qw/15   dyrstyggyr/],
218   ]);
219 }, 'populate with PKs supplied ok' );
220
221 lives_ok (sub {
222   # start a new connection, make sure rebless works
223   # test an insert with a supplied identity, followed by one without
224   my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
225   for (1..2) {
226     my $id = $_ * 20 ;
227     $schema->resultset ('Owners')->create ({ id => $id, name => "troglodoogle $id" });
228     $schema->resultset ('Owners')->create ({ name => "troglodoogle " . ($id + 1) });
229   }
230 }, 'create with/without PKs ok' );
231
232 is ($schema->resultset ('Owners')->count, 19, 'owner rows really in db' );
233
234 lives_ok ( sub {
235   # start a new connection, make sure rebless works
236   my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
237   $schema->populate ('BooksInLibrary', [
238     [qw/source  owner title   /],
239     [qw/Library 1     secrets0/],
240     [qw/Library 1     secrets1/],
241     [qw/Eatery  1     secrets2/],
242     [qw/Library 2     secrets3/],
243     [qw/Library 3     secrets4/],
244     [qw/Eatery  3     secrets5/],
245     [qw/Library 4     secrets6/],
246     [qw/Library 5     secrets7/],
247     [qw/Eatery  5     secrets8/],
248     [qw/Library 6     secrets9/],
249     [qw/Library 7     secrets10/],
250     [qw/Eatery  7     secrets11/],
251     [qw/Library 8     secrets12/],
252   ]);
253 }, 'populate without PKs supplied ok' );
254
255 #
256 # try a prefetch on tables with identically named columns
257 #
258
259 # set quote char - make sure things work while quoted
260 $schema->storage->_sql_maker->{quote_char} = [qw/[ ]/];
261 $schema->storage->_sql_maker->{name_sep} = '.';
262
263 {
264   # try a ->has_many direction
265   my $owners = $schema->resultset ('Owners')->search ({
266       'books.id' => { '!=', undef }
267     }, {
268       prefetch => 'books',
269       order_by => 'name',
270       rows     => 3,  # 8 results total
271     });
272
273   is ($owners->page(1)->all, 3, 'has_many prefetch returns correct number of rows');
274   is ($owners->page(1)->count, 3, 'has-many prefetch returns correct count');
275
276   TODO: {
277     local $TODO = 'limit past end of resultset problem';
278     is ($owners->page(3)->all, 2, 'has_many prefetch returns correct number of rows');
279     is ($owners->page(3)->count, 2, 'has-many prefetch returns correct count');
280     is ($owners->page(3)->count_rs->next, 2, 'has-many prefetch returns correct count_rs');
281
282     # make sure count does not become overly complex
283     is_same_sql_bind (
284       $owners->page(3)->count_rs->as_query,
285       '(
286         SELECT COUNT( * )
287           FROM (
288             SELECT TOP 3 [me].[id]
289               FROM [owners] [me]
290               LEFT JOIN [books] [books] ON [books].[owner] = [me].[id]
291             WHERE ( [books].[id] IS NOT NULL )
292             GROUP BY [me].[id]
293             ORDER BY [me].[id] DESC
294           ) [count_subq]
295       )',
296       [],
297     );
298   }
299
300   # try a ->belongs_to direction (no select collapse, group_by should work)
301   my $books = $schema->resultset ('BooksInLibrary')->search ({
302       'owner.name' => [qw/wiggle woggle/],
303     }, {
304       distinct => 1,
305       prefetch => 'owner',
306       rows     => 2,  # 3 results total
307       order_by => { -desc => 'owner' },
308       # there is no sane way to order by the right side of a grouped prefetch currently :(
309       #order_by => { -desc => 'owner.name' },
310     });
311
312
313   is ($books->page(1)->all, 2, 'Prefetched grouped search returns correct number of rows');
314   is ($books->page(1)->count, 2, 'Prefetched grouped search returns correct count');
315
316   TODO: {
317     local $TODO = 'limit past end of resultset problem';
318     is ($books->page(2)->all, 1, 'Prefetched grouped search returns correct number of rows');
319     is ($books->page(2)->count, 1, 'Prefetched grouped search returns correct count');
320     is ($books->page(2)->count_rs->next, 1, 'Prefetched grouped search returns correct count_rs');
321
322     # make sure count does not become overly complex (FIXME - the distinct-induced group_by is incorrect)
323     is_same_sql_bind (
324       $books->page(2)->count_rs->as_query,
325       '(
326         SELECT COUNT( * )
327           FROM (
328             SELECT TOP 2 [me].[id]
329               FROM [books] [me]
330               JOIN [owners] [owner] ON [owner].[id] = [me].[owner]
331             WHERE ( ( ( [owner].[name] = ? OR [owner].[name] = ? ) AND [source] = ? ) )
332             GROUP BY [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price]
333             ORDER BY [me].[id] DESC
334           ) [count_subq]
335       )',
336       [
337         [ 'owner.name' => 'wiggle' ],
338         [ 'owner.name' => 'woggle' ],
339         [ 'source' => 'Library' ],
340       ],
341     );
342   }
343 }
344
345 done_testing;
346
347 # clean up our mess
348 END {
349   if (my $dbh = eval { $schema->storage->_dbh }) {
350     eval { $dbh->do("DROP TABLE $_") }
351       for qw/artist money_test Books Owners/;
352   }
353 }
354 # vim:sw=2 sts=2