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.
18 use base qw/DBIx::Class::Schema::Loader/;
20 __PACKAGE__->loader_options( debug => 1 );
26 See L<DBIx::Class::Schema::Loader::Base>.
31 my ($self, $table) = @_;
32 my $dbh = $self->schema->storage->dbh;
33 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
38 while (my $row = $sth->fetchrow_hashref) {
39 push @keydata, lc $row->{COLUMN_NAME};
46 my ($self, $table) = @_;
48 my ($local_cols, $remote_cols, $remote_table, @rels);
49 my $dbh = $self->schema->storage->dbh;
50 my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
53 while (my $row = $sth->fetchrow_hashref) {
54 my $fk = $row->{FK_NAME};
55 push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
56 push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
57 $remote_table->{$fk} = $row->{PKTABLE_NAME};
60 foreach my $fk (keys %$remote_table) {
62 local_columns => \@{$local_cols->{$fk}},
63 remote_columns => \@{$remote_cols->{$fk}},
64 remote_table => $remote_table->{$fk},
71 sub _table_uniq_info {
72 my ($self, $table) = @_;
74 my $dbh = $self->schema->storage->dbh;
75 my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU
76 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME)
77 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME)
78 WHERE CCU.TABLE_NAME = @{[ $dbh->quote($table) ]} AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION});
81 while (my $row = $sth->fetchrow_hashref) {
82 my $name = lc $row->{CONSTRAINT_NAME};
83 my $col = lc $row->{COLUMN_NAME};
84 push @{$constraints->{$name}}, $col;
87 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
91 sub _extra_column_info {
92 my ($self, $table, $column, $info, $dbi_info) = @_;
95 my $dbh = $self->schema->storage->dbh;
96 my $sth = $dbh->prepare(qq{
98 FROM INFORMATION_SCHEMA.COLUMNS
99 WHERE COLUMNPROPERTY(object_id(@{[ $dbh->quote($table) ]}, 'U'), '$column', 'IsIdentity') = 1
100 AND TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($column) ]}
104 if ($sth->fetchrow_array) {
105 $extra_info{is_auto_increment} = 1;
109 $sth = $dbh->prepare(qq{
110 SELECT COLUMN_DEFAULT
111 FROM INFORMATION_SCHEMA.COLUMNS
112 WHERE TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($column) ]}
115 my ($default) = $sth->fetchrow_array;
117 if (defined $default) {
119 $default =~ s/^\( (.*) \)\z/$1/x;
121 # Literal strings are in ''s, numbers are in ()s (in some versions of
122 # MSSQL, in others they are unquoted) everything else is a function.
123 $extra_info{default_value} =
124 $default =~ /^['(] (.*) [)']\z/x ? $1 :
125 $default =~ /^\d/ ? $default : \$default;
133 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
134 L<DBIx::Class::Schema::Loader::DBI>
138 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
142 This library is free software; you can redistribute it and/or modify it under
143 the same terms as Perl itself.