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_07';
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->ensure_class_found($subclass)) {
44 eval { $self->ensure_class_loaded($subclass) };
45 croak "Failed to load $subclass: $@" if $@;
46 bless $self, $subclass unless $self->isa($subclass);
50 # Set up the default quoting character and name seperators
51 $self->{_quoter} = $self->schema->storage->sql_maker->quote_char
55 $self->{_namesep} = $self->schema->storage->sql_maker->name_sep
59 # For our usage as regex matches, concatenating multiple quoter
60 # values works fine (e.g. s/\Q<>\E// if quoter was [ '<', '>' ])
61 if( ref $self->{_quoter} eq 'ARRAY') {
62 $self->{_quoter} = join(q{}, @{$self->{_quoter}});
70 # Override this in vendor modules to do things at the end of ->new()
73 # Override this in vendor module to load a subclass if necessary
76 # Returns an array of table names
80 my $dbh = $self->schema->storage->dbh;
81 my @tables = $dbh->tables(undef, $self->db_schema, '%', '%');
82 s/\Q$self->{_quoter}\E//g for @tables;
83 s/^.*\Q$self->{_namesep}\E// for @tables;
90 We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
97 local $self->schema->storage->dbh->{RaiseError} = 1;
98 local $self->schema->storage->dbh->{PrintError} = 0;
99 $self->next::method(@_);
102 # Returns an arrayref of column names
104 my ($self, $table) = @_;
106 my $dbh = $self->schema->storage->dbh;
108 if($self->{db_schema}) {
109 $table = $self->{db_schema} . $self->{_namesep} . $table;
112 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
114 my $retval = \@{$sth->{NAME_lc}};
120 # Returns arrayref of pk col names
122 my ($self, $table) = @_;
124 my $dbh = $self->schema->storage->dbh;
126 my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table);
127 s/\Q$self->{_quoter}\E//g for @primary;
132 # Override this for vendor-specific uniq info
133 sub _table_uniq_info {
134 my ($self, $table) = @_;
136 my $dbh = $self->schema->storage->dbh;
137 if(!$dbh->can('statistics_info')) {
138 warn "No UNIQUE constraint information can be gathered for this vendor";
143 my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1);
144 while(my $row = $sth->fetchrow_hashref) {
145 # skip table-level stats, conditional indexes, and any index missing
147 next if $row->{TYPE} eq 'table'
148 || defined $row->{FILTER_CONDITION}
149 || !$row->{INDEX_NAME}
150 || !defined $row->{ORDINAL_POSITION}
151 || !$row->{COLUMN_NAME};
153 $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME};
158 foreach my $index_name (keys %indices) {
159 my $index = $indices{$index_name};
160 push(@retval, [ $index_name => [
171 my ($self, $table) = @_;
173 my $dbh = $self->schema->storage->dbh;
174 my $sth = $dbh->foreign_key_info( '', $self->db_schema, '',
175 '', $self->db_schema, $table );
180 my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
181 while(my $raw_rel = $sth->fetchrow_arrayref) {
182 my $uk_tbl = $raw_rel->[2];
183 my $uk_col = lc $raw_rel->[3];
184 my $fk_col = lc $raw_rel->[7];
185 my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
186 $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
187 $uk_col =~ s/\Q$self->{_quoter}\E//g;
188 $fk_col =~ s/\Q$self->{_quoter}\E//g;
189 $relid =~ s/\Q$self->{_quoter}\E//g;
190 $rels{$relid}->{tbl} = $uk_tbl;
191 $rels{$relid}->{cols}->{$uk_col} = $fk_col;
196 foreach my $relid (keys %rels) {
198 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
199 local_columns => [ values %{$rels{$relid}->{cols}} ],
200 remote_table => $rels{$relid}->{tbl},
207 # ported in from DBIx::Class::Storage::DBI:
208 sub _columns_info_for {
209 my ($self, $table) = @_;
211 my $dbh = $self->schema->storage->dbh;
213 if ($dbh->can('column_info')) {
216 my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' );
217 while ( my $info = $sth->fetchrow_hashref() ){
219 $column_info{data_type} = $info->{TYPE_NAME};
220 $column_info{size} = $info->{COLUMN_SIZE};
221 $column_info{is_nullable} = $info->{NULLABLE} ? 1 : 0;
222 $column_info{default_value} = $info->{COLUMN_DEF};
223 my $col_name = $info->{COLUMN_NAME};
224 $col_name =~ s/^\"(.*)\"$/$1/;
226 my $extra_info = $self->_extra_column_info($info) || {};
228 $result{$col_name} = { %column_info, %$extra_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 my $extra_info = $self->_extra_column_info($table, $columns[$i], $sth, $i) || {};
255 $result{$columns[$i]} = { %column_info, %$extra_info };
259 foreach my $col (keys %result) {
260 my $colinfo = $result{$col};
261 my $type_num = $colinfo->{data_type};
263 if(defined $type_num && $dbh->can('type_info')) {
264 my $type_info = $dbh->type_info($type_num);
265 $type_name = $type_info->{TYPE_NAME} if $type_info;
266 $colinfo->{data_type} = $type_name if $type_name;
273 # Override this in vendor class to return any additional column
275 sub _extra_column_info {}
279 L<DBIx::Class::Schema::Loader>