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 | |
1fa18849 |
9 | our $VERSION = '0.04999_13'; |
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 |
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 |
62 | sub _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 | |
70 | sub _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() |
79 | sub _setup { } |
80 | |
6ae3f335 |
81 | # Override this in vendor module to load a subclass if necessary |
82 | sub _rebless { } |
83 | |
996be9ee |
84 | # Returns an array of table names |
85 | sub _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 | |
385c593b |
93 | my $qt = qr/\Q$self->{_quoter}\E/; |
94 | |
95 | if ($self->{_quoter} && $tables[0] =~ /$qt/) { |
96 | s/.* $qt (?= .* $qt)//xg for @tables; |
97 | } else { |
98 | s/^.*\Q$self->{_namesep}\E// for @tables; |
99 | } |
100 | s/$qt//g for @tables; |
996be9ee |
101 | |
075aff97 |
102 | return $self->_filter_tables(@tables); |
103 | } |
104 | |
105 | # ignore bad tables and views |
106 | sub _filter_tables { |
107 | my ($self, @tables) = @_; |
108 | |
109 | my @filtered_tables; |
110 | |
111 | for my $table (@tables) { |
805dbe0a |
112 | eval { |
113 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
114 | $sth->execute; |
115 | }; |
075aff97 |
116 | if (not $@) { |
117 | push @filtered_tables, $table; |
118 | } |
119 | else { |
120 | warn "Bad table or view '$table', ignoring: $@\n"; |
d073740e |
121 | local $@; |
122 | eval { |
123 | my $schema = $self->schema; |
124 | # in older DBIC it's a private method |
125 | my $unregister = $schema->can('unregister_source') |
126 | || $schema->can('_unregister_source'); |
127 | $schema->$unregister($self->_table2moniker($table)); |
128 | }; |
075aff97 |
129 | } |
130 | } |
131 | |
132 | return @filtered_tables; |
996be9ee |
133 | } |
134 | |
12af3806 |
135 | =head2 load |
136 | |
137 | We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling. |
138 | |
139 | =cut |
140 | |
141 | sub load { |
142 | my $self = shift; |
143 | |
144 | local $self->schema->storage->dbh->{RaiseError} = 1; |
145 | local $self->schema->storage->dbh->{PrintError} = 0; |
146 | $self->next::method(@_); |
147 | } |
148 | |
075aff97 |
149 | sub _table_as_sql { |
996be9ee |
150 | my ($self, $table) = @_; |
151 | |
996be9ee |
152 | if($self->{db_schema}) { |
385c593b |
153 | $table = $self->{db_schema} . $self->{_namesep} . |
154 | $self->_quote_table_name($table); |
155 | } else { |
156 | $table = $self->_quote_table_name($table); |
996be9ee |
157 | } |
158 | |
075aff97 |
159 | return $table; |
160 | } |
161 | |
162 | sub _sth_for { |
163 | my ($self, $table, $fields, $where) = @_; |
164 | |
165 | my $dbh = $self->schema->storage->dbh; |
166 | |
167 | my $sth = $dbh->prepare($self->schema->storage->sql_maker |
168 | ->select(\$self->_table_as_sql($table), $fields, $where)); |
169 | |
170 | return $sth; |
171 | } |
172 | |
173 | # Returns an arrayref of column names |
174 | sub _table_columns { |
175 | my ($self, $table) = @_; |
176 | |
177 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
996be9ee |
178 | $sth->execute; |
3b7f80f9 |
179 | my $retval = \@{$sth->{NAME_lc}}; |
180 | $sth->finish; |
181 | |
182 | $retval; |
996be9ee |
183 | } |
184 | |
185 | # Returns arrayref of pk col names |
186 | sub _table_pk_info { |
fd589700 |
187 | my ($self, $table) = @_; |
996be9ee |
188 | |
189 | my $dbh = $self->schema->storage->dbh; |
190 | |
191 | my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table); |
192 | s/\Q$self->{_quoter}\E//g for @primary; |
193 | |
194 | return \@primary; |
195 | } |
196 | |
fd589700 |
197 | # Override this for vendor-specific uniq info |
996be9ee |
198 | sub _table_uniq_info { |
fd589700 |
199 | my ($self, $table) = @_; |
200 | |
201 | my $dbh = $self->schema->storage->dbh; |
202 | if(!$dbh->can('statistics_info')) { |
203 | warn "No UNIQUE constraint information can be gathered for this vendor"; |
204 | return []; |
205 | } |
206 | |
207 | my %indices; |
208 | my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1); |
209 | while(my $row = $sth->fetchrow_hashref) { |
210 | # skip table-level stats, conditional indexes, and any index missing |
211 | # critical fields |
212 | next if $row->{TYPE} eq 'table' |
213 | || defined $row->{FILTER_CONDITION} |
214 | || !$row->{INDEX_NAME} |
215 | || !defined $row->{ORDINAL_POSITION} |
216 | || !$row->{COLUMN_NAME}; |
217 | |
218 | $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME}; |
219 | } |
3b7f80f9 |
220 | $sth->finish; |
fd589700 |
221 | |
222 | my @retval; |
223 | foreach my $index_name (keys %indices) { |
224 | my $index = $indices{$index_name}; |
225 | push(@retval, [ $index_name => [ |
226 | map { $index->{$_} } |
227 | sort keys %$index |
228 | ]]); |
229 | } |
230 | |
231 | return \@retval; |
996be9ee |
232 | } |
233 | |
234 | # Find relationships |
235 | sub _table_fk_info { |
236 | my ($self, $table) = @_; |
237 | |
238 | my $dbh = $self->schema->storage->dbh; |
a168c1c4 |
239 | my $sth = $dbh->foreign_key_info( '', $self->db_schema, '', |
240 | '', $self->db_schema, $table ); |
996be9ee |
241 | return [] if !$sth; |
242 | |
243 | my %rels; |
244 | |
245 | my $i = 1; # for unnamed rels, which hopefully have only 1 column ... |
246 | while(my $raw_rel = $sth->fetchrow_arrayref) { |
247 | my $uk_tbl = $raw_rel->[2]; |
248 | my $uk_col = lc $raw_rel->[3]; |
249 | my $fk_col = lc $raw_rel->[7]; |
250 | my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ )); |
251 | $uk_tbl =~ s/\Q$self->{_quoter}\E//g; |
252 | $uk_col =~ s/\Q$self->{_quoter}\E//g; |
253 | $fk_col =~ s/\Q$self->{_quoter}\E//g; |
254 | $relid =~ s/\Q$self->{_quoter}\E//g; |
255 | $rels{$relid}->{tbl} = $uk_tbl; |
256 | $rels{$relid}->{cols}->{$uk_col} = $fk_col; |
257 | } |
3b7f80f9 |
258 | $sth->finish; |
996be9ee |
259 | |
260 | my @rels; |
261 | foreach my $relid (keys %rels) { |
262 | push(@rels, { |
263 | remote_columns => [ keys %{$rels{$relid}->{cols}} ], |
264 | local_columns => [ values %{$rels{$relid}->{cols}} ], |
265 | remote_table => $rels{$relid}->{tbl}, |
266 | }); |
267 | } |
268 | |
269 | return \@rels; |
270 | } |
271 | |
12af3806 |
272 | # ported in from DBIx::Class::Storage::DBI: |
273 | sub _columns_info_for { |
274 | my ($self, $table) = @_; |
275 | |
276 | my $dbh = $self->schema->storage->dbh; |
277 | |
278 | if ($dbh->can('column_info')) { |
279 | my %result; |
280 | eval { |
281 | my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' ); |
12af3806 |
282 | while ( my $info = $sth->fetchrow_hashref() ){ |
283 | my %column_info; |
284 | $column_info{data_type} = $info->{TYPE_NAME}; |
285 | $column_info{size} = $info->{COLUMN_SIZE}; |
286 | $column_info{is_nullable} = $info->{NULLABLE} ? 1 : 0; |
287 | $column_info{default_value} = $info->{COLUMN_DEF}; |
288 | my $col_name = $info->{COLUMN_NAME}; |
289 | $col_name =~ s/^\"(.*)\"$/$1/; |
290 | |
a8df0345 |
291 | my $extra_info = $self->_extra_column_info($info) || {}; |
78b7ccaa |
292 | |
a8df0345 |
293 | $result{$col_name} = { %column_info, %$extra_info }; |
12af3806 |
294 | } |
3b7f80f9 |
295 | $sth->finish; |
12af3806 |
296 | }; |
297 | return \%result if !$@ && scalar keys %result; |
298 | } |
299 | |
12af3806 |
300 | my %result; |
075aff97 |
301 | my $sth = $self->_sth_for($table, undef, \'1 = 0'); |
12af3806 |
302 | $sth->execute; |
303 | my @columns = @{$sth->{NAME_lc}}; |
304 | for my $i ( 0 .. $#columns ){ |
305 | my %column_info; |
6ce0bcc3 |
306 | $column_info{data_type} = $sth->{TYPE}->[$i]; |
12af3806 |
307 | $column_info{size} = $sth->{PRECISION}->[$i]; |
308 | $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0; |
309 | |
310 | if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) { |
311 | $column_info{data_type} = $1; |
312 | $column_info{size} = $2; |
313 | } |
314 | |
a8df0345 |
315 | my $extra_info = $self->_extra_column_info($table, $columns[$i], $sth, $i) || {}; |
78b7ccaa |
316 | |
a8df0345 |
317 | $result{$columns[$i]} = { %column_info, %$extra_info }; |
12af3806 |
318 | } |
6ce0bcc3 |
319 | $sth->finish; |
320 | |
321 | foreach my $col (keys %result) { |
322 | my $colinfo = $result{$col}; |
323 | my $type_num = $colinfo->{data_type}; |
324 | my $type_name; |
325 | if(defined $type_num && $dbh->can('type_info')) { |
326 | my $type_info = $dbh->type_info($type_num); |
327 | $type_name = $type_info->{TYPE_NAME} if $type_info; |
328 | $colinfo->{data_type} = $type_name if $type_name; |
329 | } |
330 | } |
12af3806 |
331 | |
332 | return \%result; |
333 | } |
334 | |
a8df0345 |
335 | # Override this in vendor class to return any additional column |
336 | # attributes |
337 | sub _extra_column_info {} |
c5baf131 |
338 | |
996be9ee |
339 | =head1 SEE ALSO |
340 | |
341 | L<DBIx::Class::Schema::Loader> |
342 | |
be80bba7 |
343 | =head1 AUTHOR |
344 | |
9cc8e7e1 |
345 | See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>. |
be80bba7 |
346 | |
347 | =head1 LICENSE |
348 | |
349 | This library is free software; you can redistribute it and/or modify it under |
350 | the same terms as Perl itself. |
351 | |
996be9ee |
352 | =cut |
353 | |
354 | 1; |