add sql_maker to @rdbms_specific_methods
[dbsrgits/DBIx-Class.git] / t / 74mssql.t
1 use strict;
2 use warnings;  
3
4 # use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else
5 BEGIN {
6   if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) {
7     unshift @INC, $_ for split /:/, $lib_dirs;
8   }
9 }
10
11 use Test::More;
12 use Test::Exception;
13 use lib qw(t/lib);
14 use DBICTest;
15
16 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_MSSQL_${_}" } qw/DSN USER PASS/};
17
18 plan skip_all => 'Set $ENV{DBICTEST_MSSQL_DSN}, _USER and _PASS to run this test'
19   unless ($dsn);
20
21 my @storage_types = (
22   'DBI::Sybase::Microsoft_SQL_Server',
23   'DBI::Sybase::Microsoft_SQL_Server::NoBindVars',
24 );
25 my $storage_idx = -1;
26 my $schema;
27
28 my $NUMBER_OF_TESTS_IN_BLOCK = 18;
29 for my $storage_type (@storage_types) {
30   $storage_idx++;
31
32   $schema = DBICTest::Schema->clone;
33
34   $schema->connection($dsn, $user, $pass);
35
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   }
46
47   if ($storage_idx == 0 && ref($schema->storage) =~ /NoBindVars\z/) {
48     my $tb = Test::More->builder;
49     $tb->skip('no placeholders') for 1..$NUMBER_OF_TESTS_IN_BLOCK;
50     next;
51   }
52
53   isa_ok($schema->storage, "DBIx::Class::Storage::$storage_type");
54
55 # start disconnected to test _ping
56   $schema->storage->_dbh->disconnect;
57
58   lives_ok {
59     $schema->storage->dbh_do(sub { $_[1]->do('select 1') })
60   } '_ping works';
61
62   my $dbh = $schema->storage->dbh;
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);");
71 # Just to test compat shim, Auto is in Core
72   $schema->class('Artist')->load_components('PK::Auto::MSSQL');
73
74 # Test PK
75   my $new = $schema->resultset('Artist')->create( { name => 'foo' } );
76   ok($new->artistid, "Auto-PK worked");
77
78 # Test LIMIT
79   for (1..6) {
80       $schema->resultset('Artist')->create( { name => 'Artist ' . $_, rank => $_ } );
81   }
82
83   my $it = $schema->resultset('Artist')->search( { },
84       { rows     => 3,
85         offset   => 2,
86         order_by => 'artistid'
87       }
88   );
89
90 # Test ? in data don't get treated as placeholders
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" );
103
104 # test MONEY column support
105   $schema->storage->dbh_do (sub {
106       my ($storage, $dbh) = @_;
107       eval { $dbh->do("DROP TABLE money_test") };
108       $dbh->do(<<'SQL');
109   CREATE TABLE money_test (
110      id INT IDENTITY PRIMARY KEY,
111      amount MONEY NULL
112   )
113 SQL
114
115   });
116
117   my $rs = $schema->resultset('Money');
118
119   my $row;
120   lives_ok {
121     $row = $rs->create({ amount => 100 });
122   } 'inserted a money value';
123
124   cmp_ok $rs->find($row->id)->amount, '==', 100, 'money value round-trip';
125
126   lives_ok {
127     $row->update({ amount => 200 });
128   } 'updated a money value';
129
130   cmp_ok $rs->find($row->id)->amount, '==', 200,
131     'updated money value round-trip';
132
133   lives_ok {
134     $row->update({ amount => undef });
135   } 'updated a money value to NULL';
136
137   is $rs->find($row->id)->amount,
138     undef, 'updated money value to NULL round-trip';
139
140   $rs->create({ amount => 300 }) for (1..3);
141
142   # test multiple active statements
143   lives_ok {
144     my $artist_rs = $schema->resultset('Artist');
145     while (my $row = $rs->next) {
146       my $artist = $artist_rs->next;
147     }
148     $rs->reset;
149   } 'multiple active statements';
150
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;
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 =
202       ($storage->{_sql_maker_opts}{limit_dialect} eq 'RowNumberOver') ? 1 : 0;
203
204     ok (($have_rno == $rno_detected),
205       'row_number() over support detected correctly');
206   }
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   }
216 }
217
218 # test op-induced autoconnect
219 lives_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
228 done_testing;
229
230 # clean up our mess
231 END {
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   }
237 }