1 package DBIx::Class::Schema::Loader::DBI;
5 use base qw/DBIx::Class::Schema::Loader::Base/;
7 use Carp::Clan qw/^DBIx::Class/;
8 use UNIVERSAL::require;
10 our $VERSION = '0.04006';
14 DBIx::Class::Schema::Loader::DBI - DBIx::Class::Schema::Loader DBI Implementation.
18 See L<DBIx::Class::Schema::Loader::Base>
22 This is the base class for L<DBIx::Class::Schema::Loader::Base> classes for
23 DBI-based storage backends, and implements the common functionality between them.
25 See L<DBIx::Class::Schema::Loader::Base> for the available options.
31 Overlays L<DBIx::Class::Schema::Loader::Base/new> to do some DBI-specific
37 my $self = shift->next::method(@_);
39 # rebless to vendor-specific class if it exists and loads
40 my $dbh = $self->schema->storage->dbh;
41 my $driver = $dbh->{Driver}->{Name};
42 my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver;
44 # must use $UNIVERSAL::require::ERROR, $@ is not safe. See RT #44444 --kane
46 if($UNIVERSAL::require::ERROR &&
47 $UNIVERSAL::require::ERROR !~ /^Can't locate /
49 croak "Failed to require $subclass: $@";
52 bless $self, "DBIx::Class::Schema::Loader::DBI::${driver}";
55 # Set up the default quoting character and name seperators
56 $self->{_quoter} = $self->schema->storage->sql_maker->quote_char
60 $self->{_namesep} = $self->schema->storage->sql_maker->name_sep
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}});
75 # Override this in vendor modules to do things at the end of ->new()
78 # Returns an array of table names
82 my $dbh = $self->schema->storage->dbh;
83 my @tables = $dbh->tables(undef, $self->db_schema, '%', '%');
84 s/\Q$self->{_quoter}\E//g for @tables;
85 s/^.*\Q$self->{_namesep}\E// for @tables;
92 We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
99 local $self->schema->storage->dbh->{RaiseError} = 1;
100 local $self->schema->storage->dbh->{PrintError} = 0;
101 $self->next::method(@_);
104 # Returns an arrayref of column names
106 my ($self, $table) = @_;
108 my $dbh = $self->schema->storage->dbh;
110 if($self->{db_schema}) {
111 $table = $self->{db_schema} . $self->{_namesep} . $table;
114 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
116 my $retval = \@{$sth->{NAME_lc}};
122 # Returns arrayref of pk col names
124 my ($self, $table) = @_;
126 my $dbh = $self->schema->storage->dbh;
128 my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table);
129 s/\Q$self->{_quoter}\E//g for @primary;
134 # Override this for vendor-specific uniq info
135 sub _table_uniq_info {
136 my ($self, $table) = @_;
138 my $dbh = $self->schema->storage->dbh;
139 if(!$dbh->can('statistics_info')) {
140 warn "No UNIQUE constraint information can be gathered for this vendor";
145 my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1);
146 while(my $row = $sth->fetchrow_hashref) {
147 # skip table-level stats, conditional indexes, and any index missing
149 next if $row->{TYPE} eq 'table'
150 || defined $row->{FILTER_CONDITION}
151 || !$row->{INDEX_NAME}
152 || !defined $row->{ORDINAL_POSITION}
153 || !$row->{COLUMN_NAME};
155 $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME};
160 foreach my $index_name (keys %indices) {
161 my $index = $indices{$index_name};
162 push(@retval, [ $index_name => [
173 my ($self, $table) = @_;
175 my $dbh = $self->schema->storage->dbh;
176 my $sth = $dbh->foreign_key_info( '', $self->db_schema, '',
177 '', $self->db_schema, $table );
182 my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
183 while(my $raw_rel = $sth->fetchrow_arrayref) {
184 my $uk_tbl = $raw_rel->[2];
185 my $uk_col = lc $raw_rel->[3];
186 my $fk_col = lc $raw_rel->[7];
187 my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
188 $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
189 $uk_col =~ s/\Q$self->{_quoter}\E//g;
190 $fk_col =~ s/\Q$self->{_quoter}\E//g;
191 $relid =~ s/\Q$self->{_quoter}\E//g;
192 $rels{$relid}->{tbl} = $uk_tbl;
193 $rels{$relid}->{cols}->{$uk_col} = $fk_col;
198 foreach my $relid (keys %rels) {
200 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
201 local_columns => [ values %{$rels{$relid}->{cols}} ],
202 remote_table => $rels{$relid}->{tbl},
209 # ported in from DBIx::Class::Storage::DBI:
210 sub _columns_info_for {
211 my ($self, $table) = @_;
213 my $dbh = $self->schema->storage->dbh;
215 if ($dbh->can('column_info')) {
218 my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' );
219 while ( my $info = $sth->fetchrow_hashref() ){
221 $column_info{data_type} = $info->{TYPE_NAME};
222 $column_info{size} = $info->{COLUMN_SIZE};
223 $column_info{is_nullable} = $info->{NULLABLE} ? 1 : 0;
224 $column_info{default_value} = $info->{COLUMN_DEF};
225 my $col_name = $info->{COLUMN_NAME};
226 $col_name =~ s/^\"(.*)\"$/$1/;
228 $result{$col_name} = \%column_info;
232 return \%result if !$@ && scalar keys %result;
235 if($self->db_schema) {
236 $table = $self->db_schema . $self->{_namesep} . $table;
239 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
241 my @columns = @{$sth->{NAME_lc}};
242 for my $i ( 0 .. $#columns ){
244 $column_info{data_type} = $sth->{TYPE}->[$i];
245 $column_info{size} = $sth->{PRECISION}->[$i];
246 $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
248 if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) {
249 $column_info{data_type} = $1;
250 $column_info{size} = $2;
253 $result{$columns[$i]} = \%column_info;
257 foreach my $col (keys %result) {
258 my $colinfo = $result{$col};
259 my $type_num = $colinfo->{data_type};
261 if(defined $type_num && $dbh->can('type_info')) {
262 my $type_info = $dbh->type_info($type_num);
263 $type_name = $type_info->{TYPE_NAME} if $type_info;
264 $colinfo->{data_type} = $type_name if $type_name;
273 L<DBIx::Class::Schema::Loader>