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