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.06000';
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
32 sub _is_case_sensitive {
35 return $self->case_sensitive_collation ? 1 : 0;
43 my $dbh = $self->schema->storage->dbh;
45 # We use the sys.databases query for the general case, and fallback to
46 # databasepropertyex() if for some reason sys.databases is not available,
47 # which does not work over DBD::ODBC with unixODBC+FreeTDS.
49 # XXX why does databasepropertyex() not work over DBD::ODBC ?
51 # more on collations here: http://msdn.microsoft.com/en-us/library/ms143515.aspx
52 my ($collation_name) =
53 eval { $dbh->selectrow_array('SELECT collation_name FROM sys.databases WHERE name = DB_NAME()') }
54 || eval { $dbh->selectrow_array("SELECT databasepropertyex(DB_NAME(), 'Collation')") };
56 if (not $collation_name) {
57 $self->case_sensitive_collation(0); # most likely not
61 my $case_sensitive = $collation_name =~ /_(?:CS|BIN2?)(?:_|\z)/;
63 $self->case_sensitive_collation($case_sensitive ? 1 : 0);
67 my ($self, $name) = @_;
69 return $self->case_sensitive_collation ? $name : lc($name);
73 my ($self, $opts) = @_;
75 my $dbh = $self->schema->storage->dbh;
76 my $sth = $dbh->prepare(<<'EOF');
78 FROM INFORMATION_SCHEMA.TABLES t
79 WHERE lower(t.table_schema) = ?
81 $sth->execute(lc $self->db_schema);
83 my @tables = map @$_, @{ $sth->fetchall_arrayref };
85 return $self->_filter_tables(\@tables, $opts);
89 my ($self, $table) = @_;
90 my $dbh = $self->schema->storage->dbh;
91 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
96 while (my $row = $sth->fetchrow_hashref) {
97 push @keydata, $self->_lc($row->{COLUMN_NAME});
104 my ($self, $table) = @_;
106 my ($local_cols, $remote_cols, $remote_table, @rels, $sth);
107 my $dbh = $self->schema->storage->dbh;
109 $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
113 while (my $row = eval { $sth->fetchrow_hashref }) {
114 my $fk = $row->{FK_NAME};
115 push @{$local_cols->{$fk}}, $self->_lc($row->{FKCOLUMN_NAME});
116 push @{$remote_cols->{$fk}}, $self->_lc($row->{PKCOLUMN_NAME});
117 $remote_table->{$fk} = $row->{PKTABLE_NAME};
120 foreach my $fk (keys %$remote_table) {
122 local_columns => \@{$local_cols->{$fk}},
123 remote_columns => \@{$remote_cols->{$fk}},
124 remote_table => $remote_table->{$fk},
131 sub _table_uniq_info {
132 my ($self, $table) = @_;
134 my $dbh = $self->schema->storage->dbh;
135 local $dbh->{FetchHashKeyName} = 'NAME_lc';
137 my $sth = $dbh->prepare(qq{
138 SELECT ccu.constraint_name, ccu.column_name
139 FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
140 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc on (ccu.constraint_name = tc.constraint_name)
141 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name)
142 wHERE lower(ccu.table_name) = @{[ $dbh->quote(lc $table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position
146 while (my $row = $sth->fetchrow_hashref) {
147 my $name = $row->{constraint_name};
148 my $col = $self->_lc($row->{column_name});
149 push @{$constraints->{$name}}, $col;
152 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
156 sub _columns_info_for {
160 my $result = $self->next::method(@_);
162 while (my ($col, $info) = each %$result) {
163 my $dbh = $self->schema->storage->dbh;
165 my $sth = $dbh->prepare(qq{
167 FROM INFORMATION_SCHEMA.COLUMNS
168 WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1
169 AND lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
171 if (eval { $sth->execute; $sth->fetchrow_array }) {
172 $info->{is_auto_increment} = 1;
173 $info->{data_type} =~ s/\s*identity//i;
174 delete $info->{size};
178 $sth = $dbh->prepare(qq{
179 SELECT column_default
180 FROM INFORMATION_SCHEMA.COLUMNS
181 wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
183 my ($default) = eval { $sth->execute; $sth->fetchrow_array };
185 if (defined $default) {
187 $default =~ s/^\( (.*) \)\z/$1/x;
189 # Literal strings are in ''s, numbers are in ()s (in some versions of
190 # MSSQL, in others they are unquoted) everything else is a function.
191 $info->{default_value} =
192 $default =~ /^['(] (.*) [)']\z/x ? $1 :
193 $default =~ /^\d/ ? $default : \$default;
202 L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>,
203 L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>,
204 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
205 L<DBIx::Class::Schema::Loader::DBI>
209 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
213 This library is free software; you can redistribute it and/or modify it under
214 the same terms as Perl itself.
219 # vim:et sts=4 sw=4 tw=0: