initial hackage, will be cleaned up
[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
457c3335 9our $VERSION = '0.04999_07';
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
49 $self->{_quoter} = $self->schema->storage->sql_maker->quote_char
50 || $dbh->get_info(29)
51 || q{"};
52
53 $self->{_namesep} = $self->schema->storage->sql_maker->name_sep
54 || $dbh->get_info(41)
55 || q{.};
56
57 # For our usage as regex matches, concatenating multiple quoter
58 # values works fine (e.g. s/\Q<>\E// if quoter was [ '<', '>' ])
59 if( ref $self->{_quoter} eq 'ARRAY') {
60 $self->{_quoter} = join(q{}, @{$self->{_quoter}});
61 }
62
63 $self->_setup;
64
65 $self;
66}
67
68# Override this in vendor modules to do things at the end of ->new()
69sub _setup { }
70
6ae3f335 71# Override this in vendor module to load a subclass if necessary
72sub _rebless { }
73
996be9ee 74# Returns an array of table names
75sub _tables_list {
76 my $self = shift;
77
78 my $dbh = $self->schema->storage->dbh;
79 my @tables = $dbh->tables(undef, $self->db_schema, '%', '%');
80 s/\Q$self->{_quoter}\E//g for @tables;
81 s/^.*\Q$self->{_namesep}\E// for @tables;
82
83 return @tables;
84}
85
12af3806 86=head2 load
87
88We override L<DBIx::Class::Schema::Loader::Base/load> here to hook in our localized settings for C<$dbh> error handling.
89
90=cut
91
92sub load {
93 my $self = shift;
94
95 local $self->schema->storage->dbh->{RaiseError} = 1;
96 local $self->schema->storage->dbh->{PrintError} = 0;
97 $self->next::method(@_);
98}
99
996be9ee 100# Returns an arrayref of column names
101sub _table_columns {
102 my ($self, $table) = @_;
103
104 my $dbh = $self->schema->storage->dbh;
105
106 if($self->{db_schema}) {
107 $table = $self->{db_schema} . $self->{_namesep} . $table;
108 }
109
a0cc2498 110 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
996be9ee 111 $sth->execute;
3b7f80f9 112 my $retval = \@{$sth->{NAME_lc}};
113 $sth->finish;
114
115 $retval;
996be9ee 116}
117
118# Returns arrayref of pk col names
119sub _table_pk_info {
fd589700 120 my ($self, $table) = @_;
996be9ee 121
122 my $dbh = $self->schema->storage->dbh;
123
124 my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table);
125 s/\Q$self->{_quoter}\E//g for @primary;
126
127 return \@primary;
128}
129
fd589700 130# Override this for vendor-specific uniq info
996be9ee 131sub _table_uniq_info {
fd589700 132 my ($self, $table) = @_;
133
134 my $dbh = $self->schema->storage->dbh;
135 if(!$dbh->can('statistics_info')) {
136 warn "No UNIQUE constraint information can be gathered for this vendor";
137 return [];
138 }
139
140 my %indices;
141 my $sth = $dbh->statistics_info(undef, $self->db_schema, $table, 1, 1);
142 while(my $row = $sth->fetchrow_hashref) {
143 # skip table-level stats, conditional indexes, and any index missing
144 # critical fields
145 next if $row->{TYPE} eq 'table'
146 || defined $row->{FILTER_CONDITION}
147 || !$row->{INDEX_NAME}
148 || !defined $row->{ORDINAL_POSITION}
149 || !$row->{COLUMN_NAME};
150
151 $indices{$row->{INDEX_NAME}}->{$row->{ORDINAL_POSITION}} = $row->{COLUMN_NAME};
152 }
3b7f80f9 153 $sth->finish;
fd589700 154
155 my @retval;
156 foreach my $index_name (keys %indices) {
157 my $index = $indices{$index_name};
158 push(@retval, [ $index_name => [
159 map { $index->{$_} }
160 sort keys %$index
161 ]]);
162 }
163
164 return \@retval;
996be9ee 165}
166
167# Find relationships
168sub _table_fk_info {
169 my ($self, $table) = @_;
170
171 my $dbh = $self->schema->storage->dbh;
a168c1c4 172 my $sth = $dbh->foreign_key_info( '', $self->db_schema, '',
173 '', $self->db_schema, $table );
996be9ee 174 return [] if !$sth;
175
176 my %rels;
177
178 my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
179 while(my $raw_rel = $sth->fetchrow_arrayref) {
180 my $uk_tbl = $raw_rel->[2];
181 my $uk_col = lc $raw_rel->[3];
182 my $fk_col = lc $raw_rel->[7];
183 my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
184 $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
185 $uk_col =~ s/\Q$self->{_quoter}\E//g;
186 $fk_col =~ s/\Q$self->{_quoter}\E//g;
187 $relid =~ s/\Q$self->{_quoter}\E//g;
188 $rels{$relid}->{tbl} = $uk_tbl;
189 $rels{$relid}->{cols}->{$uk_col} = $fk_col;
190 }
3b7f80f9 191 $sth->finish;
996be9ee 192
193 my @rels;
194 foreach my $relid (keys %rels) {
195 push(@rels, {
196 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
197 local_columns => [ values %{$rels{$relid}->{cols}} ],
198 remote_table => $rels{$relid}->{tbl},
199 });
200 }
201
202 return \@rels;
203}
204
12af3806 205# ported in from DBIx::Class::Storage::DBI:
206sub _columns_info_for {
207 my ($self, $table) = @_;
208
209 my $dbh = $self->schema->storage->dbh;
210
211 if ($dbh->can('column_info')) {
212 my %result;
213 eval {
214 my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' );
12af3806 215 while ( my $info = $sth->fetchrow_hashref() ){
216 my %column_info;
217 $column_info{data_type} = $info->{TYPE_NAME};
218 $column_info{size} = $info->{COLUMN_SIZE};
219 $column_info{is_nullable} = $info->{NULLABLE} ? 1 : 0;
220 $column_info{default_value} = $info->{COLUMN_DEF};
221 my $col_name = $info->{COLUMN_NAME};
222 $col_name =~ s/^\"(.*)\"$/$1/;
223
a8df0345 224 my $extra_info = $self->_extra_column_info($info) || {};
78b7ccaa 225
a8df0345 226 $result{$col_name} = { %column_info, %$extra_info };
12af3806 227 }
3b7f80f9 228 $sth->finish;
12af3806 229 };
230 return \%result if !$@ && scalar keys %result;
231 }
232
233 if($self->db_schema) {
234 $table = $self->db_schema . $self->{_namesep} . $table;
235 }
236 my %result;
6ce0bcc3 237 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
12af3806 238 $sth->execute;
239 my @columns = @{$sth->{NAME_lc}};
240 for my $i ( 0 .. $#columns ){
241 my %column_info;
6ce0bcc3 242 $column_info{data_type} = $sth->{TYPE}->[$i];
12af3806 243 $column_info{size} = $sth->{PRECISION}->[$i];
244 $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
245
246 if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) {
247 $column_info{data_type} = $1;
248 $column_info{size} = $2;
249 }
250
a8df0345 251 my $extra_info = $self->_extra_column_info($table, $columns[$i], $sth, $i) || {};
78b7ccaa 252
a8df0345 253 $result{$columns[$i]} = { %column_info, %$extra_info };
12af3806 254 }
6ce0bcc3 255 $sth->finish;
256
257 foreach my $col (keys %result) {
258 my $colinfo = $result{$col};
259 my $type_num = $colinfo->{data_type};
260 my $type_name;
261 if(defined $type_num && $dbh->can('type_info')) {
262 my $type_info = $dbh->type_info($type_num);
263 $type_name = $type_info->{TYPE_NAME} if $type_info;
264 $colinfo->{data_type} = $type_name if $type_name;
265 }
266 }
12af3806 267
268 return \%result;
269}
270
a8df0345 271# Override this in vendor class to return any additional column
272# attributes
273sub _extra_column_info {}
c5baf131 274
996be9ee 275=head1 SEE ALSO
276
277L<DBIx::Class::Schema::Loader>
278
279=cut
280
2811;