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