override _columns_info_for instead of _extra_column_info in MSSQL
[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;
de82711a 5use base 'DBIx::Class::Schema::Loader::DBI::Sybase::Common';
fe67d343 6use Carp::Clan qw/^DBIx::Class/;
7use Class::C3;
8
e42ec4ef 9our $VERSION = '0.05003';
fe67d343 10
11=head1 NAME
12
13DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation.
14
acfcc1fb 15=head1 DESCRIPTION
fe67d343 16
acfcc1fb 17Base driver for Microsoft SQL Server, used by
18L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server> for support
19via L<DBD::Sybase> and
20L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server> for support via
21L<DBD::ODBC>.
fe67d343 22
acfcc1fb 23See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base> for
24usage information.
fe67d343 25
acfcc1fb 26=cut
fe67d343 27
acfcc1fb 28sub _tables_list {
29 my $self = shift;
fe67d343 30
acfcc1fb 31 my $dbh = $self->schema->storage->dbh;
32 my $sth = $dbh->prepare(<<'EOF');
33select t.table_name
34from information_schema.tables t
35where t.table_schema = ?
36EOF
37 $sth->execute($self->db_schema);
fe67d343 38
acfcc1fb 39 my @tables = map lc $_, map @$_, @{ $sth->fetchall_arrayref };
40
41 return $self->_filter_tables(@tables);
42}
fe67d343 43
fe67d343 44sub _table_pk_info {
45 my ($self, $table) = @_;
46 my $dbh = $self->schema->storage->dbh;
47 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
48 $sth->execute;
49
50 my @keydata;
51
52 while (my $row = $sth->fetchrow_hashref) {
65f74457 53 push @keydata, lc $row->{COLUMN_NAME};
fe67d343 54 }
55
56 return \@keydata;
57}
58
59sub _table_fk_info {
60 my ($self, $table) = @_;
61
62 my ($local_cols, $remote_cols, $remote_table, @rels);
63 my $dbh = $self->schema->storage->dbh;
64 my $sth = $dbh->prepare(qq{sp_fkeys \@FKTABLE_NAME = '$table'});
65 $sth->execute;
66
67 while (my $row = $sth->fetchrow_hashref) {
65f74457 68 my $fk = $row->{FK_NAME};
69 push @{$local_cols->{$fk}}, lc $row->{FKCOLUMN_NAME};
70 push @{$remote_cols->{$fk}}, lc $row->{PKCOLUMN_NAME};
71 $remote_table->{$fk} = $row->{PKTABLE_NAME};
fe67d343 72 }
73
74 foreach my $fk (keys %$remote_table) {
65f74457 75 push @rels, {
76 local_columns => \@{$local_cols->{$fk}},
77 remote_columns => \@{$remote_cols->{$fk}},
78 remote_table => $remote_table->{$fk},
79 };
fe67d343 80
81 }
82 return \@rels;
83}
84
85sub _table_uniq_info {
86 my ($self, $table) = @_;
87
88 my $dbh = $self->schema->storage->dbh;
89 my $sth = $dbh->prepare(qq{SELECT CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU
90 JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON (CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME)
91 JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ON (CCU.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND CCU.COLUMN_NAME = KCU.COLUMN_NAME)
772367d3 92 WHERE CCU.TABLE_NAME = @{[ $dbh->quote($table) ]} AND CONSTRAINT_TYPE = 'UNIQUE' ORDER BY KCU.ORDINAL_POSITION});
fe67d343 93 $sth->execute;
94 my $constraints;
95 while (my $row = $sth->fetchrow_hashref) {
96 my $name = lc $row->{CONSTRAINT_NAME};
97 my $col = lc $row->{COLUMN_NAME};
98 push @{$constraints->{$name}}, $col;
99 }
100
101 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
102 return \@uniqs;
103}
104
9c9197d6 105sub _columns_info_for {
106 my $self = shift;
107 my ($table) = @_;
108
109 my $result = $self->next::method(@_);
110
111 while (my ($col, $info) = each %$result) {
112 my $dbh = $self->schema->storage->dbh;
113 my $sth = $dbh->prepare(qq{
114 SELECT COLUMN_NAME
115 FROM INFORMATION_SCHEMA.COLUMNS
116 WHERE COLUMNPROPERTY(object_id(@{[ $dbh->quote($table) ]}, 'U'), @{[ $dbh->quote($col) ]}, 'IsIdentity') = 1
117 AND TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($col) ]}
118 });
119 $sth->execute();
120
121 if ($sth->fetchrow_array) {
122 $info->{is_auto_increment} = 1;
123 $info->{data_type} =~ s/\s*identity//i;
124 delete $info->{size};
125 }
fe67d343 126
5c6fb0a1 127# get default
9c9197d6 128 $sth = $dbh->prepare(qq{
129 SELECT COLUMN_DEFAULT
130 FROM INFORMATION_SCHEMA.COLUMNS
131 WHERE TABLE_NAME = @{[ $dbh->quote($table) ]} AND COLUMN_NAME = @{[ $dbh->quote($col) ]}
132 });
133 $sth->execute;
134 my ($default) = $sth->fetchrow_array;
135
136 if (defined $default) {
137 # strip parens
138 $default =~ s/^\( (.*) \)\z/$1/x;
139
140 # Literal strings are in ''s, numbers are in ()s (in some versions of
141 # MSSQL, in others they are unquoted) everything else is a function.
142 $info->{default_value} =
143 $default =~ /^['(] (.*) [)']\z/x ? $1 :
144 $default =~ /^\d/ ? $default : \$default;
145 }
5c6fb0a1 146 }
147
9c9197d6 148 return $result;
fe67d343 149}
150
fe67d343 151=head1 SEE ALSO
152
acfcc1fb 153L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>,
154L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>,
fe67d343 155L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
156L<DBIx::Class::Schema::Loader::DBI>
157
158=head1 AUTHOR
159
9cc8e7e1 160See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
fe67d343 161
be80bba7 162=head1 LICENSE
0852b7b8 163
be80bba7 164This library is free software; you can redistribute it and/or modify it under
165the same terms as Perl itself.
0852b7b8 166
fe67d343 167=cut
168
1691;