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.03999_01';
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 if($@ && $@ !~ /^Can't locate /) {
45 croak "Failed to require $subclass: $@";
48 bless $self, "DBIx::Class::Schema::Loader::DBI::${driver}";
51 # Set up the default quoting character and name seperators
52 $self->{_quoter} = $self->schema->storage->sql_maker->quote_char
56 $self->{_namesep} = $self->schema->storage->sql_maker->name_sep
60 # For our usage as regex matches, concatenating multiple quoter
61 # values works fine (e.g. s/\Q<>\E// if quoter was [ '<', '>' ])
62 if( ref $self->{_quoter} eq 'ARRAY') {
63 $self->{_quoter} = join(q{}, @{$self->{_quoter}});
71 # Override this in vendor modules to do things at the end of ->new()
74 # Returns an array of table names
78 my $dbh = $self->schema->storage->dbh;
79 my @tables = $dbh->tables(undef, $self->db_schema, '%', '%');
80 s/\Q$self->{_quoter}\E//g for @tables;
81 s/^.*\Q$self->{_namesep}\E// for @tables;
88 We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
95 local $self->schema->storage->dbh->{RaiseError} = 1;
96 local $self->schema->storage->dbh->{PrintError} = 0;
97 $self->next::method(@_);
100 # Returns an arrayref of column names
102 my ($self, $table) = @_;
104 my $dbh = $self->schema->storage->dbh;
106 if($self->{db_schema}) {
107 $table = $self->{db_schema} . $self->{_namesep} . $table;
110 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
112 return \@{$sth->{NAME_lc}};
115 # Returns arrayref of pk col names
117 my ($self, $table) = @_;
119 my $dbh = $self->schema->storage->dbh;
121 my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table);
122 s/\Q$self->{_quoter}\E//g for @primary;
127 # Override this for vendor-specific uniq info
128 sub _table_uniq_info {
129 my ($self, $table) = @_;
131 my $dbh = $self->schema->storage->dbh;
132 if(!$dbh->can('statistics_info')) {
133 warn "No UNIQUE constraint information can be gathered for this vendor";
138 my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1);
139 while(my $row = $sth->fetchrow_hashref) {
140 # skip table-level stats, conditional indexes, and any index missing
142 next if $row->{TYPE} eq 'table'
143 || defined $row->{FILTER_CONDITION}
144 || !$row->{INDEX_NAME}
145 || !defined $row->{ORDINAL_POSITION}
146 || !$row->{COLUMN_NAME};
148 $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME};
152 foreach my $index_name (keys %indices) {
153 my $index = $indices{$index_name};
154 push(@retval, [ $index_name => [
165 my ($self, $table) = @_;
167 my $dbh = $self->schema->storage->dbh;
168 my $sth = $dbh->foreign_key_info( '', '', '', '',
169 $self->db_schema, $table );
174 my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
175 while(my $raw_rel = $sth->fetchrow_arrayref) {
176 my $uk_tbl = $raw_rel->[2];
177 my $uk_col = lc $raw_rel->[3];
178 my $fk_col = lc $raw_rel->[7];
179 my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
180 $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
181 $uk_col =~ s/\Q$self->{_quoter}\E//g;
182 $fk_col =~ s/\Q$self->{_quoter}\E//g;
183 $relid =~ s/\Q$self->{_quoter}\E//g;
184 $rels{$relid}->{tbl} = $uk_tbl;
185 $rels{$relid}->{cols}->{$uk_col} = $fk_col;
189 foreach my $relid (keys %rels) {
191 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
192 local_columns => [ values %{$rels{$relid}->{cols}} ],
193 remote_table => $rels{$relid}->{tbl},
200 # ported in from DBIx::Class::Storage::DBI:
201 sub _columns_info_for {
202 my ($self, $table) = @_;
204 my $dbh = $self->schema->storage->dbh;
206 if ($dbh->can('column_info')) {
209 my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' );
211 while ( my $info = $sth->fetchrow_hashref() ){
213 $column_info{data_type} = $info->{TYPE_NAME};
214 $column_info{size} = $info->{COLUMN_SIZE};
215 $column_info{is_nullable} = $info->{NULLABLE} ? 1 : 0;
216 $column_info{default_value} = $info->{COLUMN_DEF};
217 my $col_name = $info->{COLUMN_NAME};
218 $col_name =~ s/^\"(.*)\"$/$1/;
220 $result{$col_name} = \%column_info;
223 return \%result if !$@ && scalar keys %result;
226 if($self->db_schema) {
227 $table = $self->db_schema . $self->{_namesep} . $table;
230 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
232 my @columns = @{$sth->{NAME_lc}};
233 for my $i ( 0 .. $#columns ){
235 $column_info{data_type} = $sth->{TYPE}->[$i];
236 $column_info{size} = $sth->{PRECISION}->[$i];
237 $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
239 if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) {
240 $column_info{data_type} = $1;
241 $column_info{size} = $2;
244 $result{$columns[$i]} = \%column_info;
248 foreach my $col (keys %result) {
249 my $colinfo = $result{$col};
250 my $type_num = $colinfo->{data_type};
252 if(defined $type_num && $dbh->can('type_info')) {
253 my $type_info = $dbh->type_info($type_num);
254 $type_name = $type_info->{TYPE_NAME} if $type_info;
255 $colinfo->{data_type} = $type_name if $type_name;
264 L<DBIx::Class::Schema::Loader>