1 package DBIx::Class::Schema::Loader::DBI;
5 use base qw/DBIx::Class::Schema::Loader::Base/;
7 use Carp::Clan qw/^DBIx::Class/;
11 our $VERSION = '0.07010';
13 __PACKAGE__->mk_group_accessors('simple', qw/
15 _disable_uniq_detection
22 DBIx::Class::Schema::Loader::DBI - DBIx::Class::Schema::Loader DBI Implementation.
26 See L<DBIx::Class::Schema::Loader::Base>
30 This is the base class for L<DBIx::Class::Schema::Loader::Base> classes for
31 DBI-based storage backends, and implements the common functionality between them.
33 See L<DBIx::Class::Schema::Loader::Base> for the available options.
39 Overlays L<DBIx::Class::Schema::Loader::Base/new> to do some DBI-specific
45 my $self = shift->next::method(@_);
47 # rebless to vendor-specific class if it exists and loads and we're not in a
49 if (not $self->loader_class) {
50 my $dbh = $self->schema->storage->dbh;
51 my $driver = $dbh->{Driver}->{Name};
53 my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver;
54 if ($self->load_optional_class($subclass)) {
55 bless $self, $subclass unless $self->isa($subclass);
60 # Set up the default quoting character and name seperators
61 $self->{_quoter} = $self->_build_quoter;
62 $self->{_namesep} = $self->_build_namesep;
64 # For our usage as regex matches, concatenating multiple quoter
65 # values works fine (e.g. s/\Q<>\E// if quoter was [ '<', '>' ])
66 if( ref $self->{_quoter} eq 'ARRAY') {
67 $self->{_quoter} = join(q{}, @{$self->{_quoter}});
77 my $dbh = $self->schema->storage->dbh;
78 return $dbh->get_info(29)
79 || $self->schema->storage->sql_maker->quote_char
85 my $dbh = $self->schema->storage->dbh;
86 return $dbh->get_info(41)
87 || $self->schema->storage->sql_maker->name_sep
91 # Override this in vendor modules to do things at the end of ->new()
94 # Override this in vendor module to load a subclass if necessary
97 # Returns an array of table names
99 my ($self, $opts) = (shift, shift);
101 my ($table, $type) = @_ ? @_ : ('%', '%');
103 my $dbh = $self->schema->storage->dbh;
104 my @tables = $dbh->tables(undef, $self->db_schema, $table, $type);
106 my $qt = qr/[\Q$self->{_quoter}\E"'`\[\]]/;
108 my $all_tables_quoted = (grep /$qt/, @tables) == @tables;
110 if ($self->{_quoter} && $all_tables_quoted) {
111 s/.* $qt (?= .* $qt)//xg for @tables;
113 s/^.*\Q$self->{_namesep}\E// for @tables;
115 s/$qt//g for @tables;
117 return $self->_filter_tables(\@tables, $opts);
120 # apply constraint/exclude and ignore bad tables and views
122 my ($self, $tables, $opts) = @_;
124 my @tables = @$tables;
128 my $constraint = $opts->{constraint};
129 my $exclude = $opts->{exclude};
131 @tables = grep { /$constraint/ } @$tables if defined $constraint;
132 @tables = grep { ! /$exclude/ } @$tables if defined $exclude;
134 LOOP: for my $table (@tables) {
136 local $^W = 0; # for ADO
137 my $sth = $self->_sth_for($table, undef, \'1 = 0');
141 warn "Bad table or view '$table', ignoring: $_\n";
142 $self->_unregister_source_for_table($table);
143 no warnings 'exiting';
147 push @filtered_tables, $table;
150 return @filtered_tables;
155 We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
162 local $self->schema->storage->dbh->{RaiseError} = 1;
163 local $self->schema->storage->dbh->{PrintError} = 0;
164 $self->next::method(@_);
168 my ($self, $table) = @_;
170 if($self->{db_schema}) {
171 $table = $self->{db_schema} . $self->{_namesep} .
172 $self->_quote_table_name($table);
174 $table = $self->_quote_table_name($table);
181 my ($self, $table, $fields, $where) = @_;
183 my $dbh = $self->schema->storage->dbh;
185 my $sth = $dbh->prepare($self->schema->storage->sql_maker
186 ->select(\$self->_table_as_sql($table), $fields, $where));
191 # Returns an arrayref of column names
193 my ($self, $table) = @_;
195 my $sth = $self->_sth_for($table, undef, \'1 = 0');
197 my $retval = $self->preserve_case ? \@{$sth->{NAME}} : \@{$sth->{NAME_lc}};
203 # Returns arrayref of pk col names
205 my ($self, $table) = @_;
207 return [] if $self->_disable_pk_detection;
209 my $dbh = $self->schema->storage->dbh;
212 $dbh->primary_key('', $self->db_schema, $table);
215 warn "Cannot find primary keys for this driver: $_";
216 $self->_disable_pk_detection(1);
220 return [] if not @primary;
222 @primary = map { $self->_lc($_) } @primary;
223 s/\Q$self->{_quoter}\E//g for @primary;
228 # Override this for vendor-specific uniq info
229 sub _table_uniq_info {
230 my ($self, $table) = @_;
232 return [] if $self->_disable_uniq_detection;
234 my $dbh = $self->schema->storage->dbh;
236 if (not $dbh->can('statistics_info')) {
237 warn "No UNIQUE constraint information can be gathered for this driver";
238 $self->_disable_uniq_detection(1);
243 my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1);
244 while(my $row = $sth->fetchrow_hashref) {
245 # skip table-level stats, conditional indexes, and any index missing
247 next if $row->{TYPE} eq 'table'
248 || defined $row->{FILTER_CONDITION}
249 || !$row->{INDEX_NAME}
250 || !defined $row->{ORDINAL_POSITION}
251 || !$row->{COLUMN_NAME};
253 $indices{$row->{INDEX_NAME}}[$row->{ORDINAL_POSITION}] = $self->_lc($row->{COLUMN_NAME});
258 foreach my $index_name (keys %indices) {
259 my $index = $indices{$index_name};
260 push(@retval, [ $index_name => [ @$index[1..$#$index] ] ]);
268 my ($self, $table) = @_;
270 return [] if $self->_disable_fk_detection;
272 my $dbh = $self->schema->storage->dbh;
274 $dbh->foreign_key_info( '', $self->db_schema, '',
275 '', $self->db_schema, $table );
278 warn "Cannot introspect relationships for this driver: $_";
279 $self->_disable_fk_detection(1);
287 my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
288 while(my $raw_rel = $sth->fetchrow_arrayref) {
289 my $uk_tbl = $raw_rel->[2];
290 my $uk_col = $self->_lc($raw_rel->[3]);
291 my $fk_col = $self->_lc($raw_rel->[7]);
292 my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
293 $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
294 $uk_col =~ s/\Q$self->{_quoter}\E//g;
295 $fk_col =~ s/\Q$self->{_quoter}\E//g;
296 $relid =~ s/\Q$self->{_quoter}\E//g;
297 $rels{$relid}->{tbl} = $uk_tbl;
298 $rels{$relid}->{cols}{$uk_col} = $fk_col;
303 foreach my $relid (keys %rels) {
305 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
306 local_columns => [ values %{$rels{$relid}->{cols}} ],
307 remote_table => $rels{$relid}->{tbl},
314 # ported in from DBIx::Class::Storage::DBI:
315 sub _columns_info_for {
316 my ($self, $table) = @_;
318 my $dbh = $self->schema->storage->dbh;
322 if ($dbh->can('column_info')) {
323 my $sth = $self->_dbh_column_info($dbh, undef, $self->db_schema, $table, '%' );
324 while ( my $info = $sth->fetchrow_hashref() ){
325 my $column_info = {};
326 $column_info->{data_type} = lc $info->{TYPE_NAME};
328 my $size = $info->{COLUMN_SIZE};
330 if (defined $size && defined $info->{DECIMAL_DIGITS}) {
331 $column_info->{size} = [$size, $info->{DECIMAL_DIGITS}];
333 elsif (defined $size) {
334 $column_info->{size} = $size;
337 $column_info->{is_nullable} = $info->{NULLABLE} ? 1 : 0;
338 $column_info->{default_value} = $info->{COLUMN_DEF} if defined $info->{COLUMN_DEF};
339 my $col_name = $info->{COLUMN_NAME};
340 $col_name =~ s/^\"(.*)\"$/$1/;
342 $col_name = $self->_lc($col_name);
344 my $extra_info = $self->_extra_column_info(
345 $table, $col_name, $column_info, $info
347 $column_info = { %$column_info, %$extra_info };
349 $result{$col_name} = $column_info;
353 return \%result if %result;
356 my $sth = $self->_sth_for($table, undef, \'1 = 0');
359 my @columns = @{ $sth->{NAME} };
361 for my $i (0 .. $#columns) {
362 my $column_info = {};
363 $column_info->{data_type} = lc $sth->{TYPE}[$i];
365 my $size = $sth->{PRECISION}[$i];
367 if (defined $size && defined $sth->{SCALE}[$i]) {
368 $column_info->{size} = [$size, $sth->{SCALE}[$i]];
370 elsif (defined $size) {
371 $column_info->{size} = $size;
374 $column_info->{is_nullable} = $sth->{NULLABLE}[$i] ? 1 : 0;
376 if ($column_info->{data_type} =~ m/^(.*?)\((.*?)\)$/) {
377 $column_info->{data_type} = $1;
378 $column_info->{size} = $2;
381 my $extra_info = $self->_extra_column_info($table, $columns[$i], $column_info) || {};
382 $column_info = { %$column_info, %$extra_info };
384 $result{ $self->_lc($columns[$i]) } = $column_info;
388 foreach my $col (keys %result) {
389 my $colinfo = $result{$col};
390 my $type_num = $colinfo->{data_type};
392 if (defined $type_num && $type_num =~ /^-?\d+\z/ && $dbh->can('type_info')) {
393 my $type_name = $self->_dbh_type_info_type_name($type_num);
394 $colinfo->{data_type} = lc $type_name if $type_name;
401 # Need to override this for the buggy Firebird ODBC driver.
402 sub _dbh_type_info_type_name {
403 my ($self, $type_num) = @_;
405 my $dbh = $self->schema->storage->dbh;
407 my $type_info = $dbh->type_info($type_num);
409 return $type_info ? $type_info->{TYPE_NAME} : undef;
412 # do not use this, override _columns_info_for instead
413 sub _extra_column_info {}
415 # override to mask warnings if needed (see mysql)
416 sub _dbh_column_info {
417 my ($self, $dbh) = (shift, shift);
419 return $dbh->column_info(@_);
422 # If a coderef uses DBI->connect, this should get its connect info.
423 sub _try_infer_connect_info_from_coderef {
424 my ($self, $code) = @_;
426 my ($dsn, $user, $pass, $params);
428 no warnings 'redefine';
430 local *DBI::connect = sub {
431 (undef, $dsn, $user, $pass, $params) = @_;
436 return ($dsn, $user, $pass, $params);
441 L<DBIx::Class::Schema::Loader>
445 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
449 This library is free software; you can redistribute it and/or modify it under
450 the same terms as Perl itself.
455 # vim:et sts=4 sw=4 tw=0: