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