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 | |
2a8e93e9 |
13 | our $VERSION = '0.06000'; |
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 | |
103e90da |
40 | To manually set or unset case-sensitive mode, put: |
b065e3df |
41 | |
42 | case_sensitive_collation => 1 |
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 | |
9c9197d6 |
189 | my $sth = $dbh->prepare(qq{ |
020f3c3a |
190 | SELECT column_name |
060f5ecd |
191 | FROM INFORMATION_SCHEMA.COLUMNS |
bfb43060 |
192 | WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1 |
193 | AND lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]} |
9c9197d6 |
194 | }); |
020f3c3a |
195 | if (eval { $sth->execute; $sth->fetchrow_array }) { |
9c9197d6 |
196 | $info->{is_auto_increment} = 1; |
197 | $info->{data_type} =~ s/\s*identity//i; |
198 | delete $info->{size}; |
199 | } |
fe67d343 |
200 | |
5c6fb0a1 |
201 | # get default |
9c9197d6 |
202 | $sth = $dbh->prepare(qq{ |
020f3c3a |
203 | SELECT column_default |
060f5ecd |
204 | FROM INFORMATION_SCHEMA.COLUMNS |
bfb43060 |
205 | wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]} |
9c9197d6 |
206 | }); |
020f3c3a |
207 | my ($default) = eval { $sth->execute; $sth->fetchrow_array }; |
9c9197d6 |
208 | |
209 | if (defined $default) { |
210 | # strip parens |
211 | $default =~ s/^\( (.*) \)\z/$1/x; |
212 | |
213 | # Literal strings are in ''s, numbers are in ()s (in some versions of |
214 | # MSSQL, in others they are unquoted) everything else is a function. |
215 | $info->{default_value} = |
216 | $default =~ /^['(] (.*) [)']\z/x ? $1 : |
217 | $default =~ /^\d/ ? $default : \$default; |
218 | } |
5c6fb0a1 |
219 | } |
220 | |
9c9197d6 |
221 | return $result; |
fe67d343 |
222 | } |
223 | |
fe67d343 |
224 | =head1 SEE ALSO |
225 | |
acfcc1fb |
226 | L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>, |
227 | L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>, |
fe67d343 |
228 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
229 | L<DBIx::Class::Schema::Loader::DBI> |
230 | |
231 | =head1 AUTHOR |
232 | |
9cc8e7e1 |
233 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
fe67d343 |
234 | |
be80bba7 |
235 | =head1 LICENSE |
0852b7b8 |
236 | |
be80bba7 |
237 | This library is free software; you can redistribute it and/or modify it under |
238 | the same terms as Perl itself. |
0852b7b8 |
239 | |
fe67d343 |
240 | =cut |
241 | |
242 | 1; |
bfb43060 |
243 | # vim:et sts=4 sw=4 tw=0: |