add sql_maker to @rdbms_specific_methods
[dbsrgits/DBIx-Class.git] / t / 74mssql.t
CommitLineData
70350518 1use strict;
2use warnings;
3
46e3af47 4# use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
5BEGIN {
6 if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
7 unshift @INC, $_ for split /:/, $lib_dirs;
8 }
9}
10
70350518 11use Test::More;
8c52ffd3 12use Test::Exception;
70350518 13use lib qw(t/lib);
14use DBICTest;
eef2ff6c 15
16my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
17
70350518 18plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
eef2ff6c 19 unless ($dsn);
20
7379eb67 21my @storage_types = (
c29ce317 22 'DBI::Sybase::Microsoft_SQL_Server',
23 'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
7379eb67 24);
25my $storage_idx = -1;
26my $schema;
27
1a789a72 28my $NUMBER_OF_TESTS_IN_BLOCK = 18;
7379eb67 29for my $storage_type (@storage_types) {
30 $storage_idx++;
b3f41261 31
7379eb67 32 $schema = DBICTest::Schema->clone;
33
7379eb67 34 $schema->connection($dsn, $user, $pass);
8c52ffd3 35
918ab8ae 36 if ($storage_idx != 0) { # autodetect
37 no warnings 'redefine';
38 local *DBIx::Class::Storage::DBI::_typeless_placeholders_supported =
39 sub { 0 };
40# $schema->storage_type("::$storage_type");
41 $schema->storage->ensure_connected;
42 }
43 else {
44 $schema->storage->ensure_connected;
45 }
eef2ff6c 46
7379eb67 47 if ($storage_idx == 0 && ref($schema->storage) =~ /NoBindVars\z/) {
48 my $tb = Test::More->builder;
1a789a72 49 $tb->skip('no placeholders') for 1..$NUMBER_OF_TESTS_IN_BLOCK;
7379eb67 50 next;
51 }
52
c29ce317 53 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
7379eb67 54
ecdf1ac8 55# start disconnected to test _ping
7379eb67 56 $schema->storage->_dbh->disconnect;
57
ecdf1ac8 58 lives_ok {
59 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
60 } '_ping works';
61
62 my $dbh = $schema->storage->dbh;
7379eb67 63
64 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
65 DROP TABLE artist");
66 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
67 DROP TABLE cd");
68
69 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
70 $dbh->do("CREATE TABLE cd (cdid INT IDENTITY PRIMARY KEY, artist INT, title VARCHAR(100), year VARCHAR(100), genreid INT NULL, single_track INT NULL);");
3ff5b740 71# Just to test compat shim, Auto is in Core
7379eb67 72 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
eef2ff6c 73
74# Test PK
7379eb67 75 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
76 ok($new->artistid, "Auto-PK worked");
eef2ff6c 77
78# Test LIMIT
7379eb67 79 for (1..6) {
80 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
81 }
eef2ff6c 82
7379eb67 83 my $it = $schema->resultset('Artist')->search( { },
84 { rows => 3,
85 offset => 2,
86 order_by => 'artistid'
87 }
88 );
eef2ff6c 89
b4474f31 90# Test ? in data don't get treated as placeholders
7379eb67 91 my $cd = $schema->resultset('CD')->create( {
92 artist => 1,
93 title => 'Does this break things?',
94 year => 2007,
95 } );
96 ok($cd->id, 'Not treating ? in data as placeholders');
97
98 is( $it->count, 3, "LIMIT count ok" );
99 ok( $it->next->name, "iterator->next ok" );
100 $it->next;
101 $it->next;
102 is( $it->next, undef, "next past end of resultset ok" );
eef2ff6c 103
5064f5c3 104# test MONEY column support
7379eb67 105 $schema->storage->dbh_do (sub {
106 my ($storage, $dbh) = @_;
107 eval { $dbh->do("DROP TABLE money_test") };
108 $dbh->do(<<'SQL');
7379eb67 109 CREATE TABLE money_test (
110 id INT IDENTITY PRIMARY KEY,
111 amount MONEY NULL
112 )
5064f5c3 113SQL
114
7379eb67 115 });
5064f5c3 116
7379eb67 117 my $rs = $schema->resultset('Money');
5064f5c3 118
7379eb67 119 my $row;
120 lives_ok {
121 $row = $rs->create({ amount => 100 });
122 } 'inserted a money value';
5064f5c3 123
a33d2444 124 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
5064f5c3 125
7379eb67 126 lives_ok {
127 $row->update({ amount => 200 });
128 } 'updated a money value';
5064f5c3 129
a33d2444 130 cmp_ok $rs->find($row->id)->amount, '==', 200,
131 'updated money value round-trip';
5064f5c3 132
7379eb67 133 lives_ok {
134 $row->update({ amount => undef });
135 } 'updated a money value to NULL';
5064f5c3 136
c8365716 137 is $rs->find($row->id)->amount,
138 undef, 'updated money value to NULL round-trip';
a467a0c9 139
8a0720e2 140 $rs->create({ amount => 300 }) for (1..3);
141
a467a0c9 142 # test multiple active statements
143 lives_ok {
a467a0c9 144 my $artist_rs = $schema->resultset('Artist');
145 while (my $row = $rs->next) {
146 my $artist = $artist_rs->next;
147 }
8a0720e2 148 $rs->reset;
a467a0c9 149 } 'multiple active statements';
8a0720e2 150
b90d7eba 151 $rs->delete;
152
153 # test simple transaction with commit
154 lives_ok {
155 $schema->txn_do(sub {
156 $rs->create({ amount => 400 });
157 });
158 } 'simple transaction';
159
160 cmp_ok $rs->first->amount, '==', 400, 'committed';
161 $rs->reset;
162
163 $rs->delete;
164
165 # test rollback
166 throws_ok {
167 $schema->txn_do(sub {
168 $rs->create({ amount => 400 });
169 die 'mtfnpy';
170 });
171 } qr/mtfnpy/, 'simple failed txn';
172
173 is $rs->first, undef, 'rolled back';
174 $rs->reset;
a218ef4e 175
176 # test RNO detection when version detection fails
177 SKIP: {
178 my $storage = $schema->storage;
179 my $version = $storage->_server_info->{normalized_dbms_version};
180
181 skip 1, 'could not detect SQL Server version' if not defined $version;
182
183 my $have_rno = $version >= 9 ? 1 : 0;
184
185 # Delete version information to force RNO check when rebuilding SQLA
186 # instance.
187 no strict 'refs';
188 no warnings 'redefine';
189 local *{(ref $storage).'::_get_server_version'} = sub { undef };
190
191 my $server_info = { %{ $storage->_server_info_hash } }; # clone
192
193 delete @$server_info{qw/dbms_version normalized_dbms_version/};
194
195 local $storage->{_server_info_hash} = $server_info;
196 local $storage->{_sql_maker} = undef;
197 local $storage->{_sql_maker_opts} = undef;
198
199 $storage->sql_maker;
200
201 my $rno_detected =
233c3a46 202 ($storage->{_sql_maker_opts}{limit_dialect} eq 'RowNumberOver') ? 1 : 0;
a218ef4e 203
233c3a46 204 ok (($have_rno == $rno_detected),
a218ef4e 205 'row_number() over support detected correctly');
206 }
0a064375 207
208 {
209 my $schema = DBICTest::Schema->clone;
210 $schema->connection($dsn, $user, $pass);
211
212 like $schema->storage->sql_maker->{limit_dialect},
213 qr/^(?:Top|RowNumberOver)\z/,
214 'sql_maker is correct on unconnected schema';
215 }
7379eb67 216}
5064f5c3 217
559ae74c 218# test op-induced autoconnect
219lives_ok (sub {
220
221 my $schema = DBICTest::Schema->clone;
222 $schema->connection($dsn, $user, $pass);
223
224 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
225 is ($artist->id, 1, 'Artist retrieved successfully');
226}, 'Query-induced autoconnect works');
227
1a789a72 228done_testing;
229
3ff5b740 230# clean up our mess
231END {
7379eb67 232 if (my $dbh = eval { $schema->storage->dbh }) {
233 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
234 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
235 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");
236 }
3ff5b740 237}