1 package DBIx::Class::Schema::Loader::DBI;
5 use base qw/DBIx::Class::Schema::Loader::Base/;
7 use Carp::Clan qw/^DBIx::Class/;
9 our $VERSION = '0.05002';
13 DBIx::Class::Schema::Loader::DBI - DBIx::Class::Schema::Loader DBI Implementation.
17 See L<DBIx::Class::Schema::Loader::Base>
21 This is the base class for L<DBIx::Class::Schema::Loader::Base> classes for
22 DBI-based storage backends, and implements the common functionality between them.
24 See L<DBIx::Class::Schema::Loader::Base> for the available options.
30 Overlays L<DBIx::Class::Schema::Loader::Base/new> to do some DBI-specific
36 my $self = shift->next::method(@_);
38 # rebless to vendor-specific class if it exists and loads
39 my $dbh = $self->schema->storage->dbh;
40 my $driver = $dbh->{Driver}->{Name};
42 my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver;
43 if ($self->load_optional_class($subclass)) {
44 bless $self, $subclass unless $self->isa($subclass);
48 # Set up the default quoting character and name seperators
49 $self->{_quoter} = $self->_build_quoter;
50 $self->{_namesep} = $self->_build_namesep;
51 # For our usage as regex matches, concatenating multiple quoter
52 # values works fine (e.g. s/\Q<>\E// if quoter was [ '<', '>' ])
53 if( ref $self->{_quoter} eq 'ARRAY') {
54 $self->{_quoter} = join(q{}, @{$self->{_quoter}});
64 my $dbh = $self->schema->storage->dbh;
65 return $dbh->get_info(29)
66 || $self->schema->storage->sql_maker->quote_char
72 my $dbh = $self->schema->storage->dbh;
73 return $dbh->get_info(41)
74 || $self->schema->storage->sql_maker->name_sep
78 # Override this in vendor modules to do things at the end of ->new()
81 # Override this in vendor module to load a subclass if necessary
84 # Returns an array of table names
88 my ($table, $type) = @_ ? @_ : ('%', '%');
90 my $dbh = $self->schema->storage->dbh;
91 my @tables = $dbh->tables(undef, $self->db_schema, $table, $type);
93 my $qt = qr/[\Q$self->{_quoter}\E"'`\[\]]/;
95 my $all_tables_quoted = (grep /$qt/, @tables) == @tables;
97 if ($self->{_quoter} && $all_tables_quoted) {
98 s/.* $qt (?= .* $qt)//xg for @tables;
100 s/^.*\Q$self->{_namesep}\E// for @tables;
102 s/$qt//g for @tables;
104 return $self->_filter_tables(@tables);
107 # ignore bad tables and views
109 my ($self, @tables) = @_;
113 for my $table (@tables) {
115 my $sth = $self->_sth_for($table, undef, \'1 = 0');
119 push @filtered_tables, $table;
122 warn "Bad table or view '$table', ignoring: $@\n";
125 my $schema = $self->schema;
126 # in older DBIC it's a private method
127 my $unregister = $schema->can('unregister_source')
128 || $schema->can('_unregister_source');
129 $schema->$unregister($self->_table2moniker($table));
134 return @filtered_tables;
139 We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
146 local $self->schema->storage->dbh->{RaiseError} = 1;
147 local $self->schema->storage->dbh->{PrintError} = 0;
148 $self->next::method(@_);
152 my ($self, $table) = @_;
154 if($self->{db_schema}) {
155 $table = $self->{db_schema} . $self->{_namesep} .
156 $self->_quote_table_name($table);
158 $table = $self->_quote_table_name($table);
165 my ($self, $table, $fields, $where) = @_;
167 my $dbh = $self->schema->storage->dbh;
169 my $sth = $dbh->prepare($self->schema->storage->sql_maker
170 ->select(\$self->_table_as_sql($table), $fields, $where));
175 # Returns an arrayref of column names
177 my ($self, $table) = @_;
179 my $sth = $self->_sth_for($table, undef, \'1 = 0');
181 my $retval = \@{$sth->{NAME_lc}};
187 # Returns arrayref of pk col names
189 my ($self, $table) = @_;
191 my $dbh = $self->schema->storage->dbh;
193 my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table);
194 s/\Q$self->{_quoter}\E//g for @primary;
199 # Override this for vendor-specific uniq info
200 sub _table_uniq_info {
201 my ($self, $table) = @_;
203 my $dbh = $self->schema->storage->dbh;
204 if(!$dbh->can('statistics_info')) {
205 warn "No UNIQUE constraint information can be gathered for this vendor";
210 my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1);
211 while(my $row = $sth->fetchrow_hashref) {
212 # skip table-level stats, conditional indexes, and any index missing
214 next if $row->{TYPE} eq 'table'
215 || defined $row->{FILTER_CONDITION}
216 || !$row->{INDEX_NAME}
217 || !defined $row->{ORDINAL_POSITION}
218 || !$row->{COLUMN_NAME};
220 $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME};
225 foreach my $index_name (keys %indices) {
226 my $index = $indices{$index_name};
227 push(@retval, [ $index_name => [
238 my ($self, $table) = @_;
240 my $dbh = $self->schema->storage->dbh;
241 my $sth = $dbh->foreign_key_info( '', $self->db_schema, '',
242 '', $self->db_schema, $table );
247 my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
248 while(my $raw_rel = $sth->fetchrow_arrayref) {
249 my $uk_tbl = $raw_rel->[2];
250 my $uk_col = lc $raw_rel->[3];
251 my $fk_col = lc $raw_rel->[7];
252 my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
253 $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
254 $uk_col =~ s/\Q$self->{_quoter}\E//g;
255 $fk_col =~ s/\Q$self->{_quoter}\E//g;
256 $relid =~ s/\Q$self->{_quoter}\E//g;
257 $rels{$relid}->{tbl} = $uk_tbl;
258 $rels{$relid}->{cols}->{$uk_col} = $fk_col;
263 foreach my $relid (keys %rels) {
265 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
266 local_columns => [ values %{$rels{$relid}->{cols}} ],
267 remote_table => $rels{$relid}->{tbl},
274 # ported in from DBIx::Class::Storage::DBI:
275 sub _columns_info_for {
276 my ($self, $table) = @_;
278 my $dbh = $self->schema->storage->dbh;
280 if ($dbh->can('column_info')) {
283 my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' );
284 while ( my $info = $sth->fetchrow_hashref() ){
285 my $column_info = {};
286 $column_info->{data_type} = $info->{TYPE_NAME};
287 $column_info->{size} = $info->{COLUMN_SIZE};
288 $column_info->{is_nullable} = $info->{NULLABLE} ? 1 : 0;
289 $column_info->{default_value} = $info->{COLUMN_DEF};
290 my $col_name = $info->{COLUMN_NAME};
291 $col_name =~ s/^\"(.*)\"$/$1/;
293 my $extra_info = $self->_extra_column_info($info) || {};
294 $column_info = { %$column_info, %$extra_info };
296 my $custom_info = $self->_custom_column_info( $table, $col_name, $column_info ) || {};
297 $column_info = { %$column_info, %$custom_info };
299 my $datetime_info = $self->_datetime_column_info( $table, $col_name, $column_info ) || {};
300 $column_info = { %$column_info, %$datetime_info };
302 $result{$col_name} = $column_info;
306 return \%result if !$@ && scalar keys %result;
310 my $sth = $self->_sth_for($table, undef, \'1 = 0');
312 my @columns = @{$sth->{NAME_lc}};
313 for my $i ( 0 .. $#columns ){
314 my $column_info = {};
315 $column_info->{data_type} = $sth->{TYPE}->[$i];
316 $column_info->{size} = $sth->{PRECISION}->[$i];
317 $column_info->{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
319 if ($column_info->{data_type} =~ m/^(.*?)\((.*?)\)$/) {
320 $column_info->{data_type} = $1;
321 $column_info->{size} = $2;
324 my $extra_info = $self->_extra_column_info($table, $columns[$i], $sth, $i) || {};
325 $column_info = { %$column_info, %$extra_info };
327 my $custom_info = $self->_custom_column_info( $table, $columns[$i], $column_info ) || {};
328 $column_info = { %$column_info, %$custom_info };
330 my $datetime_info = $self->_datetime_column_info( $table, $columns[$i], $column_info ) || {};
331 $column_info = { %$column_info, %$datetime_info };
333 $result{$columns[$i]} = $column_info;
337 foreach my $col (keys %result) {
338 my $colinfo = $result{$col};
339 my $type_num = $colinfo->{data_type};
341 if(defined $type_num && $dbh->can('type_info')) {
342 my $type_info = $dbh->type_info($type_num);
343 $type_name = $type_info->{TYPE_NAME} if $type_info;
344 $colinfo->{data_type} = $type_name if $type_name;
351 # Override this in vendor class to return any additional column
353 sub _extra_column_info {}
357 L<DBIx::Class::Schema::Loader>
361 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
365 This library is free software; you can redistribute it and/or modify it under
366 the same terms as Perl itself.