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