1 package DBIx::Class::Schema::Loader::DBI::MSSQL;
6 DBIx::Class::Schema::Loader::DBI
7 DBIx::Class::Schema::Loader::DBI::Sybase::Common
9 use Carp::Clan qw/^DBIx::Class/;
12 our $VERSION = '0.04999_10';
16 DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation.
21 use base qw/DBIx::Class::Schema::Loader/;
23 __PACKAGE__->loader_options( debug => 1 );
29 See L<DBIx::Class::Schema::Loader::Base>.
36 $self->next::method(@_);
37 $self->{db_schema} ||= $self->_build_db_schema;
38 $self->_set_quote_char_and_name_sep;
41 # remove 'IDENTITY' from column data_type
42 sub _columns_info_for {
44 my $result = $self->next::method(@_);
46 for my $col (keys %$result) {
47 $result->{$col}->{data_type} =~ s/\s* identity \s*//ix;
54 my ($self, $table) = @_;
55 my $dbh = $self->schema->storage->dbh;
56 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
61 while (my $row = $sth->fetchrow_hashref) {
62 push @keydata, lc $row->{COLUMN_NAME};
69 my ($self, $table) = @_;
71 my ($local_cols, $remote_cols, $remote_table, @rels);
72 my $dbh = $self->schema->storage->dbh;
73 my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
76 while (my $row = $sth->fetchrow_hashref) {
77 my $fk = $row->{FK_NAME};
78 push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
79 push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
80 $remote_table->{$fk} = $row->{PKTABLE_NAME};
83 foreach my $fk (keys %$remote_table) {
85 local_columns => \@{$local_cols->{$fk}},
86 remote_columns => \@{$remote_cols->{$fk}},
87 remote_table => $remote_table->{$fk},
94 sub _table_uniq_info {
95 my ($self, $table) = @_;
97 my $dbh = $self->schema->storage->dbh;
98 my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU
99 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME)
100 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME)
101 WHERE CCU.TABLE_NAME = '$table' AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION});
104 while (my $row = $sth->fetchrow_hashref) {
105 my $name = lc $row->{CONSTRAINT_NAME};
106 my $col = lc $row->{COLUMN_NAME};
107 push @{$constraints->{$name}}, $col;
110 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
114 sub _extra_column_info {
115 my ($self, $info) = @_;
118 my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
120 my $dbh = $self->schema->storage->dbh;
121 my $sth = $dbh->prepare(qq{SELECT COLUMN_NAME
122 FROM INFORMATION_SCHEMA.COLUMNS
123 WHERE COLUMNPROPERTY(object_id('$table', 'U'), '$column', 'IsIdentity') = 1 AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column'
127 if ($sth->fetchrow_array) {
128 $extra_info{is_auto_increment} = 1;
137 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
138 L<DBIx::Class::Schema::Loader::DBI>
142 Justin Hunter C<justin.d.hunter@gmail.com>
146 Rafael Kitover <rkitover@cpan.org>