release 0.07028
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI.pm
1 package DBIx::Class::Schema::Loader::DBI;
2
3 use strict;
4 use warnings;
5 use base qw/DBIx::Class::Schema::Loader::Base/;
6 use mro 'c3';
7 use Try::Tiny;
8 use List::MoreUtils 'any';
9 use Carp::Clan qw/^DBIx::Class/;
10 use namespace::clean;
11 use DBIx::Class::Schema::Loader::Table ();
12
13 our $VERSION = '0.07028';
14
15 __PACKAGE__->mk_group_accessors('simple', qw/
16     _disable_pk_detection
17     _disable_uniq_detection
18     _disable_fk_detection
19     _passwords
20     quote_char
21     name_sep
22 /);
23
24 =head1 NAME
25
26 DBIx::Class::Schema::Loader::DBI - DBIx::Class::Schema::Loader DBI Implementation.
27
28 =head1 SYNOPSIS
29
30 See L<DBIx::Class::Schema::Loader::Base>
31
32 =head1 DESCRIPTION
33
34 This is the base class for L<DBIx::Class::Schema::Loader::Base> classes for
35 DBI-based storage backends, and implements the common functionality between them.
36
37 See L<DBIx::Class::Schema::Loader::Base> for the available options.
38
39 =head1 METHODS
40
41 =head2 new
42
43 Overlays L<DBIx::Class::Schema::Loader::Base/new> to do some DBI-specific
44 things.
45
46 =cut
47
48 sub new {
49     my $self = shift->next::method(@_);
50
51     # rebless to vendor-specific class if it exists and loads and we're not in a
52     # custom class.
53     if (not $self->loader_class) {
54         my $driver = $self->dbh->{Driver}->{Name};
55
56         my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver;
57         if ((not $self->isa($subclass)) && $self->load_optional_class($subclass)) {
58             bless $self, $subclass;
59             $self->_rebless;
60             Class::C3::reinitialize() if $] < 5.009005;
61         }
62     }
63
64     # Set up the default quoting character and name seperators
65     $self->quote_char($self->_build_quote_char);
66     $self->name_sep($self->_build_name_sep);
67
68     $self->_setup;
69
70     return $self;
71 }
72
73 sub _build_quote_char {
74     my $self = shift;
75
76     my $quote_char = $self->dbh->get_info(29)
77            || $self->schema->storage->sql_maker->quote_char
78            || q{"};
79
80     # For our usage as regex matches, concatenating multiple quote_char
81     # values works fine (e.g. s/[\Q<>\E]// if quote_char was [ '<', '>' ])
82     if (ref $quote_char eq 'ARRAY') {
83         $quote_char = join '', @$quote_char;
84     }
85
86     return $quote_char;
87 }
88
89 sub _build_name_sep {
90     my $self = shift;
91     return $self->dbh->get_info(41)
92            || $self->schema->storage->sql_maker->name_sep
93            || '.';
94 }
95
96 # Override this in vendor modules to do things at the end of ->new()
97 sub _setup { }
98
99 # Override this in vendor module to load a subclass if necessary
100 sub _rebless { }
101
102 sub _system_schemas {
103     return ('information_schema');
104 }
105
106 sub _system_tables {
107     return ();
108 }
109
110 sub _dbh_tables {
111     my ($self, $schema) = (shift, shift);
112
113     my ($table_pattern, $table_type_pattern) = @_ ? @_ : ('%', '%');
114
115     return $self->dbh->tables(undef, $schema, $table_pattern, $table_type_pattern);
116 }
117
118 # default to be overridden in subclasses if necessary
119 sub _supports_db_schema { 1 }
120
121 # Returns an array of table objects
122 sub _tables_list { 
123     my ($self, $opts) = (shift, shift);
124
125     my @tables;
126
127     my $qt  = qr/[\Q$self->{quote_char}\E"'`\[\]]/;
128     my $nqt = qr/[^\Q$self->{quote_char}\E"'`\[\]]/;
129     my $ns  = qr/[\Q$self->{name_sep}\E]/;
130     my $nns = qr/[^\Q$self->{name_sep}\E]/;
131
132     foreach my $schema (@{ $self->db_schema || [undef] }) {
133         my @raw_table_names = $self->_dbh_tables($schema, @_);
134
135         TABLE: foreach my $raw_table_name (@raw_table_names) {
136             my $quoted = $raw_table_name =~ /^$qt/;
137
138             # These regexes are not entirely correct, but hopefully they will work
139             # in most cases. RT reports welcome.
140             my ($schema_name, $table_name1, $table_name2) = $quoted ?
141                 $raw_table_name =~ /^(?:${qt}(${nqt}+?)${qt}${ns})?(?:${qt}(.+?)${qt}|(${nns}+))\z/
142                 :
143                 $raw_table_name =~ /^(?:(${nns}+?)${ns})?(?:${qt}(.+?)${qt}|(${nns}+))\z/;
144
145             my $table_name = $table_name1 || $table_name2;
146
147             foreach my $system_schema ($self->_system_schemas) {
148                 if ($schema_name) {
149                     my $matches = 0;
150
151                     if (ref $system_schema) {
152                         $matches = 1
153                             if $schema_name =~ $system_schema
154                                  && $schema !~ $system_schema;
155                     }
156                     else {
157                         $matches = 1
158                             if $schema_name eq $system_schema
159                                 && $schema  ne $system_schema;
160                     }
161
162                     next TABLE if $matches;
163                 }
164             }
165
166             foreach my $system_table ($self->_system_tables) {
167                 my $matches = 0;
168
169                 if (ref $system_table) {
170                     $matches = 1 if $table_name =~ $system_table;
171                 }
172                 else {
173                     $matches = 1 if $table_name eq $system_table
174                 }
175
176                 next TABLE if $matches;
177             }
178
179             $schema_name ||= $schema;
180
181             my $table = DBIx::Class::Schema::Loader::Table->new(
182                 loader => $self,
183                 name   => $table_name,
184                 schema => $schema_name,
185                 ($self->_supports_db_schema ? () : (
186                     ignore_schema => 1
187                 )),
188             );
189
190             push @tables, $table;
191         }
192     }
193
194     return $self->_filter_tables(\@tables, $opts);
195 }
196
197 # apply constraint/exclude and ignore bad tables and views
198 sub _filter_tables {
199     my ($self, $tables, $opts) = @_;
200
201     my @tables = @$tables;
202     my @filtered_tables;
203
204     $opts ||= {};
205     my $constraint   = $opts->{constraint};
206     my $exclude      = $opts->{exclude};
207
208     @tables = grep { /$constraint/ } @tables if defined $constraint;
209     @tables = grep { ! /$exclude/  } @tables if defined $exclude;
210
211     TABLE: for my $table (@tables) {
212         try {
213             local $^W = 0; # for ADO
214             my $sth = $self->_sth_for($table, undef, \'1 = 0');
215             $sth->execute;
216             1;
217         }
218         catch {
219             warn "Bad table or view '$table', ignoring: $_\n";
220             0;
221         } or next TABLE;
222
223         push @filtered_tables, $table;
224     }
225
226     return @filtered_tables;
227 }
228
229 =head2 load
230
231 We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
232
233 =cut
234
235 sub load {
236     my $self = shift;
237
238     local $self->dbh->{RaiseError} = 1;
239     local $self->dbh->{PrintError} = 0;
240
241     $self->next::method(@_);
242
243     $self->schema->storage->disconnect unless $self->dynamic;
244 }
245
246 sub _sth_for {
247     my ($self, $table, $fields, $where) = @_;
248
249     my $sth = $self->dbh->prepare($self->schema->storage->sql_maker
250         ->select(\$table->sql_name, $fields, $where));
251
252     return $sth;
253 }
254
255 # Returns an arrayref of column names
256 sub _table_columns {
257     my ($self, $table) = @_;
258
259     my $sth = $self->_sth_for($table, undef, \'1 = 0');
260     $sth->execute;
261
262     my $retval = [ map $self->_lc($_), @{$sth->{NAME}} ];
263
264     $sth->finish;
265
266     return $retval;
267 }
268
269 # Returns arrayref of pk col names
270 sub _table_pk_info { 
271     my ($self, $table) = @_;
272
273     return [] if $self->_disable_pk_detection;
274
275     my @primary = try {
276         $self->dbh->primary_key('', $table->schema, $table->name);
277     }
278     catch {
279         warn "Cannot find primary keys for this driver: $_";
280         $self->_disable_pk_detection(1);
281         return ();
282     };
283
284     return [] if not @primary;
285
286     @primary = map { $self->_lc($_) } @primary;
287     s/[\Q$self->{quote_char}\E]//g for @primary;
288
289     return \@primary;
290 }
291
292 # Override this for vendor-specific uniq info
293 sub _table_uniq_info {
294     my ($self, $table) = @_;
295
296     return [] if $self->_disable_uniq_detection;
297
298     if (not $self->dbh->can('statistics_info')) {
299         warn "No UNIQUE constraint information can be gathered for this driver";
300         $self->_disable_uniq_detection(1);
301         return [];
302     }
303
304     my %indices;
305     my $sth = $self->dbh->statistics_info(undef, $table->schema, $table->name, 1, 1);
306     while(my $row = $sth->fetchrow_hashref) {
307         # skip table-level stats, conditional indexes, and any index missing
308         #  critical fields
309         next if $row->{TYPE} eq 'table'
310             || defined $row->{FILTER_CONDITION}
311             || !$row->{INDEX_NAME}
312             || !defined $row->{ORDINAL_POSITION}
313             || !$row->{COLUMN_NAME};
314
315         $indices{$row->{INDEX_NAME}}[$row->{ORDINAL_POSITION}] = $self->_lc($row->{COLUMN_NAME});
316     }
317     $sth->finish;
318
319     my @retval;
320     foreach my $index_name (keys %indices) {
321         my $index = $indices{$index_name};
322         push(@retval, [ $index_name => [ @$index[1..$#$index] ] ]);
323     }
324
325     return \@retval;
326 }
327
328 sub _table_comment {
329     my ($self, $table) = @_;
330     my $dbh = $self->dbh;
331
332     my $comments_table = $table->clone;
333     $comments_table->name($self->table_comments_table);
334
335     my ($comment) =
336         (exists $self->_tables->{$comments_table->sql_name} || undef)
337         && try { $dbh->selectrow_array(<<"EOF") };
338 SELECT comment_text
339 FROM @{[ $comments_table->sql_name ]}
340 WHERE table_name = @{[ $dbh->quote($table->name) ]}
341 EOF
342
343     # Failback: try the REMARKS column on table_info
344     if (!$comment && $dbh->can('table_info')) {
345         my $sth = $self->_dbh_table_info( $dbh, undef, $table->schema, $table->name );
346         my $info = $sth->fetchrow_hashref();
347         $comment = $info->{REMARKS};
348     }
349
350     return $comment;
351 }
352
353 sub _column_comment {
354     my ($self, $table, $column_number, $column_name) = @_;
355     my $dbh = $self->dbh;
356
357     my $comments_table = $table->clone;
358     $comments_table->name($self->column_comments_table);
359
360     my ($comment) =
361         (exists $self->_tables->{$comments_table->sql_name} || undef)
362         && try { $dbh->selectrow_array(<<"EOF") };
363 SELECT comment_text
364 FROM @{[ $comments_table->sql_name ]}
365 WHERE table_name = @{[ $dbh->quote($table->name) ]}
366 AND column_name = @{[ $dbh->quote($column_name) ]}
367 EOF
368
369     # Failback: try the REMARKS column on column_info
370     if (!$comment && $dbh->can('column_info')) {
371         if (my $sth = try { $self->_dbh_column_info( $dbh, undef, $table->schema, $table->name, $column_name ) }) {
372             my $info = $sth->fetchrow_hashref();
373             $comment = $info->{REMARKS};
374         }
375     }
376
377     return $comment;
378 }
379
380 # Find relationships
381 sub _table_fk_info {
382     my ($self, $table) = @_;
383
384     return [] if $self->_disable_fk_detection;
385
386     my $sth = try {
387         $self->dbh->foreign_key_info( '', '', '',
388                                 '', ($table->schema || ''), $table->name );
389     }
390     catch {
391         warn "Cannot introspect relationships for this driver: $_";
392         $self->_disable_fk_detection(1);
393         return undef;
394     };
395
396     return [] if !$sth;
397
398     my %rels;
399
400     my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
401     REL: while(my $raw_rel = $sth->fetchrow_arrayref) {
402         my $uk_scm  = $raw_rel->[1];
403         my $uk_tbl  = $raw_rel->[2];
404         my $uk_col  = $self->_lc($raw_rel->[3]);
405         my $fk_scm  = $raw_rel->[5];
406         my $fk_col  = $self->_lc($raw_rel->[7]);
407         my $key_seq = $raw_rel->[8] - 1;
408         my $relid   = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
409
410         foreach my $var ($uk_scm, $uk_tbl, $uk_col, $fk_scm, $fk_col, $relid) {
411             $var =~ s/[\Q$self->{quote_char}\E]//g if defined $var;
412         }
413
414         if ($self->db_schema && $self->db_schema->[0] ne '%'
415             && (not any { $_ eq $uk_scm } @{ $self->db_schema })) {
416
417             next REL;
418         }
419
420         $rels{$relid}{tbl} = DBIx::Class::Schema::Loader::Table->new(
421             loader => $self,
422             name   => $uk_tbl,
423             schema => $uk_scm,
424             ($self->_supports_db_schema ? () : (
425                 ignore_schema => 1
426             )),
427         );
428         
429         # Add this data IN ORDER
430         $rels{$relid}{rcols}[$key_seq] = $uk_col;
431         $rels{$relid}{lcols}[$key_seq] = $fk_col;
432     }
433     $sth->finish;
434
435     my @rels;
436     foreach my $relid (keys %rels) {
437         push(@rels, {
438             remote_columns => [ grep defined, @{ $rels{$relid}{rcols} } ],
439             local_columns  => [ grep defined, @{ $rels{$relid}{lcols} } ],
440             remote_table   => $rels{$relid}->{tbl},
441         });
442     }
443
444     return \@rels;
445 }
446
447 # ported in from DBIx::Class::Storage::DBI:
448 sub _columns_info_for {
449     my ($self, $table) = @_;
450
451     my $dbh = $self->schema->storage->dbh;
452
453     my %result;
454
455     if (my $sth = try { $self->_dbh_column_info($dbh, undef, $table->schema, $table->name, '%' ) }) {
456         COL_INFO: while (my $info = try { $sth->fetchrow_hashref } catch { +{} }) {
457             next COL_INFO unless %$info;
458
459             my $column_info = {};
460             $column_info->{data_type}     = lc $info->{TYPE_NAME};
461
462             my $size = $info->{COLUMN_SIZE};
463
464             if (defined $size && defined $info->{DECIMAL_DIGITS}) {
465                 $column_info->{size} = [$size, $info->{DECIMAL_DIGITS}];
466             }
467             elsif (defined $size) {
468                 $column_info->{size} = $size;
469             }
470
471             $column_info->{is_nullable}   = $info->{NULLABLE} ? 1 : 0;
472             $column_info->{default_value} = $info->{COLUMN_DEF} if defined $info->{COLUMN_DEF};
473             my $col_name = $info->{COLUMN_NAME};
474             $col_name =~ s/^\"(.*)\"$/$1/;
475
476             my $extra_info = $self->_extra_column_info(
477                 $table, $col_name, $column_info, $info
478             ) || {};
479             $column_info = { %$column_info, %$extra_info };
480
481             $result{$col_name} = $column_info;
482         }
483         $sth->finish;
484     }
485
486     my $sth = $self->_sth_for($table, undef, \'1 = 0');
487     $sth->execute;
488
489     my @columns = @{ $sth->{NAME} };
490
491     COL: for my $i (0 .. $#columns) {
492         next COL if %{ $result{ $columns[$i] }||{} };
493
494         my $column_info = {};
495         $column_info->{data_type} = lc $sth->{TYPE}[$i];
496
497         my $size = $sth->{PRECISION}[$i];
498
499         if (defined $size && defined $sth->{SCALE}[$i]) {
500             $column_info->{size} = [$size, $sth->{SCALE}[$i]];
501         }
502         elsif (defined $size) {
503             $column_info->{size} = $size;
504         }
505
506         $column_info->{is_nullable} = $sth->{NULLABLE}[$i] ? 1 : 0;
507
508         if ($column_info->{data_type} =~ m/^(.*?)\((.*?)\)$/) {
509             $column_info->{data_type} = $1;
510             $column_info->{size}    = $2;
511         }
512
513         my $extra_info = $self->_extra_column_info($table, $columns[$i], $column_info, $sth) || {};
514         $column_info = { %$column_info, %$extra_info };
515
516         $result{ $columns[$i] } = $column_info;
517     }
518     $sth->finish;
519
520     foreach my $col (keys %result) {
521         my $colinfo = $result{$col};
522         my $type_num = $colinfo->{data_type};
523         my $type_name;
524         if (defined $type_num && $type_num =~ /^-?\d+\z/ && $dbh->can('type_info')) {
525             my $type_name = $self->_dbh_type_info_type_name($type_num);
526             $colinfo->{data_type} = lc $type_name if $type_name;
527         }
528     }
529
530     # check for instances of the same column name with different case in preserve_case=0 mode
531     if (not $self->preserve_case) {
532         my %lc_colnames;
533
534         foreach my $col (keys %result) {
535             push @{ $lc_colnames{lc $col} }, $col;
536         }
537
538         if (keys %lc_colnames != keys %result) {
539             my @offending_colnames = map @$_, grep @$_ > 1, values %lc_colnames;
540
541             my $offending_colnames = join ", ", map "'$_'", @offending_colnames;
542
543             croak "columns $offending_colnames in table @{[ $table->sql_name ]} collide in preserve_case=0 mode. preserve_case=1 mode required";
544         }
545
546         # apply lowercasing
547         my %lc_result;
548
549         while (my ($col, $info) = each %result) {
550             $lc_result{ $self->_lc($col) } = $info;
551         }
552
553         %result = %lc_result;
554     }
555
556     return \%result;
557 }
558
559 # Need to override this for the buggy Firebird ODBC driver.
560 sub _dbh_type_info_type_name {
561     my ($self, $type_num) = @_;
562
563     # We wrap it in a try block for MSSQL+DBD::Sybase, which can have issues.
564     # TODO investigate further
565     my $type_info = try { $self->dbh->type_info($type_num) };
566     
567     return $type_info ? $type_info->{TYPE_NAME} : undef;
568 }
569
570 # do not use this, override _columns_info_for instead
571 sub _extra_column_info {}
572
573 # override to mask warnings if needed
574 sub _dbh_table_info {
575     my ($self, $dbh) = (shift, shift);
576
577     return $dbh->table_info(@_);
578 }
579
580 # override to mask warnings if needed (see mysql)
581 sub _dbh_column_info {
582     my ($self, $dbh) = (shift, shift);
583
584     return $dbh->column_info(@_);
585 }
586
587 # If a coderef uses DBI->connect, this should get its connect info.
588 sub _try_infer_connect_info_from_coderef {
589     my ($self, $code) = @_;
590
591     my ($dsn, $user, $pass, $params);
592
593     no warnings 'redefine';
594
595     local *DBI::connect = sub {
596         (undef, $dsn, $user, $pass, $params) = @_;
597     };
598
599     $code->();
600
601     return ($dsn, $user, $pass, $params);
602 }
603
604 sub dbh {
605     my $self = shift;
606
607     return $self->schema->storage->dbh;
608 }
609
610 =head1 SEE ALSO
611
612 L<DBIx::Class::Schema::Loader>
613
614 =head1 AUTHOR
615
616 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
617
618 =head1 LICENSE
619
620 This library is free software; you can redistribute it and/or modify it under
621 the same terms as Perl itself.
622
623 =cut
624
625 1;
626 # vim:et sts=4 sw=4 tw=0: