don't set result_namespace if it's 'Result'
[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
3559ae79 9__PACKAGE__->mk_group_accessors('simple', qw/
10 case_sensitive_collation
11/);
12
2a8e93e9 13our $VERSION = '0.06000';
fe67d343 14
15=head1 NAME
16
17DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation.
18
acfcc1fb 19=head1 DESCRIPTION
fe67d343 20
acfcc1fb 21Base driver for Microsoft SQL Server, used by
22L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server> for support
23via L<DBD::Sybase> and
24L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server> for support via
25L<DBD::ODBC>.
fe67d343 26
acfcc1fb 27See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base> for
28usage information.
fe67d343 29
acfcc1fb 30=cut
fe67d343 31
3559ae79 32sub _is_case_sensitive {
33 my $self = shift;
34
35 return $self->case_sensitive_collation ? 1 : 0;
36}
37
38sub _setup {
39 my $self = shift;
40
41 $self->next::method;
42
43 my $dbh = $self->schema->storage->dbh;
44
45 my ($collation_name) = $dbh->selectrow_array(<<'EOS');
46SELECT collation_name
47FROM sys.databases
48WHERE name = DB_NAME()
49EOS
50
51 my ($sensitivity) = $collation_name =~ /(C\w)_[A-z]+\z/;
52
53 $self->case_sensitive_collation($sensitivity eq 'CS' ? 1 : 0);
54}
55
56sub _lc {
57 my ($self, $name) = @_;
58
59 return $self->case_sensitive_collation ? $name : lc($name);
60}
61
acfcc1fb 62sub _tables_list {
bfb43060 63 my ($self, $opts) = @_;
fe67d343 64
acfcc1fb 65 my $dbh = $self->schema->storage->dbh;
66 my $sth = $dbh->prepare(<<'EOF');
020f3c3a 67SELECT t.table_name
68FROM information_schema.tables t
43170884 69WHERE lower(t.table_schema) = ?
acfcc1fb 70EOF
43170884 71 $sth->execute(lc $self->db_schema);
fe67d343 72
bfb43060 73 my @tables = map @$_, @{ $sth->fetchall_arrayref };
acfcc1fb 74
bfb43060 75 return $self->_filter_tables(\@tables, $opts);
acfcc1fb 76}
fe67d343 77
fe67d343 78sub _table_pk_info {
79 my ($self, $table) = @_;
80 my $dbh = $self->schema->storage->dbh;
81 my $sth = $dbh->prepare(qq{sp_pkeys '$table'});
82 $sth->execute;
83
84 my @keydata;
85
86 while (my $row = $sth->fetchrow_hashref) {
3559ae79 87 push @keydata, $self->_lc($row->{COLUMN_NAME});
fe67d343 88 }
89
90 return \@keydata;
91}
92
93sub _table_fk_info {
94 my ($self, $table) = @_;
95
a3e05a4b 96 my ($local_cols, $remote_cols, $remote_table, @rels, $sth);
fe67d343 97 my $dbh = $self->schema->storage->dbh;
a3e05a4b 98 eval {
ea8d5d7c 99 $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'});
a3e05a4b 100 $sth->execute;
101 };
fe67d343 102
a3e05a4b 103 while (my $row = eval { $sth->fetchrow_hashref }) {
65f74457 104 my $fk = $row->{FK_NAME};
3559ae79 105 push @{$local_cols->{$fk}}, $self->_lc($row->{FKCOLUMN_NAME});
106 push @{$remote_cols->{$fk}}, $self->_lc($row->{PKCOLUMN_NAME});
bfb43060 107 $remote_table->{$fk} = $row->{PKTABLE_NAME};
fe67d343 108 }
109
110 foreach my $fk (keys %$remote_table) {
65f74457 111 push @rels, {
112 local_columns => \@{$local_cols->{$fk}},
113 remote_columns => \@{$remote_cols->{$fk}},
114 remote_table => $remote_table->{$fk},
115 };
fe67d343 116
117 }
118 return \@rels;
119}
120
121sub _table_uniq_info {
122 my ($self, $table) = @_;
123
124 my $dbh = $self->schema->storage->dbh;
020f3c3a 125 local $dbh->{FetchHashKeyName} = 'NAME_lc';
126
127 my $sth = $dbh->prepare(qq{
128SELECT ccu.constraint_name, ccu.column_name
129FROM information_schema.constraint_column_usage ccu
130JOIN information_schema.table_constraints tc on (ccu.constraint_name = tc.constraint_name)
131JOIN information_schema.key_column_usage kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name)
bfb43060 132wHERE lower(ccu.table_name) = @{[ $dbh->quote(lc $table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position
020f3c3a 133 });
fe67d343 134 $sth->execute;
135 my $constraints;
136 while (my $row = $sth->fetchrow_hashref) {
020f3c3a 137 my $name = $row->{constraint_name};
3559ae79 138 my $col = $self->_lc($row->{column_name});
fe67d343 139 push @{$constraints->{$name}}, $col;
140 }
141
142 my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints;
143 return \@uniqs;
144}
145
9c9197d6 146sub _columns_info_for {
147 my $self = shift;
148 my ($table) = @_;
149
150 my $result = $self->next::method(@_);
151
152 while (my ($col, $info) = each %$result) {
153 my $dbh = $self->schema->storage->dbh;
020f3c3a 154
9c9197d6 155 my $sth = $dbh->prepare(qq{
020f3c3a 156SELECT column_name
157FROM information_schema.columns
bfb43060 158WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1
159AND lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
9c9197d6 160 });
020f3c3a 161 if (eval { $sth->execute; $sth->fetchrow_array }) {
9c9197d6 162 $info->{is_auto_increment} = 1;
163 $info->{data_type} =~ s/\s*identity//i;
164 delete $info->{size};
165 }
fe67d343 166
5c6fb0a1 167# get default
9c9197d6 168 $sth = $dbh->prepare(qq{
020f3c3a 169SELECT column_default
170FROM information_schema.columns
bfb43060 171wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]}
9c9197d6 172 });
020f3c3a 173 my ($default) = eval { $sth->execute; $sth->fetchrow_array };
9c9197d6 174
175 if (defined $default) {
176 # strip parens
177 $default =~ s/^\( (.*) \)\z/$1/x;
178
179 # Literal strings are in ''s, numbers are in ()s (in some versions of
180 # MSSQL, in others they are unquoted) everything else is a function.
181 $info->{default_value} =
182 $default =~ /^['(] (.*) [)']\z/x ? $1 :
183 $default =~ /^\d/ ? $default : \$default;
184 }
5c6fb0a1 185 }
186
9c9197d6 187 return $result;
fe67d343 188}
189
fe67d343 190=head1 SEE ALSO
191
acfcc1fb 192L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>,
193L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>,
fe67d343 194L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
195L<DBIx::Class::Schema::Loader::DBI>
196
197=head1 AUTHOR
198
9cc8e7e1 199See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
fe67d343 200
be80bba7 201=head1 LICENSE
0852b7b8 202
be80bba7 203This library is free software; you can redistribute it and/or modify it under
204the same terms as Perl itself.
0852b7b8 205
fe67d343 206=cut
207
2081;
bfb43060 209# vim:et sts=4 sw=4 tw=0: