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.07001';
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 and we're not in a
40 if (not $self->loader_class) {
41 my $dbh = $self->schema->storage->dbh;
42 my $driver = $dbh->{Driver}->{Name};
44 my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver;
45 if ($self->load_optional_class($subclass)) {
46 bless $self, $subclass unless $self->isa($subclass);
51 # Set up the default quoting character and name seperators
52 $self->{_quoter} = $self->_build_quoter;
53 $self->{_namesep} = $self->_build_namesep;
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}});
68 my $dbh = $self->schema->storage->dbh;
69 return $dbh->get_info(29)
70 || $self->schema->storage->sql_maker->quote_char
76 my $dbh = $self->schema->storage->dbh;
77 return $dbh->get_info(41)
78 || $self->schema->storage->sql_maker->name_sep
82 # Override this in vendor modules to do things at the end of ->new()
85 # Override this in vendor module to load a subclass if necessary
88 # Returns an array of table names
90 my ($self, $opts) = (shift, shift);
92 my ($table, $type) = @_ ? @_ : ('%', '%');
94 my $dbh = $self->schema->storage->dbh;
95 my @tables = $dbh->tables(undef, $self->db_schema, $table, $type);
97 my $qt = qr/[\Q$self->{_quoter}\E"'`\[\]]/;
99 my $all_tables_quoted = (grep /$qt/, @tables) == @tables;
101 if ($self->{_quoter} && $all_tables_quoted) {
102 s/.* $qt (?= .* $qt)//xg for @tables;
104 s/^.*\Q$self->{_namesep}\E// for @tables;
106 s/$qt//g for @tables;
108 return $self->_filter_tables(\@tables, $opts);
111 # apply constraint/exclude and ignore bad tables and views
113 my ($self, $tables, $opts) = @_;
115 my @tables = @$tables;
119 my $constraint = $opts->{constraint};
120 my $exclude = $opts->{exclude};
122 @tables = grep { /$constraint/ } @$tables if defined $constraint;
123 @tables = grep { ! /$exclude/ } @$tables if defined $exclude;
125 for my $table (@tables) {
127 my $sth = $self->_sth_for($table, undef, \'1 = 0');
131 push @filtered_tables, $table;
134 warn "Bad table or view '$table', ignoring: $@\n";
135 $self->_unregister_source_for_table($table);
139 return @filtered_tables;
144 We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
151 local $self->schema->storage->dbh->{RaiseError} = 1;
152 local $self->schema->storage->dbh->{PrintError} = 0;
153 $self->next::method(@_);
157 my ($self, $table) = @_;
159 if($self->{db_schema}) {
160 $table = $self->{db_schema} . $self->{_namesep} .
161 $self->_quote_table_name($table);
163 $table = $self->_quote_table_name($table);
170 my ($self, $table, $fields, $where) = @_;
172 my $dbh = $self->schema->storage->dbh;
174 my $sth = $dbh->prepare($self->schema->storage->sql_maker
175 ->select(\$self->_table_as_sql($table), $fields, $where));
180 # Returns an arrayref of column names
182 my ($self, $table) = @_;
184 my $sth = $self->_sth_for($table, undef, \'1 = 0');
186 my $retval = $self->preserve_case ? \@{$sth->{NAME}} : \@{$sth->{NAME_lc}};
192 # Returns arrayref of pk col names
194 my ($self, $table) = @_;
196 my $dbh = $self->schema->storage->dbh;
198 my @primary = map { $self->_lc($_) } $dbh->primary_key('', $self->db_schema, $table);
199 s/\Q$self->{_quoter}\E//g for @primary;
204 # Override this for vendor-specific uniq info
205 sub _table_uniq_info {
206 my ($self, $table) = @_;
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";
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
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};
225 $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME};
230 foreach my $index_name (keys %indices) {
231 my $index = $indices{$index_name};
232 push(@retval, [ $index_name => [
243 my ($self, $table) = @_;
245 my $dbh = $self->schema->storage->dbh;
246 my $sth = $dbh->foreign_key_info( '', $self->db_schema, '',
247 '', $self->db_schema, $table );
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];
255 my $uk_col = $self->_lc($raw_rel->[3]);
256 my $fk_col = $self->_lc($raw_rel->[7]);
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;
263 $rels{$relid}->{cols}{$uk_col} = $fk_col;
268 foreach my $relid (keys %rels) {
270 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
271 local_columns => [ values %{$rels{$relid}->{cols}} ],
272 remote_table => $rels{$relid}->{tbl},
279 # ported in from DBIx::Class::Storage::DBI:
280 sub _columns_info_for {
281 my ($self, $table) = @_;
283 my $dbh = $self->schema->storage->dbh;
285 if ($dbh->can('column_info')) {
288 my $sth = eval { local $SIG{__WARN__} = sub {}; $dbh->column_info( undef, $self->db_schema, $table, '%' ); };
289 while ( my $info = $sth->fetchrow_hashref() ){
290 my $column_info = {};
291 $column_info->{data_type} = lc $info->{TYPE_NAME};
293 my $size = $info->{COLUMN_SIZE};
295 if (defined $size && defined $info->{DECIMAL_DIGITS}) {
296 $column_info->{size} = [$size, $info->{DECIMAL_DIGITS}];
298 elsif (defined $size) {
299 $column_info->{size} = $size;
302 $column_info->{is_nullable} = $info->{NULLABLE} ? 1 : 0;
303 $column_info->{default_value} = $info->{COLUMN_DEF} if defined $info->{COLUMN_DEF};
304 my $col_name = $info->{COLUMN_NAME};
305 $col_name =~ s/^\"(.*)\"$/$1/;
307 my $extra_info = $self->_extra_column_info(
308 $table, $col_name, $column_info, $info
310 $column_info = { %$column_info, %$extra_info };
312 $result{$col_name} = $column_info;
316 return \%result if !$@ && scalar keys %result;
320 my $sth = $self->_sth_for($table, undef, \'1 = 0');
322 my @columns = @{ $self->preserve_case ? $sth->{NAME} : $sth->{NAME_lc} };
323 for my $i ( 0 .. $#columns ){
324 my $column_info = {};
325 $column_info->{data_type} = lc $sth->{TYPE}->[$i];
327 my $size = $sth->{PRECISION}[$i];
329 if (defined $size && defined $sth->{SCALE}[$i]) {
330 $column_info->{size} = [$size, $sth->{SCALE}[$i]];
332 elsif (defined $size) {
333 $column_info->{size} = $size;
336 $column_info->{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
338 if ($column_info->{data_type} =~ m/^(.*?)\((.*?)\)$/) {
339 $column_info->{data_type} = $1;
340 $column_info->{size} = $2;
343 my $extra_info = $self->_extra_column_info($table, $columns[$i], $column_info) || {};
344 $column_info = { %$column_info, %$extra_info };
346 $result{$columns[$i]} = $column_info;
350 foreach my $col (keys %result) {
351 my $colinfo = $result{$col};
352 my $type_num = $colinfo->{data_type};
354 if(defined $type_num && $type_num =~ /^\d+\z/ && $dbh->can('type_info')) {
355 my $type_info = $dbh->type_info($type_num);
356 $type_name = $type_info->{TYPE_NAME} if $type_info;
357 $colinfo->{data_type} = lc $type_name if $type_name;
364 # Override this in vendor class to return any additional column
366 sub _extra_column_info {}
370 L<DBIx::Class::Schema::Loader>
374 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
378 This library is free software; you can redistribute it and/or modify it under
379 the same terms as Perl itself.
384 # vim:et sts=4 sw=4 tw=0: