better FreeTDS support
[dbsrgits/DBIx-Class-Historic.git] / t / 746mssql.t
CommitLineData
c1cac633 1use strict;
b9a2c3a5 2use warnings;
c1cac633 3
4use Test::More;
893403c8 5use Test::Exception;
c1cac633 6use lib qw(t/lib);
7use DBICTest;
b2d16f1f 8use DBIC::SqlMakerTest;
c1cac633 9
10my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_ODBC_${_}" } qw/DSN USER PASS/};
11
12plan skip_all => 'Set $ENV{DBICTEST_MSSQL_ODBC_DSN}, _USER and _PASS to run this test'
13 unless ($dsn && $user);
14
f6b185e1 15plan tests => 33;
c1cac633 16
42e5b103 17my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
c1cac633 18
8c0104fe 19{
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 };
24
25 $schema->storage->ensure_connected;
26
27 is( $connect_count, 1, 'only one connection made');
28}
9b3e916d 29
c1cac633 30isa_ok( $schema->storage, 'DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server' );
31
c5f77f6c 32$schema->storage->dbh_do (sub {
33 my ($storage, $dbh) = @_;
34 eval { $dbh->do("DROP TABLE artist") };
35 $dbh->do(<<'SQL');
c1cac633 36CREATE TABLE artist (
37 artistid INT IDENTITY NOT NULL,
a0dd8679 38 name VARCHAR(100),
39da2a2b 39 rank INT NOT NULL DEFAULT '13',
2eebd801 40 charfield CHAR(10) NULL,
c1cac633 41 primary key(artistid)
42)
c5f77f6c 43SQL
44
45});
46
c1cac633 47my %seen_id;
48
2eebd801 49# fresh $schema so we start unconnected
c6fc5645 50$schema = DBICTest::Schema->connect($dsn, $user, $pass);
2eebd801 51
c1cac633 52# test primary key handling
53my $new = $schema->resultset('Artist')->create({ name => 'foo' });
54ok($new->artistid > 0, "Auto-PK worked");
55
56$seen_id{$new->artistid}++;
57
58# test LIMIT support
59for (1..6) {
60 $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
61 is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
62 $seen_id{$new->artistid}++;
63}
64
65my $it = $schema->resultset('Artist')->search( {}, {
66 rows => 3,
67 order_by => 'artistid',
68});
69
70is( $it->count, 3, "LIMIT count ok" );
71is( $it->next->name, "foo", "iterator->next ok" );
72$it->next;
73is( $it->next->name, "Artist 2", "iterator->next ok" );
74is( $it->next, undef, "next past end of resultset ok" );
75
818ec409 76# test MONEY type
77$schema->storage->dbh_do (sub {
78 my ($storage, $dbh) = @_;
79 eval { $dbh->do("DROP TABLE money_test") };
80 $dbh->do(<<'SQL');
818ec409 81CREATE TABLE money_test (
82 id INT IDENTITY PRIMARY KEY,
5064f5c3 83 amount MONEY NULL
818ec409 84)
818ec409 85SQL
818ec409 86});
87
88my $rs = $schema->resultset('Money');
89
90my $row;
91lives_ok {
d68f21ee 92 $row = $rs->create({ amount => 100 });
818ec409 93} 'inserted a money value';
94
95is $rs->find($row->id)->amount, '100.00', 'money value round-trip';
96
d68f21ee 97lives_ok {
98 $row->update({ amount => 200 });
99} 'updated a money value';
100
101is $rs->find($row->id)->amount, '200.00', 'updated money value round-trip';
102
f6b185e1 103lives_ok {
104 $row->update({ amount => undef });
105} 'updated a money value to NULL';
106
107is $rs->find($row->id)->amount, undef,'updated money value to NULL round-trip';
108
b9a2c3a5 109$schema->storage->dbh_do (sub {
110 my ($storage, $dbh) = @_;
111 eval { $dbh->do("DROP TABLE Owners") };
112 eval { $dbh->do("DROP TABLE Books") };
113 $dbh->do(<<'SQL');
b9a2c3a5 114CREATE TABLE Books (
115 id INT IDENTITY (1, 1) NOT NULL,
116 source VARCHAR(100),
117 owner INT,
118 title VARCHAR(10),
119 price INT NULL
120)
121
122CREATE TABLE Owners (
123 id INT IDENTITY (1, 1) NOT NULL,
42e5b103 124 name VARCHAR(100),
b9a2c3a5 125)
b9a2c3a5 126SQL
127
128});
893403c8 129
130lives_ok ( sub {
131 $schema->populate ('Owners', [
132 [qw/id name /],
133 [qw/1 wiggle/],
134 [qw/2 woggle/],
135 [qw/3 boggle/],
136 [qw/4 fREW/],
137 [qw/5 fRIOUX/],
138 [qw/6 fROOH/],
139 [qw/7 fRUE/],
140 [qw/8 fISMBoC/],
141 [qw/9 station/],
142 [qw/10 mirror/],
143 [qw/11 dimly/],
144 [qw/12 face_to_face/],
145 [qw/13 icarus/],
146 [qw/14 dream/],
147 [qw/15 dyrstyggyr/],
148 ]);
149}, 'populate with PKs supplied ok' );
150
151lives_ok ( sub {
152 $schema->populate ('BooksInLibrary', [
153 [qw/source owner title /],
154 [qw/Library 1 secrets0/],
155 [qw/Library 1 secrets1/],
156 [qw/Eatery 1 secrets2/],
157 [qw/Library 2 secrets3/],
158 [qw/Library 3 secrets4/],
159 [qw/Eatery 3 secrets5/],
160 [qw/Library 4 secrets6/],
161 [qw/Library 5 secrets7/],
162 [qw/Eatery 5 secrets8/],
163 [qw/Library 6 secrets9/],
164 [qw/Library 7 secrets10/],
165 [qw/Eatery 7 secrets11/],
166 [qw/Library 8 secrets12/],
167 ]);
168}, 'populate without PKs supplied ok' );
b9a2c3a5 169
170#
02d133f0 171# try a prefetch on tables with identically named columns
b9a2c3a5 172#
173
02d133f0 174# set quote char - make sure things work while quoted
175$schema->storage->_sql_maker->{quote_char} = [qw/[ ]/];
176$schema->storage->_sql_maker->{name_sep} = '.';
177
b9a2c3a5 178{
893403c8 179 # try a ->has_many direction
fc85215b 180 my $owners = $schema->resultset ('Owners')->search ({
181 'books.id' => { '!=', undef }
182 }, {
183 prefetch => 'books',
ff1ef65b 184 order_by => 'name',
56d2561e 185 rows => 3, # 8 results total
fc85215b 186 });
187
56d2561e 188 is ($owners->page(1)->all, 3, 'has_many prefetch returns correct number of rows');
189 is ($owners->page(1)->count, 3, 'has-many prefetch returns correct count');
190
618a0fe3 191 TODO: {
192 local $TODO = 'limit past end of resultset problem';
56d2561e 193 is ($owners->page(3)->all, 2, 'has_many prefetch returns correct number of rows');
194 is ($owners->page(3)->count, 2, 'has-many prefetch returns correct count');
64c7c000 195 is ($owners->page(3)->count_rs->next, 2, 'has-many prefetch returns correct count_rs');
196
197 # make sure count does not become overly complex FIXME
198 is_same_sql_bind (
199 $owners->page(3)->count_rs->as_query,
200 '(
201 SELECT COUNT( * )
202 FROM (
02d133f0 203 SELECT TOP 3 [me].[id]
204 FROM [owners] [me]
205 LEFT JOIN [books] [books] ON [books].[owner] = [me].[id]
206 WHERE ( [books].[id] IS NOT NULL )
207 GROUP BY [me].[id]
208 ORDER BY [me].[id] DESC
209 ) [count_subq]
64c7c000 210 )',
211 [],
212 );
618a0fe3 213 }
b9a2c3a5 214
42e5b103 215 # try a ->belongs_to direction (no select collapse, group_by should work)
fc85215b 216 my $books = $schema->resultset ('BooksInLibrary')->search ({
56d2561e 217 'owner.name' => [qw/wiggle woggle/],
fc85215b 218 }, {
fc85215b 219 distinct => 1,
42e5b103 220 prefetch => 'owner',
56d2561e 221 rows => 2, # 3 results total
ac93965c 222 order_by => { -desc => 'owner' },
223 # there is no sane way to order by the right side of a grouped prefetch currently :(
224 #order_by => { -desc => 'owner.name' },
fc85215b 225 });
226
b1e1d073 227
56d2561e 228 is ($books->page(1)->all, 2, 'Prefetched grouped search returns correct number of rows');
229 is ($books->page(1)->count, 2, 'Prefetched grouped search returns correct count');
b1e1d073 230
618a0fe3 231 TODO: {
232 local $TODO = 'limit past end of resultset problem';
56d2561e 233 is ($books->page(2)->all, 1, 'Prefetched grouped search returns correct number of rows');
234 is ($books->page(2)->count, 1, 'Prefetched grouped search returns correct count');
64c7c000 235 is ($books->page(2)->count_rs->next, 1, 'Prefetched grouped search returns correct count_rs');
236
237 # make sure count does not become overly complex FIXME
238 is_same_sql_bind (
239 $books->page(2)->count_rs->as_query,
240 '(
241 SELECT COUNT( * )
242 FROM (
02d133f0 243 SELECT TOP 2 [me].[id]
244 FROM [books] [me]
245 JOIN [owners] [owner] ON [owner].[id] = [me].[owner]
246 WHERE ( ( ( [owner].[name] = ? OR [owner].[name] = ? ) AND [source] = ? ) )
247 GROUP BY [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price], [owner].[id], [owner].[name]
248 ORDER BY [me].[id] DESC
249 ) [count_subq]
64c7c000 250 )',
251 [
252 [ 'owner.name' => 'wiggle' ],
253 [ 'owner.name' => 'woggle' ],
254 [ 'source' => 'Library' ],
255 ],
256 );
618a0fe3 257 }
b2d16f1f 258
b9a2c3a5 259}
c1cac633 260
261# clean up our mess
262END {
e06ad5d5 263 if (my $dbh = eval { $schema->storage->_dbh }) {
264 eval { $dbh->do("DROP TABLE $_") }
265 for qw/artist money_test Books Owners/;
266 }
c1cac633 267}
fc85215b 268# vim:sw=2 sts=2