result class is generated for table with dot in name, but doesn't work yet
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI.pm
1 package DBIx::Class::Schema::Loader::DBI;
2
3 use strict;
4 use warnings;
5 use base qw/DBIx::Class::Schema::Loader::Base/;
6 use Class::C3;
7 use Carp::Clan qw/^DBIx::Class/;
8
9 our $VERSION = '0.04999_07';
10
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};
41
42     my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver;
43     if ($self->load_optional_class($subclass)) {
44         bless $self, $subclass unless $self->isa($subclass);
45         $self->_rebless;
46     }
47
48     # Set up the default quoting character and name seperators
49     $self->{_quoter} = $self->_build_quoter;
50     $self->{_namesep} = $self->_build_namesep;
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
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
78 # Override this in vendor modules to do things at the end of ->new()
79 sub _setup { }
80
81 # Override this in vendor module to load a subclass if necessary
82 sub _rebless { }
83
84 # Returns an array of table names
85 sub _tables_list { 
86     my $self = shift;
87
88     my ($table, $type) = @_ ? @_ : ('%', '%');
89
90     my $dbh = $self->schema->storage->dbh;
91     my @tables = $dbh->tables(undef, $self->db_schema, $table, $type);
92
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;
101
102     return @tables;
103 }
104
105 =head2 load
106
107 We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
108
109 =cut
110
111 sub load {
112     my $self = shift;
113
114     local $self->schema->storage->dbh->{RaiseError} = 1;
115     local $self->schema->storage->dbh->{PrintError} = 0;
116     $self->next::method(@_);
117 }
118
119 sub _quote_table_name {
120     my ($self, $table) = @_;
121
122     my $qt = $self->schema->storage->sql_maker->quote_char;
123
124     if (ref $qt) {
125         return $qt->[0] . $table . $qt->[1];
126     }
127
128     return $qt . $table . $qt;
129 }
130
131 # Returns an arrayref of column names
132 sub _table_columns {
133     my ($self, $table) = @_;
134
135     my $dbh = $self->schema->storage->dbh;
136
137     if($self->{db_schema}) {
138         $table = $self->{db_schema} . $self->{_namesep} .
139             $self->_quote_table_name($table);
140     } else {
141         $table = $self->_quote_table_name($table);
142     }
143
144     my $sth = $dbh->prepare($self->schema->storage->sql_maker->select(\$table, undef, \'1 = 0'));
145     $sth->execute;
146     my $retval = \@{$sth->{NAME_lc}};
147     $sth->finish;
148
149     $retval;
150 }
151
152 # Returns arrayref of pk col names
153 sub _table_pk_info { 
154     my ($self, $table) = @_;
155
156     my $dbh = $self->schema->storage->dbh;
157
158     my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table);
159     s/\Q$self->{_quoter}\E//g for @primary;
160
161     return \@primary;
162 }
163
164 # Override this for vendor-specific uniq info
165 sub _table_uniq_info {
166     my ($self, $table) = @_;
167
168     my $dbh = $self->schema->storage->dbh;
169     if(!$dbh->can('statistics_info')) {
170         warn "No UNIQUE constraint information can be gathered for this vendor";
171         return [];
172     }
173
174     my %indices;
175     my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1);
176     while(my $row = $sth->fetchrow_hashref) {
177         # skip table-level stats, conditional indexes, and any index missing
178         #  critical fields
179         next if $row->{TYPE} eq 'table'
180             || defined $row->{FILTER_CONDITION}
181             || !$row->{INDEX_NAME}
182             || !defined $row->{ORDINAL_POSITION}
183             || !$row->{COLUMN_NAME};
184
185         $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME};
186     }
187     $sth->finish;
188
189     my @retval;
190     foreach my $index_name (keys %indices) {
191         my $index = $indices{$index_name};
192         push(@retval, [ $index_name => [
193             map { $index->{$_} }
194                 sort keys %$index
195         ]]);
196     }
197
198     return \@retval;
199 }
200
201 # Find relationships
202 sub _table_fk_info {
203     my ($self, $table) = @_;
204
205     my $dbh = $self->schema->storage->dbh;
206     my $sth = $dbh->foreign_key_info( '', $self->db_schema, '',
207                                       '', $self->db_schema, $table );
208     return [] if !$sth;
209
210     my %rels;
211
212     my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
213     while(my $raw_rel = $sth->fetchrow_arrayref) {
214         my $uk_tbl  = $raw_rel->[2];
215         my $uk_col  = lc $raw_rel->[3];
216         my $fk_col  = lc $raw_rel->[7];
217         my $relid   = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
218         $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
219         $uk_col =~ s/\Q$self->{_quoter}\E//g;
220         $fk_col =~ s/\Q$self->{_quoter}\E//g;
221         $relid  =~ s/\Q$self->{_quoter}\E//g;
222         $rels{$relid}->{tbl} = $uk_tbl;
223         $rels{$relid}->{cols}->{$uk_col} = $fk_col;
224     }
225     $sth->finish;
226
227     my @rels;
228     foreach my $relid (keys %rels) {
229         push(@rels, {
230             remote_columns => [ keys   %{$rels{$relid}->{cols}} ],
231             local_columns  => [ values %{$rels{$relid}->{cols}} ],
232             remote_table   => $rels{$relid}->{tbl},
233         });
234     }
235
236     return \@rels;
237 }
238
239 # ported in from DBIx::Class::Storage::DBI:
240 sub _columns_info_for {
241     my ($self, $table) = @_;
242
243     my $dbh = $self->schema->storage->dbh;
244
245     if ($dbh->can('column_info')) {
246         my %result;
247         eval {
248             my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' );
249             while ( my $info = $sth->fetchrow_hashref() ){
250                 my %column_info;
251                 $column_info{data_type}   = $info->{TYPE_NAME};
252                 $column_info{size}      = $info->{COLUMN_SIZE};
253                 $column_info{is_nullable}   = $info->{NULLABLE} ? 1 : 0;
254                 $column_info{default_value} = $info->{COLUMN_DEF};
255                 my $col_name = $info->{COLUMN_NAME};
256                 $col_name =~ s/^\"(.*)\"$/$1/;
257
258                 my $extra_info = $self->_extra_column_info($info) || {};
259
260                 $result{$col_name} = { %column_info, %$extra_info };
261             }
262             $sth->finish;
263         };
264       return \%result if !$@ && scalar keys %result;
265     }
266
267     if($self->db_schema) {
268         $table = $self->db_schema . $self->{_namesep} . $table;
269     }
270     my %result;
271     my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
272     $sth->execute;
273     my @columns = @{$sth->{NAME_lc}};
274     for my $i ( 0 .. $#columns ){
275         my %column_info;
276         $column_info{data_type} = $sth->{TYPE}->[$i];
277         $column_info{size} = $sth->{PRECISION}->[$i];
278         $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
279
280         if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) {
281             $column_info{data_type} = $1;
282             $column_info{size}    = $2;
283         }
284
285         my $extra_info = $self->_extra_column_info($table, $columns[$i], $sth, $i) || {};
286
287         $result{$columns[$i]} = { %column_info, %$extra_info };
288     }
289     $sth->finish;
290
291     foreach my $col (keys %result) {
292         my $colinfo = $result{$col};
293         my $type_num = $colinfo->{data_type};
294         my $type_name;
295         if(defined $type_num && $dbh->can('type_info')) {
296             my $type_info = $dbh->type_info($type_num);
297             $type_name = $type_info->{TYPE_NAME} if $type_info;
298             $colinfo->{data_type} = $type_name if $type_name;
299         }
300     }
301
302     return \%result;
303 }
304
305 # Override this in vendor class to return any additional column
306 # attributes
307 sub _extra_column_info {}
308
309 =head1 SEE ALSO
310
311 L<DBIx::Class::Schema::Loader>
312
313 =cut
314
315 1;