Deprecate rolled-out hash-condition in search()
[dbsrgits/DBIx-Class-Historic.git] / t / 74mssql.t
CommitLineData
70350518 1use strict;
bbdda281 2use warnings;
70350518 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
77c7628c 21{
22 my $srv_ver = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info->{dbms_version};
23 ok ($srv_ver, 'Got a test server version on fresh schema: ' . ($srv_ver||'???') );
24}
25
bbdda281 26my $testdb_supports_placeholders = DBICTest::Schema->connect($dsn, $user, $pass)
27 ->storage
28 ->_supports_typeless_placeholders;
29my @test_storages = (
30 $testdb_supports_placeholders ? 'DBI::Sybase::Microsoft_SQL_Server' : (),
c29ce317 31 'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
7379eb67 32);
8c52ffd3 33
bbdda281 34my $schema;
35for my $storage_type (@test_storages) {
36 $schema = DBICTest::Schema->connect($dsn, $user, $pass);
eef2ff6c 37
bbdda281 38 if ($storage_type =~ /NoBindVars\z/) {
39 # since we want to use the nobindvar - disable the capability so the
40 # rebless happens to the correct class
41 $schema->storage->_use_typeless_placeholders (0);
7379eb67 42 }
43
bbdda281 44 $schema->storage->ensure_connected;
c29ce317 45 isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
7379eb67 46
99083752 47 SKIP: {
48 skip 'This version of DBD::Sybase segfaults on disconnect', 1 if DBD::Sybase->VERSION < 1.08;
7379eb67 49
99083752 50 # start disconnected to test _ping
51 $schema->storage->_dbh->disconnect;
52
53 lives_ok {
54 $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
55 } '_ping works';
56 }
ecdf1ac8 57
58 my $dbh = $schema->storage->dbh;
7379eb67 59
60 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL
61 DROP TABLE artist");
62 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL
63 DROP TABLE cd");
64
65 $dbh->do("CREATE TABLE artist (artistid INT IDENTITY PRIMARY KEY, name VARCHAR(100), rank INT DEFAULT '13', charfield CHAR(10) NULL);");
66 $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 67# Just to test compat shim, Auto is in Core
7379eb67 68 $schema->class('Artist')->load_components('PK::Auto::MSSQL');
eef2ff6c 69
70# Test PK
7379eb67 71 my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
72 ok($new->artistid, "Auto-PK worked");
eef2ff6c 73
74# Test LIMIT
7379eb67 75 for (1..6) {
76 $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
77 }
eef2ff6c 78
7379eb67 79 my $it = $schema->resultset('Artist')->search( { },
80 { rows => 3,
81 offset => 2,
82 order_by => 'artistid'
83 }
84 );
eef2ff6c 85
b4474f31 86# Test ? in data don't get treated as placeholders
7379eb67 87 my $cd = $schema->resultset('CD')->create( {
88 artist => 1,
89 title => 'Does this break things?',
90 year => 2007,
91 } );
92 ok($cd->id, 'Not treating ? in data as placeholders');
93
94 is( $it->count, 3, "LIMIT count ok" );
95 ok( $it->next->name, "iterator->next ok" );
96 $it->next;
97 $it->next;
98 is( $it->next, undef, "next past end of resultset ok" );
eef2ff6c 99
5064f5c3 100# test MONEY column support
7379eb67 101 $schema->storage->dbh_do (sub {
102 my ($storage, $dbh) = @_;
103 eval { $dbh->do("DROP TABLE money_test") };
104 $dbh->do(<<'SQL');
7379eb67 105 CREATE TABLE money_test (
106 id INT IDENTITY PRIMARY KEY,
107 amount MONEY NULL
108 )
5064f5c3 109SQL
110
7379eb67 111 });
5064f5c3 112
7379eb67 113 my $rs = $schema->resultset('Money');
5064f5c3 114
7379eb67 115 my $row;
116 lives_ok {
117 $row = $rs->create({ amount => 100 });
118 } 'inserted a money value';
5064f5c3 119
a33d2444 120 cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
5064f5c3 121
7379eb67 122 lives_ok {
123 $row->update({ amount => 200 });
124 } 'updated a money value';
5064f5c3 125
a33d2444 126 cmp_ok $rs->find($row->id)->amount, '==', 200,
127 'updated money value round-trip';
5064f5c3 128
7379eb67 129 lives_ok {
130 $row->update({ amount => undef });
131 } 'updated a money value to NULL';
5064f5c3 132
c8365716 133 is $rs->find($row->id)->amount,
134 undef, 'updated money value to NULL round-trip';
a467a0c9 135
8a0720e2 136 $rs->create({ amount => 300 }) for (1..3);
137
a467a0c9 138 # test multiple active statements
139 lives_ok {
a467a0c9 140 my $artist_rs = $schema->resultset('Artist');
141 while (my $row = $rs->next) {
142 my $artist = $artist_rs->next;
143 }
8a0720e2 144 $rs->reset;
a467a0c9 145 } 'multiple active statements';
8a0720e2 146
b90d7eba 147 $rs->delete;
148
149 # test simple transaction with commit
150 lives_ok {
151 $schema->txn_do(sub {
152 $rs->create({ amount => 400 });
153 });
154 } 'simple transaction';
155
156 cmp_ok $rs->first->amount, '==', 400, 'committed';
157 $rs->reset;
158
159 $rs->delete;
160
161 # test rollback
162 throws_ok {
163 $schema->txn_do(sub {
164 $rs->create({ amount => 400 });
165 die 'mtfnpy';
166 });
167 } qr/mtfnpy/, 'simple failed txn';
168
169 is $rs->first, undef, 'rolled back';
170 $rs->reset;
a218ef4e 171
172 # test RNO detection when version detection fails
173 SKIP: {
174 my $storage = $schema->storage;
175 my $version = $storage->_server_info->{normalized_dbms_version};
99083752 176
177 skip 'could not detect SQL Server version', 1 if not defined $version;
a218ef4e 178
179 my $have_rno = $version >= 9 ? 1 : 0;
180
bbdda281 181 local $storage->{_dbh_details}{info} = {}; # delete cache
4282b6f8 182
a218ef4e 183 my $rno_detected =
6a247f33 184 ($storage->sql_limit_dialect eq 'RowNumberOver') ? 1 : 0;
a218ef4e 185
233c3a46 186 ok (($have_rno == $rno_detected),
a218ef4e 187 'row_number() over support detected correctly');
188 }
0a064375 189
190 {
191 my $schema = DBICTest::Schema->clone;
192 $schema->connection($dsn, $user, $pass);
193
194 like $schema->storage->sql_maker->{limit_dialect},
195 qr/^(?:Top|RowNumberOver)\z/,
196 'sql_maker is correct on unconnected schema';
197 }
7379eb67 198}
5064f5c3 199
559ae74c 200# test op-induced autoconnect
201lives_ok (sub {
202
203 my $schema = DBICTest::Schema->clone;
204 $schema->connection($dsn, $user, $pass);
205
206 my $artist = $schema->resultset ('Artist')->search ({}, { order_by => 'artistid' })->next;
207 is ($artist->id, 1, 'Artist retrieved successfully');
208}, 'Query-induced autoconnect works');
209
1a789a72 210done_testing;
211
3ff5b740 212# clean up our mess
213END {
7379eb67 214 if (my $dbh = eval { $schema->storage->dbh }) {
215 $dbh->do("IF OBJECT_ID('artist', 'U') IS NOT NULL DROP TABLE artist");
216 $dbh->do("IF OBJECT_ID('cd', 'U') IS NOT NULL DROP TABLE cd");
217 $dbh->do("IF OBJECT_ID('money_test', 'U') IS NOT NULL DROP TABLE money_test");
218 }
3ff5b740 219}