Commit | Line | Data |
fe67d343 |
1 | package DBIx::Class::Schema::Loader::DBI::MSSQL; |
2 | |
3 | use strict; |
4 | use warnings; |
de82711a |
5 | use base 'DBIx::Class::Schema::Loader::DBI::Sybase::Common'; |
fe67d343 |
6 | use Carp::Clan qw/^DBIx::Class/; |
7 | use Class::C3; |
8 | |
3559ae79 |
9 | __PACKAGE__->mk_group_accessors('simple', qw/ |
10 | case_sensitive_collation |
11 | /); |
12 | |
9990e58f |
13 | our $VERSION = '0.07000'; |
fe67d343 |
14 | |
15 | =head1 NAME |
16 | |
17 | DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation. |
18 | |
acfcc1fb |
19 | =head1 DESCRIPTION |
fe67d343 |
20 | |
acfcc1fb |
21 | Base driver for Microsoft SQL Server, used by |
22 | L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server> for support |
23 | via L<DBD::Sybase> and |
24 | L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server> for support via |
25 | L<DBD::ODBC>. |
fe67d343 |
26 | |
acfcc1fb |
27 | See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base> for |
28 | usage information. |
fe67d343 |
29 | |
b065e3df |
30 | =head1 CASE SENSITIVITY |
31 | |
32 | Most MSSQL databases use C<CI> (case-insensitive) collation, for this reason |
33 | generated column names are lower-cased as this makes them easier to work with |
34 | in L<DBIx::Class>. |
35 | |
36 | We attempt to detect the database collation at startup, and set the column |
37 | lowercasing behavior accordingly, as lower-cased column names do not work on |
38 | case-sensitive databases. |
39 | |
81ade4d9 |
40 | To manually control case-sensitive mode, put: |
b065e3df |
41 | |
81ade4d9 |
42 | case_sensitive_collation => 1|0 |
b065e3df |
43 | |
103e90da |
44 | in your Loader options. |
b065e3df |
45 | |
acfcc1fb |
46 | =cut |
fe67d343 |
47 | |
3559ae79 |
48 | sub _is_case_sensitive { |
49 | my $self = shift; |
50 | |
51 | return $self->case_sensitive_collation ? 1 : 0; |
52 | } |
53 | |
54 | sub _setup { |
55 | my $self = shift; |
56 | |
57 | $self->next::method; |
58 | |
b065e3df |
59 | return if defined $self->case_sensitive_collation; |
60 | |
3559ae79 |
61 | my $dbh = $self->schema->storage->dbh; |
62 | |
b7a0a040 |
63 | # We use the sys.databases query for the general case, and fallback to |
64 | # databasepropertyex() if for some reason sys.databases is not available, |
65 | # which does not work over DBD::ODBC with unixODBC+FreeTDS. |
66 | # |
67 | # XXX why does databasepropertyex() not work over DBD::ODBC ? |
40914006 |
68 | # |
69 | # more on collations here: http://msdn.microsoft.com/en-us/library/ms143515.aspx |
b7a0a040 |
70 | my ($collation_name) = |
71 | eval { $dbh->selectrow_array('SELECT collation_name FROM sys.databases WHERE name = DB_NAME()') } |
103e90da |
72 | || eval { $dbh->selectrow_array("SELECT CAST(databasepropertyex(DB_NAME(), 'Collation') AS VARCHAR)") }; |
b7a0a040 |
73 | |
74 | if (not $collation_name) { |
b065e3df |
75 | warn <<'EOF'; |
76 | |
77 | WARNING: MSSQL Collation detection failed. Defaulting to case-insensitive mode. |
78 | Override the 'case_sensitive_collation' attribute in your Loader options if |
79 | needed. |
80 | EOF |
81 | $self->case_sensitive_collation(0); |
b7a0a040 |
82 | return; |
83 | } |
3559ae79 |
84 | |
5ac72caa |
85 | my $case_sensitive = $collation_name =~ /_(?:CS|BIN2?)(?:_|\z)/; |
3559ae79 |
86 | |
5ac72caa |
87 | $self->case_sensitive_collation($case_sensitive ? 1 : 0); |
3559ae79 |
88 | } |
89 | |
90 | sub _lc { |
91 | my ($self, $name) = @_; |
92 | |
93 | return $self->case_sensitive_collation ? $name : lc($name); |
94 | } |
95 | |
acfcc1fb |
96 | sub _tables_list { |
bfb43060 |
97 | my ($self, $opts) = @_; |
fe67d343 |
98 | |
acfcc1fb |
99 | my $dbh = $self->schema->storage->dbh; |
100 | my $sth = $dbh->prepare(<<'EOF'); |
020f3c3a |
101 | SELECT t.table_name |
060f5ecd |
102 | FROM INFORMATION_SCHEMA.TABLES t |
43170884 |
103 | WHERE lower(t.table_schema) = ? |
acfcc1fb |
104 | EOF |
43170884 |
105 | $sth->execute(lc $self->db_schema); |
fe67d343 |
106 | |
bfb43060 |
107 | my @tables = map @$_, @{ $sth->fetchall_arrayref }; |
acfcc1fb |
108 | |
bfb43060 |
109 | return $self->_filter_tables(\@tables, $opts); |
acfcc1fb |
110 | } |
fe67d343 |
111 | |
fe67d343 |
112 | sub _table_pk_info { |
113 | my ($self, $table) = @_; |
114 | my $dbh = $self->schema->storage->dbh; |
115 | my $sth = $dbh->prepare(qq{sp_pkeys '$table'}); |
116 | $sth->execute; |
117 | |
118 | my @keydata; |
119 | |
120 | while (my $row = $sth->fetchrow_hashref) { |
3559ae79 |
121 | push @keydata, $self->_lc($row->{COLUMN_NAME}); |
fe67d343 |
122 | } |
123 | |
124 | return \@keydata; |
125 | } |
126 | |
127 | sub _table_fk_info { |
128 | my ($self, $table) = @_; |
129 | |
a3e05a4b |
130 | my ($local_cols, $remote_cols, $remote_table, @rels, $sth); |
fe67d343 |
131 | my $dbh = $self->schema->storage->dbh; |
a3e05a4b |
132 | eval { |
ea8d5d7c |
133 | $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'}); |
a3e05a4b |
134 | $sth->execute; |
135 | }; |
fe67d343 |
136 | |
a3e05a4b |
137 | while (my $row = eval { $sth->fetchrow_hashref }) { |
65f74457 |
138 | my $fk = $row->{FK_NAME}; |
3559ae79 |
139 | push @{$local_cols->{$fk}}, $self->_lc($row->{FKCOLUMN_NAME}); |
140 | push @{$remote_cols->{$fk}}, $self->_lc($row->{PKCOLUMN_NAME}); |
bfb43060 |
141 | $remote_table->{$fk} = $row->{PKTABLE_NAME}; |
fe67d343 |
142 | } |
143 | |
144 | foreach my $fk (keys %$remote_table) { |
65f74457 |
145 | push @rels, { |
146 | local_columns => \@{$local_cols->{$fk}}, |
147 | remote_columns => \@{$remote_cols->{$fk}}, |
148 | remote_table => $remote_table->{$fk}, |
149 | }; |
fe67d343 |
150 | |
151 | } |
152 | return \@rels; |
153 | } |
154 | |
155 | sub _table_uniq_info { |
156 | my ($self, $table) = @_; |
157 | |
158 | my $dbh = $self->schema->storage->dbh; |
020f3c3a |
159 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; |
160 | |
161 | my $sth = $dbh->prepare(qq{ |
162 | SELECT ccu.constraint_name, ccu.column_name |
060f5ecd |
163 | FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu |
164 | JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc on (ccu.constraint_name = tc.constraint_name) |
165 | JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name) |
bfb43060 |
166 | wHERE lower(ccu.table_name) = @{[ $dbh->quote(lc $table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position |
020f3c3a |
167 | }); |
fe67d343 |
168 | $sth->execute; |
169 | my $constraints; |
170 | while (my $row = $sth->fetchrow_hashref) { |
020f3c3a |
171 | my $name = $row->{constraint_name}; |
3559ae79 |
172 | my $col = $self->_lc($row->{column_name}); |
fe67d343 |
173 | push @{$constraints->{$name}}, $col; |
174 | } |
175 | |
176 | my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints; |
177 | return \@uniqs; |
178 | } |
179 | |
9c9197d6 |
180 | sub _columns_info_for { |
181 | my $self = shift; |
182 | my ($table) = @_; |
183 | |
184 | my $result = $self->next::method(@_); |
185 | |
186 | while (my ($col, $info) = each %$result) { |
187 | my $dbh = $self->schema->storage->dbh; |
020f3c3a |
188 | |
81ade4d9 |
189 | # find identities |
9c9197d6 |
190 | my $sth = $dbh->prepare(qq{ |
020f3c3a |
191 | SELECT column_name |
060f5ecd |
192 | FROM INFORMATION_SCHEMA.COLUMNS |
bfb43060 |
193 | WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1 |
194 | AND lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]} |
9c9197d6 |
195 | }); |
020f3c3a |
196 | if (eval { $sth->execute; $sth->fetchrow_array }) { |
9c9197d6 |
197 | $info->{is_auto_increment} = 1; |
198 | $info->{data_type} =~ s/\s*identity//i; |
199 | delete $info->{size}; |
200 | } |
fe67d343 |
201 | |
81ade4d9 |
202 | # fix types |
203 | if ($info->{data_type} eq 'int') { |
204 | $info->{data_type} = 'integer'; |
205 | } |
206 | elsif ($info->{data_type} eq 'timestamp') { |
207 | $info->{inflate_datetime} = 0; |
208 | } |
209 | elsif ($info->{data_type} =~ /^(?:numeric|decimal)\z/) { |
210 | if (ref($info->{size}) && $info->{size}[0] == 18 && $info->{size}[1] == 0) { |
211 | delete $info->{size}; |
212 | } |
213 | } |
214 | elsif ($info->{data_type} eq 'real') { |
215 | $info->{data_type} = 'float'; |
216 | $info->{size} = 24; |
217 | } |
218 | elsif ($info->{data_type} eq 'float') { |
219 | $info->{data_type} = 'double precision'; |
220 | } |
221 | elsif ($info->{data_type} =~ /^(?:small)?datetime\z/) { |
222 | # fixup for DBD::Sybase |
223 | if ($info->{default_value} && $info->{default_value} eq '3') { |
224 | delete $info->{default_value}; |
225 | } |
226 | } |
227 | elsif ($info->{data_type} eq 'datetimeoffset') { |
228 | $info->{size} = { |
229 | 26 => 0, |
230 | 28 => 1, |
231 | 29 => 2, |
232 | 30 => 3, |
233 | 31 => 4, |
234 | 32 => 5, |
235 | 33 => 6, |
236 | 34 => 7, |
237 | }->{$info->{size}}; |
238 | |
239 | delete $info->{size} if $info->{size} == 7; |
240 | } |
241 | elsif ($info->{data_type} eq 'datetime2') { |
242 | $info->{size} = { |
243 | 19 => 0, |
244 | 21 => 1, |
245 | 22 => 2, |
246 | 23 => 3, |
247 | 24 => 4, |
248 | 25 => 5, |
249 | 26 => 6, |
250 | 27 => 7, |
251 | }->{$info->{size}}; |
252 | |
253 | delete $info->{size} if $info->{size} == 7; |
254 | } |
255 | elsif ($info->{data_type} eq 'time') { |
256 | $info->{size} = { |
257 | 8 => 0, |
258 | 10 => 1, |
259 | 11 => 2, |
260 | 12 => 3, |
261 | 13 => 4, |
262 | 14 => 5, |
263 | 15 => 6, |
264 | 16 => 7, |
265 | }->{$info->{size}}; |
266 | |
267 | delete $info->{size} if $info->{size} == 7; |
268 | } |
269 | |
270 | if ($info->{data_type} !~ /^(?:n?char|n?varchar|binary|varbinary|numeric|decimal|float|datetime(?:2|offset)|time)\z/) { |
271 | delete $info->{size}; |
272 | } |
273 | |
5c6fb0a1 |
274 | # get default |
9c9197d6 |
275 | $sth = $dbh->prepare(qq{ |
020f3c3a |
276 | SELECT column_default |
060f5ecd |
277 | FROM INFORMATION_SCHEMA.COLUMNS |
bfb43060 |
278 | wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]} |
9c9197d6 |
279 | }); |
020f3c3a |
280 | my ($default) = eval { $sth->execute; $sth->fetchrow_array }; |
9c9197d6 |
281 | |
282 | if (defined $default) { |
283 | # strip parens |
284 | $default =~ s/^\( (.*) \)\z/$1/x; |
285 | |
286 | # Literal strings are in ''s, numbers are in ()s (in some versions of |
287 | # MSSQL, in others they are unquoted) everything else is a function. |
288 | $info->{default_value} = |
289 | $default =~ /^['(] (.*) [)']\z/x ? $1 : |
290 | $default =~ /^\d/ ? $default : \$default; |
8a64178e |
291 | |
292 | if (eval { lc ${ $info->{default_value} } }||'' eq 'getdate()') { |
293 | ${ $info->{default_value} } = 'CURRENT_TIMESTAMP'; |
294 | } |
9c9197d6 |
295 | } |
5c6fb0a1 |
296 | } |
297 | |
9c9197d6 |
298 | return $result; |
fe67d343 |
299 | } |
300 | |
fe67d343 |
301 | =head1 SEE ALSO |
302 | |
acfcc1fb |
303 | L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>, |
304 | L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>, |
fe67d343 |
305 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
306 | L<DBIx::Class::Schema::Loader::DBI> |
307 | |
308 | =head1 AUTHOR |
309 | |
9cc8e7e1 |
310 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
fe67d343 |
311 | |
be80bba7 |
312 | =head1 LICENSE |
0852b7b8 |
313 | |
be80bba7 |
314 | This library is free software; you can redistribute it and/or modify it under |
315 | the same terms as Perl itself. |
0852b7b8 |
316 | |
fe67d343 |
317 | =cut |
318 | |
319 | 1; |
bfb43060 |
320 | # vim:et sts=4 sw=4 tw=0: |