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.05002';
13 DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation.
18 use base qw/DBIx::Class::Schema::Loader/;
20 __PACKAGE__->loader_options( debug => 1 );
26 See L<DBIx::Class::Schema::Loader::Base>.
33 $self->next::method(@_);
34 $self->{db_schema} ||= $self->_build_db_schema;
35 $self->_set_quote_char_and_name_sep;
39 my ($self, $table) = @_;
40 my $dbh = $self->schema->storage->dbh;
41 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
46 while (my $row = $sth->fetchrow_hashref) {
47 push @keydata, lc $row->{COLUMN_NAME};
54 my ($self, $table) = @_;
56 my ($local_cols, $remote_cols, $remote_table, @rels);
57 my $dbh = $self->schema->storage->dbh;
58 my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
61 while (my $row = $sth->fetchrow_hashref) {
62 my $fk = $row->{FK_NAME};
63 push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
64 push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
65 $remote_table->{$fk} = $row->{PKTABLE_NAME};
68 foreach my $fk (keys %$remote_table) {
70 local_columns => \@{$local_cols->{$fk}},
71 remote_columns => \@{$remote_cols->{$fk}},
72 remote_table => $remote_table->{$fk},
79 sub _table_uniq_info {
80 my ($self, $table) = @_;
82 my $dbh = $self->schema->storage->dbh;
83 my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU
84 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME)
85 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME)
86 WHERE CCU.TABLE_NAME = @{[ $dbh->quote($table) ]} AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION});
89 while (my $row = $sth->fetchrow_hashref) {
90 my $name = lc $row->{CONSTRAINT_NAME};
91 my $col = lc $row->{COLUMN_NAME};
92 push @{$constraints->{$name}}, $col;
95 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
99 sub _extra_column_info {
100 my ($self, $info) = @_;
103 my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
105 my $dbh = $self->schema->storage->dbh;
106 my $sth = $dbh->prepare(qq{
108 FROM INFORMATION_SCHEMA.COLUMNS
109 WHERE COLUMNPROPERTY(object_id(@{[ $dbh->quote($table) ]}, 'U'), '$column', 'IsIdentity') = 1
110 AND TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($column) ]}
114 if ($sth->fetchrow_array) {
115 $extra_info{is_auto_increment} = 1;
119 $sth = $dbh->prepare(qq{
120 SELECT COLUMN_DEFAULT
121 FROM INFORMATION_SCHEMA.COLUMNS
122 WHERE TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($column) ]}
125 my ($default) = $sth->fetchrow_array;
127 if (defined $default) {
129 $default =~ s/^\( (.*) \)\z/$1/x;
131 # Literal strings are in ''s, numbers are in ()s (in some versions of
132 # MSSQL, in others they are unquoted) everything else is a function.
133 $extra_info{default_value} =
134 $default =~ /^['(] (.*) [)']\z/x ? $1 :
135 $default =~ /^\d/ ? $default : \$default;
143 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
144 L<DBIx::Class::Schema::Loader::DBI>
148 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
152 This library is free software; you can redistribute it and/or modify it under
153 the same terms as Perl itself.