1 package DBIx::Class::Schema::Loader::DBI::MSSQL;
5 use base 'DBIx::Class::Schema::Loader::DBI::Sybase::Common';
6 use Carp::Clan qw/^DBIx::Class/;
9 our $VERSION = '0.07006';
13 DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation.
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
23 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base> for
26 =head1 CASE SENSITIVITY
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
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.
36 To manually control case-sensitive mode, put:
40 in your Loader options.
42 See L<preserve_case|DBIx::Class::Schema::Loader::Base/preserve_case>.
44 B<NOTE:> this option used to be called C<case_sensitive_collation>, but has
45 been renamed to a more generic option.
52 $self->next::method(@_);
54 return if defined $self->preserve_case;
56 my $dbh = $self->schema->storage->dbh;
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.
62 # XXX why does databasepropertyex() not work over DBD::ODBC ?
64 # more on collations here: http://msdn.microsoft.com/en-us/library/ms143515.aspx
65 my ($collation_name) =
66 eval { $dbh->selectrow_array('SELECT collation_name FROM sys.databases WHERE name = DB_NAME()') }
67 || eval { $dbh->selectrow_array("SELECT CAST(databasepropertyex(DB_NAME(), 'Collation') AS VARCHAR)") };
69 if (not $collation_name) {
72 WARNING: MSSQL Collation detection failed. Defaulting to case-insensitive mode.
73 Override the 'preserve_case' attribute in your Loader options if needed.
75 See 'preserve_case' in
76 perldoc DBIx::Class::Schema::Loader::Base
78 $self->preserve_case(0);
82 my $case_sensitive = $collation_name =~ /_(?:CS|BIN2?)(?:_|\z)/;
84 $self->preserve_case($case_sensitive ? 1 : 0);
88 my ($self, $opts) = @_;
90 my $dbh = $self->schema->storage->dbh;
91 my $sth = $dbh->prepare(<<'EOF');
93 FROM INFORMATION_SCHEMA.TABLES t
94 WHERE t.table_schema = ?
96 $sth->execute($self->db_schema);
98 my @tables = map @$_, @{ $sth->fetchall_arrayref };
100 return $self->_filter_tables(\@tables, $opts);
104 my ($self, $table) = @_;
105 my $dbh = $self->schema->storage->dbh;
106 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
111 while (my $row = $sth->fetchrow_hashref) {
112 push @keydata, $self->_lc($row->{COLUMN_NAME});
119 my ($self, $table) = @_;
121 my ($local_cols, $remote_cols, $remote_table, @rels, $sth);
122 my $dbh = $self->schema->storage->dbh;
124 $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
128 while (my $row = eval { $sth->fetchrow_hashref }) {
129 my $fk = $row->{FK_NAME};
130 push @{$local_cols->{$fk}}, $self->_lc($row->{FKCOLUMN_NAME});
131 push @{$remote_cols->{$fk}}, $self->_lc($row->{PKCOLUMN_NAME});
132 $remote_table->{$fk} = $row->{PKTABLE_NAME};
135 foreach my $fk (keys %$remote_table) {
137 local_columns => \@{$local_cols->{$fk}},
138 remote_columns => \@{$remote_cols->{$fk}},
139 remote_table => $remote_table->{$fk},
146 sub _table_uniq_info {
147 my ($self, $table) = @_;
149 my $dbh = $self->schema->storage->dbh;
150 local $dbh->{FetchHashKeyName} = 'NAME_lc';
152 my $sth = $dbh->prepare(qq{
153 SELECT ccu.constraint_name, ccu.column_name
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)
157 wHERE ccu.table_name = @{[ $dbh->quote($table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position
161 while (my $row = $sth->fetchrow_hashref) {
162 my $name = $row->{constraint_name};
163 my $col = $self->_lc($row->{column_name});
164 push @{$constraints->{$name}}, $col;
167 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
171 sub _columns_info_for {
175 my $result = $self->next::method(@_);
177 while (my ($col, $info) = each %$result) {
178 my $dbh = $self->schema->storage->dbh;
181 my $sth = $dbh->prepare(qq{
183 FROM INFORMATION_SCHEMA.COLUMNS
184 WHERE columnproperty(object_id(@{[ $dbh->quote($table) ]}, 'U'), @{[ $dbh->quote($col) ]}, 'IsIdentity') = 1
185 AND table_name = @{[ $dbh->quote($table) ]} AND column_name = @{[ $dbh->quote($col) ]}
187 if (eval { $sth->execute; $sth->fetchrow_array }) {
188 $info->{is_auto_increment} = 1;
189 $info->{data_type} =~ s/\s*identity//i;
190 delete $info->{size};
193 # get datetime precision
194 $sth = $dbh->prepare(qq{
195 SELECT datetime_precision
196 FROM INFORMATION_SCHEMA.COLUMNS
197 WHERE table_name = @{[ $dbh->quote($table) ]} AND column_name = @{[ $dbh->quote($col) ]}
200 my ($datetime_precision) = $sth->fetchrow_array;
203 if ($info->{data_type} eq 'int') {
204 $info->{data_type} = 'integer';
206 elsif ($info->{data_type} eq 'timestamp') {
207 $info->{inflate_datetime} = 0;
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};
214 elsif ($info->{data_type} eq 'float') {
215 $info->{data_type} = 'double precision';
217 elsif ($info->{data_type} =~ /^(?:small)?datetime\z/) {
218 # fixup for DBD::Sybase
219 if ($info->{default_value} && $info->{default_value} eq '3') {
220 delete $info->{default_value};
223 elsif ($info->{data_type} =~ /^(?:datetime(?:2|offset)|time)\z/) {
224 $info->{size} = $datetime_precision;
226 delete $info->{size} if $info->{size} == 7;
228 elsif ($info->{data_type} eq 'varchar' && $info->{size} == 0) {
229 $info->{data_type} = 'text';
230 delete $info->{size};
232 elsif ($info->{data_type} eq 'nvarchar' && $info->{size} == 0) {
233 $info->{data_type} = 'ntext';
234 delete $info->{size};
236 elsif ($info->{data_type} eq 'varbinary' && $info->{size} == 0) {
237 $info->{data_type} = 'image';
238 delete $info->{size};
241 if ($info->{data_type} !~ /^(?:n?char|n?varchar|binary|varbinary|numeric|decimal|float|datetime(?:2|offset)|time)\z/) {
242 delete $info->{size};
246 $sth = $dbh->prepare(qq{
247 SELECT column_default
248 FROM INFORMATION_SCHEMA.COLUMNS
249 wHERE table_name = @{[ $dbh->quote($table) ]} AND column_name = @{[ $dbh->quote($col) ]}
251 my ($default) = eval { $sth->execute; $sth->fetchrow_array };
253 if (defined $default) {
255 $default =~ s/^\( (.*) \)\z/$1/x;
257 # Literal strings are in ''s, numbers are in ()s (in some versions of
258 # MSSQL, in others they are unquoted) everything else is a function.
259 $info->{default_value} =
260 $default =~ /^['(] (.*) [)']\z/x ? $1 :
261 $default =~ /^\d/ ? $default : \$default;
263 if ((eval { lc ${ $info->{default_value} } }||'') eq 'getdate()') {
264 ${ $info->{default_value} } = 'current_timestamp';
266 my $getdate = 'getdate()';
267 $info->{original}{default_value} = \$getdate;
277 L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>,
278 L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>,
279 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
280 L<DBIx::Class::Schema::Loader::DBI>
284 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
288 This library is free software; you can redistribute it and/or modify it under
289 the same terms as Perl itself.
294 # vim:et sts=4 sw=4 tw=0: