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.05003';
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
31 my $dbh = $self->schema->storage->dbh;
32 my $sth = $dbh->prepare(<<'EOF');
34 from information_schema.tables t
35 where t.table_schema = ?
37 $sth->execute($self->db_schema);
39 my @tables = map lc $_, map @$_, @{ $sth->fetchall_arrayref };
41 return $self->_filter_tables(@tables);
45 my ($self, $table) = @_;
46 my $dbh = $self->schema->storage->dbh;
47 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
52 while (my $row = $sth->fetchrow_hashref) {
53 push @keydata, lc $row->{COLUMN_NAME};
60 my ($self, $table) = @_;
62 my ($local_cols, $remote_cols, $remote_table, @rels);
63 my $dbh = $self->schema->storage->dbh;
64 my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
67 while (my $row = $sth->fetchrow_hashref) {
68 my $fk = $row->{FK_NAME};
69 push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
70 push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
71 $remote_table->{$fk} = $row->{PKTABLE_NAME};
74 foreach my $fk (keys %$remote_table) {
76 local_columns => \@{$local_cols->{$fk}},
77 remote_columns => \@{$remote_cols->{$fk}},
78 remote_table => $remote_table->{$fk},
85 sub _table_uniq_info {
86 my ($self, $table) = @_;
88 my $dbh = $self->schema->storage->dbh;
89 my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU
90 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME)
91 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME)
92 WHERE CCU.TABLE_NAME = @{[ $dbh->quote($table) ]} AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION});
95 while (my $row = $sth->fetchrow_hashref) {
96 my $name = lc $row->{CONSTRAINT_NAME};
97 my $col = lc $row->{COLUMN_NAME};
98 push @{$constraints->{$name}}, $col;
101 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
105 sub _columns_info_for {
109 my $result = $self->next::method(@_);
111 while (my ($col, $info) = each %$result) {
112 my $dbh = $self->schema->storage->dbh;
113 my $sth = $dbh->prepare(qq{
115 FROM INFORMATION_SCHEMA.COLUMNS
116 WHERE COLUMNPROPERTY(object_id(@{[ $dbh->quote($table) ]}, 'U'), @{[ $dbh->quote($col) ]}, 'IsIdentity') = 1
117 AND TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($col) ]}
121 if ($sth->fetchrow_array) {
122 $info->{is_auto_increment} = 1;
123 $info->{data_type} =~ s/\s*identity//i;
124 delete $info->{size};
128 $sth = $dbh->prepare(qq{
129 SELECT COLUMN_DEFAULT
130 FROM INFORMATION_SCHEMA.COLUMNS
131 WHERE TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($col) ]}
134 my ($default) = $sth->fetchrow_array;
136 if (defined $default) {
138 $default =~ s/^\( (.*) \)\z/$1/x;
140 # Literal strings are in ''s, numbers are in ()s (in some versions of
141 # MSSQL, in others they are unquoted) everything else is a function.
142 $info->{default_value} =
143 $default =~ /^['(] (.*) [)']\z/x ? $1 :
144 $default =~ /^\d/ ? $default : \$default;
153 L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>,
154 L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>,
155 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
156 L<DBIx::Class::Schema::Loader::DBI>
160 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
164 This library is free software; you can redistribute it and/or modify it under
165 the same terms as Perl itself.