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 ?
50 my ($collation_name) =
51 eval { $dbh->selectrow_array('SELECT collation_name FROM sys.databases WHERE name = DB_NAME()') }
52 || eval { $dbh->selectrow_array("SELECT databasepropertyex(DB_NAME(), 'collation')") };
54 if (not $collation_name) {
55 $self->case_sensitive_collation(0); # most likely not
59 my ($sensitivity) = $collation_name =~ /(C\w)_[A-z]+\z/;
61 $self->case_sensitive_collation($sensitivity eq 'CS' ? 1 : 0);
65 my ($self, $name) = @_;
67 return $self->case_sensitive_collation ? $name : lc($name);
71 my ($self, $opts) = @_;
73 my $dbh = $self->schema->storage->dbh;
74 my $sth = $dbh->prepare(<<'EOF');
76 FROM information_schema.tables t
77 WHERE lower(t.table_schema) = ?
79 $sth->execute(lc $self->db_schema);
81 my @tables = map @$_, @{ $sth->fetchall_arrayref };
83 return $self->_filter_tables(\@tables, $opts);
87 my ($self, $table) = @_;
88 my $dbh = $self->schema->storage->dbh;
89 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
94 while (my $row = $sth->fetchrow_hashref) {
95 push @keydata, $self->_lc($row->{COLUMN_NAME});
102 my ($self, $table) = @_;
104 my ($local_cols, $remote_cols, $remote_table, @rels, $sth);
105 my $dbh = $self->schema->storage->dbh;
107 $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
111 while (my $row = eval { $sth->fetchrow_hashref }) {
112 my $fk = $row->{FK_NAME};
113 push @{$local_cols->{$fk}}, $self->_lc($row->{FKCOLUMN_NAME});
114 push @{$remote_cols->{$fk}}, $self->_lc($row->{PKCOLUMN_NAME});
115 $remote_table->{$fk} = $row->{PKTABLE_NAME};
118 foreach my $fk (keys %$remote_table) {
120 local_columns => \@{$local_cols->{$fk}},
121 remote_columns => \@{$remote_cols->{$fk}},
122 remote_table => $remote_table->{$fk},
129 sub _table_uniq_info {
130 my ($self, $table) = @_;
132 my $dbh = $self->schema->storage->dbh;
133 local $dbh->{FetchHashKeyName} = 'NAME_lc';
135 my $sth = $dbh->prepare(qq{
136 SELECT ccu.constraint_name, ccu.column_name
137 FROM information_schema.constraint_column_usage ccu
138 JOIN information_schema.table_constraints tc on (ccu.constraint_name = tc.constraint_name)
139 JOIN information_schema.key_column_usage kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name)
140 wHERE lower(ccu.table_name) = @{[ $dbh->quote(lc $table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position
144 while (my $row = $sth->fetchrow_hashref) {
145 my $name = $row->{constraint_name};
146 my $col = $self->_lc($row->{column_name});
147 push @{$constraints->{$name}}, $col;
150 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
154 sub _columns_info_for {
158 my $result = $self->next::method(@_);
160 while (my ($col, $info) = each %$result) {
161 my $dbh = $self->schema->storage->dbh;
163 my $sth = $dbh->prepare(qq{
165 FROM information_schema.columns
166 WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1
167 AND lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
169 if (eval { $sth->execute; $sth->fetchrow_array }) {
170 $info->{is_auto_increment} = 1;
171 $info->{data_type} =~ s/\s*identity//i;
172 delete $info->{size};
176 $sth = $dbh->prepare(qq{
177 SELECT column_default
178 FROM information_schema.columns
179 wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
181 my ($default) = eval { $sth->execute; $sth->fetchrow_array };
183 if (defined $default) {
185 $default =~ s/^\( (.*) \)\z/$1/x;
187 # Literal strings are in ''s, numbers are in ()s (in some versions of
188 # MSSQL, in others they are unquoted) everything else is a function.
189 $info->{default_value} =
190 $default =~ /^['(] (.*) [)']\z/x ? $1 :
191 $default =~ /^\d/ ? $default : \$default;
200 L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>,
201 L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>,
202 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
203 L<DBIx::Class::Schema::Loader::DBI>
207 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
211 This library is free software; you can redistribute it and/or modify it under
212 the same terms as Perl itself.
217 # vim:et sts=4 sw=4 tw=0: