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