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