Commit | Line | Data |
046e344c |
1 | use strict; |
05d322c8 |
2 | use warnings; |
3a89a69f |
3 | use Test::More; |
4 | use Test::Exception; |
05d322c8 |
5 | |
6 | # use this if you keep a copy of DBD::Sybase linked to FreeTDS somewhere else |
7 | BEGIN { |
8 | if (my $lib_dirs = $ENV{DBICTEST_MSSQL_PERL5LIB}) { |
9 | unshift @INC, $_ for split /:/, $lib_dirs; |
10 | } |
11 | } |
12 | |
8dcf4292 |
13 | my ($dsns, $common_version); |
14 | for (qw/MSSQL MSSQL_ODBC/) { |
15 | next unless $ENV{"DBICTEST_${_}_DSN"}; |
16 | |
17 | $dsns->{$_}{dsn} = $ENV{"DBICTEST_${_}_DSN"}; |
18 | $dsns->{$_}{user} = $ENV{"DBICTEST_${_}_USER"}; |
19 | $dsns->{$_}{password} = $ENV{"DBICTEST_${_}_PASS"}; |
20 | |
21 | require DBI; |
22 | my $dbh = DBI->connect (@{$dsns->{$_}}{qw/dsn user password/}, { RaiseError => 1, PrintError => 0} ); |
23 | my $srv_ver = eval { |
24 | $dbh->get_info(18) |
25 | || |
26 | $dbh->selectrow_hashref('master.dbo.xp_msver ProductVersion')->{Character_Value} |
27 | } || 0; |
28 | |
29 | my ($maj_srv_ver) = $srv_ver =~ /^(\d+)/; |
30 | |
31 | if (! defined $common_version or $common_version > $maj_srv_ver ) { |
32 | $common_version = $maj_srv_ver; |
33 | } |
34 | } |
35 | |
36 | plan skip_all => 'You need to set the DBICTEST_MSSQL_DSN, _USER and _PASS and/or the DBICTEST_MSSQL_ODBC_DSN, _USER and _PASS environment variables' |
37 | unless $dsns; |
38 | |
046e344c |
39 | use lib qw(t/lib); |
40 | use dbixcsl_common_tests; |
41 | |
8dcf4292 |
42 | my $mssql_2008_new_data_types = { |
43 | date => { data_type => 'date' }, |
44 | time => { data_type => 'time' }, |
45 | 'time(0)'=> { data_type => 'time', size => 0 }, |
46 | 'time(1)'=> { data_type => 'time', size => 1 }, |
47 | 'time(2)'=> { data_type => 'time', size => 2 }, |
48 | 'time(3)'=> { data_type => 'time', size => 3 }, |
49 | 'time(4)'=> { data_type => 'time', size => 4 }, |
50 | 'time(5)'=> { data_type => 'time', size => 5 }, |
51 | 'time(6)'=> { data_type => 'time', size => 6 }, |
52 | 'time(7)'=> { data_type => 'time' }, |
53 | datetimeoffset => { data_type => 'datetimeoffset' }, |
54 | 'datetimeoffset(0)' => { data_type => 'datetimeoffset', size => 0 }, |
55 | 'datetimeoffset(1)' => { data_type => 'datetimeoffset', size => 1 }, |
56 | 'datetimeoffset(2)' => { data_type => 'datetimeoffset', size => 2 }, |
57 | 'datetimeoffset(3)' => { data_type => 'datetimeoffset', size => 3 }, |
58 | 'datetimeoffset(4)' => { data_type => 'datetimeoffset', size => 4 }, |
59 | 'datetimeoffset(5)' => { data_type => 'datetimeoffset', size => 5 }, |
60 | 'datetimeoffset(6)' => { data_type => 'datetimeoffset', size => 6 }, |
61 | 'datetimeoffset(7)' => { data_type => 'datetimeoffset' }, |
62 | datetime2 => { data_type => 'datetime2' }, |
63 | 'datetime2(0)' => { data_type => 'datetime2', size => 0 }, |
64 | 'datetime2(1)' => { data_type => 'datetime2', size => 1 }, |
65 | 'datetime2(2)' => { data_type => 'datetime2', size => 2 }, |
66 | 'datetime2(3)' => { data_type => 'datetime2', size => 3 }, |
67 | 'datetime2(4)' => { data_type => 'datetime2', size => 4 }, |
68 | 'datetime2(5)' => { data_type => 'datetime2', size => 5 }, |
69 | 'datetime2(6)' => { data_type => 'datetime2', size => 6 }, |
70 | 'datetime2(7)' => { data_type => 'datetime2' }, |
3a89a69f |
71 | |
8dcf4292 |
72 | hierarchyid => { data_type => 'hierarchyid' }, |
73 | }; |
046e344c |
74 | |
75 | my $tester = dbixcsl_common_tests->new( |
b1e43108 |
76 | vendor => 'mssql', |
046e344c |
77 | auto_inc_pk => 'INTEGER IDENTITY NOT NULL PRIMARY KEY', |
41968729 |
78 | default_function_def => 'DATETIME DEFAULT getdate()', |
8dcf4292 |
79 | connect_info => [values %$dsns], |
b1d11550 |
80 | preserve_case_mode_is_exclusive => 1, |
81 | quote_char => [ qw/[ ]/ ], |
8dcf4292 |
82 | basic_date_datatype => ($common_version >= 10) ? 'DATE' : 'SMALLDATETIME', |
deedd576 |
83 | data_types => { |
81ade4d9 |
84 | # http://msdn.microsoft.com/en-us/library/ms187752.aspx |
85 | |
86 | # numeric types |
87 | 'int identity' => { data_type => 'integer', is_auto_increment => 1 }, |
88 | bigint => { data_type => 'bigint' }, |
89 | int => { data_type => 'integer' }, |
90 | integer => { data_type => 'integer' }, |
91 | smallint => { data_type => 'smallint' }, |
92 | tinyint => { data_type => 'tinyint' }, |
93 | money => { data_type => 'money' }, |
94 | smallmoney => { data_type => 'smallmoney' }, |
95 | bit => { data_type => 'bit' }, |
8c41c3ce |
96 | real => { data_type => 'real' }, |
97 | 'float(14)' => { data_type => 'real' }, |
98 | 'float(24)' => { data_type => 'real' }, |
81ade4d9 |
99 | 'float(25)' => { data_type => 'double precision' }, |
100 | 'float(53)' => { data_type => 'double precision' }, |
101 | float => { data_type => 'double precision' }, |
102 | 'double precision' |
103 | => { data_type => 'double precision' }, |
104 | 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] }, |
105 | 'decimal(6,3)' => { data_type => 'decimal', size => [6,3] }, |
106 | 'dec(6,3)' => { data_type => 'decimal', size => [6,3] }, |
107 | numeric => { data_type => 'numeric' }, |
108 | decimal => { data_type => 'decimal' }, |
109 | dec => { data_type => 'decimal' }, |
110 | |
111 | # datetime types |
81ade4d9 |
112 | datetime => { data_type => 'datetime' }, |
6e566cc4 |
113 | # test rewriting getdate() to current_timestamp |
114 | 'datetime default getdate()' |
701cd3e3 |
115 | => { data_type => 'datetime', default_value => \'current_timestamp', |
116 | original => { default_value => \'getdate()' } }, |
81ade4d9 |
117 | smalldatetime => { data_type => 'smalldatetime' }, |
8dcf4292 |
118 | |
119 | ($common_version >= 10) ? %$mssql_2008_new_data_types : (), |
81ade4d9 |
120 | |
121 | # string types |
122 | char => { data_type => 'char', size => 1 }, |
123 | 'char(2)' => { data_type => 'char', size => 2 }, |
c7e6dc1f |
124 | character => { data_type => 'char', size => 1 }, |
125 | 'character(2)' => { data_type => 'char', size => 2 }, |
81ade4d9 |
126 | 'varchar(2)' => { data_type => 'varchar', size => 2 }, |
127 | nchar => { data_type => 'nchar', size => 1 }, |
128 | 'nchar(2)' => { data_type => 'nchar', size => 2 }, |
129 | 'nvarchar(2)' => { data_type => 'nvarchar', size => 2 }, |
130 | |
131 | # binary types |
8c41c3ce |
132 | 'binary' => { data_type => 'binary', size => 1 }, |
133 | 'binary(2)' => { data_type => 'binary', size => 2 }, |
134 | 'varbinary(2)' => { data_type => 'varbinary', size => 2 }, |
81ade4d9 |
135 | |
136 | # blob types |
137 | 'varchar(max)' => { data_type => 'text' }, |
138 | text => { data_type => 'text' }, |
139 | 'nvarchar(max)' => { data_type => 'ntext' }, |
140 | ntext => { data_type => 'ntext' }, |
141 | 'varbinary(max)' => { data_type => 'image' }, |
142 | image => { data_type => 'image' }, |
143 | |
144 | # other types |
145 | timestamp => { data_type => 'timestamp', inflate_datetime => 0 }, |
16773d6d |
146 | rowversion => { data_type => 'rowversion' }, |
81ade4d9 |
147 | uniqueidentifier => { data_type => 'uniqueidentifier' }, |
81ade4d9 |
148 | sql_variant => { data_type => 'sql_variant' }, |
149 | xml => { data_type => 'xml' }, |
deedd576 |
150 | }, |
3a89a69f |
151 | extra => { |
152 | create => [ |
153 | q{ |
154 | CREATE TABLE [mssql_loader_test1.dot] ( |
155 | id INT IDENTITY NOT NULL PRIMARY KEY, |
156 | dat VARCHAR(8) |
157 | ) |
158 | }, |
159 | q{ |
160 | CREATE TABLE mssql_loader_test3 ( |
161 | id INT IDENTITY NOT NULL PRIMARY KEY |
162 | ) |
163 | }, |
164 | q{ |
165 | CREATE VIEW mssql_loader_test4 AS |
166 | SELECT * FROM mssql_loader_test3 |
167 | }, |
020f3c3a |
168 | # test capitalization of cols in unique constraints and rels |
ccce0e82 |
169 | q{ SET QUOTED_IDENTIFIER ON }, |
170 | q{ SET ANSI_NULLS ON }, |
deedd576 |
171 | q{ |
020f3c3a |
172 | CREATE TABLE [MSSQL_Loader_Test5] ( |
173 | [Id] INT IDENTITY NOT NULL PRIMARY KEY, |
ccce0e82 |
174 | [FooCol] INT NOT NULL, |
175 | [BarCol] INT NOT NULL, |
176 | UNIQUE ([FooCol], [BarCol]) |
deedd576 |
177 | ) |
178 | }, |
020f3c3a |
179 | q{ |
180 | CREATE TABLE [MSSQL_Loader_Test6] ( |
181 | [Five_Id] INT REFERENCES [MSSQL_Loader_Test5] ([Id]) |
182 | ) |
183 | }, |
3a89a69f |
184 | ], |
185 | pre_drop_ddl => [ |
186 | 'CREATE TABLE mssql_loader_test3 (id INT IDENTITY NOT NULL PRIMARY KEY)', |
187 | 'DROP VIEW mssql_loader_test4', |
188 | ], |
189 | drop => [ |
190 | '[mssql_loader_test1.dot]', |
deedd576 |
191 | 'mssql_loader_test3', |
060f5ecd |
192 | 'MSSQL_Loader_Test6', |
193 | 'MSSQL_Loader_Test5', |
3a89a69f |
194 | ], |
bfb43060 |
195 | count => 10, |
3a89a69f |
196 | run => sub { |
197 | my ($schema, $monikers, $classes) = @_; |
198 | |
199 | # Test that the table above (with '.' in name) gets loaded correctly. |
200 | ok((my $rs = eval { |
201 | $schema->resultset($monikers->{'[mssql_loader_test1.dot]'}) }), |
202 | 'got a resultset for table with dot in name'); |
203 | |
204 | ok((my $from = eval { $rs->result_source->from }), |
205 | 'got an $rsrc->from for table with dot in name'); |
206 | |
207 | is ref($from), 'SCALAR', '->table with dot in name is a scalar ref'; |
208 | |
209 | is eval { $$from }, "[mssql_loader_test1.dot]", |
210 | '->table with dot in name has correct name'; |
211 | |
deedd576 |
212 | # Test capitalization of columns and unique constraints |
213 | ok ((my $rsrc = $schema->resultset($monikers->{mssql_loader_test5})->result_source), |
214 | 'got result_source'); |
215 | |
bc1cb85e |
216 | if ($schema->_loader->preserve_case) { |
060f5ecd |
217 | is_deeply [ $rsrc->columns ], [qw/Id FooCol BarCol/], |
218 | 'column name case is preserved with case-sensitive collation'; |
020f3c3a |
219 | |
060f5ecd |
220 | my %uniqs = $rsrc->unique_constraints; |
221 | delete $uniqs{primary}; |
3a89a69f |
222 | |
060f5ecd |
223 | is_deeply ((values %uniqs)[0], [qw/FooCol BarCol/], |
224 | 'column name case is preserved in unique constraint with case-sensitive collation'); |
225 | } |
226 | else { |
227 | is_deeply [ $rsrc->columns ], [qw/id foocol barcol/], |
228 | 'column names are lowercased for case-insensitive collation'; |
229 | |
230 | my %uniqs = $rsrc->unique_constraints; |
231 | delete $uniqs{primary}; |
3a89a69f |
232 | |
060f5ecd |
233 | is_deeply ((values %uniqs)[0], [qw/foocol barcol/], |
234 | 'columns in unique constraint lowercased for case-insensitive collation'); |
235 | } |
3a89a69f |
236 | |
020f3c3a |
237 | lives_and { |
060f5ecd |
238 | my $five_row = $schema->resultset($monikers->{mssql_loader_test5})->new_result({}); |
16773d6d |
239 | |
240 | if ($schema->_loader->preserve_case) { |
241 | $five_row->foo_col(1); |
242 | $five_row->bar_col(2); |
243 | } |
244 | else { |
245 | $five_row->foocol(1); |
246 | $five_row->barcol(2); |
247 | } |
060f5ecd |
248 | $five_row->insert; |
249 | |
16773d6d |
250 | my $six_row = $five_row->create_related('mssql_loader_test6s', {}); |
020f3c3a |
251 | |
252 | is $six_row->five->id, 1; |
253 | } 'relationships for mixed-case tables/columns detected'; |
254 | |
3a89a69f |
255 | # Test that a bad view (where underlying table is gone) is ignored. |
256 | my $dbh = $schema->storage->dbh; |
257 | $dbh->do("DROP TABLE mssql_loader_test3"); |
258 | |
259 | my @warnings; |
260 | { |
261 | local $SIG{__WARN__} = sub { push @warnings, $_[0] }; |
262 | $schema->rescan; |
263 | } |
264 | ok ((grep /^Bad table or view 'mssql_loader_test4'/, @warnings), |
265 | 'bad view ignored'); |
266 | |
267 | throws_ok { |
268 | $schema->resultset($monikers->{mssql_loader_test4}) |
269 | } qr/Can't find source/, |
270 | 'no source registered for bad view'; |
271 | }, |
272 | }, |
046e344c |
273 | ); |
274 | |
8dcf4292 |
275 | $tester->run_tests(); |
276 | |
060f5ecd |
277 | # vim:et sts=4 sw=4 tw=0: |