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