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.04999_12';
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 if ($self->{_quoter} && $tables[0] =~ /$qt/) {
96 s/.* $qt (?= .* $qt)//xg for @tables;
98 s/^.*\Q$self->{_namesep}\E// for @tables;
100 s/$qt//g for @tables;
107 We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
114 local $self->schema->storage->dbh->{RaiseError} = 1;
115 local $self->schema->storage->dbh->{PrintError} = 0;
116 $self->next::method(@_);
119 # Returns an arrayref of column names
121 my ($self, $table) = @_;
123 my $dbh = $self->schema->storage->dbh;
125 if($self->{db_schema}) {
126 $table = $self->{db_schema} . $self->{_namesep} .
127 $self->_quote_table_name($table);
129 $table = $self->_quote_table_name($table);
132 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select(\$table, undef, \'1 = 0'));
134 my $retval = \@{$sth->{NAME_lc}};
140 # Returns arrayref of pk col names
142 my ($self, $table) = @_;
144 my $dbh = $self->schema->storage->dbh;
146 my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table);
147 s/\Q$self->{_quoter}\E//g for @primary;
152 # Override this for vendor-specific uniq info
153 sub _table_uniq_info {
154 my ($self, $table) = @_;
156 my $dbh = $self->schema->storage->dbh;
157 if(!$dbh->can('statistics_info')) {
158 warn "No UNIQUE constraint information can be gathered for this vendor";
163 my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1);
164 while(my $row = $sth->fetchrow_hashref) {
165 # skip table-level stats, conditional indexes, and any index missing
167 next if $row->{TYPE} eq 'table'
168 || defined $row->{FILTER_CONDITION}
169 || !$row->{INDEX_NAME}
170 || !defined $row->{ORDINAL_POSITION}
171 || !$row->{COLUMN_NAME};
173 $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME};
178 foreach my $index_name (keys %indices) {
179 my $index = $indices{$index_name};
180 push(@retval, [ $index_name => [
191 my ($self, $table) = @_;
193 my $dbh = $self->schema->storage->dbh;
194 my $sth = $dbh->foreign_key_info( '', $self->db_schema, '',
195 '', $self->db_schema, $table );
200 my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
201 while(my $raw_rel = $sth->fetchrow_arrayref) {
202 my $uk_tbl = $raw_rel->[2];
203 my $uk_col = lc $raw_rel->[3];
204 my $fk_col = lc $raw_rel->[7];
205 my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
206 $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
207 $uk_col =~ s/\Q$self->{_quoter}\E//g;
208 $fk_col =~ s/\Q$self->{_quoter}\E//g;
209 $relid =~ s/\Q$self->{_quoter}\E//g;
210 $rels{$relid}->{tbl} = $uk_tbl;
211 $rels{$relid}->{cols}->{$uk_col} = $fk_col;
216 foreach my $relid (keys %rels) {
218 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
219 local_columns => [ values %{$rels{$relid}->{cols}} ],
220 remote_table => $rels{$relid}->{tbl},
227 # ported in from DBIx::Class::Storage::DBI:
228 sub _columns_info_for {
229 my ($self, $table) = @_;
231 my $dbh = $self->schema->storage->dbh;
233 if ($dbh->can('column_info')) {
236 my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' );
237 while ( my $info = $sth->fetchrow_hashref() ){
239 $column_info{data_type} = $info->{TYPE_NAME};
240 $column_info{size} = $info->{COLUMN_SIZE};
241 $column_info{is_nullable} = $info->{NULLABLE} ? 1 : 0;
242 $column_info{default_value} = $info->{COLUMN_DEF};
243 my $col_name = $info->{COLUMN_NAME};
244 $col_name =~ s/^\"(.*)\"$/$1/;
246 my $extra_info = $self->_extra_column_info($info) || {};
248 $result{$col_name} = { %column_info, %$extra_info };
252 return \%result if !$@ && scalar keys %result;
255 if($self->db_schema) {
256 $table = $self->db_schema . $self->{_namesep} . $table;
259 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
261 my @columns = @{$sth->{NAME_lc}};
262 for my $i ( 0 .. $#columns ){
264 $column_info{data_type} = $sth->{TYPE}->[$i];
265 $column_info{size} = $sth->{PRECISION}->[$i];
266 $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
268 if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) {
269 $column_info{data_type} = $1;
270 $column_info{size} = $2;
273 my $extra_info = $self->_extra_column_info($table, $columns[$i], $sth, $i) || {};
275 $result{$columns[$i]} = { %column_info, %$extra_info };
279 foreach my $col (keys %result) {
280 my $colinfo = $result{$col};
281 my $type_num = $colinfo->{data_type};
283 if(defined $type_num && $dbh->can('type_info')) {
284 my $type_info = $dbh->type_info($type_num);
285 $type_name = $type_info->{TYPE_NAME} if $type_info;
286 $colinfo->{data_type} = $type_name if $type_name;
293 # Override this in vendor class to return any additional column
295 sub _extra_column_info {}
299 L<DBIx::Class::Schema::Loader>
303 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
307 This library is free software; you can redistribute it and/or modify it under
308 the same terms as Perl itself.