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