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 | |
2a8e93e9 |
13 | our $VERSION = '0.06000'; |
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 | |
b7a0a040 |
45 | # We use the sys.databases query for the general case, and fallback to |
46 | # databasepropertyex() if for some reason sys.databases is not available, |
47 | # which does not work over DBD::ODBC with unixODBC+FreeTDS. |
48 | # |
49 | # XXX why does databasepropertyex() not work over DBD::ODBC ? |
40914006 |
50 | # |
51 | # more on collations here: http://msdn.microsoft.com/en-us/library/ms143515.aspx |
b7a0a040 |
52 | my ($collation_name) = |
53 | eval { $dbh->selectrow_array('SELECT collation_name FROM sys.databases WHERE name = DB_NAME()') } |
060f5ecd |
54 | || eval { $dbh->selectrow_array("SELECT databasepropertyex(DB_NAME(), 'Collation')") }; |
b7a0a040 |
55 | |
56 | if (not $collation_name) { |
57 | $self->case_sensitive_collation(0); # most likely not |
58 | return; |
59 | } |
3559ae79 |
60 | |
5ac72caa |
61 | my $case_sensitive = $collation_name =~ /_(?:CS|BIN2?)(?:_|\z)/; |
3559ae79 |
62 | |
5ac72caa |
63 | $self->case_sensitive_collation($case_sensitive ? 1 : 0); |
3559ae79 |
64 | } |
65 | |
66 | sub _lc { |
67 | my ($self, $name) = @_; |
68 | |
69 | return $self->case_sensitive_collation ? $name : lc($name); |
70 | } |
71 | |
acfcc1fb |
72 | sub _tables_list { |
bfb43060 |
73 | my ($self, $opts) = @_; |
fe67d343 |
74 | |
acfcc1fb |
75 | my $dbh = $self->schema->storage->dbh; |
76 | my $sth = $dbh->prepare(<<'EOF'); |
020f3c3a |
77 | SELECT t.table_name |
060f5ecd |
78 | FROM INFORMATION_SCHEMA.TABLES t |
43170884 |
79 | WHERE lower(t.table_schema) = ? |
acfcc1fb |
80 | EOF |
43170884 |
81 | $sth->execute(lc $self->db_schema); |
fe67d343 |
82 | |
bfb43060 |
83 | my @tables = map @$_, @{ $sth->fetchall_arrayref }; |
acfcc1fb |
84 | |
bfb43060 |
85 | return $self->_filter_tables(\@tables, $opts); |
acfcc1fb |
86 | } |
fe67d343 |
87 | |
fe67d343 |
88 | sub _table_pk_info { |
89 | my ($self, $table) = @_; |
90 | my $dbh = $self->schema->storage->dbh; |
91 | my $sth = $dbh->prepare(qq{sp_pkeys '$table'}); |
92 | $sth->execute; |
93 | |
94 | my @keydata; |
95 | |
96 | while (my $row = $sth->fetchrow_hashref) { |
3559ae79 |
97 | push @keydata, $self->_lc($row->{COLUMN_NAME}); |
fe67d343 |
98 | } |
99 | |
100 | return \@keydata; |
101 | } |
102 | |
103 | sub _table_fk_info { |
104 | my ($self, $table) = @_; |
105 | |
a3e05a4b |
106 | my ($local_cols, $remote_cols, $remote_table, @rels, $sth); |
fe67d343 |
107 | my $dbh = $self->schema->storage->dbh; |
a3e05a4b |
108 | eval { |
ea8d5d7c |
109 | $sth = $dbh->prepare(qq{sp_fkeys \@fktable_name = '$table'}); |
a3e05a4b |
110 | $sth->execute; |
111 | }; |
fe67d343 |
112 | |
a3e05a4b |
113 | while (my $row = eval { $sth->fetchrow_hashref }) { |
65f74457 |
114 | my $fk = $row->{FK_NAME}; |
3559ae79 |
115 | push @{$local_cols->{$fk}}, $self->_lc($row->{FKCOLUMN_NAME}); |
116 | push @{$remote_cols->{$fk}}, $self->_lc($row->{PKCOLUMN_NAME}); |
bfb43060 |
117 | $remote_table->{$fk} = $row->{PKTABLE_NAME}; |
fe67d343 |
118 | } |
119 | |
120 | foreach my $fk (keys %$remote_table) { |
65f74457 |
121 | push @rels, { |
122 | local_columns => \@{$local_cols->{$fk}}, |
123 | remote_columns => \@{$remote_cols->{$fk}}, |
124 | remote_table => $remote_table->{$fk}, |
125 | }; |
fe67d343 |
126 | |
127 | } |
128 | return \@rels; |
129 | } |
130 | |
131 | sub _table_uniq_info { |
132 | my ($self, $table) = @_; |
133 | |
134 | my $dbh = $self->schema->storage->dbh; |
020f3c3a |
135 | local $dbh->{FetchHashKeyName} = 'NAME_lc'; |
136 | |
137 | my $sth = $dbh->prepare(qq{ |
138 | SELECT ccu.constraint_name, ccu.column_name |
060f5ecd |
139 | FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu |
140 | JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc on (ccu.constraint_name = tc.constraint_name) |
141 | JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu on (ccu.constraint_name = kcu.constraint_name and ccu.column_name = kcu.column_name) |
bfb43060 |
142 | wHERE lower(ccu.table_name) = @{[ $dbh->quote(lc $table) ]} AND constraint_type = 'UNIQUE' ORDER BY kcu.ordinal_position |
020f3c3a |
143 | }); |
fe67d343 |
144 | $sth->execute; |
145 | my $constraints; |
146 | while (my $row = $sth->fetchrow_hashref) { |
020f3c3a |
147 | my $name = $row->{constraint_name}; |
3559ae79 |
148 | my $col = $self->_lc($row->{column_name}); |
fe67d343 |
149 | push @{$constraints->{$name}}, $col; |
150 | } |
151 | |
152 | my @uniqs = map { [ $_ => $constraints->{$_} ] } keys %$constraints; |
153 | return \@uniqs; |
154 | } |
155 | |
9c9197d6 |
156 | sub _columns_info_for { |
157 | my $self = shift; |
158 | my ($table) = @_; |
159 | |
160 | my $result = $self->next::method(@_); |
161 | |
162 | while (my ($col, $info) = each %$result) { |
163 | my $dbh = $self->schema->storage->dbh; |
020f3c3a |
164 | |
9c9197d6 |
165 | my $sth = $dbh->prepare(qq{ |
020f3c3a |
166 | SELECT column_name |
060f5ecd |
167 | FROM INFORMATION_SCHEMA.COLUMNS |
bfb43060 |
168 | WHERE columnproperty(object_id(@{[ $dbh->quote(lc $table) ]}, 'U'), @{[ $dbh->quote(lc $col) ]}, 'IsIdentity') = 1 |
169 | AND lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]} |
9c9197d6 |
170 | }); |
020f3c3a |
171 | if (eval { $sth->execute; $sth->fetchrow_array }) { |
9c9197d6 |
172 | $info->{is_auto_increment} = 1; |
173 | $info->{data_type} =~ s/\s*identity//i; |
174 | delete $info->{size}; |
175 | } |
fe67d343 |
176 | |
5c6fb0a1 |
177 | # get default |
9c9197d6 |
178 | $sth = $dbh->prepare(qq{ |
020f3c3a |
179 | SELECT column_default |
060f5ecd |
180 | FROM INFORMATION_SCHEMA.COLUMNS |
bfb43060 |
181 | wHERE lower(table_name) = @{[ $dbh->quote(lc $table) ]} AND lower(column_name) = @{[ $dbh->quote(lc $col) ]} |
9c9197d6 |
182 | }); |
020f3c3a |
183 | my ($default) = eval { $sth->execute; $sth->fetchrow_array }; |
9c9197d6 |
184 | |
185 | if (defined $default) { |
186 | # strip parens |
187 | $default =~ s/^\( (.*) \)\z/$1/x; |
188 | |
189 | # Literal strings are in ''s, numbers are in ()s (in some versions of |
190 | # MSSQL, in others they are unquoted) everything else is a function. |
191 | $info->{default_value} = |
192 | $default =~ /^['(] (.*) [)']\z/x ? $1 : |
193 | $default =~ /^\d/ ? $default : \$default; |
194 | } |
5c6fb0a1 |
195 | } |
196 | |
9c9197d6 |
197 | return $result; |
fe67d343 |
198 | } |
199 | |
fe67d343 |
200 | =head1 SEE ALSO |
201 | |
acfcc1fb |
202 | L<DBIx::Class::Schema::Loader::DBI::Sybase::Microsoft_SQL_Server>, |
203 | L<DBIx::Class::Schema::Loader::DBI::ODBC::Microsoft_SQL_Server>, |
fe67d343 |
204 | L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>, |
205 | L<DBIx::Class::Schema::Loader::DBI> |
206 | |
207 | =head1 AUTHOR |
208 | |
9cc8e7e1 |
209 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
fe67d343 |
210 | |
be80bba7 |
211 | =head1 LICENSE |
0852b7b8 |
212 | |
be80bba7 |
213 | This library is free software; you can redistribute it and/or modify it under |
214 | the same terms as Perl itself. |
0852b7b8 |
215 | |
fe67d343 |
216 | =cut |
217 | |
218 | 1; |
bfb43060 |
219 | # vim:et sts=4 sw=4 tw=0: |