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.05003';
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 $self->schema->storage->sql_maker->quote_char($self->{_quoter});
52 $self->schema->storage->sql_maker->name_sep($self->{_namesep});
54 # For our usage as regex matches, concatenating multiple quoter
55 # values works fine (e.g. s/\Q<>\E// if quoter was [ '<', '>' ])
56 if( ref $self->{_quoter} eq 'ARRAY') {
57 $self->{_quoter} = join(q{}, @{$self->{_quoter}});
67 my $dbh = $self->schema->storage->dbh;
68 return $dbh->get_info(29)
69 || $self->schema->storage->sql_maker->quote_char
75 my $dbh = $self->schema->storage->dbh;
76 return $dbh->get_info(41)
77 || $self->schema->storage->sql_maker->name_sep
81 # Override this in vendor modules to do things at the end of ->new()
84 # Override this in vendor module to load a subclass if necessary
87 # Returns an array of table names
91 my ($table, $type) = @_ ? @_ : ('%', '%');
93 my $dbh = $self->schema->storage->dbh;
94 my @tables = $dbh->tables(undef, $self->db_schema, $table, $type);
96 my $qt = qr/[\Q$self->{_quoter}\E"'`\[\]]/;
98 my $all_tables_quoted = (grep /$qt/, @tables) == @tables;
100 if ($self->{_quoter} && $all_tables_quoted) {
101 s/.* $qt (?= .* $qt)//xg for @tables;
103 s/^.*\Q$self->{_namesep}\E// for @tables;
105 s/$qt//g for @tables;
107 return $self->_filter_tables(@tables);
110 # ignore bad tables and views
112 my ($self, @tables) = @_;
116 for my $table (@tables) {
118 my $sth = $self->_sth_for($table, undef, \'1 = 0');
122 push @filtered_tables, $table;
125 warn "Bad table or view '$table', ignoring: $@\n";
128 my $schema = $self->schema;
129 # in older DBIC it's a private method
130 my $unregister = $schema->can('unregister_source')
131 || $schema->can('_unregister_source');
132 $schema->$unregister($self->_table2moniker($table));
137 return @filtered_tables;
142 We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
149 local $self->schema->storage->dbh->{RaiseError} = 1;
150 local $self->schema->storage->dbh->{PrintError} = 0;
151 $self->next::method(@_);
155 my ($self, $table) = @_;
157 if($self->{db_schema}) {
158 $table = $self->{db_schema} . $self->{_namesep} .
159 $self->_quote_table_name($table);
161 $table = $self->_quote_table_name($table);
168 my ($self, $table, $fields, $where) = @_;
170 my $dbh = $self->schema->storage->dbh;
172 my $sth = $dbh->prepare($self->schema->storage->sql_maker
173 ->select(\$self->_table_as_sql($table), $fields, $where));
178 # Returns an arrayref of column names
180 my ($self, $table) = @_;
182 my $sth = $self->_sth_for($table, undef, \'1 = 0');
184 my $retval = $self->_is_case_sensitive ? \@{$sth->{NAME}} : \@{$sth->{NAME_lc}};
190 # Returns arrayref of pk col names
192 my ($self, $table) = @_;
194 my $dbh = $self->schema->storage->dbh;
196 my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table);
197 s/\Q$self->{_quoter}\E//g for @primary;
202 # Override this for vendor-specific uniq info
203 sub _table_uniq_info {
204 my ($self, $table) = @_;
206 my $dbh = $self->schema->storage->dbh;
207 if(!$dbh->can('statistics_info')) {
208 warn "No UNIQUE constraint information can be gathered for this vendor";
213 my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1);
214 while(my $row = $sth->fetchrow_hashref) {
215 # skip table-level stats, conditional indexes, and any index missing
217 next if $row->{TYPE} eq 'table'
218 || defined $row->{FILTER_CONDITION}
219 || !$row->{INDEX_NAME}
220 || !defined $row->{ORDINAL_POSITION}
221 || !$row->{COLUMN_NAME};
223 $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME};
228 foreach my $index_name (keys %indices) {
229 my $index = $indices{$index_name};
230 push(@retval, [ $index_name => [
241 my ($self, $table) = @_;
243 my $dbh = $self->schema->storage->dbh;
244 my $sth = $dbh->foreign_key_info( '', $self->db_schema, '',
245 '', $self->db_schema, $table );
250 my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
251 while(my $raw_rel = $sth->fetchrow_arrayref) {
252 my $uk_tbl = $raw_rel->[2];
253 my $uk_col = lc $raw_rel->[3];
254 my $fk_col = lc $raw_rel->[7];
255 my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
256 $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
257 $uk_col =~ s/\Q$self->{_quoter}\E//g;
258 $fk_col =~ s/\Q$self->{_quoter}\E//g;
259 $relid =~ s/\Q$self->{_quoter}\E//g;
260 $rels{$relid}->{tbl} = $uk_tbl;
261 $rels{$relid}->{cols}->{$uk_col} = $fk_col;
266 foreach my $relid (keys %rels) {
268 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
269 local_columns => [ values %{$rels{$relid}->{cols}} ],
270 remote_table => $rels{$relid}->{tbl},
277 # ported in from DBIx::Class::Storage::DBI:
278 sub _columns_info_for {
279 my ($self, $table) = @_;
281 my $dbh = $self->schema->storage->dbh;
283 if ($dbh->can('column_info')) {
286 my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' );
287 while ( my $info = $sth->fetchrow_hashref() ){
288 my $column_info = {};
289 $column_info->{data_type} = $info->{TYPE_NAME};
290 $column_info->{size} = $info->{COLUMN_SIZE};
291 $column_info->{is_nullable} = $info->{NULLABLE} ? 1 : 0;
292 $column_info->{default_value} = $info->{COLUMN_DEF};
293 my $col_name = $info->{COLUMN_NAME};
294 $col_name =~ s/^\"(.*)\"$/$1/;
296 my $extra_info = $self->_extra_column_info(
297 $table, $col_name, $column_info, $info
299 $column_info = { %$column_info, %$extra_info };
301 $result{$col_name} = $column_info;
305 return \%result if !$@ && scalar keys %result;
309 my $sth = $self->_sth_for($table, undef, \'1 = 0');
311 my @columns = @{ $self->_is_case_sensitive ? $sth->{NAME} : $sth->{NAME_lc} };
312 for my $i ( 0 .. $#columns ){
313 my $column_info = {};
314 $column_info->{data_type} = $sth->{TYPE}->[$i];
315 $column_info->{size} = $sth->{PRECISION}->[$i];
316 $column_info->{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
318 if ($column_info->{data_type} =~ m/^(.*?)\((.*?)\)$/) {
319 $column_info->{data_type} = $1;
320 $column_info->{size} = $2;
323 my $extra_info = $self->_extra_column_info($table, $columns[$i], $column_info) || {};
324 $column_info = { %$column_info, %$extra_info };
326 $result{$columns[$i]} = $column_info;
330 foreach my $col (keys %result) {
331 my $colinfo = $result{$col};
332 my $type_num = $colinfo->{data_type};
334 if(defined $type_num && $type_num =~ /^\d+\z/ && $dbh->can('type_info')) {
335 my $type_info = $dbh->type_info($type_num);
336 $type_name = $type_info->{TYPE_NAME} if $type_info;
337 $colinfo->{data_type} = $type_name if $type_name;
344 # Override this in vendor class to return any additional column
346 sub _extra_column_info {}
350 L<DBIx::Class::Schema::Loader>
354 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
358 This library is free software; you can redistribute it and/or modify it under
359 the same terms as Perl itself.