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 my ($collation_name) = $dbh->selectrow_array(<<'EOS');
48 WHERE name = DB_NAME()
51 my ($sensitivity) = $collation_name =~ /(C\w)_[A-z]+\z/;
53 $self->case_sensitive_collation($sensitivity eq 'CS' ? 1 : 0);
57 my ($self, $name) = @_;
59 return $self->case_sensitive_collation ? $name : lc($name);
63 my ($self, $opts) = @_;
65 my $dbh = $self->schema->storage->dbh;
66 my $sth = $dbh->prepare(<<'EOF');
68 FROM information_schema.tables t
69 WHERE lower(t.table_schema) = ?
71 $sth->execute(lc $self->db_schema);
73 my @tables = map @$_, @{ $sth->fetchall_arrayref };
75 return $self->_filter_tables(\@tables, $opts);
79 my ($self, $table) = @_;
80 my $dbh = $self->schema->storage->dbh;
81 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
86 while (my $row = $sth->fetchrow_hashref) {
87 push @keydata, $self->_lc($row->{COLUMN_NAME});
94 my ($self, $table) = @_;
96 my ($local_cols, $remote_cols, $remote_table, @rels, $sth);
97 my $dbh = $self->schema->storage->dbh;
99 $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
103 while (my $row = eval { $sth->fetchrow_hashref }) {
104 my $fk = $row->{FK_NAME};
105 push @{$local_cols->{$fk}}, $self->_lc($row->{FKCOLUMN_NAME});
106 push @{$remote_cols->{$fk}}, $self->_lc($row->{PKCOLUMN_NAME});
107 $remote_table->{$fk} = $row->{PKTABLE_NAME};
110 foreach my $fk (keys %$remote_table) {
112 local_columns => \@{$local_cols->{$fk}},
113 remote_columns => \@{$remote_cols->{$fk}},
114 remote_table => $remote_table->{$fk},
121 sub _table_uniq_info {
122 my ($self, $table) = @_;
124 my $dbh = $self->schema->storage->dbh;
125 local $dbh->{FetchHashKeyName} = 'NAME_lc';
127 my $sth = $dbh->prepare(qq{
128 SELECT ccu.constraint_name, ccu.column_name
129 FROM information_schema.constraint_column_usage ccu
130 JOIN information_schema.table_constraints tc on (ccu.constraint_name = tc.constraint_name)
131 JOIN information_schema.key_column_usage kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name)
132 wHERE lower(ccu.table_name) = @{[ $dbh->quote(lc $table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position
136 while (my $row = $sth->fetchrow_hashref) {
137 my $name = $row->{constraint_name};
138 my $col = $self->_lc($row->{column_name});
139 push @{$constraints->{$name}}, $col;
142 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
146 sub _columns_info_for {
150 my $result = $self->next::method(@_);
152 while (my ($col, $info) = each %$result) {
153 my $dbh = $self->schema->storage->dbh;
155 my $sth = $dbh->prepare(qq{
157 FROM information_schema.columns
158 WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1
159 AND lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
161 if (eval { $sth->execute; $sth->fetchrow_array }) {
162 $info->{is_auto_increment} = 1;
163 $info->{data_type} =~ s/\s*identity//i;
164 delete $info->{size};
168 $sth = $dbh->prepare(qq{
169 SELECT column_default
170 FROM information_schema.columns
171 wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
173 my ($default) = eval { $sth->execute; $sth->fetchrow_array };
175 if (defined $default) {
177 $default =~ s/^\( (.*) \)\z/$1/x;
179 # Literal strings are in ''s, numbers are in ()s (in some versions of
180 # MSSQL, in others they are unquoted) everything else is a function.
181 $info->{default_value} =
182 $default =~ /^['(] (.*) [)']\z/x ? $1 :
183 $default =~ /^\d/ ? $default : \$default;
192 L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>,
193 L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>,
194 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
195 L<DBIx::Class::Schema::Loader::DBI>
199 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
203 This library is free software; you can redistribute it and/or modify it under
204 the same terms as Perl itself.
209 # vim:et sts=4 sw=4 tw=0: