1 package DBIx::Class::Schema::Loader::DBI::MSSQL;
5 use base 'DBIx::Class::Schema::Loader::DBI::Sybase::Common';
7 use Carp::Clan qw/^DBIx::Class/;
11 our $VERSION = '0.07007';
15 DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation.
19 Base driver for Microsoft SQL Server, used by
20 L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server> for support
21 via L<DBD::Sybase> and
22 L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server> for support via
25 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base> for
28 =head1 CASE SENSITIVITY
30 Most MSSQL databases use C<CI> (case-insensitive) collation, for this reason
31 generated column names are lower-cased as this makes them easier to work with
34 We attempt to detect the database collation at startup, and set the column
35 lowercasing behavior accordingly, as lower-cased column names do not work on
36 case-sensitive databases.
38 To manually control case-sensitive mode, put:
42 in your Loader options.
44 See L<preserve_case|DBIx::Class::Schema::Loader::Base/preserve_case>.
46 B<NOTE:> this option used to be called C<case_sensitive_collation>, but has
47 been renamed to a more generic option.
54 $self->next::method(@_);
56 return if defined $self->preserve_case;
58 my $dbh = $self->schema->storage->dbh;
60 # We use the sys.databases query for the general case, and fallback to
61 # databasepropertyex() if for some reason sys.databases is not available,
62 # which does not work over DBD::ODBC with unixODBC+FreeTDS.
64 # XXX why does databasepropertyex() not work over DBD::ODBC ?
66 # more on collations here: http://msdn.microsoft.com/en-us/library/ms143515.aspx
67 my ($collation_name) =
68 eval { $dbh->selectrow_array('SELECT collation_name FROM sys.databases WHERE name = DB_NAME()') }
69 || eval { $dbh->selectrow_array("SELECT CAST(databasepropertyex(DB_NAME(), 'Collation') AS VARCHAR)") };
71 if (not $collation_name) {
74 WARNING: MSSQL Collation detection failed. Defaulting to case-insensitive mode.
75 Override the 'preserve_case' attribute in your Loader options if needed.
77 See 'preserve_case' in
78 perldoc DBIx::Class::Schema::Loader::Base
80 $self->preserve_case(0);
84 my $case_sensitive = $collation_name =~ /_(?:CS|BIN2?)(?:_|\z)/;
86 $self->preserve_case($case_sensitive ? 1 : 0);
90 my ($self, $opts) = @_;
92 my $dbh = $self->schema->storage->dbh;
93 my $sth = $dbh->prepare(<<'EOF');
95 FROM INFORMATION_SCHEMA.TABLES t
96 WHERE t.table_schema = ?
98 $sth->execute($self->db_schema);
100 my @tables = map @$_, @{ $sth->fetchall_arrayref };
102 return $self->_filter_tables(\@tables, $opts);
106 my ($self, $table) = @_;
107 my $dbh = $self->schema->storage->dbh;
108 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
113 while (my $row = $sth->fetchrow_hashref) {
114 push @keydata, $self->_lc($row->{COLUMN_NAME});
121 my ($self, $table) = @_;
123 my ($local_cols, $remote_cols, $remote_table, @rels, $sth);
124 my $dbh = $self->schema->storage->dbh;
126 $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
130 while (my $row = eval { $sth->fetchrow_hashref }) {
131 my $fk = $row->{FK_NAME};
132 push @{$local_cols->{$fk}}, $self->_lc($row->{FKCOLUMN_NAME});
133 push @{$remote_cols->{$fk}}, $self->_lc($row->{PKCOLUMN_NAME});
134 $remote_table->{$fk} = $row->{PKTABLE_NAME};
137 foreach my $fk (keys %$remote_table) {
139 local_columns => \@{$local_cols->{$fk}},
140 remote_columns => \@{$remote_cols->{$fk}},
141 remote_table => $remote_table->{$fk},
148 sub _table_uniq_info {
149 my ($self, $table) = @_;
151 my $dbh = $self->schema->storage->dbh;
152 local $dbh->{FetchHashKeyName} = 'NAME_lc';
154 my $sth = $dbh->prepare(qq{
155 SELECT ccu.constraint_name, ccu.column_name
156 FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
157 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc on (ccu.constraint_name = tc.constraint_name)
158 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name)
159 wHERE ccu.table_name = @{[ $dbh->quote($table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position
163 while (my $row = $sth->fetchrow_hashref) {
164 my $name = $row->{constraint_name};
165 my $col = $self->_lc($row->{column_name});
166 push @{$constraints->{$name}}, $col;
169 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
173 sub _columns_info_for {
177 my $result = $self->next::method(@_);
179 my $dbh = $self->schema->storage->dbh;
181 while (my ($col, $info) = each %$result) {
183 my $sth = $dbh->prepare(qq{
184 SELECT character_maximum_length, data_type, datetime_precision
185 FROM INFORMATION_SCHEMA.COLUMNS
186 WHERE table_name = @{[ $dbh->quote($table) ]} AND column_name = @{[ $dbh->quote($col) ]}
189 my ($char_max_length, $data_type, $datetime_precision) = $sth->fetchrow_array;
191 $info->{data_type} = $data_type;
193 if (defined $char_max_length) {
194 $info->{size} = $char_max_length;
195 $info->{size} = 0 if $char_max_length < 0;
199 $sth = $dbh->prepare(qq{
201 FROM INFORMATION_SCHEMA.COLUMNS
202 WHERE columnproperty(object_id(@{[ $dbh->quote($table) ]}, 'U'), @{[ $dbh->quote($col) ]}, 'IsIdentity') = 1
203 AND table_name = @{[ $dbh->quote($table) ]} AND column_name = @{[ $dbh->quote($col) ]}
205 if (try { $sth->execute; $sth->fetchrow_array }) {
206 $info->{is_auto_increment} = 1;
207 $info->{data_type} =~ s/\s*identity//i;
208 delete $info->{size};
212 if ($data_type eq 'int') {
213 $info->{data_type} = 'integer';
215 elsif ($data_type eq 'timestamp') {
216 $info->{inflate_datetime} = 0;
218 elsif ($data_type =~ /^(?:numeric|decimal)\z/) {
219 if (ref($info->{size}) && $info->{size}[0] == 18 && $info->{size}[1] == 0) {
220 delete $info->{size};
223 elsif ($data_type eq 'float') {
224 $info->{data_type} = 'double precision';
225 delete $info->{size};
227 elsif ($data_type =~ /^(?:small)?datetime\z/) {
228 # fixup for DBD::Sybase
229 if ($info->{default_value} && $info->{default_value} eq '3') {
230 delete $info->{default_value};
233 elsif ($data_type =~ /^(?:datetime(?:2|offset)|time)\z/) {
234 $info->{size} = $datetime_precision;
236 delete $info->{size} if $info->{size} == 7;
238 elsif ($data_type eq 'varchar' && $info->{size} == 0) {
239 $info->{data_type} = 'text';
240 delete $info->{size};
242 elsif ($data_type eq 'nvarchar' && $info->{size} == 0) {
243 $info->{data_type} = 'ntext';
244 delete $info->{size};
246 elsif ($data_type eq 'varbinary' && $info->{size} == 0) {
247 $info->{data_type} = 'image';
248 delete $info->{size};
251 if ($data_type !~ /^(?:n?char|n?varchar|binary|varbinary|numeric|decimal|float|datetime(?:2|offset)|time)\z/) {
252 delete $info->{size};
256 $sth = $dbh->prepare(qq{
257 SELECT column_default
258 FROM INFORMATION_SCHEMA.COLUMNS
259 wHERE table_name = @{[ $dbh->quote($table) ]} AND column_name = @{[ $dbh->quote($col) ]}
261 my ($default) = eval { $sth->execute; $sth->fetchrow_array };
263 if (defined $default) {
265 $default =~ s/^\( (.*) \)\z/$1/x;
267 # Literal strings are in ''s, numbers are in ()s (in some versions of
268 # MSSQL, in others they are unquoted) everything else is a function.
269 $info->{default_value} =
270 $default =~ /^['(] (.*) [)']\z/x ? $1 :
271 $default =~ /^\d/ ? $default : \$default;
273 if ((eval { lc ${ $info->{default_value} } }||'') eq 'getdate()') {
274 ${ $info->{default_value} } = 'current_timestamp';
276 my $getdate = 'getdate()';
277 $info->{original}{default_value} = \$getdate;
287 L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>,
288 L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>,
289 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
290 L<DBIx::Class::Schema::Loader::DBI>
294 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
298 This library is free software; you can redistribute it and/or modify it under
299 the same terms as Perl itself.
304 # vim:et sts=4 sw=4 tw=0: