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