better type info for Oracle
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Oracle.pm
CommitLineData
d87d939a 1package DBIx::Class::Schema::Loader::DBI::Oracle;
e7262300 2
3use strict;
4use warnings;
6b0e47fc 5use base qw/
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
8/;
e7262300 9use Carp::Clan qw/^DBIx::Class/;
10use Class::C3;
11
9990e58f 12our $VERSION = '0.07000';
e7262300 13
14=head1 NAME
15
16DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI
17Oracle Implementation.
18
e7262300 19=head1 DESCRIPTION
20
21See L<DBIx::Class::Schema::Loader::Base>.
22
e7262300 23=cut
24
d0e184e9 25sub _setup {
26 my $self = shift;
27
28 $self->next::method(@_);
29
30 my $dbh = $self->schema->storage->dbh;
46065bcb 31
32 my ($current_schema) = $dbh->selectrow_array('SELECT USER FROM DUAL', {});
33
34 $self->{db_schema} ||= $current_schema;
35
36 if (lc($self->db_schema) ne lc($current_schema)) {
37 $dbh->do('ALTER SESSION SET current_schema=' . $self->db_schema);
38 }
bc1cb85e 39
40 if (not defined $self->preserve_case) {
41 $self->preserve_case(0);
42 }
d0e184e9 43}
44
075aff97 45sub _table_as_sql {
e7262300 46 my ($self, $table) = @_;
47
075aff97 48 return $self->_quote_table_name($table);
e7262300 49}
50
51sub _tables_list {
bfb43060 52 my ($self, $opts) = @_;
e7262300 53
54 my $dbh = $self->schema->storage->dbh;
55
56 my @tables;
57 for my $table ( $dbh->tables(undef, $self->db_schema, '%', 'TABLE,VIEW') ) { #catalog, schema, table, type
58 my $quoter = $dbh->get_info(29);
59 $table =~ s/$quoter//g;
60
61 # remove "user." (schema) prefixes
62 $table =~ s/\w+\.//;
63
64 next if $table eq 'PLAN_TABLE';
65 $table = lc $table;
66 push @tables, $1
67 if $table =~ /\A(\w+)\z/;
68 }
ffb03c96 69
bfb43060 70 return $self->_filter_tables(\@tables, $opts);
e7262300 71}
72
4337bddf 73sub _table_columns {
74 my ($self, $table) = @_;
75
76 my $dbh = $self->schema->storage->dbh;
77
78 my $sth = $dbh->column_info(undef, $self->db_schema, uc $table, '%');
79 return [ map lc($_->{COLUMN_NAME}), @{ $sth->fetchall_arrayref({ COLUMN_NAME => 1 }) || [] } ];
80}
81
e7262300 82sub _table_uniq_info {
83 my ($self, $table) = @_;
84
e7262300 85 my $dbh = $self->schema->storage->dbh;
86
87 my $sth = $dbh->prepare_cached(
65ab592d 88 q{
c7bf4194 89 SELECT constraint_name, acc.column_name
90 FROM all_constraints JOIN all_cons_columns acc USING (constraint_name)
8803e4ed 91 WHERE acc.table_name=? and acc.owner = ? AND constraint_type='U'
c7bf4194 92 ORDER BY acc.position
65ab592d 93 },
94 {}, 1);
e7262300 95
8803e4ed 96 $sth->execute(uc $table,$self->{db_schema} );
e7262300 97 my %constr_names;
98 while(my $constr = $sth->fetchrow_arrayref) {
65ab592d 99 my $constr_name = lc $constr->[0];
100 my $constr_def = lc $constr->[1];
e7262300 101 $constr_name =~ s/\Q$self->{_quoter}\E//;
102 $constr_def =~ s/\Q$self->{_quoter}\E//;
65ab592d 103 push @{$constr_names{$constr_name}}, $constr_def;
e7262300 104 }
65ab592d 105
106 my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
e7262300 107 return \@uniqs;
108}
109
110sub _table_pk_info {
65ab592d 111 my ($self, $table) = @_;
112 return $self->next::method(uc $table);
e7262300 113}
114
115sub _table_fk_info {
116 my ($self, $table) = @_;
117
65ab592d 118 my $rels = $self->next::method(uc $table);
e7262300 119
65ab592d 120 foreach my $rel (@$rels) {
121 $rel->{remote_table} = lc $rel->{remote_table};
e7262300 122 }
123
65ab592d 124 return $rels;
125}
126
127sub _columns_info_for {
760fd65c 128 my ($self, $table) = (shift, shift);
e7262300 129
760fd65c 130 my $result = $self->next::method(uc $table, @_);
fb328d1a 131
fb328d1a 132 my $dbh = $self->schema->storage->dbh;
fb328d1a 133
760fd65c 134 my $sth = $dbh->prepare_cached(q{
135SELECT atc.column_name
136FROM all_triggers ut
137JOIN all_trigger_cols atc USING (trigger_name)
138WHERE atc.table_name = ?
139AND lower(column_usage) LIKE '%new%' AND lower(column_usage) LIKE '%out%'
140AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
141 }, {}, 1);
142
143 $sth->execute(uc $table);
144
145 while (my ($col_name) = $sth->fetchrow_array) {
146 $result->{lc $col_name}{is_auto_increment} = 1;
147 }
148
149 while (my ($col, $info) = each %$result) {
150 no warnings 'uninitialized';
151
152 if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
153 delete $info->{size};
154 }
155
156 if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
157 $info->{size} = $info->{size} / 2;
158 }
159 elsif (lc($info->{data_type}) eq 'number') {
160 $info->{data_type} = 'numeric';
161
162 if (eval { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
163 $info->{data_type} = 'integer';
164 delete $info->{size};
165 }
166 }
167 elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
168 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
169
170 if ($precision == 6) {
171 delete $info->{size};
172 }
173 else {
174 $info->{size} = $precision;
175 }
176 }
177 elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
178 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
179
180 if ($precision == 2) {
181 delete $info->{size};
182 }
183 else {
184 $info->{size} = $precision;
185 }
186 }
187 elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
188 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
189
190 if ($day_precision == 2 && $second_precision == 6) {
191 delete $info->{size};
192 }
193 else {
194 $info->{size} = [ $day_precision, $second_precision ];
195 }
196 }
197 elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
198 delete $info->{size};
199 }
200
201 if (eval { lc(${ $info->{default_value} }) eq 'sysdate' }) {
202 ${ $info->{default_value} } = 'current_timestamp';
203 }
fb328d1a 204 }
205
760fd65c 206 return $result;
fb328d1a 207}
208
e7262300 209=head1 SEE ALSO
210
211L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
212L<DBIx::Class::Schema::Loader::DBI>
213
214=head1 AUTHOR
215
9cc8e7e1 216See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
e7262300 217
be80bba7 218=head1 LICENSE
219
220This library is free software; you can redistribute it and/or modify it under
221the same terms as Perl itself.
fb328d1a 222
e7262300 223=cut
224
2251;
760fd65c 226# vim:et sts=4 sw=4 tw=0: