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