multi db_schema support
[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 mro 'c3';
10
11 our $VERSION = '0.07010';
12
13 =head1 NAME
14
15 DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI 
16 Oracle Implementation.
17
18 =head1 DESCRIPTION
19
20 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
21
22 =cut
23
24 sub _setup {
25     my $self = shift;
26
27     $self->next::method(@_);
28
29     my ($current_schema) = $self->dbh->selectrow_array('SELECT USER FROM DUAL');
30
31     $self->db_schema([ $current_schema ]) unless $self->db_schema;
32
33     if (@{ $self->db_schema } == 1 && $self->db_schema->[0] ne '%'
34         && lc($self->db_schema->[0]) ne lc($current_schema)) {
35         $self->dbh->do('ALTER SESSION SET current_schema=' . $self->db_schema->[0]);
36     }
37
38     if (not defined $self->preserve_case) {
39         $self->preserve_case(0);
40     }
41     elsif ($self->preserve_case) {
42         $self->schema->storage->sql_maker->quote_char('"');
43         $self->schema->storage->sql_maker->name_sep('.');
44     }
45 }
46
47 sub _build_name_sep { '.' }
48
49 sub _system_schemas {
50     my $self = shift;
51
52     # From http://www.adp-gmbh.ch/ora/misc/known_schemas.html
53
54     return ($self->next::method(@_), qw/ANONYMOUS APEX_PUBLIC_USER APEX_030200 APPQOSSYS CTXSYS DBSNMP DIP DMSYS EXFSYS LBACSYS MDDATA MDSYS MGMT_VIEW OLAPSYS ORACLE_OCM ORDDATA ORDPLUGINS ORDSYS OUTLN SI_INFORMTN_SCHEMA SPATIAL_CSW_ADMIN_USR SPATIAL_WFS_ADMIN_USR SYS SYSMAN SYSTEM TRACESRV MTSSYS OASPUBLIC OWBSYS OWBSYS_AUDIT WEBSYS WK_PROXY WKSYS WK_TEST WMSYS XDB OSE$HTTP$ADMIN AURORA$JIS$UTILITY$ AURORA$ORB$UNAUTHENTICATED/, qr/^FLOWS_\d\d\d\d\d\d\z/);
55 }
56
57 sub _system_tables {
58     my $self = shift;
59
60     return ($self->next::method(@_), 'PLAN_TABLE');
61 }
62
63 sub _dbh_tables {
64     my ($self, $schema) = @_;
65
66     return $self->dbh->tables(undef, $schema, '%', 'TABLE,VIEW');
67 }
68
69 sub _filter_tables {
70     my $self = shift;
71
72     # silence a warning from older DBD::Oracles in tests
73     my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
74     local $SIG{__WARN__} = sub {
75         $warn_handler->(@_)
76         unless $_[0] =~ /^Field \d+ has an Oracle type \(\d+\) which is not explicitly supported/;
77     };
78
79     return $self->next::method(@_);
80 }
81
82 sub _table_columns {
83     my ($self, $table) = @_;
84
85     my $sth = $self->dbh->column_info(undef, $table->schema, $table, '%');
86
87     return [ map $self->_lc($_->{COLUMN_NAME}), @{ $sth->fetchall_arrayref({ COLUMN_NAME => 1 }) || [] } ];
88 }
89
90 sub _table_uniq_info {
91     my ($self, $table) = @_;
92
93     my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
94 SELECT constraint_name, acc.column_name
95 FROM all_constraints
96 JOIN all_cons_columns acc USING (constraint_name)
97 WHERE acc.table_name=? and acc.owner = ? AND constraint_type='U'
98 ORDER BY acc.position
99 EOF
100
101     $sth->execute($table->name, $table->schema);
102
103     my %constr_names;
104
105     while(my $constr = $sth->fetchrow_arrayref) {
106         my $constr_name = $self->_lc($constr->[0]);
107         my $constr_col  = $self->_lc($constr->[1]);
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_comment {
116     my $self = shift;
117     my ($table) = @_;
118
119     my $table_comment = $self->next::method(@_);
120
121     return $table_comment if $table_comment;
122
123     ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name);
124 SELECT comments FROM all_tab_comments
125 WHERE owner = ? 
126   AND table_name = ?
127   AND (table_type = 'TABLE' OR table_type = 'VIEW')
128 EOF
129
130     return $table_comment
131 }
132
133 sub _column_comment {
134     my $self = shift;
135     my ($table, $column_number, $column_name) = @_;
136
137     my $column_comment = $self->next::method(@_);
138
139     return $column_comment if $column_comment;
140
141     ($column_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $self->_uc($column_name));
142 SELECT comments FROM all_col_comments
143 WHERE owner = ? 
144   AND table_name = ?
145   AND column_name = ?
146 EOF
147
148     return $column_comment
149 }
150
151 sub _columns_info_for {
152     my $self = shift;
153     my ($table) = @_;
154
155     my $result = $self->next::method(@_);
156
157     local $self->dbh->{LongReadLen} = 100000;
158     local $self->dbh->{LongTruncOk} = 1;
159
160     my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
161 SELECT atc.column_name, ut.trigger_body
162 FROM all_triggers ut
163 JOIN all_trigger_cols atc USING (trigger_name)
164 WHERE atc.table_name = ?
165 AND lower(column_usage) LIKE '%new%' AND lower(column_usage) LIKE '%out%'
166 AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
167 EOF
168
169     $sth->execute($table->name);
170
171     while (my ($col_name, $trigger_body) = $sth->fetchrow_array) {
172         $col_name = $self->_lc($col_name);
173
174         $result->{$col_name}{is_auto_increment} = 1;
175
176         if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) {
177             $seq_schema = $self->_lc($seq_schema || $table->schema);
178             $seq_name   = $self->_lc($seq_name);
179
180             $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
181         }
182     }
183
184     while (my ($col, $info) = each %$result) {
185         no warnings 'uninitialized';
186
187         if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
188             delete $info->{size};
189         }
190
191         if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
192             $info->{size} = $info->{size} / 2;
193         }
194         elsif (lc($info->{data_type}) eq 'number') {
195             $info->{original}{data_type} = 'number';
196             $info->{data_type}           = 'numeric';
197
198             if (eval { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
199                 $info->{original}{size} = $info->{size};
200
201                 $info->{data_type} = 'integer';
202                 delete $info->{size};
203             }
204         }
205         elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
206             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
207
208             if ($precision == 6) {
209                 delete $info->{size};
210             }
211             else {
212                 $info->{size} = $precision;
213             }
214         }
215         elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
216             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
217
218             if ($precision == 2) {
219                 delete $info->{size};
220             }
221             else {
222                 $info->{size} = $precision;
223             }
224         }
225         elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
226             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
227
228             if ($day_precision == 2 && $second_precision == 6) {
229                 delete $info->{size};
230             }
231             else {
232                 $info->{size} = [ $day_precision, $second_precision ];
233             }
234         }
235         elsif (lc($info->{data_type}) eq 'float') {
236             $info->{original}{data_type} = 'float';
237             $info->{original}{size}      = $info->{size};
238
239             if ($info->{size} <= 63) {
240                 $info->{data_type} = 'real';
241             }
242             else {
243                 $info->{data_type} = 'double precision';
244             }
245             delete $info->{size};
246         }
247         elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
248             delete $info->{size};
249         }
250         elsif (lc($info->{data_type}) eq 'date') {
251             $info->{data_type}           = 'datetime';
252             $info->{original}{data_type} = 'date';
253         }
254         elsif (lc($info->{data_type}) eq 'binary_float') {
255             $info->{data_type}           = 'real';
256             $info->{original}{data_type} = 'binary_float';
257         } 
258         elsif (lc($info->{data_type}) eq 'binary_double') {
259             $info->{data_type}           = 'double precision';
260             $info->{original}{data_type} = 'binary_double';
261         } 
262
263         if ((eval { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
264             my $current_timestamp  = 'current_timestamp';
265             $info->{default_value} = \$current_timestamp;
266
267             my $sysdate = 'sysdate';
268             $info->{original}{default_value} = \$sysdate;
269         }
270     }
271
272     return $result;
273 }
274
275 =head1 SEE ALSO
276
277 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
278 L<DBIx::Class::Schema::Loader::DBI>
279
280 =head1 AUTHOR
281
282 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
283
284 =head1 LICENSE
285
286 This library is free software; you can redistribute it and/or modify it under
287 the same terms as Perl itself.
288
289 =cut
290
291 1;
292 # vim:et sts=4 sw=4 tw=0: