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 __PACKAGE__->mk_group_accessors('simple', qw/
10 case_sensitive_collation
13 our $VERSION = '0.06001';
17 DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation.
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
27 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base> for
30 =head1 CASE SENSITIVITY
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
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.
40 To manually control case-sensitive mode, put:
42 case_sensitive_collation => 1|0
44 in your Loader options.
48 sub _is_case_sensitive {
51 return $self->case_sensitive_collation ? 1 : 0;
59 return if defined $self->case_sensitive_collation;
61 my $dbh = $self->schema->storage->dbh;
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.
67 # XXX why does databasepropertyex() not work over DBD::ODBC ?
69 # more on collations here: http://msdn.microsoft.com/en-us/library/ms143515.aspx
70 my ($collation_name) =
71 eval { $dbh->selectrow_array('SELECT collation_name FROM sys.databases WHERE name = DB_NAME()') }
72 || eval { $dbh->selectrow_array("SELECT CAST(databasepropertyex(DB_NAME(), 'Collation') AS VARCHAR)") };
74 if (not $collation_name) {
77 WARNING: MSSQL Collation detection failed. Defaulting to case-insensitive mode.
78 Override the 'case_sensitive_collation' attribute in your Loader options if
81 $self->case_sensitive_collation(0);
85 my $case_sensitive = $collation_name =~ /_(?:CS|BIN2?)(?:_|\z)/;
87 $self->case_sensitive_collation($case_sensitive ? 1 : 0);
91 my ($self, $name) = @_;
93 return $self->case_sensitive_collation ? $name : lc($name);
97 my ($self, $opts) = @_;
99 my $dbh = $self->schema->storage->dbh;
100 my $sth = $dbh->prepare(<<'EOF');
102 FROM INFORMATION_SCHEMA.TABLES t
103 WHERE lower(t.table_schema) = ?
105 $sth->execute(lc $self->db_schema);
107 my @tables = map @$_, @{ $sth->fetchall_arrayref };
109 return $self->_filter_tables(\@tables, $opts);
113 my ($self, $table) = @_;
114 my $dbh = $self->schema->storage->dbh;
115 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
120 while (my $row = $sth->fetchrow_hashref) {
121 push @keydata, $self->_lc($row->{COLUMN_NAME});
128 my ($self, $table) = @_;
130 my ($local_cols, $remote_cols, $remote_table, @rels, $sth);
131 my $dbh = $self->schema->storage->dbh;
133 $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
137 while (my $row = eval { $sth->fetchrow_hashref }) {
138 my $fk = $row->{FK_NAME};
139 push @{$local_cols->{$fk}}, $self->_lc($row->{FKCOLUMN_NAME});
140 push @{$remote_cols->{$fk}}, $self->_lc($row->{PKCOLUMN_NAME});
141 $remote_table->{$fk} = $row->{PKTABLE_NAME};
144 foreach my $fk (keys %$remote_table) {
146 local_columns => \@{$local_cols->{$fk}},
147 remote_columns => \@{$remote_cols->{$fk}},
148 remote_table => $remote_table->{$fk},
155 sub _table_uniq_info {
156 my ($self, $table) = @_;
158 my $dbh = $self->schema->storage->dbh;
159 local $dbh->{FetchHashKeyName} = 'NAME_lc';
161 my $sth = $dbh->prepare(qq{
162 SELECT ccu.constraint_name, ccu.column_name
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)
166 wHERE lower(ccu.table_name) = @{[ $dbh->quote(lc $table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position
170 while (my $row = $sth->fetchrow_hashref) {
171 my $name = $row->{constraint_name};
172 my $col = $self->_lc($row->{column_name});
173 push @{$constraints->{$name}}, $col;
176 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
180 sub _columns_info_for {
184 my $result = $self->next::method(@_);
186 while (my ($col, $info) = each %$result) {
187 my $dbh = $self->schema->storage->dbh;
190 my $sth = $dbh->prepare(qq{
192 FROM INFORMATION_SCHEMA.COLUMNS
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) ]}
196 if (eval { $sth->execute; $sth->fetchrow_array }) {
197 $info->{is_auto_increment} = 1;
198 $info->{data_type} =~ s/\s*identity//i;
199 delete $info->{size};
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 'real') {
215 $info->{data_type} = 'float';
218 elsif ($info->{data_type} eq 'float') {
219 $info->{data_type} = 'double precision';
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};
227 elsif ($info->{data_type} eq 'datetimeoffset') {
239 delete $info->{size} if $info->{size} == 7;
241 elsif ($info->{data_type} eq 'datetime2') {
253 delete $info->{size} if $info->{size} == 7;
255 elsif ($info->{data_type} eq 'time') {
267 delete $info->{size} if $info->{size} == 7;
270 if ($info->{data_type} !~ /^(?:n?char|n?varchar|binary|varbinary|numeric|decimal|float|datetime(?:2|offset)|time)\z/) {
271 delete $info->{size};
275 $sth = $dbh->prepare(qq{
276 SELECT column_default
277 FROM INFORMATION_SCHEMA.COLUMNS
278 wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
280 my ($default) = eval { $sth->execute; $sth->fetchrow_array };
282 if (defined $default) {
284 $default =~ s/^\( (.*) \)\z/$1/x;
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;
292 if (eval { lc ${ $info->{default_value} } }||'' eq 'getdate()') {
293 ${ $info->{default_value} } = 'CURRENT_TIMESTAMP';
303 L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>,
304 L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>,
305 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
306 L<DBIx::Class::Schema::Loader::DBI>
310 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
314 This library is free software; you can redistribute it and/or modify it under
315 the same terms as Perl itself.
320 # vim:et sts=4 sw=4 tw=0: