Commit | Line | Data |
fe67d343 |
1 | package DBIx::Class::Schema::Loader::DBI::MSSQL; |
2 | |
3 | use strict; |
4 | use warnings; |
de82711a |
5 | use base 'DBIx::Class::Schema::Loader::DBI::Sybase::Common'; |
fe67d343 |
6 | use Carp::Clan qw/^DBIx::Class/; |
7 | use Class::C3; |
8 | |
3559ae79 |
9 | __PACKAGE__->mk_group_accessors('simple', qw/ |
10 | case_sensitive_collation |
11 | /); |
12 | |
e42ec4ef |
13 | our $VERSION = '0.05003'; |
fe67d343 |
14 | |
15 | =head1 NAME |
16 | |
17 | DBIx::Class::Schema::Loader::DBI::MSSQL - DBIx::Class::Schema::Loader::DBI MSSQL Implementation. |
18 | |
acfcc1fb |
19 | =head1 DESCRIPTION |
fe67d343 |
20 | |
acfcc1fb |
21 | Base driver for Microsoft SQL Server, used by |
22 | L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server> for support |
23 | via L<DBD::Sybase> and |
24 | L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server> for support via |
25 | L<DBD::ODBC>. |
fe67d343 |
26 | |
acfcc1fb |
27 | See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base> for |
28 | usage information. |
fe67d343 |
29 | |
acfcc1fb |
30 | =cut |
fe67d343 |
31 | |
3559ae79 |
32 | sub _is_case_sensitive { |
33 | my $self = shift; |
34 | |
35 | return $self->case_sensitive_collation ? 1 : 0; |
36 | } |
37 | |
38 | sub _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'); |
46 | SELECT collation_name |
47 | FROM sys.databases |
48 | WHERE name = DB_NAME() |
49 | EOS |
50 | |
51 | my ($sensitivity) = $collation_name =~ /(C\w)_[A-z]+\z/; |
52 | |
53 | $self->case_sensitive_collation($sensitivity eq 'CS' ? 1 : 0); |
54 | } |
55 | |
56 | sub _lc { |
57 | my ($self, $name) = @_; |
58 | |
59 | return $self->case_sensitive_collation ? $name : lc($name); |
60 | } |
61 | |
acfcc1fb |
62 | sub _tables_list { |
bfb43060 |
63 | my ($self, $opts) = @_; |
fe67d343 |
64 | |
acfcc1fb |
65 | my $dbh = $self->schema->storage->dbh; |
66 | my $sth = $dbh->prepare(<<'EOF'); |
020f3c3a |
67 | SELECT t.table_name |
68 | FROM information_schema.tables t |
43170884 |
69 | WHERE lower(t.table_schema) = ? |
acfcc1fb |
70 | EOF |
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 |
78 | sub _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 | |
93 | sub _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 | |
121 | sub _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{ |
128 | SELECT ccu.constraint_name, ccu.column_name |
129 | FROM information_schema.constraint_column_usage ccu |
130 | JOIN information_schema.table_constraints tc on (ccu.constraint_name = tc.constraint_name) |
131 | JOIN information_schema.key_column_usage kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name) |
bfb43060 |
132 | wHERE 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 |
146 | sub _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 |
156 | SELECT column_name |
157 | FROM information_schema.columns |
bfb43060 |
158 | WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1 |
159 | AND 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 |
169 | SELECT column_default |
170 | FROM information_schema.columns |
bfb43060 |
171 | wHERE 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 |
192 | L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>, |
193 | L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>, |
fe67d343 |
194 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
195 | L<DBIx::Class::Schema::Loader::DBI> |
196 | |
197 | =head1 AUTHOR |
198 | |
9cc8e7e1 |
199 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
fe67d343 |
200 | |
be80bba7 |
201 | =head1 LICENSE |
0852b7b8 |
202 | |
be80bba7 |
203 | This library is free software; you can redistribute it and/or modify it under |
204 | the same terms as Perl itself. |
0852b7b8 |
205 | |
fe67d343 |
206 | =cut |
207 | |
208 | 1; |
bfb43060 |
209 | # vim:et sts=4 sw=4 tw=0: |