Commit | Line | Data |
996be9ee |
1 | package DBIx::Class::Schema::Loader::DBI; |
2 | |
3 | use strict; |
4 | use warnings; |
abaf2c66 |
5 | use base qw/DBIx::Class::Schema::Loader::Base/; |
942bd5e0 |
6 | use mro 'c3'; |
fa994d3c |
7 | use Carp::Clan qw/^DBIx::Class/; |
61d1cca1 |
8 | use Try::Tiny; |
9 | use namespace::clean; |
996be9ee |
10 | |
3b71e53b |
11 | our $VERSION = '0.07007'; |
32f784fc |
12 | |
996be9ee |
13 | =head1 NAME |
14 | |
15 | DBIx::Class::Schema::Loader::DBI - DBIx::Class::Schema::Loader DBI Implementation. |
16 | |
17 | =head1 SYNOPSIS |
18 | |
19 | See L<DBIx::Class::Schema::Loader::Base> |
20 | |
21 | =head1 DESCRIPTION |
22 | |
23 | This is the base class for L<DBIx::Class::Schema::Loader::Base> classes for |
24 | DBI-based storage backends, and implements the common functionality between them. |
25 | |
26 | See L<DBIx::Class::Schema::Loader::Base> for the available options. |
27 | |
28 | =head1 METHODS |
29 | |
30 | =head2 new |
31 | |
32 | Overlays L<DBIx::Class::Schema::Loader::Base/new> to do some DBI-specific |
33 | things. |
34 | |
35 | =cut |
36 | |
37 | sub new { |
38 | my $self = shift->next::method(@_); |
39 | |
71a6e88a |
40 | # rebless to vendor-specific class if it exists and loads and we're not in a |
41 | # custom class. |
42 | if (not $self->loader_class) { |
43 | my $dbh = $self->schema->storage->dbh; |
44 | my $driver = $dbh->{Driver}->{Name}; |
45 | |
46 | my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver; |
47 | if ($self->load_optional_class($subclass)) { |
48 | bless $self, $subclass unless $self->isa($subclass); |
49 | $self->_rebless; |
50 | } |
996be9ee |
51 | } |
52 | |
53 | # Set up the default quoting character and name seperators |
243c6ebc |
54 | $self->{_quoter} = $self->_build_quoter; |
77d3753e |
55 | $self->{_namesep} = $self->_build_namesep; |
243c6ebc |
56 | |
996be9ee |
57 | # For our usage as regex matches, concatenating multiple quoter |
58 | # values works fine (e.g. s/\Q<>\E// if quoter was [ '<', '>' ]) |
59 | if( ref $self->{_quoter} eq 'ARRAY') { |
60 | $self->{_quoter} = join(q{}, @{$self->{_quoter}}); |
61 | } |
62 | |
63 | $self->_setup; |
64 | |
65 | $self; |
66 | } |
67 | |
77d3753e |
68 | sub _build_quoter { |
69 | my $self = shift; |
70 | my $dbh = $self->schema->storage->dbh; |
71 | return $dbh->get_info(29) |
72 | || $self->schema->storage->sql_maker->quote_char |
73 | || q{"}; |
74 | } |
75 | |
76 | sub _build_namesep { |
77 | my $self = shift; |
78 | my $dbh = $self->schema->storage->dbh; |
79 | return $dbh->get_info(41) |
80 | || $self->schema->storage->sql_maker->name_sep |
81 | || q{.}; |
82 | } |
83 | |
996be9ee |
84 | # Override this in vendor modules to do things at the end of ->new() |
85 | sub _setup { } |
86 | |
6ae3f335 |
87 | # Override this in vendor module to load a subclass if necessary |
88 | sub _rebless { } |
89 | |
996be9ee |
90 | # Returns an array of table names |
91 | sub _tables_list { |
bfb43060 |
92 | my ($self, $opts) = (shift, shift); |
996be9ee |
93 | |
e57fd726 |
94 | my ($table, $type) = @_ ? @_ : ('%', '%'); |
95 | |
996be9ee |
96 | my $dbh = $self->schema->storage->dbh; |
e57fd726 |
97 | my @tables = $dbh->tables(undef, $self->db_schema, $table, $type); |
77d3753e |
98 | |
c5ff1f26 |
99 | my $qt = qr/[\Q$self->{_quoter}\E"'`\[\]]/; |
385c593b |
100 | |
c5ff1f26 |
101 | my $all_tables_quoted = (grep /$qt/, @tables) == @tables; |
102 | |
103 | if ($self->{_quoter} && $all_tables_quoted) { |
385c593b |
104 | s/.* $qt (?= .* $qt)//xg for @tables; |
105 | } else { |
106 | s/^.*\Q$self->{_namesep}\E// for @tables; |
107 | } |
108 | s/$qt//g for @tables; |
996be9ee |
109 | |
bfb43060 |
110 | return $self->_filter_tables(\@tables, $opts); |
075aff97 |
111 | } |
112 | |
bfb43060 |
113 | # apply constraint/exclude and ignore bad tables and views |
075aff97 |
114 | sub _filter_tables { |
bfb43060 |
115 | my ($self, $tables, $opts) = @_; |
075aff97 |
116 | |
bfb43060 |
117 | my @tables = @$tables; |
075aff97 |
118 | my @filtered_tables; |
119 | |
bfb43060 |
120 | $opts ||= {}; |
121 | my $constraint = $opts->{constraint}; |
122 | my $exclude = $opts->{exclude}; |
123 | |
124 | @tables = grep { /$constraint/ } @$tables if defined $constraint; |
125 | @tables = grep { ! /$exclude/ } @$tables if defined $exclude; |
126 | |
b48f3f80 |
127 | LOOP: for my $table (@tables) { |
61d1cca1 |
128 | try { |
805dbe0a |
129 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
130 | $sth->execute; |
075aff97 |
131 | } |
61d1cca1 |
132 | catch { |
133 | warn "Bad table or view '$table', ignoring: $_\n"; |
0c1d5b47 |
134 | $self->_unregister_source_for_table($table); |
b48f3f80 |
135 | no warnings 'exiting'; |
136 | next LOOP; |
61d1cca1 |
137 | }; |
138 | |
139 | push @filtered_tables, $table; |
075aff97 |
140 | } |
141 | |
142 | return @filtered_tables; |
996be9ee |
143 | } |
144 | |
12af3806 |
145 | =head2 load |
146 | |
147 | We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling. |
148 | |
149 | =cut |
150 | |
151 | sub load { |
152 | my $self = shift; |
153 | |
154 | local $self->schema->storage->dbh->{RaiseError} = 1; |
155 | local $self->schema->storage->dbh->{PrintError} = 0; |
156 | $self->next::method(@_); |
157 | } |
158 | |
075aff97 |
159 | sub _table_as_sql { |
996be9ee |
160 | my ($self, $table) = @_; |
161 | |
996be9ee |
162 | if($self->{db_schema}) { |
385c593b |
163 | $table = $self->{db_schema} . $self->{_namesep} . |
164 | $self->_quote_table_name($table); |
165 | } else { |
166 | $table = $self->_quote_table_name($table); |
996be9ee |
167 | } |
168 | |
075aff97 |
169 | return $table; |
170 | } |
171 | |
172 | sub _sth_for { |
173 | my ($self, $table, $fields, $where) = @_; |
174 | |
175 | my $dbh = $self->schema->storage->dbh; |
176 | |
177 | my $sth = $dbh->prepare($self->schema->storage->sql_maker |
178 | ->select(\$self->_table_as_sql($table), $fields, $where)); |
179 | |
180 | return $sth; |
181 | } |
182 | |
183 | # Returns an arrayref of column names |
184 | sub _table_columns { |
185 | my ($self, $table) = @_; |
186 | |
187 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
996be9ee |
188 | $sth->execute; |
bc1cb85e |
189 | my $retval = $self->preserve_case ? \@{$sth->{NAME}} : \@{$sth->{NAME_lc}}; |
3b7f80f9 |
190 | $sth->finish; |
191 | |
192 | $retval; |
996be9ee |
193 | } |
194 | |
195 | # Returns arrayref of pk col names |
196 | sub _table_pk_info { |
fd589700 |
197 | my ($self, $table) = @_; |
996be9ee |
198 | |
199 | my $dbh = $self->schema->storage->dbh; |
200 | |
eb040f78 |
201 | my @primary = map { $self->_lc($_) } $dbh->primary_key('', $self->db_schema, $table); |
996be9ee |
202 | s/\Q$self->{_quoter}\E//g for @primary; |
203 | |
204 | return \@primary; |
205 | } |
206 | |
fd589700 |
207 | # Override this for vendor-specific uniq info |
996be9ee |
208 | sub _table_uniq_info { |
fd589700 |
209 | my ($self, $table) = @_; |
210 | |
211 | my $dbh = $self->schema->storage->dbh; |
212 | if(!$dbh->can('statistics_info')) { |
213 | warn "No UNIQUE constraint information can be gathered for this vendor"; |
214 | return []; |
215 | } |
216 | |
217 | my %indices; |
218 | my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1); |
219 | while(my $row = $sth->fetchrow_hashref) { |
220 | # skip table-level stats, conditional indexes, and any index missing |
221 | # critical fields |
222 | next if $row->{TYPE} eq 'table' |
223 | || defined $row->{FILTER_CONDITION} |
224 | || !$row->{INDEX_NAME} |
225 | || !defined $row->{ORDINAL_POSITION} |
226 | || !$row->{COLUMN_NAME}; |
227 | |
228 | $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME}; |
229 | } |
3b7f80f9 |
230 | $sth->finish; |
fd589700 |
231 | |
232 | my @retval; |
233 | foreach my $index_name (keys %indices) { |
234 | my $index = $indices{$index_name}; |
235 | push(@retval, [ $index_name => [ |
236 | map { $index->{$_} } |
237 | sort keys %$index |
238 | ]]); |
239 | } |
240 | |
241 | return \@retval; |
996be9ee |
242 | } |
243 | |
244 | # Find relationships |
245 | sub _table_fk_info { |
246 | my ($self, $table) = @_; |
247 | |
248 | my $dbh = $self->schema->storage->dbh; |
a168c1c4 |
249 | my $sth = $dbh->foreign_key_info( '', $self->db_schema, '', |
250 | '', $self->db_schema, $table ); |
996be9ee |
251 | return [] if !$sth; |
252 | |
253 | my %rels; |
254 | |
255 | my $i = 1; # for unnamed rels, which hopefully have only 1 column ... |
256 | while(my $raw_rel = $sth->fetchrow_arrayref) { |
257 | my $uk_tbl = $raw_rel->[2]; |
c930f78b |
258 | my $uk_col = $self->_lc($raw_rel->[3]); |
259 | my $fk_col = $self->_lc($raw_rel->[7]); |
996be9ee |
260 | my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ )); |
261 | $uk_tbl =~ s/\Q$self->{_quoter}\E//g; |
262 | $uk_col =~ s/\Q$self->{_quoter}\E//g; |
263 | $fk_col =~ s/\Q$self->{_quoter}\E//g; |
264 | $relid =~ s/\Q$self->{_quoter}\E//g; |
265 | $rels{$relid}->{tbl} = $uk_tbl; |
c930f78b |
266 | $rels{$relid}->{cols}{$uk_col} = $fk_col; |
996be9ee |
267 | } |
3b7f80f9 |
268 | $sth->finish; |
996be9ee |
269 | |
270 | my @rels; |
271 | foreach my $relid (keys %rels) { |
272 | push(@rels, { |
273 | remote_columns => [ keys %{$rels{$relid}->{cols}} ], |
274 | local_columns => [ values %{$rels{$relid}->{cols}} ], |
275 | remote_table => $rels{$relid}->{tbl}, |
276 | }); |
277 | } |
278 | |
279 | return \@rels; |
280 | } |
281 | |
12af3806 |
282 | # ported in from DBIx::Class::Storage::DBI: |
283 | sub _columns_info_for { |
284 | my ($self, $table) = @_; |
285 | |
286 | my $dbh = $self->schema->storage->dbh; |
287 | |
61d1cca1 |
288 | my %result; |
289 | |
12af3806 |
290 | if ($dbh->can('column_info')) { |
61d1cca1 |
291 | my $sth = $self->_dbh_column_info($dbh, undef, $self->db_schema, $table, '%' ); |
292 | while ( my $info = $sth->fetchrow_hashref() ){ |
293 | my $column_info = {}; |
294 | $column_info->{data_type} = lc $info->{TYPE_NAME}; |
295 | |
296 | my $size = $info->{COLUMN_SIZE}; |
297 | |
298 | if (defined $size && defined $info->{DECIMAL_DIGITS}) { |
299 | $column_info->{size} = [$size, $info->{DECIMAL_DIGITS}]; |
12af3806 |
300 | } |
61d1cca1 |
301 | elsif (defined $size) { |
302 | $column_info->{size} = $size; |
303 | } |
304 | |
305 | $column_info->{is_nullable} = $info->{NULLABLE} ? 1 : 0; |
306 | $column_info->{default_value} = $info->{COLUMN_DEF} if defined $info->{COLUMN_DEF}; |
307 | my $col_name = $info->{COLUMN_NAME}; |
308 | $col_name =~ s/^\"(.*)\"$/$1/; |
309 | |
310 | $col_name = $self->_lc($col_name); |
c38ec663 |
311 | |
61d1cca1 |
312 | my $extra_info = $self->_extra_column_info( |
313 | $table, $col_name, $column_info, $info |
314 | ) || {}; |
315 | $column_info = { %$column_info, %$extra_info }; |
316 | |
317 | $result{$col_name} = $column_info; |
318 | } |
319 | $sth->finish; |
320 | |
321 | return \%result if %result; |
12af3806 |
322 | } |
323 | |
075aff97 |
324 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
12af3806 |
325 | $sth->execute; |
61d1cca1 |
326 | |
ed18888f |
327 | my @columns = @{ $sth->{NAME} }; |
61d1cca1 |
328 | |
329 | for my $i (0 .. $#columns) { |
23d1f36b |
330 | my $column_info = {}; |
57a9fc92 |
331 | $column_info->{data_type} = lc $sth->{TYPE}[$i]; |
26334ec1 |
332 | |
333 | my $size = $sth->{PRECISION}[$i]; |
334 | |
335 | if (defined $size && defined $sth->{SCALE}[$i]) { |
336 | $column_info->{size} = [$size, $sth->{SCALE}[$i]]; |
337 | } |
338 | elsif (defined $size) { |
339 | $column_info->{size} = $size; |
340 | } |
341 | |
57a9fc92 |
342 | $column_info->{is_nullable} = $sth->{NULLABLE}[$i] ? 1 : 0; |
23d1f36b |
343 | |
344 | if ($column_info->{data_type} =~ m/^(.*?)\((.*?)\)$/) { |
345 | $column_info->{data_type} = $1; |
346 | $column_info->{size} = $2; |
12af3806 |
347 | } |
348 | |
45be2ce7 |
349 | my $extra_info = $self->_extra_column_info($table, $columns[$i], $column_info) || {}; |
23d1f36b |
350 | $column_info = { %$column_info, %$extra_info }; |
351 | |
61d1cca1 |
352 | $result{ $self->_lc($columns[$i]) } = $column_info; |
12af3806 |
353 | } |
6ce0bcc3 |
354 | $sth->finish; |
355 | |
356 | foreach my $col (keys %result) { |
357 | my $colinfo = $result{$col}; |
358 | my $type_num = $colinfo->{data_type}; |
359 | my $type_name; |
61d1cca1 |
360 | if (defined $type_num && $type_num =~ /^\d+\z/ && $dbh->can('type_info')) { |
6ce0bcc3 |
361 | my $type_info = $dbh->type_info($type_num); |
362 | $type_name = $type_info->{TYPE_NAME} if $type_info; |
cf0ba25b |
363 | $colinfo->{data_type} = lc $type_name if $type_name; |
6ce0bcc3 |
364 | } |
365 | } |
12af3806 |
366 | |
367 | return \%result; |
368 | } |
369 | |
db9c411a |
370 | # do not use this, override _columns_info_for instead |
a8df0345 |
371 | sub _extra_column_info {} |
c5baf131 |
372 | |
db9c411a |
373 | # override to mask warnings if needed (see mysql) |
374 | sub _dbh_column_info { |
375 | my ($self, $dbh) = (shift, shift); |
376 | |
377 | return $dbh->column_info(@_); |
378 | } |
379 | |
996be9ee |
380 | =head1 SEE ALSO |
381 | |
382 | L<DBIx::Class::Schema::Loader> |
383 | |
be80bba7 |
384 | =head1 AUTHOR |
385 | |
9cc8e7e1 |
386 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
387 | |
388 | =head1 LICENSE |
389 | |
390 | This library is free software; you can redistribute it and/or modify it under |
391 | the same terms as Perl itself. |
392 | |
996be9ee |
393 | =cut |
394 | |
395 | 1; |
26334ec1 |
396 | # vim:et sts=4 sw=4 tw=0: |