columns_info_for imported from DBIx::Class
[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 8use UNIVERSAL::require;
9
32f784fc 10our $VERSION = '0.03999_01';
11
996be9ee 12=head1 NAME
13
14DBIx::Class::Schema::Loader::DBI - DBIx::Class::Schema::Loader DBI Implementation.
15
16=head1 SYNOPSIS
17
18See L<DBIx::Class::Schema::Loader::Base>
19
20=head1 DESCRIPTION
21
22This is the base class for L<DBIx::Class::Schema::Loader::Base> classes for
23DBI-based storage backends, and implements the common functionality between them.
24
25See L<DBIx::Class::Schema::Loader::Base> for the available options.
26
27=head1 METHODS
28
29=head2 new
30
31Overlays L<DBIx::Class::Schema::Loader::Base/new> to do some DBI-specific
32things.
33
34=cut
35
36sub new {
37 my $self = shift->next::method(@_);
38
39 # rebless to vendor-specific class if it exists and loads
40 my $dbh = $self->schema->storage->dbh;
41 my $driver = $dbh->{Driver}->{Name};
42 my $subclass = 'DBIx::Class::Schema::Loader::DBI::' . $driver;
43 $subclass->require;
44 if($@ && $@ !~ /^Can't locate /) {
25328cc4 45 croak "Failed to require $subclass: $@";
996be9ee 46 }
47 elsif(!$@) {
48 bless $self, "DBIx::Class::Schema::Loader::DBI::${driver}";
49 }
50
51 # Set up the default quoting character and name seperators
52 $self->{_quoter} = $self->schema->storage->sql_maker->quote_char
53 || $dbh->get_info(29)
54 || q{"};
55
56 $self->{_namesep} = $self->schema->storage->sql_maker->name_sep
57 || $dbh->get_info(41)
58 || q{.};
59
60 # For our usage as regex matches, concatenating multiple quoter
61 # values works fine (e.g. s/\Q<>\E// if quoter was [ '<', '>' ])
62 if( ref $self->{_quoter} eq 'ARRAY') {
63 $self->{_quoter} = join(q{}, @{$self->{_quoter}});
64 }
65
66 $self->_setup;
67
68 $self;
69}
70
71# Override this in vendor modules to do things at the end of ->new()
72sub _setup { }
73
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
110 my $sth = $dbh->prepare("SELECT * FROM $table WHERE 1=0");
111 $sth->execute;
112 return \@{$sth->{NAME_lc}};
113}
114
115# Returns arrayref of pk col names
116sub _table_pk_info {
117 my ( $self, $table ) = @_;
118
119 my $dbh = $self->schema->storage->dbh;
120
121 my @primary = map { lc } $dbh->primary_key('', $self->db_schema, $table);
122 s/\Q$self->{_quoter}\E//g for @primary;
123
124 return \@primary;
125}
126
127# Override this for uniq info
128sub _table_uniq_info {
8f9d7ce5 129 warn "No UNIQUE constraint information can be gathered for this vendor";
996be9ee 130 return [];
131}
132
133# Find relationships
134sub _table_fk_info {
135 my ($self, $table) = @_;
136
137 my $dbh = $self->schema->storage->dbh;
138 my $sth = $dbh->foreign_key_info( '', '', '', '',
139 $self->db_schema, $table );
140 return [] if !$sth;
141
142 my %rels;
143
144 my $i = 1; # for unnamed rels, which hopefully have only 1 column ...
145 while(my $raw_rel = $sth->fetchrow_arrayref) {
146 my $uk_tbl = $raw_rel->[2];
147 my $uk_col = lc $raw_rel->[3];
148 my $fk_col = lc $raw_rel->[7];
149 my $relid = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
150 $uk_tbl =~ s/\Q$self->{_quoter}\E//g;
151 $uk_col =~ s/\Q$self->{_quoter}\E//g;
152 $fk_col =~ s/\Q$self->{_quoter}\E//g;
153 $relid =~ s/\Q$self->{_quoter}\E//g;
154 $rels{$relid}->{tbl} = $uk_tbl;
155 $rels{$relid}->{cols}->{$uk_col} = $fk_col;
156 }
157
158 my @rels;
159 foreach my $relid (keys %rels) {
160 push(@rels, {
161 remote_columns => [ keys %{$rels{$relid}->{cols}} ],
162 local_columns => [ values %{$rels{$relid}->{cols}} ],
163 remote_table => $rels{$relid}->{tbl},
164 });
165 }
166
167 return \@rels;
168}
169
12af3806 170# ported in from DBIx::Class::Storage::DBI:
171sub _columns_info_for {
172 my ($self, $table) = @_;
173
174 my $dbh = $self->schema->storage->dbh;
175
176 if ($dbh->can('column_info')) {
177 my %result;
178 eval {
179 my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' );
180 $sth->execute();
181 while ( my $info = $sth->fetchrow_hashref() ){
182 my %column_info;
183 $column_info{data_type} = $info->{TYPE_NAME};
184 $column_info{size} = $info->{COLUMN_SIZE};
185 $column_info{is_nullable} = $info->{NULLABLE} ? 1 : 0;
186 $column_info{default_value} = $info->{COLUMN_DEF};
187 my $col_name = $info->{COLUMN_NAME};
188 $col_name =~ s/^\"(.*)\"$/$1/;
189
190 $result{$col_name} = \%column_info;
191 }
192 };
193 return \%result if !$@ && scalar keys %result;
194 }
195
196 if($self->db_schema) {
197 $table = $self->db_schema . $self->{_namesep} . $table;
198 }
199 my %result;
200 my $sth = $dbh->prepare("SELECT * FROM $table WHERE 1=0");
201 $sth->execute;
202 my @columns = @{$sth->{NAME_lc}};
203 for my $i ( 0 .. $#columns ){
204 my %column_info;
205 my $type_num = $sth->{TYPE}->[$i];
206 my $type_name;
207 if(defined $type_num && $dbh->can('type_info')) {
208 my $type_info = $dbh->type_info($type_num);
209 $type_name = $type_info->{TYPE_NAME} if $type_info;
210 }
211 $column_info{data_type} = $type_name ? $type_name : $type_num;
212 $column_info{size} = $sth->{PRECISION}->[$i];
213 $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
214
215 if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) {
216 $column_info{data_type} = $1;
217 $column_info{size} = $2;
218 }
219
220 $result{$columns[$i]} = \%column_info;
221 }
222
223 return \%result;
224}
225
226
996be9ee 227=head1 SEE ALSO
228
229L<DBIx::Class::Schema::Loader>
230
231=cut
232
2331;