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
29 my ($self, $opts) = @_;
31 my $dbh = $self->schema->storage->dbh;
32 my $sth = $dbh->prepare(<<'EOF');
34 FROM information_schema.tables t
35 WHERE lower(t.table_schema) = ?
37 $sth->execute(lc $self->db_schema);
39 my @tables = map @$_, @{ $sth->fetchall_arrayref };
41 return $self->_filter_tables(\@tables, $opts);
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, $sth);
63 my $dbh = $self->schema->storage->dbh;
65 $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
69 while (my $row = eval { $sth->fetchrow_hashref }) {
70 my $fk = $row->{FK_NAME};
71 push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
72 push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
73 $remote_table->{$fk} = $row->{PKTABLE_NAME};
76 foreach my $fk (keys %$remote_table) {
78 local_columns => \@{$local_cols->{$fk}},
79 remote_columns => \@{$remote_cols->{$fk}},
80 remote_table => $remote_table->{$fk},
87 sub _table_uniq_info {
88 my ($self, $table) = @_;
90 my $dbh = $self->schema->storage->dbh;
91 local $dbh->{FetchHashKeyName} = 'NAME_lc';
93 my $sth = $dbh->prepare(qq{
94 SELECT ccu.constraint_name, ccu.column_name
95 FROM information_schema.constraint_column_usage ccu
96 JOIN information_schema.table_constraints tc on (ccu.constraint_name = tc.constraint_name)
97 JOIN information_schema.key_column_usage kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name)
98 wHERE lower(ccu.table_name) = @{[ $dbh->quote(lc $table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position
102 while (my $row = $sth->fetchrow_hashref) {
103 my $name = $row->{constraint_name};
104 my $col = lc $row->{column_name};
105 push @{$constraints->{$name}}, $col;
108 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
112 sub _columns_info_for {
116 my $result = $self->next::method(@_);
118 while (my ($col, $info) = each %$result) {
119 my $dbh = $self->schema->storage->dbh;
121 my $sth = $dbh->prepare(qq{
123 FROM information_schema.columns
124 WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1
125 AND lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
127 if (eval { $sth->execute; $sth->fetchrow_array }) {
128 $info->{is_auto_increment} = 1;
129 $info->{data_type} =~ s/\s*identity//i;
130 delete $info->{size};
134 $sth = $dbh->prepare(qq{
135 SELECT column_default
136 FROM information_schema.columns
137 wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
139 my ($default) = eval { $sth->execute; $sth->fetchrow_array };
141 if (defined $default) {
143 $default =~ s/^\( (.*) \)\z/$1/x;
145 # Literal strings are in ''s, numbers are in ()s (in some versions of
146 # MSSQL, in others they are unquoted) everything else is a function.
147 $info->{default_value} =
148 $default =~ /^['(] (.*) [)']\z/x ? $1 :
149 $default =~ /^\d/ ? $default : \$default;
158 L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>,
159 L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>,
160 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
161 L<DBIx::Class::Schema::Loader::DBI>
165 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
169 This library is free software; you can redistribute it and/or modify it under
170 the same terms as Perl itself.
175 # vim:et sts=4 sw=4 tw=0: