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 | |
4295c4b4 |
11 | our $VERSION = '0.07010'; |
32f784fc |
12 | |
3b17d988 |
13 | __PACKAGE__->mk_group_accessors('simple', qw/ |
14 | _disable_pk_detection |
15 | _disable_uniq_detection |
16 | _disable_fk_detection |
17 | _passwords |
18 | /); |
19 | |
996be9ee |
20 | =head1 NAME |
21 | |
22 | DBIx::Class::Schema::Loader::DBI - DBIx::Class::Schema::Loader DBI Implementation. |
23 | |
24 | =head1 SYNOPSIS |
25 | |
26 | See L<DBIx::Class::Schema::Loader::Base> |
27 | |
28 | =head1 DESCRIPTION |
29 | |
30 | This is the base class for L<DBIx::Class::Schema::Loader::Base> classes for |
31 | DBI-based storage backends, and implements the common functionality between them. |
32 | |
33 | See L<DBIx::Class::Schema::Loader::Base> for the available options. |
34 | |
35 | =head1 METHODS |
36 | |
37 | =head2 new |
38 | |
39 | Overlays L<DBIx::Class::Schema::Loader::Base/new> to do some DBI-specific |
40 | things. |
41 | |
42 | =cut |
43 | |
44 | sub new { |
45 | my $self = shift->next::method(@_); |
46 | |
71a6e88a |
47 | # rebless to vendor-specific class if it exists and loads and we're not in a |
48 | # custom class. |
49 | if (not $self->loader_class) { |
50 | my $dbh = $self->schema->storage->dbh; |
51 | my $driver = $dbh->{Driver}->{Name}; |
52 | |
53 | my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver; |
54 | if ($self->load_optional_class($subclass)) { |
55 | bless $self, $subclass unless $self->isa($subclass); |
56 | $self->_rebless; |
57 | } |
996be9ee |
58 | } |
59 | |
60 | # Set up the default quoting character and name seperators |
243c6ebc |
61 | $self->{_quoter} = $self->_build_quoter; |
77d3753e |
62 | $self->{_namesep} = $self->_build_namesep; |
243c6ebc |
63 | |
996be9ee |
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 | |
77d3753e |
75 | sub _build_quoter { |
76 | my $self = shift; |
77 | my $dbh = $self->schema->storage->dbh; |
78 | return $dbh->get_info(29) |
79 | || $self->schema->storage->sql_maker->quote_char |
80 | || q{"}; |
81 | } |
82 | |
83 | sub _build_namesep { |
84 | my $self = shift; |
85 | my $dbh = $self->schema->storage->dbh; |
86 | return $dbh->get_info(41) |
87 | || $self->schema->storage->sql_maker->name_sep |
88 | || q{.}; |
89 | } |
90 | |
996be9ee |
91 | # Override this in vendor modules to do things at the end of ->new() |
92 | sub _setup { } |
93 | |
6ae3f335 |
94 | # Override this in vendor module to load a subclass if necessary |
95 | sub _rebless { } |
96 | |
996be9ee |
97 | # Returns an array of table names |
98 | sub _tables_list { |
bfb43060 |
99 | my ($self, $opts) = (shift, shift); |
996be9ee |
100 | |
e57fd726 |
101 | my ($table, $type) = @_ ? @_ : ('%', '%'); |
102 | |
996be9ee |
103 | my $dbh = $self->schema->storage->dbh; |
e57fd726 |
104 | my @tables = $dbh->tables(undef, $self->db_schema, $table, $type); |
77d3753e |
105 | |
c5ff1f26 |
106 | my $qt = qr/[\Q$self->{_quoter}\E"'`\[\]]/; |
385c593b |
107 | |
c5ff1f26 |
108 | my $all_tables_quoted = (grep /$qt/, @tables) == @tables; |
109 | |
110 | if ($self->{_quoter} && $all_tables_quoted) { |
12b86f07 |
111 | s/.* $qt (?= .* $qt\z)//xg for @tables; |
385c593b |
112 | } else { |
113 | s/^.*\Q$self->{_namesep}\E// for @tables; |
114 | } |
115 | s/$qt//g for @tables; |
996be9ee |
116 | |
bfb43060 |
117 | return $self->_filter_tables(\@tables, $opts); |
075aff97 |
118 | } |
119 | |
bfb43060 |
120 | # apply constraint/exclude and ignore bad tables and views |
075aff97 |
121 | sub _filter_tables { |
bfb43060 |
122 | my ($self, $tables, $opts) = @_; |
075aff97 |
123 | |
bfb43060 |
124 | my @tables = @$tables; |
075aff97 |
125 | my @filtered_tables; |
126 | |
bfb43060 |
127 | $opts ||= {}; |
128 | my $constraint = $opts->{constraint}; |
129 | my $exclude = $opts->{exclude}; |
130 | |
131 | @tables = grep { /$constraint/ } @$tables if defined $constraint; |
132 | @tables = grep { ! /$exclude/ } @$tables if defined $exclude; |
133 | |
b48f3f80 |
134 | LOOP: for my $table (@tables) { |
61d1cca1 |
135 | try { |
afcd3c32 |
136 | local $^W = 0; # for ADO |
805dbe0a |
137 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
138 | $sth->execute; |
075aff97 |
139 | } |
61d1cca1 |
140 | catch { |
141 | warn "Bad table or view '$table', ignoring: $_\n"; |
0c1d5b47 |
142 | $self->_unregister_source_for_table($table); |
b48f3f80 |
143 | no warnings 'exiting'; |
144 | next LOOP; |
61d1cca1 |
145 | }; |
146 | |
147 | push @filtered_tables, $table; |
075aff97 |
148 | } |
149 | |
150 | return @filtered_tables; |
996be9ee |
151 | } |
152 | |
12af3806 |
153 | =head2 load |
154 | |
155 | We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling. |
156 | |
157 | =cut |
158 | |
159 | sub load { |
160 | my $self = shift; |
161 | |
162 | local $self->schema->storage->dbh->{RaiseError} = 1; |
163 | local $self->schema->storage->dbh->{PrintError} = 0; |
164 | $self->next::method(@_); |
165 | } |
166 | |
075aff97 |
167 | sub _table_as_sql { |
996be9ee |
168 | my ($self, $table) = @_; |
169 | |
12b86f07 |
170 | my $sql_maker = $self->schema->storage->sql_maker; |
171 | my $name_sep = $sql_maker->name_sep; |
172 | my $db_schema = $self->db_schema; |
173 | |
174 | if($db_schema) { |
175 | return $self->_quote($self->{db_schema}) |
176 | . $name_sep |
177 | . $self->_quote($table); |
996be9ee |
178 | } |
179 | |
12b86f07 |
180 | return $self->_quote($table); |
075aff97 |
181 | } |
182 | |
183 | sub _sth_for { |
184 | my ($self, $table, $fields, $where) = @_; |
185 | |
186 | my $dbh = $self->schema->storage->dbh; |
187 | |
188 | my $sth = $dbh->prepare($self->schema->storage->sql_maker |
189 | ->select(\$self->_table_as_sql($table), $fields, $where)); |
190 | |
191 | return $sth; |
192 | } |
193 | |
194 | # Returns an arrayref of column names |
195 | sub _table_columns { |
196 | my ($self, $table) = @_; |
197 | |
198 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
996be9ee |
199 | $sth->execute; |
bc1cb85e |
200 | my $retval = $self->preserve_case ? \@{$sth->{NAME}} : \@{$sth->{NAME_lc}}; |
3b7f80f9 |
201 | $sth->finish; |
202 | |
203 | $retval; |
996be9ee |
204 | } |
205 | |
206 | # Returns arrayref of pk col names |
207 | sub _table_pk_info { |
fd589700 |
208 | my ($self, $table) = @_; |
996be9ee |
209 | |
3b17d988 |
210 | return [] if $self->_disable_pk_detection; |
211 | |
996be9ee |
212 | my $dbh = $self->schema->storage->dbh; |
213 | |
3b17d988 |
214 | my @primary = try { |
215 | $dbh->primary_key('', $self->db_schema, $table); |
216 | } |
217 | catch { |
218 | warn "Cannot find primary keys for this driver: $_"; |
219 | $self->_disable_pk_detection(1); |
220 | return (); |
221 | }; |
222 | |
223 | return [] if not @primary; |
224 | |
225 | @primary = map { $self->_lc($_) } @primary; |
996be9ee |
226 | s/\Q$self->{_quoter}\E//g for @primary; |
227 | |
228 | return \@primary; |
229 | } |
230 | |
fd589700 |
231 | # Override this for vendor-specific uniq info |
996be9ee |
232 | sub _table_uniq_info { |
fd589700 |
233 | my ($self, $table) = @_; |
234 | |
3b17d988 |
235 | return [] if $self->_disable_uniq_detection; |
236 | |
fd589700 |
237 | my $dbh = $self->schema->storage->dbh; |
3b17d988 |
238 | |
239 | if (not $dbh->can('statistics_info')) { |
240 | warn "No UNIQUE constraint information can be gathered for this driver"; |
241 | $self->_disable_uniq_detection(1); |
fd589700 |
242 | return []; |
243 | } |
244 | |
245 | my %indices; |
246 | my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1); |
247 | while(my $row = $sth->fetchrow_hashref) { |
248 | # skip table-level stats, conditional indexes, and any index missing |
249 | # critical fields |
250 | next if $row->{TYPE} eq 'table' |
251 | || defined $row->{FILTER_CONDITION} |
252 | || !$row->{INDEX_NAME} |
253 | || !defined $row->{ORDINAL_POSITION} |
254 | || !$row->{COLUMN_NAME}; |
255 | |
3b17d988 |
256 | $indices{$row->{INDEX_NAME}}[$row->{ORDINAL_POSITION}] = $self->_lc($row->{COLUMN_NAME}); |
fd589700 |
257 | } |
3b7f80f9 |
258 | $sth->finish; |
fd589700 |
259 | |
260 | my @retval; |
261 | foreach my $index_name (keys %indices) { |
262 | my $index = $indices{$index_name}; |
3b17d988 |
263 | push(@retval, [ $index_name => [ @$index[1..$#$index] ] ]); |
fd589700 |
264 | } |
265 | |
266 | return \@retval; |
996be9ee |
267 | } |
268 | |
5c06aa08 |
269 | sub _table_found { |
270 | my ( $self, $table ) = @_; |
271 | return grep {lc($_) eq lc($table)} $self->_tables_list({}); |
272 | } |
273 | |
274 | sub _table_found_cached { |
275 | my ( $self, $table ) = @_; |
276 | if (not exists ($self->{found_table}->{$table})) { |
277 | $self->{found_table}->{$table} = $self->_table_found($table); |
278 | } |
279 | return $self->{found_table}->{$table}; |
280 | } |
281 | |
282 | sub _table_columns_found { |
283 | my ( $self, $table, @columns ) = @_; |
284 | my %known_column = map {(lc($_)=>$_)} @{$self->_table_columns($table)}; |
285 | for my $column (@columns) { |
286 | if (not exists $known_column{lc($column)}) { |
287 | return(); |
288 | } |
289 | } |
290 | # In scalar context, whether or not all columns were found. |
291 | # In list context, all of the found columns. |
292 | return map $known_column{lc($_)}, @columns; |
293 | } |
294 | |
295 | sub _table_columns_found_cached { |
296 | my ( $self, $table, @columns ) = @_; |
297 | my $key = join chr(28), $table, @columns; |
298 | if (not exists $self->{found_table_columns}->{$key}) { |
299 | $self->{found_table_columns}->{$key} |
300 | = [$self->_table_columns_found($table, @columns)]; |
301 | } |
302 | return @{ $self->{found_table_columns}{$key} }; |
303 | } |
304 | |
305 | sub _table_comment { |
306 | my ( $self, $table ) = @_; |
307 | my $table_comments = $self->table_comments_table; |
308 | if ($self->_table_found_cached($table_comments) and |
309 | $self->_table_columns_found_cached( |
310 | $table_comments, 'table_name', 'comment_text') |
311 | ) { |
312 | my ($comment) = $self->schema->storage->dbh->selectrow_array( |
313 | qq{SELECT comment_text |
314 | FROM $table_comments |
315 | WHERE table_name = ? |
316 | }, undef, $table); |
317 | return $comment; |
318 | } |
319 | return undef; |
320 | } |
321 | |
322 | sub _column_comment { |
323 | my ( $self, $table, $column_counter, $column_name ) = @_; |
324 | my $column_comments = $self->column_comments_table; |
325 | if ($self->_table_found_cached($column_comments) and |
326 | $self->_table_columns_found_cached( |
327 | $column_comments, 'table_name', 'column_name', 'comment_text') |
328 | ) { |
329 | my ($comment) = $self->schema->storage->dbh->selectrow_array( |
330 | qq{SELECT comment_text |
331 | FROM $column_comments |
332 | WHERE table_name = ? |
333 | AND column_name = ? |
334 | }, undef, $table, $column_name); |
335 | return $comment; |
336 | } |
337 | return undef; |
338 | } |
339 | |
996be9ee |
340 | # Find relationships |
341 | sub _table_fk_info { |
342 | my ($self, $table) = @_; |
343 | |
3b17d988 |
344 | return [] if $self->_disable_fk_detection; |
345 | |
996be9ee |
346 | my $dbh = $self->schema->storage->dbh; |
3b17d988 |
347 | my $sth = try { |
348 | $dbh->foreign_key_info( '', $self->db_schema, '', |
349 | '', $self->db_schema, $table ); |
350 | } |
351 | catch { |
352 | warn "Cannot introspect relationships for this driver: $_"; |
353 | $self->_disable_fk_detection(1); |
354 | return undef; |
355 | }; |
356 | |
996be9ee |
357 | return [] if !$sth; |
358 | |
359 | my %rels; |
360 | |
361 | my $i = 1; # for unnamed rels, which hopefully have only 1 column ... |
362 | while(my $raw_rel = $sth->fetchrow_arrayref) { |
363 | my $uk_tbl = $raw_rel->[2]; |
c930f78b |
364 | my $uk_col = $self->_lc($raw_rel->[3]); |
365 | my $fk_col = $self->_lc($raw_rel->[7]); |
996be9ee |
366 | my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ )); |
367 | $uk_tbl =~ s/\Q$self->{_quoter}\E//g; |
368 | $uk_col =~ s/\Q$self->{_quoter}\E//g; |
369 | $fk_col =~ s/\Q$self->{_quoter}\E//g; |
370 | $relid =~ s/\Q$self->{_quoter}\E//g; |
371 | $rels{$relid}->{tbl} = $uk_tbl; |
c930f78b |
372 | $rels{$relid}->{cols}{$uk_col} = $fk_col; |
996be9ee |
373 | } |
3b7f80f9 |
374 | $sth->finish; |
996be9ee |
375 | |
376 | my @rels; |
377 | foreach my $relid (keys %rels) { |
378 | push(@rels, { |
379 | remote_columns => [ keys %{$rels{$relid}->{cols}} ], |
380 | local_columns => [ values %{$rels{$relid}->{cols}} ], |
381 | remote_table => $rels{$relid}->{tbl}, |
382 | }); |
383 | } |
384 | |
385 | return \@rels; |
386 | } |
387 | |
12af3806 |
388 | # ported in from DBIx::Class::Storage::DBI: |
389 | sub _columns_info_for { |
390 | my ($self, $table) = @_; |
391 | |
392 | my $dbh = $self->schema->storage->dbh; |
393 | |
61d1cca1 |
394 | my %result; |
395 | |
12af3806 |
396 | if ($dbh->can('column_info')) { |
61d1cca1 |
397 | my $sth = $self->_dbh_column_info($dbh, undef, $self->db_schema, $table, '%' ); |
398 | while ( my $info = $sth->fetchrow_hashref() ){ |
399 | my $column_info = {}; |
400 | $column_info->{data_type} = lc $info->{TYPE_NAME}; |
401 | |
402 | my $size = $info->{COLUMN_SIZE}; |
403 | |
404 | if (defined $size && defined $info->{DECIMAL_DIGITS}) { |
405 | $column_info->{size} = [$size, $info->{DECIMAL_DIGITS}]; |
12af3806 |
406 | } |
61d1cca1 |
407 | elsif (defined $size) { |
408 | $column_info->{size} = $size; |
409 | } |
410 | |
411 | $column_info->{is_nullable} = $info->{NULLABLE} ? 1 : 0; |
412 | $column_info->{default_value} = $info->{COLUMN_DEF} if defined $info->{COLUMN_DEF}; |
413 | my $col_name = $info->{COLUMN_NAME}; |
414 | $col_name =~ s/^\"(.*)\"$/$1/; |
415 | |
416 | $col_name = $self->_lc($col_name); |
c38ec663 |
417 | |
61d1cca1 |
418 | my $extra_info = $self->_extra_column_info( |
419 | $table, $col_name, $column_info, $info |
420 | ) || {}; |
421 | $column_info = { %$column_info, %$extra_info }; |
422 | |
423 | $result{$col_name} = $column_info; |
424 | } |
425 | $sth->finish; |
426 | |
427 | return \%result if %result; |
12af3806 |
428 | } |
429 | |
075aff97 |
430 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
12af3806 |
431 | $sth->execute; |
61d1cca1 |
432 | |
ed18888f |
433 | my @columns = @{ $sth->{NAME} }; |
61d1cca1 |
434 | |
435 | for my $i (0 .. $#columns) { |
23d1f36b |
436 | my $column_info = {}; |
57a9fc92 |
437 | $column_info->{data_type} = lc $sth->{TYPE}[$i]; |
26334ec1 |
438 | |
439 | my $size = $sth->{PRECISION}[$i]; |
440 | |
441 | if (defined $size && defined $sth->{SCALE}[$i]) { |
442 | $column_info->{size} = [$size, $sth->{SCALE}[$i]]; |
443 | } |
444 | elsif (defined $size) { |
445 | $column_info->{size} = $size; |
446 | } |
447 | |
57a9fc92 |
448 | $column_info->{is_nullable} = $sth->{NULLABLE}[$i] ? 1 : 0; |
23d1f36b |
449 | |
450 | if ($column_info->{data_type} =~ m/^(.*?)\((.*?)\)$/) { |
451 | $column_info->{data_type} = $1; |
452 | $column_info->{size} = $2; |
12af3806 |
453 | } |
454 | |
698c11d8 |
455 | my $extra_info = $self->_extra_column_info($table, $columns[$i], $column_info, $sth) || {}; |
23d1f36b |
456 | $column_info = { %$column_info, %$extra_info }; |
457 | |
61d1cca1 |
458 | $result{ $self->_lc($columns[$i]) } = $column_info; |
12af3806 |
459 | } |
6ce0bcc3 |
460 | $sth->finish; |
461 | |
462 | foreach my $col (keys %result) { |
463 | my $colinfo = $result{$col}; |
464 | my $type_num = $colinfo->{data_type}; |
465 | my $type_name; |
afcd3c32 |
466 | if (defined $type_num && $type_num =~ /^-?\d+\z/ && $dbh->can('type_info')) { |
5111e5d0 |
467 | my $type_name = $self->_dbh_type_info_type_name($type_num); |
cf0ba25b |
468 | $colinfo->{data_type} = lc $type_name if $type_name; |
6ce0bcc3 |
469 | } |
470 | } |
12af3806 |
471 | |
472 | return \%result; |
473 | } |
474 | |
23024b3f |
475 | # Need to override this for the buggy Firebird ODBC driver. |
5111e5d0 |
476 | sub _dbh_type_info_type_name { |
23024b3f |
477 | my ($self, $type_num) = @_; |
478 | |
479 | my $dbh = $self->schema->storage->dbh; |
480 | |
5111e5d0 |
481 | my $type_info = $dbh->type_info($type_num); |
482 | |
483 | return $type_info ? $type_info->{TYPE_NAME} : undef; |
23024b3f |
484 | } |
485 | |
db9c411a |
486 | # do not use this, override _columns_info_for instead |
a8df0345 |
487 | sub _extra_column_info {} |
c5baf131 |
488 | |
db9c411a |
489 | # override to mask warnings if needed (see mysql) |
490 | sub _dbh_column_info { |
491 | my ($self, $dbh) = (shift, shift); |
492 | |
493 | return $dbh->column_info(@_); |
494 | } |
495 | |
3b17d988 |
496 | # If a coderef uses DBI->connect, this should get its connect info. |
497 | sub _try_infer_connect_info_from_coderef { |
498 | my ($self, $code) = @_; |
499 | |
500 | my ($dsn, $user, $pass, $params); |
501 | |
502 | no warnings 'redefine'; |
503 | |
504 | local *DBI::connect = sub { |
505 | (undef, $dsn, $user, $pass, $params) = @_; |
506 | }; |
507 | |
508 | $code->(); |
509 | |
510 | return ($dsn, $user, $pass, $params); |
511 | } |
512 | |
996be9ee |
513 | =head1 SEE ALSO |
514 | |
515 | L<DBIx::Class::Schema::Loader> |
516 | |
be80bba7 |
517 | =head1 AUTHOR |
518 | |
9cc8e7e1 |
519 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
520 | |
521 | =head1 LICENSE |
522 | |
523 | This library is free software; you can redistribute it and/or modify it under |
524 | the same terms as Perl itself. |
525 | |
996be9ee |
526 | =cut |
527 | |
528 | 1; |
26334ec1 |
529 | # vim:et sts=4 sw=4 tw=0: |