fix column name case bug
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI.pm
CommitLineData
996be9ee 1package DBIx::Class::Schema::Loader::DBI;
2
3use strict;
4use warnings;
abaf2c66 5use base qw/DBIx::Class::Schema::Loader::Base/;
942bd5e0 6use mro 'c3';
fa994d3c 7use Carp::Clan qw/^DBIx::Class/;
61d1cca1 8use Try::Tiny;
9use namespace::clean;
996be9ee 10
f671b630 11our $VERSION = '0.07002';
32f784fc 12
996be9ee 13=head1 NAME
14
15DBIx::Class::Schema::Loader::DBI - DBIx::Class::Schema::Loader DBI Implementation.
16
17=head1 SYNOPSIS
18
19See L<DBIx::Class::Schema::Loader::Base>
20
21=head1 DESCRIPTION
22
23This is the base class for L<DBIx::Class::Schema::Loader::Base> classes for
24DBI-based storage backends, and implements the common functionality between them.
25
26See L<DBIx::Class::Schema::Loader::Base> for the available options.
27
28=head1 METHODS
29
30=head2 new
31
32Overlays L<DBIx::Class::Schema::Loader::Base/new> to do some DBI-specific
33things.
34
35=cut
36
37sub new {
38 my $self = shift->next::method(@_);
39
71a6e88a 40 # rebless to vendor-specific class if it exists and loads and we're not in a
41 # custom class.
42 if (not $self->loader_class) {
43 my $dbh = $self->schema->storage->dbh;
44 my $driver = $dbh->{Driver}->{Name};
45
46 my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver;
47 if ($self->load_optional_class($subclass)) {
48 bless $self, $subclass unless $self->isa($subclass);
49 $self->_rebless;
50 }
996be9ee 51 }
52
53 # Set up the default quoting character and name seperators
243c6ebc 54 $self->{_quoter} = $self->_build_quoter;
77d3753e 55 $self->{_namesep} = $self->_build_namesep;
243c6ebc 56
996be9ee 57 # For our usage as regex matches, concatenating multiple quoter
58 # values works fine (e.g. s/\Q<>\E// if quoter was [ '<', '>' ])
59 if( ref $self->{_quoter} eq 'ARRAY') {
60 $self->{_quoter} = join(q{}, @{$self->{_quoter}});
61 }
62
63 $self->_setup;
64
65 $self;
66}
67
77d3753e 68sub _build_quoter {
69 my $self = shift;
70 my $dbh = $self->schema->storage->dbh;
71 return $dbh->get_info(29)
72 || $self->schema->storage->sql_maker->quote_char
73 || q{"};
74}
75
76sub _build_namesep {
77 my $self = shift;
78 my $dbh = $self->schema->storage->dbh;
79 return $dbh->get_info(41)
80 || $self->schema->storage->sql_maker->name_sep
81 || q{.};
82}
83
996be9ee 84# Override this in vendor modules to do things at the end of ->new()
85sub _setup { }
86
6ae3f335 87# Override this in vendor module to load a subclass if necessary
88sub _rebless { }
89
996be9ee 90# Returns an array of table names
91sub _tables_list {
bfb43060 92 my ($self, $opts) = (shift, shift);
996be9ee 93
e57fd726 94 my ($table, $type) = @_ ? @_ : ('%', '%');
95
996be9ee 96 my $dbh = $self->schema->storage->dbh;
e57fd726 97 my @tables = $dbh->tables(undef, $self->db_schema, $table, $type);
77d3753e 98
c5ff1f26 99 my $qt = qr/[\Q$self->{_quoter}\E"'`\[\]]/;
385c593b 100
c5ff1f26 101 my $all_tables_quoted = (grep /$qt/, @tables) == @tables;
102
103 if ($self->{_quoter} && $all_tables_quoted) {
385c593b 104 s/.* $qt (?= .* $qt)//xg for @tables;
105 } else {
106 s/^.*\Q$self->{_namesep}\E// for @tables;
107 }
108 s/$qt//g for @tables;
996be9ee 109
bfb43060 110 return $self->_filter_tables(\@tables, $opts);
075aff97 111}
112
bfb43060 113# apply constraint/exclude and ignore bad tables and views
075aff97 114sub _filter_tables {
bfb43060 115 my ($self, $tables, $opts) = @_;
075aff97 116
bfb43060 117 my @tables = @$tables;
075aff97 118 my @filtered_tables;
119
bfb43060 120 $opts ||= {};
121 my $constraint = $opts->{constraint};
122 my $exclude = $opts->{exclude};
123
124 @tables = grep { /$constraint/ } @$tables if defined $constraint;
125 @tables = grep { ! /$exclude/ } @$tables if defined $exclude;
126
075aff97 127 for my $table (@tables) {
61d1cca1 128 try {
805dbe0a 129 my $sth = $self->_sth_for($table, undef, \'1 = 0');
130 $sth->execute;
075aff97 131 }
61d1cca1 132 catch {
133 warn "Bad table or view '$table', ignoring: $_\n";
0c1d5b47 134 $self->_unregister_source_for_table($table);
61d1cca1 135 };
136
137 push @filtered_tables, $table;
075aff97 138 }
139
140 return @filtered_tables;
996be9ee 141}
142
12af3806 143=head2 load
144
145We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
146
147=cut
148
149sub load {
150 my $self = shift;
151
152 local $self->schema->storage->dbh->{RaiseError} = 1;
153 local $self->schema->storage->dbh->{PrintError} = 0;
154 $self->next::method(@_);
155}
156
075aff97 157sub _table_as_sql {
996be9ee 158 my ($self, $table) = @_;
159
996be9ee 160 if($self->{db_schema}) {
385c593b 161 $table = $self->{db_schema} . $self->{_namesep} .
162 $self->_quote_table_name($table);
163 } else {
164 $table = $self->_quote_table_name($table);
996be9ee 165 }
166
075aff97 167 return $table;
168}
169
170sub _sth_for {
171 my ($self, $table, $fields, $where) = @_;
172
173 my $dbh = $self->schema->storage->dbh;
174
175 my $sth = $dbh->prepare($self->schema->storage->sql_maker
176 ->select(\$self->_table_as_sql($table), $fields, $where));
177
178 return $sth;
179}
180
181# Returns an arrayref of column names
182sub _table_columns {
183 my ($self, $table) = @_;
184
185 my $sth = $self->_sth_for($table, undef, \'1 = 0');
996be9ee 186 $sth->execute;
bc1cb85e 187 my $retval = $self->preserve_case ? \@{$sth->{NAME}} : \@{$sth->{NAME_lc}};
3b7f80f9 188 $sth->finish;
189
190 $retval;
996be9ee 191}
192
193# Returns arrayref of pk col names
194sub _table_pk_info {
fd589700 195 my ($self, $table) = @_;
996be9ee 196
197 my $dbh = $self->schema->storage->dbh;
198
eb040f78 199 my @primary = map { $self->_lc($_) } $dbh->primary_key('', $self->db_schema, $table);
996be9ee 200 s/\Q$self->{_quoter}\E//g for @primary;
201
202 return \@primary;
203}
204
fd589700 205# Override this for vendor-specific uniq info
996be9ee 206sub _table_uniq_info {
fd589700 207 my ($self, $table) = @_;
208
209 my $dbh = $self->schema->storage->dbh;
210 if(!$dbh->can('statistics_info')) {
211 warn "No UNIQUE constraint information can be gathered for this vendor";
212 return [];
213 }
214
215 my %indices;
216 my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1);
217 while(my $row = $sth->fetchrow_hashref) {
218 # skip table-level stats, conditional indexes, and any index missing
219 # critical fields
220 next if $row->{TYPE} eq 'table'
221 || defined $row->{FILTER_CONDITION}
222 || !$row->{INDEX_NAME}
223 || !defined $row->{ORDINAL_POSITION}
224 || !$row->{COLUMN_NAME};
225
226 $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME};
227 }
3b7f80f9 228 $sth->finish;
fd589700 229
230 my @retval;
231 foreach my $index_name (keys %indices) {
232 my $index = $indices{$index_name};
233 push(@retval, [ $index_name => [
234 map { $index->{$_} }
235 sort keys %$index
236 ]]);
237 }
238
239 return \@retval;
996be9ee 240}
241
242# Find relationships
243sub _table_fk_info {
244 my ($self, $table) = @_;
245
246 my $dbh = $self->schema->storage->dbh;
a168c1c4 247 my $sth = $dbh->foreign_key_info( '', $self->db_schema, '',
248 '', $self->db_schema, $table );
996be9ee 249 return [] if !$sth;
250
251 my %rels;
252
253 my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
254 while(my $raw_rel = $sth->fetchrow_arrayref) {
255 my $uk_tbl = $raw_rel->[2];
c930f78b 256 my $uk_col = $self->_lc($raw_rel->[3]);
257 my $fk_col = $self->_lc($raw_rel->[7]);
996be9ee 258 my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
259 $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
260 $uk_col =~ s/\Q$self->{_quoter}\E//g;
261 $fk_col =~ s/\Q$self->{_quoter}\E//g;
262 $relid =~ s/\Q$self->{_quoter}\E//g;
263 $rels{$relid}->{tbl} = $uk_tbl;
c930f78b 264 $rels{$relid}->{cols}{$uk_col} = $fk_col;
996be9ee 265 }
3b7f80f9 266 $sth->finish;
996be9ee 267
268 my @rels;
269 foreach my $relid (keys %rels) {
270 push(@rels, {
271 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
272 local_columns => [ values %{$rels{$relid}->{cols}} ],
273 remote_table => $rels{$relid}->{tbl},
274 });
275 }
276
277 return \@rels;
278}
279
12af3806 280# ported in from DBIx::Class::Storage::DBI:
281sub _columns_info_for {
282 my ($self, $table) = @_;
283
284 my $dbh = $self->schema->storage->dbh;
285
61d1cca1 286 my %result;
287
12af3806 288 if ($dbh->can('column_info')) {
61d1cca1 289 my $sth = $self->_dbh_column_info($dbh, undef, $self->db_schema, $table, '%' );
290 while ( my $info = $sth->fetchrow_hashref() ){
291 my $column_info = {};
292 $column_info->{data_type} = lc $info->{TYPE_NAME};
293
294 my $size = $info->{COLUMN_SIZE};
295
296 if (defined $size && defined $info->{DECIMAL_DIGITS}) {
297 $column_info->{size} = [$size, $info->{DECIMAL_DIGITS}];
12af3806 298 }
61d1cca1 299 elsif (defined $size) {
300 $column_info->{size} = $size;
301 }
302
303 $column_info->{is_nullable} = $info->{NULLABLE} ? 1 : 0;
304 $column_info->{default_value} = $info->{COLUMN_DEF} if defined $info->{COLUMN_DEF};
305 my $col_name = $info->{COLUMN_NAME};
306 $col_name =~ s/^\"(.*)\"$/$1/;
307
308 $col_name = $self->_lc($col_name);
c38ec663 309
61d1cca1 310 my $extra_info = $self->_extra_column_info(
311 $table, $col_name, $column_info, $info
312 ) || {};
313 $column_info = { %$column_info, %$extra_info };
314
315 $result{$col_name} = $column_info;
316 }
317 $sth->finish;
318
319 return \%result if %result;
12af3806 320 }
321
075aff97 322 my $sth = $self->_sth_for($table, undef, \'1 = 0');
12af3806 323 $sth->execute;
61d1cca1 324
325 my @columns = $sth->{NAME};
326
327 for my $i (0 .. $#columns) {
23d1f36b 328 my $column_info = {};
26334ec1 329 $column_info->{data_type} = lc $sth->{TYPE}->[$i];
330
331 my $size = $sth->{PRECISION}[$i];
332
333 if (defined $size && defined $sth->{SCALE}[$i]) {
334 $column_info->{size} = [$size, $sth->{SCALE}[$i]];
335 }
336 elsif (defined $size) {
337 $column_info->{size} = $size;
338 }
339
23d1f36b 340 $column_info->{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
341
342 if ($column_info->{data_type} =~ m/^(.*?)\((.*?)\)$/) {
343 $column_info->{data_type} = $1;
344 $column_info->{size} = $2;
12af3806 345 }
346
45be2ce7 347 my $extra_info = $self->_extra_column_info($table, $columns[$i], $column_info) || {};
23d1f36b 348 $column_info = { %$column_info, %$extra_info };
349
61d1cca1 350 $result{ $self->_lc($columns[$i]) } = $column_info;
12af3806 351 }
6ce0bcc3 352 $sth->finish;
353
354 foreach my $col (keys %result) {
355 my $colinfo = $result{$col};
356 my $type_num = $colinfo->{data_type};
357 my $type_name;
61d1cca1 358 if (defined $type_num && $type_num =~ /^\d+\z/ && $dbh->can('type_info')) {
6ce0bcc3 359 my $type_info = $dbh->type_info($type_num);
360 $type_name = $type_info->{TYPE_NAME} if $type_info;
cf0ba25b 361 $colinfo->{data_type} = lc $type_name if $type_name;
6ce0bcc3 362 }
363 }
12af3806 364
365 return \%result;
366}
367
db9c411a 368# do not use this, override _columns_info_for instead
a8df0345 369sub _extra_column_info {}
c5baf131 370
db9c411a 371# override to mask warnings if needed (see mysql)
372sub _dbh_column_info {
373 my ($self, $dbh) = (shift, shift);
374
375 return $dbh->column_info(@_);
376}
377
996be9ee 378=head1 SEE ALSO
379
380L<DBIx::Class::Schema::Loader>
381
be80bba7 382=head1 AUTHOR
383
9cc8e7e1 384See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
be80bba7 385
386=head1 LICENSE
387
388This library is free software; you can redistribute it and/or modify it under
389the same terms as Perl itself.
390
996be9ee 391=cut
392
3931;
26334ec1 394# vim:et sts=4 sw=4 tw=0: