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'; |
61d1cca1 |
7 | use Try::Tiny; |
c4a69b87 |
8 | use List::MoreUtils 'any'; |
116431d6 |
9 | use Carp::Clan qw/^DBIx::Class/; |
61d1cca1 |
10 | use namespace::clean; |
c4a69b87 |
11 | use DBIx::Class::Schema::Loader::Table (); |
996be9ee |
12 | |
53c4835c |
13 | our $VERSION = '0.07040'; |
32f784fc |
14 | |
3b17d988 |
15 | __PACKAGE__->mk_group_accessors('simple', qw/ |
16 | _disable_pk_detection |
17 | _disable_uniq_detection |
18 | _disable_fk_detection |
19 | _passwords |
c4a69b87 |
20 | quote_char |
21 | name_sep |
3b17d988 |
22 | /); |
23 | |
996be9ee |
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 | |
71a6e88a |
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) { |
c4a69b87 |
54 | my $driver = $self->dbh->{Driver}->{Name}; |
71a6e88a |
55 | |
56 | my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver; |
383bd2a8 |
57 | if ((not $self->isa($subclass)) && $self->load_optional_class($subclass)) { |
58 | bless $self, $subclass; |
71a6e88a |
59 | $self->_rebless; |
383bd2a8 |
60 | Class::C3::reinitialize() if $] < 5.009005; |
71a6e88a |
61 | } |
996be9ee |
62 | } |
63 | |
dee01f3b |
64 | # Set up the default quoting character and name separators |
c4a69b87 |
65 | $self->quote_char($self->_build_quote_char); |
66 | $self->name_sep($self->_build_name_sep); |
996be9ee |
67 | |
68 | $self->_setup; |
69 | |
383bd2a8 |
70 | return $self; |
996be9ee |
71 | } |
72 | |
c4a69b87 |
73 | sub _build_quote_char { |
77d3753e |
74 | my $self = shift; |
c4a69b87 |
75 | |
76 | my $quote_char = $self->dbh->get_info(29) |
77d3753e |
77 | || $self->schema->storage->sql_maker->quote_char |
78 | || q{"}; |
c4a69b87 |
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; |
77d3753e |
87 | } |
88 | |
c4a69b87 |
89 | sub _build_name_sep { |
77d3753e |
90 | my $self = shift; |
c4a69b87 |
91 | return $self->dbh->get_info(41) |
77d3753e |
92 | || $self->schema->storage->sql_maker->name_sep |
c4a69b87 |
93 | || '.'; |
77d3753e |
94 | } |
95 | |
996be9ee |
96 | # Override this in vendor modules to do things at the end of ->new() |
97 | sub _setup { } |
98 | |
6ae3f335 |
99 | # Override this in vendor module to load a subclass if necessary |
100 | sub _rebless { } |
101 | |
c4a69b87 |
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 |
a40434df |
122 | sub _tables_list { |
bfb43060 |
123 | my ($self, $opts) = (shift, shift); |
996be9ee |
124 | |
c4a69b87 |
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; |
e57fd726 |
168 | |
c4a69b87 |
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 | } |
77d3753e |
175 | |
c4a69b87 |
176 | next TABLE if $matches; |
177 | } |
178 | |
179 | $schema_name ||= $schema; |
385c593b |
180 | |
c4a69b87 |
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 | ); |
c5ff1f26 |
189 | |
c4a69b87 |
190 | push @tables, $table; |
191 | } |
385c593b |
192 | } |
996be9ee |
193 | |
bfb43060 |
194 | return $self->_filter_tables(\@tables, $opts); |
075aff97 |
195 | } |
196 | |
b187901e |
197 | sub _recurse_constraint { |
198 | my ($constraint, @parts) = @_; |
199 | |
200 | my $name = shift @parts; |
201 | |
202 | # If there are any parts left, the constraint must be an arrayref |
203 | croak "depth of constraint/exclude array does not match length of moniker_parts" |
204 | unless !!@parts == !!(ref $constraint eq 'ARRAY'); |
205 | |
206 | # if ths is the last part, use the constraint directly |
207 | return $name =~ $constraint unless @parts; |
208 | |
209 | # recurse into the first matching subconstraint |
210 | foreach (@{$constraint}) { |
211 | my ($re, $sub) = @{$_}; |
212 | return _recurse_constraint($sub, @parts) |
213 | if $name =~ $re; |
214 | } |
215 | return 0; |
216 | } |
217 | |
218 | sub _check_constraint { |
219 | my ($include, $constraint, @tables) = @_; |
220 | |
221 | return @tables unless defined $constraint; |
222 | |
223 | return grep { !$include xor _recurse_constraint($constraint, @{$_}) } @tables |
224 | if ref $constraint eq 'ARRAY'; |
225 | |
226 | return grep { !$include xor /$constraint/ } @tables; |
227 | } |
228 | |
229 | |
230 | |
bfb43060 |
231 | # apply constraint/exclude and ignore bad tables and views |
075aff97 |
232 | sub _filter_tables { |
bfb43060 |
233 | my ($self, $tables, $opts) = @_; |
075aff97 |
234 | |
bfb43060 |
235 | my @tables = @$tables; |
075aff97 |
236 | my @filtered_tables; |
237 | |
bfb43060 |
238 | $opts ||= {}; |
b187901e |
239 | @tables = _check_constraint(1, $opts->{constraint}, @tables); |
240 | @tables = _check_constraint(0, $opts->{exclude}, @tables); |
bfb43060 |
241 | |
c4a69b87 |
242 | TABLE: for my $table (@tables) { |
61d1cca1 |
243 | try { |
afcd3c32 |
244 | local $^W = 0; # for ADO |
805dbe0a |
245 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
246 | $sth->execute; |
c4a69b87 |
247 | 1; |
075aff97 |
248 | } |
61d1cca1 |
249 | catch { |
250 | warn "Bad table or view '$table', ignoring: $_\n"; |
c4a69b87 |
251 | 0; |
252 | } or next TABLE; |
61d1cca1 |
253 | |
254 | push @filtered_tables, $table; |
075aff97 |
255 | } |
256 | |
257 | return @filtered_tables; |
996be9ee |
258 | } |
259 | |
12af3806 |
260 | =head2 load |
261 | |
262 | We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling. |
263 | |
264 | =cut |
265 | |
266 | sub load { |
267 | my $self = shift; |
268 | |
c4a69b87 |
269 | local $self->dbh->{RaiseError} = 1; |
270 | local $self->dbh->{PrintError} = 0; |
12b86f07 |
271 | |
c4a69b87 |
272 | $self->next::method(@_); |
075aff97 |
273 | } |
274 | |
275 | sub _sth_for { |
276 | my ($self, $table, $fields, $where) = @_; |
277 | |
c4a69b87 |
278 | my $sth = $self->dbh->prepare($self->schema->storage->sql_maker |
279 | ->select(\$table->sql_name, $fields, $where)); |
075aff97 |
280 | |
281 | return $sth; |
282 | } |
283 | |
284 | # Returns an arrayref of column names |
285 | sub _table_columns { |
286 | my ($self, $table) = @_; |
287 | |
288 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
996be9ee |
289 | $sth->execute; |
1af21646 |
290 | |
291 | my $retval = [ map $self->_lc($_), @{$sth->{NAME}} ]; |
292 | |
3b7f80f9 |
293 | $sth->finish; |
294 | |
1af21646 |
295 | return $retval; |
996be9ee |
296 | } |
297 | |
298 | # Returns arrayref of pk col names |
a40434df |
299 | sub _table_pk_info { |
fd589700 |
300 | my ($self, $table) = @_; |
996be9ee |
301 | |
3b17d988 |
302 | return [] if $self->_disable_pk_detection; |
303 | |
3b17d988 |
304 | my @primary = try { |
c4a69b87 |
305 | $self->dbh->primary_key('', $table->schema, $table->name); |
3b17d988 |
306 | } |
307 | catch { |
308 | warn "Cannot find primary keys for this driver: $_"; |
309 | $self->_disable_pk_detection(1); |
310 | return (); |
311 | }; |
312 | |
313 | return [] if not @primary; |
314 | |
315 | @primary = map { $self->_lc($_) } @primary; |
c4a69b87 |
316 | s/[\Q$self->{quote_char}\E]//g for @primary; |
996be9ee |
317 | |
318 | return \@primary; |
319 | } |
320 | |
fd589700 |
321 | # Override this for vendor-specific uniq info |
996be9ee |
322 | sub _table_uniq_info { |
fd589700 |
323 | my ($self, $table) = @_; |
324 | |
3b17d988 |
325 | return [] if $self->_disable_uniq_detection; |
326 | |
c4a69b87 |
327 | if (not $self->dbh->can('statistics_info')) { |
3b17d988 |
328 | warn "No UNIQUE constraint information can be gathered for this driver"; |
329 | $self->_disable_uniq_detection(1); |
fd589700 |
330 | return []; |
331 | } |
332 | |
333 | my %indices; |
c4a69b87 |
334 | my $sth = $self->dbh->statistics_info(undef, $table->schema, $table->name, 1, 1); |
fd589700 |
335 | while(my $row = $sth->fetchrow_hashref) { |
336 | # skip table-level stats, conditional indexes, and any index missing |
337 | # critical fields |
338 | next if $row->{TYPE} eq 'table' |
339 | || defined $row->{FILTER_CONDITION} |
340 | || !$row->{INDEX_NAME} |
b32f8189 |
341 | || !defined $row->{ORDINAL_POSITION}; |
fd589700 |
342 | |
b32f8189 |
343 | $indices{$row->{INDEX_NAME}}[$row->{ORDINAL_POSITION}] = $self->_lc($row->{COLUMN_NAME} || ''); |
fd589700 |
344 | } |
3b7f80f9 |
345 | $sth->finish; |
fd589700 |
346 | |
347 | my @retval; |
6c4f5a4a |
348 | foreach my $index_name (sort keys %indices) { |
b32f8189 |
349 | my (undef, @cols) = @{$indices{$index_name}}; |
350 | # skip indexes with missing column names (e.g. expression indexes) |
351 | next unless @cols == grep $_, @cols; |
352 | push(@retval, [ $index_name => \@cols ]); |
fd589700 |
353 | } |
354 | |
355 | return \@retval; |
996be9ee |
356 | } |
357 | |
c4a69b87 |
358 | sub _table_comment { |
359 | my ($self, $table) = @_; |
7732645c |
360 | my $dbh = $self->dbh; |
5c06aa08 |
361 | |
b21abfca |
362 | my $comments_table = $table->clone; |
363 | $comments_table->name($self->table_comments_table); |
5c06aa08 |
364 | |
07307014 |
365 | my ($comment) = |
366 | (exists $self->_tables->{$comments_table->sql_name} || undef) |
367 | && try { $dbh->selectrow_array(<<"EOF") }; |
c4a69b87 |
368 | SELECT comment_text |
b21abfca |
369 | FROM @{[ $comments_table->sql_name ]} |
7732645c |
370 | WHERE table_name = @{[ $dbh->quote($table->name) ]} |
c4a69b87 |
371 | EOF |
5c06aa08 |
372 | |
5ffed88c |
373 | # Failback: try the REMARKS column on table_info |
68c6c83f |
374 | if (!$comment) { |
375 | my $info = $self->_dbh_table_info( $dbh, $table ); |
376 | $comment = $info->{REMARKS} if $info; |
5ffed88c |
377 | } |
378 | |
c4a69b87 |
379 | return $comment; |
5c06aa08 |
380 | } |
381 | |
382 | sub _column_comment { |
b21abfca |
383 | my ($self, $table, $column_number, $column_name) = @_; |
7732645c |
384 | my $dbh = $self->dbh; |
c4a69b87 |
385 | |
b21abfca |
386 | my $comments_table = $table->clone; |
387 | $comments_table->name($self->column_comments_table); |
c4a69b87 |
388 | |
07307014 |
389 | my ($comment) = |
390 | (exists $self->_tables->{$comments_table->sql_name} || undef) |
391 | && try { $dbh->selectrow_array(<<"EOF") }; |
c4a69b87 |
392 | SELECT comment_text |
b21abfca |
393 | FROM @{[ $comments_table->sql_name ]} |
7732645c |
394 | WHERE table_name = @{[ $dbh->quote($table->name) ]} |
395 | AND column_name = @{[ $dbh->quote($column_name) ]} |
c4a69b87 |
396 | EOF |
5ffed88c |
397 | |
398 | # Failback: try the REMARKS column on column_info |
5ffed88c |
399 | if (!$comment && $dbh->can('column_info')) { |
1af21646 |
400 | if (my $sth = try { $self->_dbh_column_info( $dbh, undef, $table->schema, $table->name, $column_name ) }) { |
401 | my $info = $sth->fetchrow_hashref(); |
402 | $comment = $info->{REMARKS}; |
403 | } |
5ffed88c |
404 | } |
405 | |
c4a69b87 |
406 | return $comment; |
5c06aa08 |
407 | } |
408 | |
996be9ee |
409 | # Find relationships |
410 | sub _table_fk_info { |
411 | my ($self, $table) = @_; |
412 | |
3b17d988 |
413 | return [] if $self->_disable_fk_detection; |
414 | |
3b17d988 |
415 | my $sth = try { |
c4a69b87 |
416 | $self->dbh->foreign_key_info( '', '', '', |
417 | '', ($table->schema || ''), $table->name ); |
3b17d988 |
418 | } |
419 | catch { |
420 | warn "Cannot introspect relationships for this driver: $_"; |
421 | $self->_disable_fk_detection(1); |
422 | return undef; |
423 | }; |
424 | |
996be9ee |
425 | return [] if !$sth; |
426 | |
427 | my %rels; |
428 | |
a40434df |
429 | my @rules = ( |
430 | 'CASCADE', |
431 | 'RESTRICT', |
432 | 'SET NULL', |
433 | 'NO ACTION', |
434 | 'SET DEFAULT', |
435 | ); |
436 | |
996be9ee |
437 | my $i = 1; # for unnamed rels, which hopefully have only 1 column ... |
c4a69b87 |
438 | REL: while(my $raw_rel = $sth->fetchrow_arrayref) { |
439 | my $uk_scm = $raw_rel->[1]; |
996be9ee |
440 | my $uk_tbl = $raw_rel->[2]; |
c930f78b |
441 | my $uk_col = $self->_lc($raw_rel->[3]); |
c4a69b87 |
442 | my $fk_scm = $raw_rel->[5]; |
c930f78b |
443 | my $fk_col = $self->_lc($raw_rel->[7]); |
a273b50c |
444 | my $key_seq = $raw_rel->[8] - 1; |
996be9ee |
445 | my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ )); |
c4a69b87 |
446 | |
a40434df |
447 | my $update_rule = $raw_rel->[9]; |
448 | my $delete_rule = $raw_rel->[10]; |
449 | |
450 | $update_rule = $rules[$update_rule] if defined $update_rule; |
451 | $delete_rule = $rules[$delete_rule] if defined $delete_rule; |
452 | |
453 | my $is_deferrable = $raw_rel->[13]; |
454 | |
455 | ($is_deferrable = $is_deferrable == 7 ? 0 : 1) |
456 | if defined $is_deferrable; |
457 | |
c4a69b87 |
458 | foreach my $var ($uk_scm, $uk_tbl, $uk_col, $fk_scm, $fk_col, $relid) { |
ba9954e0 |
459 | $var =~ s/[\Q$self->{quote_char}\E]//g if defined $var; |
c4a69b87 |
460 | } |
461 | |
462 | if ($self->db_schema && $self->db_schema->[0] ne '%' |
463 | && (not any { $_ eq $uk_scm } @{ $self->db_schema })) { |
464 | |
465 | next REL; |
466 | } |
467 | |
a40434df |
468 | $rels{$relid}{tbl} ||= DBIx::Class::Schema::Loader::Table->new( |
c4a69b87 |
469 | loader => $self, |
470 | name => $uk_tbl, |
471 | schema => $uk_scm, |
472 | ($self->_supports_db_schema ? () : ( |
473 | ignore_schema => 1 |
474 | )), |
475 | ); |
a40434df |
476 | |
477 | $rels{$relid}{attrs}{on_delete} = $delete_rule if $delete_rule; |
478 | $rels{$relid}{attrs}{on_update} = $update_rule if $update_rule; |
479 | $rels{$relid}{attrs}{is_deferrable} = $is_deferrable if defined $is_deferrable; |
480 | |
a273b50c |
481 | # Add this data IN ORDER |
482 | $rels{$relid}{rcols}[$key_seq] = $uk_col; |
483 | $rels{$relid}{lcols}[$key_seq] = $fk_col; |
996be9ee |
484 | } |
3b7f80f9 |
485 | $sth->finish; |
996be9ee |
486 | |
487 | my @rels; |
488 | foreach my $relid (keys %rels) { |
489 | push(@rels, { |
79137580 |
490 | remote_columns => [ grep defined, @{ $rels{$relid}{rcols} } ], |
491 | local_columns => [ grep defined, @{ $rels{$relid}{lcols} } ], |
996be9ee |
492 | remote_table => $rels{$relid}->{tbl}, |
a40434df |
493 | (exists $rels{$relid}{attrs} ? |
494 | (attrs => $rels{$relid}{attrs}) |
495 | : |
496 | () |
497 | ), |
498 | _constraint_name => $relid, |
996be9ee |
499 | }); |
500 | } |
501 | |
502 | return \@rels; |
503 | } |
504 | |
12af3806 |
505 | # ported in from DBIx::Class::Storage::DBI: |
506 | sub _columns_info_for { |
507 | my ($self, $table) = @_; |
508 | |
509 | my $dbh = $self->schema->storage->dbh; |
510 | |
61d1cca1 |
511 | my %result; |
512 | |
1af21646 |
513 | if (my $sth = try { $self->_dbh_column_info($dbh, undef, $table->schema, $table->name, '%' ) }) { |
514 | COL_INFO: while (my $info = try { $sth->fetchrow_hashref } catch { +{} }) { |
515 | next COL_INFO unless %$info; |
516 | |
61d1cca1 |
517 | my $column_info = {}; |
518 | $column_info->{data_type} = lc $info->{TYPE_NAME}; |
519 | |
520 | my $size = $info->{COLUMN_SIZE}; |
521 | |
522 | if (defined $size && defined $info->{DECIMAL_DIGITS}) { |
523 | $column_info->{size} = [$size, $info->{DECIMAL_DIGITS}]; |
12af3806 |
524 | } |
61d1cca1 |
525 | elsif (defined $size) { |
526 | $column_info->{size} = $size; |
527 | } |
528 | |
529 | $column_info->{is_nullable} = $info->{NULLABLE} ? 1 : 0; |
530 | $column_info->{default_value} = $info->{COLUMN_DEF} if defined $info->{COLUMN_DEF}; |
531 | my $col_name = $info->{COLUMN_NAME}; |
532 | $col_name =~ s/^\"(.*)\"$/$1/; |
533 | |
61d1cca1 |
534 | my $extra_info = $self->_extra_column_info( |
535 | $table, $col_name, $column_info, $info |
536 | ) || {}; |
537 | $column_info = { %$column_info, %$extra_info }; |
538 | |
539 | $result{$col_name} = $column_info; |
540 | } |
541 | $sth->finish; |
12af3806 |
542 | } |
543 | |
075aff97 |
544 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
12af3806 |
545 | $sth->execute; |
61d1cca1 |
546 | |
ed18888f |
547 | my @columns = @{ $sth->{NAME} }; |
61d1cca1 |
548 | |
1af21646 |
549 | COL: for my $i (0 .. $#columns) { |
22f91663 |
550 | next COL if %{ $result{ $columns[$i] }||{} }; |
1af21646 |
551 | |
23d1f36b |
552 | my $column_info = {}; |
57a9fc92 |
553 | $column_info->{data_type} = lc $sth->{TYPE}[$i]; |
26334ec1 |
554 | |
555 | my $size = $sth->{PRECISION}[$i]; |
556 | |
557 | if (defined $size && defined $sth->{SCALE}[$i]) { |
558 | $column_info->{size} = [$size, $sth->{SCALE}[$i]]; |
559 | } |
560 | elsif (defined $size) { |
561 | $column_info->{size} = $size; |
562 | } |
563 | |
57a9fc92 |
564 | $column_info->{is_nullable} = $sth->{NULLABLE}[$i] ? 1 : 0; |
23d1f36b |
565 | |
566 | if ($column_info->{data_type} =~ m/^(.*?)\((.*?)\)$/) { |
567 | $column_info->{data_type} = $1; |
568 | $column_info->{size} = $2; |
12af3806 |
569 | } |
570 | |
698c11d8 |
571 | my $extra_info = $self->_extra_column_info($table, $columns[$i], $column_info, $sth) || {}; |
23d1f36b |
572 | $column_info = { %$column_info, %$extra_info }; |
573 | |
116431d6 |
574 | $result{ $columns[$i] } = $column_info; |
12af3806 |
575 | } |
6ce0bcc3 |
576 | $sth->finish; |
577 | |
578 | foreach my $col (keys %result) { |
579 | my $colinfo = $result{$col}; |
580 | my $type_num = $colinfo->{data_type}; |
581 | my $type_name; |
afcd3c32 |
582 | if (defined $type_num && $type_num =~ /^-?\d+\z/ && $dbh->can('type_info')) { |
5111e5d0 |
583 | my $type_name = $self->_dbh_type_info_type_name($type_num); |
cf0ba25b |
584 | $colinfo->{data_type} = lc $type_name if $type_name; |
6ce0bcc3 |
585 | } |
586 | } |
12af3806 |
587 | |
116431d6 |
588 | # check for instances of the same column name with different case in preserve_case=0 mode |
589 | if (not $self->preserve_case) { |
590 | my %lc_colnames; |
591 | |
592 | foreach my $col (keys %result) { |
593 | push @{ $lc_colnames{lc $col} }, $col; |
594 | } |
595 | |
596 | if (keys %lc_colnames != keys %result) { |
597 | my @offending_colnames = map @$_, grep @$_ > 1, values %lc_colnames; |
598 | |
599 | my $offending_colnames = join ", ", map "'$_'", @offending_colnames; |
600 | |
601 | croak "columns $offending_colnames in table @{[ $table->sql_name ]} collide in preserve_case=0 mode. preserve_case=1 mode required"; |
602 | } |
603 | |
604 | # apply lowercasing |
605 | my %lc_result; |
606 | |
607 | while (my ($col, $info) = each %result) { |
608 | $lc_result{ $self->_lc($col) } = $info; |
609 | } |
610 | |
611 | %result = %lc_result; |
612 | } |
613 | |
12af3806 |
614 | return \%result; |
615 | } |
616 | |
23024b3f |
617 | # Need to override this for the buggy Firebird ODBC driver. |
5111e5d0 |
618 | sub _dbh_type_info_type_name { |
23024b3f |
619 | my ($self, $type_num) = @_; |
620 | |
354b6942 |
621 | # We wrap it in a try block for MSSQL+DBD::Sybase, which can have issues. |
622 | # TODO investigate further |
623 | my $type_info = try { $self->dbh->type_info($type_num) }; |
a40434df |
624 | |
5111e5d0 |
625 | return $type_info ? $type_info->{TYPE_NAME} : undef; |
23024b3f |
626 | } |
627 | |
db9c411a |
628 | # do not use this, override _columns_info_for instead |
a8df0345 |
629 | sub _extra_column_info {} |
c5baf131 |
630 | |
5ffed88c |
631 | # override to mask warnings if needed |
632 | sub _dbh_table_info { |
68c6c83f |
633 | my ($self, $dbh, $table) = (shift, shift, shift); |
634 | |
635 | return undef if !$dbh->can('table_info'); |
636 | my $sth = $dbh->table_info(undef, $table->schema, $table->name); |
637 | while (my $info = $sth->fetchrow_hashref) { |
638 | next if !$self->_table_info_matches($table, $info); |
639 | return $info; |
640 | } |
641 | return undef; |
642 | } |
643 | |
644 | sub _table_info_matches { |
645 | my ($self, $table, $info) = @_; |
5ffed88c |
646 | |
68c6c83f |
647 | no warnings 'uninitialized'; |
648 | return $info->{TABLE_SCHEM} eq $table->schema |
649 | && $info->{TABLE_NAME} eq $table->name; |
5ffed88c |
650 | } |
651 | |
db9c411a |
652 | # override to mask warnings if needed (see mysql) |
653 | sub _dbh_column_info { |
654 | my ($self, $dbh) = (shift, shift); |
655 | |
656 | return $dbh->column_info(@_); |
657 | } |
658 | |
3b17d988 |
659 | # If a coderef uses DBI->connect, this should get its connect info. |
660 | sub _try_infer_connect_info_from_coderef { |
661 | my ($self, $code) = @_; |
662 | |
663 | my ($dsn, $user, $pass, $params); |
664 | |
665 | no warnings 'redefine'; |
666 | |
667 | local *DBI::connect = sub { |
668 | (undef, $dsn, $user, $pass, $params) = @_; |
669 | }; |
670 | |
671 | $code->(); |
672 | |
673 | return ($dsn, $user, $pass, $params); |
674 | } |
675 | |
c4a69b87 |
676 | sub dbh { |
677 | my $self = shift; |
678 | |
679 | return $self->schema->storage->dbh; |
680 | } |
681 | |
ce2f102a |
682 | sub _table_is_view { |
683 | my ($self, $table) = @_; |
684 | |
685 | my $info = $self->_dbh_table_info($self->dbh, $table) |
686 | or return 0; |
687 | return $info->{TABLE_TYPE} eq 'VIEW'; |
688 | } |
689 | |
996be9ee |
690 | =head1 SEE ALSO |
691 | |
692 | L<DBIx::Class::Schema::Loader> |
693 | |
be80bba7 |
694 | =head1 AUTHOR |
695 | |
9cc8e7e1 |
696 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
697 | |
698 | =head1 LICENSE |
699 | |
700 | This library is free software; you can redistribute it and/or modify it under |
701 | the same terms as Perl itself. |
702 | |
996be9ee |
703 | =cut |
704 | |
705 | 1; |
26334ec1 |
706 | # vim:et sts=4 sw=4 tw=0: |