add a test for mssql through dbd::sybase (which fails) and make mssql_dot_in_table_na...
[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
6ed0a90c 30sub _rebless {
31 my $self = shift;
32
33 $self->schema->storage->sql_maker->quote_char([qw/[ ]/])
34 unless $self->schema->storage->sql_maker->quote_char;
35
36 $self->schema->storage->sql_maker->name_sep('.')
37 unless $self->schema->storage->sql_maker->name_sep;
38}
39
fe67d343 40sub _setup {
41 my $self = shift;
42
43 $self->next::method(@_);
44 $self->{db_schema} ||= 'dbo';
45}
46
77d3753e 47# DBD::Sybase doesn't implement get_info properly
48#sub _build_quoter { [qw/[ ]/] }
49sub _build_quoter { '"' }
50sub _build_namesep { '.' }
51
fe67d343 52sub _table_pk_info {
53 my ($self, $table) = @_;
54 my $dbh = $self->schema->storage->dbh;
55 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
56 $sth->execute;
57
58 my @keydata;
59
60 while (my $row = $sth->fetchrow_hashref) {
65f74457 61 push @keydata, lc $row->{COLUMN_NAME};
fe67d343 62 }
63
64 return \@keydata;
65}
66
67sub _table_fk_info {
68 my ($self, $table) = @_;
69
70 my ($local_cols, $remote_cols, $remote_table, @rels);
71 my $dbh = $self->schema->storage->dbh;
72 my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
73 $sth->execute;
74
75 while (my $row = $sth->fetchrow_hashref) {
65f74457 76 my $fk = $row->{FK_NAME};
77 push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
78 push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
79 $remote_table->{$fk} = $row->{PKTABLE_NAME};
fe67d343 80 }
81
82 foreach my $fk (keys %$remote_table) {
65f74457 83 push @rels, {
84 local_columns => \@{$local_cols->{$fk}},
85 remote_columns => \@{$remote_cols->{$fk}},
86 remote_table => $remote_table->{$fk},
87 };
fe67d343 88
89 }
90 return \@rels;
91}
92
93sub _table_uniq_info {
94 my ($self, $table) = @_;
95
96 my $dbh = $self->schema->storage->dbh;
97 my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU
98 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME)
99 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME)
100 WHERE CCU.TABLE_NAME = '$table' AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION});
101 $sth->execute;
102 my $constraints;
103 while (my $row = $sth->fetchrow_hashref) {
104 my $name = lc $row->{CONSTRAINT_NAME};
105 my $col = lc $row->{COLUMN_NAME};
106 push @{$constraints->{$name}}, $col;
107 }
108
109 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
110 return \@uniqs;
111}
112
113sub _extra_column_info {
114 my ($self, $info) = @_;
115 my %extra_info;
116
117 my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
118
119 my $dbh = $self->schema->storage->dbh;
120 my $sth = $dbh->prepare(qq{SELECT COLUMN_NAME
121 FROM INFORMATION_SCHEMA.COLUMNS
122 WHERE COLUMNPROPERTY(object_id('$table', 'U'), '$column', 'IsIdentity') = 1 AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column'
123 });
124 $sth->execute();
125
126 if ($sth->fetchrow_array) {
127 $extra_info{is_auto_increment} = 1;
128 }
129
130 return \%extra_info;
131}
132
133
134=head1 SEE ALSO
135
136L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
137L<DBIx::Class::Schema::Loader::DBI>
138
139=head1 AUTHOR
140
141Justin Hunter C<justin.d.hunter@gmail.com>
142
143=cut
144
1451;