Bump version for 0.04006 release
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI.pm
CommitLineData
996be9ee 1package DBIx::Class::Schema::Loader::DBI;
2
3use strict;
4use warnings;
abaf2c66 5use base qw/DBIx::Class::Schema::Loader::Base/;
996be9ee 6use Class::C3;
fa994d3c 7use Carp::Clan qw/^DBIx::Class/;
996be9ee 8use UNIVERSAL::require;
9
eb1de218 10our $VERSION = '0.04006';
32f784fc 11
996be9ee 12=head1 NAME
13
14DBIx::Class::Schema::Loader::DBI - DBIx::Class::Schema::Loader DBI Implementation.
15
16=head1 SYNOPSIS
17
18See L<DBIx::Class::Schema::Loader::Base>
19
20=head1 DESCRIPTION
21
22This is the base class for L<DBIx::Class::Schema::Loader::Base> classes for
23DBI-based storage backends, and implements the common functionality between them.
24
25See L<DBIx::Class::Schema::Loader::Base> for the available options.
26
27=head1 METHODS
28
29=head2 new
30
31Overlays L<DBIx::Class::Schema::Loader::Base/new> to do some DBI-specific
32things.
33
34=cut
35
36sub new {
37 my $self = shift->next::method(@_);
38
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;
fa1c4dc2 43
44 # must use $UNIVERSAL::require::ERROR, $@ is not safe. See RT #44444 --kane
996be9ee 45 $subclass->require;
fa1c4dc2 46 if($UNIVERSAL::require::ERROR &&
47 $UNIVERSAL::require::ERROR !~ /^Can't locate /
48 ) {
25328cc4 49 croak "Failed to require $subclass: $@";
996be9ee 50 }
51 elsif(!$@) {
52 bless $self, "DBIx::Class::Schema::Loader::DBI::${driver}";
53 }
54
55 # Set up the default quoting character and name seperators
56 $self->{_quoter} = $self->schema->storage->sql_maker->quote_char
57 || $dbh->get_info(29)
58 || q{"};
59
60 $self->{_namesep} = $self->schema->storage->sql_maker->name_sep
61 || $dbh->get_info(41)
62 || q{.};
63
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}});
68 }
69
70 $self->_setup;
71
72 $self;
73}
74
75# Override this in vendor modules to do things at the end of ->new()
76sub _setup { }
77
78# Returns an array of table names
79sub _tables_list {
80 my $self = shift;
81
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;
86
87 return @tables;
88}
89
12af3806 90=head2 load
91
92We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
93
94=cut
95
96sub load {
97 my $self = shift;
98
99 local $self->schema->storage->dbh->{RaiseError} = 1;
100 local $self->schema->storage->dbh->{PrintError} = 0;
101 $self->next::method(@_);
102}
103
996be9ee 104# Returns an arrayref of column names
105sub _table_columns {
106 my ($self, $table) = @_;
107
108 my $dbh = $self->schema->storage->dbh;
109
110 if($self->{db_schema}) {
111 $table = $self->{db_schema} . $self->{_namesep} . $table;
112 }
113
a0cc2498 114 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
996be9ee 115 $sth->execute;
3b7f80f9 116 my $retval = \@{$sth->{NAME_lc}};
117 $sth->finish;
118
119 $retval;
996be9ee 120}
121
122# Returns arrayref of pk col names
123sub _table_pk_info {
fd589700 124 my ($self, $table) = @_;
996be9ee 125
126 my $dbh = $self->schema->storage->dbh;
127
128 my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table);
129 s/\Q$self->{_quoter}\E//g for @primary;
130
131 return \@primary;
132}
133
fd589700 134# Override this for vendor-specific uniq info
996be9ee 135sub _table_uniq_info {
fd589700 136 my ($self, $table) = @_;
137
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";
141 return [];
142 }
143
144 my %indices;
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
148 # critical fields
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};
154
155 $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME};
156 }
3b7f80f9 157 $sth->finish;
fd589700 158
159 my @retval;
160 foreach my $index_name (keys %indices) {
161 my $index = $indices{$index_name};
162 push(@retval, [ $index_name => [
163 map { $index->{$_} }
164 sort keys %$index
165 ]]);
166 }
167
168 return \@retval;
996be9ee 169}
170
171# Find relationships
172sub _table_fk_info {
173 my ($self, $table) = @_;
174
175 my $dbh = $self->schema->storage->dbh;
a168c1c4 176 my $sth = $dbh->foreign_key_info( '', $self->db_schema, '',
177 '', $self->db_schema, $table );
996be9ee 178 return [] if !$sth;
179
180 my %rels;
181
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;
194 }
3b7f80f9 195 $sth->finish;
996be9ee 196
197 my @rels;
198 foreach my $relid (keys %rels) {
199 push(@rels, {
200 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
201 local_columns => [ values %{$rels{$relid}->{cols}} ],
202 remote_table => $rels{$relid}->{tbl},
203 });
204 }
205
206 return \@rels;
207}
208
12af3806 209# ported in from DBIx::Class::Storage::DBI:
210sub _columns_info_for {
211 my ($self, $table) = @_;
212
213 my $dbh = $self->schema->storage->dbh;
214
215 if ($dbh->can('column_info')) {
216 my %result;
217 eval {
218 my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' );
12af3806 219 while ( my $info = $sth->fetchrow_hashref() ){
220 my %column_info;
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/;
227
228 $result{$col_name} = \%column_info;
229 }
3b7f80f9 230 $sth->finish;
12af3806 231 };
232 return \%result if !$@ && scalar keys %result;
233 }
234
235 if($self->db_schema) {
236 $table = $self->db_schema . $self->{_namesep} . $table;
237 }
238 my %result;
6ce0bcc3 239 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
12af3806 240 $sth->execute;
241 my @columns = @{$sth->{NAME_lc}};
242 for my $i ( 0 .. $#columns ){
243 my %column_info;
6ce0bcc3 244 $column_info{data_type} = $sth->{TYPE}->[$i];
12af3806 245 $column_info{size} = $sth->{PRECISION}->[$i];
246 $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
247
248 if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) {
249 $column_info{data_type} = $1;
250 $column_info{size} = $2;
251 }
252
253 $result{$columns[$i]} = \%column_info;
254 }
6ce0bcc3 255 $sth->finish;
256
257 foreach my $col (keys %result) {
258 my $colinfo = $result{$col};
259 my $type_num = $colinfo->{data_type};
260 my $type_name;
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;
265 }
266 }
12af3806 267
268 return \%result;
269}
270
996be9ee 271=head1 SEE ALSO
272
273L<DBIx::Class::Schema::Loader>
274
275=cut
276
2771;