dev release
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / MSSQL.pm
1 package DBIx::Class::Schema::Loader::DBI::MSSQL;
2
3 use strict;
4 use warnings;
5 use base qw/
6     DBIx::Class::Schema::Loader::DBI
7     DBIx::Class::Schema::Loader::DBI::Sybase::Common
8 /;
9 use Carp::Clan qw/^DBIx::Class/;
10 use Class::C3;
11
12 our $VERSION = '0.04999_08';
13
14 =head1 NAME
15
16 DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation.
17
18 =head1 SYNOPSIS
19
20   package My::Schema;
21   use base qw/DBIx::Class::Schema::Loader/;
22
23   __PACKAGE__->loader_options( debug => 1 );
24
25   1;
26
27 =head1 DESCRIPTION
28
29 See L<DBIx::Class::Schema::Loader::Base>.
30
31 =cut
32
33 sub _setup {
34     my $self = shift;
35
36     $self->next::method(@_);
37     $self->{db_schema} ||= $self->_build_db_schema;
38     $self->_set_quote_char_and_name_sep;
39 }
40
41 sub _table_pk_info {
42     my ($self, $table) = @_;
43     my $dbh = $self->schema->storage->dbh;
44     my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
45     $sth->execute;
46
47     my @keydata;
48
49     while (my $row = $sth->fetchrow_hashref) {
50         push @keydata, lc $row->{COLUMN_NAME};
51     }
52
53     return \@keydata;
54 }
55
56 sub _table_fk_info {
57     my ($self, $table) = @_;
58
59     my ($local_cols, $remote_cols, $remote_table, @rels);
60     my $dbh = $self->schema->storage->dbh;
61     my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
62     $sth->execute;
63
64     while (my $row = $sth->fetchrow_hashref) {
65         my $fk = $row->{FK_NAME};
66         push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
67         push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
68         $remote_table->{$fk} = $row->{PKTABLE_NAME};
69     }
70
71     foreach my $fk (keys %$remote_table) {
72         push @rels, {
73                       local_columns => \@{$local_cols->{$fk}},
74                       remote_columns => \@{$remote_cols->{$fk}},
75                       remote_table => $remote_table->{$fk},
76                     };
77
78     }
79     return \@rels;
80 }
81
82 sub _table_uniq_info {
83     my ($self, $table) = @_;
84
85     my $dbh = $self->schema->storage->dbh;
86     my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU
87                                JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME)
88                                JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME)
89                                WHERE CCU.TABLE_NAME = '$table' AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION});
90     $sth->execute;
91     my $constraints;
92     while (my $row = $sth->fetchrow_hashref) {
93         my $name = lc $row->{CONSTRAINT_NAME};
94         my $col  = lc $row->{COLUMN_NAME};
95         push @{$constraints->{$name}}, $col;
96     }
97
98     my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
99     return \@uniqs;
100 }
101
102 sub _extra_column_info {
103     my ($self, $info) = @_;
104     my %extra_info;
105
106     my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
107
108     my $dbh = $self->schema->storage->dbh;
109     my $sth = $dbh->prepare(qq{SELECT COLUMN_NAME 
110                                FROM INFORMATION_SCHEMA.COLUMNS
111                                WHERE COLUMNPROPERTY(object_id('$table', 'U'), '$column', 'IsIdentity') = 1 AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column'
112                               });
113     $sth->execute();
114
115     if ($sth->fetchrow_array) {
116         $extra_info{is_auto_increment} = 1;
117     }
118
119     return \%extra_info;
120 }
121
122
123 =head1 SEE ALSO
124
125 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
126 L<DBIx::Class::Schema::Loader::DBI>
127
128 =head1 AUTHOR
129
130 Justin Hunter C<justin.d.hunter@gmail.com>
131
132 =head1 CONTRIBUTORS
133
134 Rafael Kitover <rkitover@cpan.org>
135
136 =cut
137
138 1;