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