release 0.07022
[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 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault';
6 use mro 'c3';
7 use Try::Tiny;
8 use namespace::clean;
9
10 our $VERSION = '0.07022';
11
12 =head1 NAME
13
14 DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI 
15 Oracle Implementation.
16
17 =head1 DESCRIPTION
18
19 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
20
21 =cut
22
23 sub _setup {
24     my $self = shift;
25
26     $self->next::method(@_);
27
28     my ($current_schema) = $self->dbh->selectrow_array('SELECT USER FROM DUAL');
29
30     $self->db_schema([ $current_schema ]) unless $self->db_schema;
31
32     if (@{ $self->db_schema } == 1 && $self->db_schema->[0] ne '%'
33         && lc($self->db_schema->[0]) ne lc($current_schema)) {
34         $self->dbh->do('ALTER SESSION SET current_schema=' . $self->db_schema->[0]);
35     }
36
37     if (not defined $self->preserve_case) {
38         $self->preserve_case(0);
39     }
40     elsif ($self->preserve_case) {
41         $self->schema->storage->sql_maker->quote_char('"');
42         $self->schema->storage->sql_maker->name_sep('.');
43     }
44 }
45
46 sub _build_name_sep { '.' }
47
48 sub _system_schemas {
49     my $self = shift;
50
51     # From http://www.adp-gmbh.ch/ora/misc/known_schemas.html
52
53     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/);
54 }
55
56 sub _system_tables {
57     my $self = shift;
58
59     return ($self->next::method(@_), 'PLAN_TABLE');
60 }
61
62 sub _dbh_tables {
63     my ($self, $schema) = @_;
64
65     return $self->dbh->tables(undef, $schema, '%', 'TABLE,VIEW');
66 }
67
68 sub _filter_tables {
69     my $self = shift;
70
71     # silence a warning from older DBD::Oracles in tests
72     my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
73     local $SIG{__WARN__} = sub {
74         $warn_handler->(@_)
75         unless $_[0] =~ /^Field \d+ has an Oracle type \(\d+\) which is not explicitly supported/;
76     };
77
78     return $self->next::method(@_);
79 }
80
81 sub _table_uniq_info {
82     my ($self, $table) = @_;
83
84     my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
85 SELECT ac.constraint_name, acc.column_name
86 FROM all_constraints ac, all_cons_columns acc
87 WHERE acc.table_name=? AND acc.owner = ?
88     AND ac.table_name = acc.table_name AND ac.owner = acc.owner
89     AND acc.constraint_name = ac.constraint_name
90     AND ac.constraint_type='U'
91 ORDER BY acc.position
92 EOF
93
94     $sth->execute($table->name, $table->schema);
95
96     my %constr_names;
97
98     while(my $constr = $sth->fetchrow_arrayref) {
99         my $constr_name = $self->_lc($constr->[0]);
100         my $constr_col  = $self->_lc($constr->[1]);
101         push @{$constr_names{$constr_name}}, $constr_col;
102     }
103     
104     my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
105     return \@uniqs;
106 }
107
108 sub _table_comment {
109     my $self = shift;
110     my ($table) = @_;
111
112     my $table_comment = $self->next::method(@_);
113
114     return $table_comment if $table_comment;
115
116     ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name);
117 SELECT comments FROM all_tab_comments
118 WHERE owner = ? 
119   AND table_name = ?
120   AND (table_type = 'TABLE' OR table_type = 'VIEW')
121 EOF
122
123     return $table_comment
124 }
125
126 sub _column_comment {
127     my $self = shift;
128     my ($table, $column_number, $column_name) = @_;
129
130     my $column_comment = $self->next::method(@_);
131
132     return $column_comment if $column_comment;
133
134     ($column_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $self->_uc($column_name));
135 SELECT comments FROM all_col_comments
136 WHERE owner = ? 
137   AND table_name = ?
138   AND column_name = ?
139 EOF
140
141     return $column_comment
142 }
143
144 sub _columns_info_for {
145     my $self = shift;
146     my ($table) = @_;
147
148     my $result = $self->next::method(@_);
149
150     local $self->dbh->{LongReadLen} = 1_000_000;
151     local $self->dbh->{LongTruncOk} = 1;
152
153     my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
154 SELECT trigger_body
155 FROM all_triggers
156 WHERE table_name = ? AND table_owner = ?
157 AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
158 EOF
159
160     $sth->execute($table->name, $table->schema);
161
162     while (my ($trigger_body) = $sth->fetchrow_array) {
163         if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) {
164             if (my ($col_name) = $trigger_body =~ /:new\.(\w+)/i) {
165                 $col_name = $self->_lc($col_name);
166
167                 $result->{$col_name}{is_auto_increment} = 1;
168
169                 $seq_schema = $self->_lc($seq_schema || $table->schema);
170                 $seq_name   = $self->_lc($seq_name);
171
172                 $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
173             }
174         }
175     }
176
177     while (my ($col, $info) = each %$result) {
178         no warnings 'uninitialized';
179
180         my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
181 SELECT data_type, data_length
182 FROM all_tab_columns
183 WHERE column_name = ? AND table_name = ? AND owner = ?
184 EOF
185         $sth->execute($self->_uc($col), $table->name, $table->schema);
186         my ($data_type, $data_length) = $sth->fetchrow_array;
187         $sth->finish;
188         $data_type = lc $data_type;
189
190         if ($data_type =~ /^(?:n(?:var)?char2?|u?rowid|nclob|timestamp\(\d+\)(?: with(?: local)? time zone)?|binary_(?:float|double))\z/i) {
191             $info->{data_type} = $data_type;
192
193             if ($data_type =~ /^u?rowid\z/i) {
194                 $info->{size} = $data_length;
195             }
196         }
197
198         if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
199             delete $info->{size};
200         }
201
202         if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
203             if (ref $info->{size}) {
204                 $info->{size} = $info->{size}[0] / 8;
205             }
206             else {
207                 $info->{size} = $info->{size} / 2;
208             }
209         }
210         elsif ($info->{data_type} =~ /^(?:var)?char2?\z/i) {
211             if (ref $info->{size}) {
212                 $info->{size} = $info->{size}[0];
213             }
214         }
215         elsif (lc($info->{data_type}) =~ /^(?:number|decimal)\z/i) {
216             $info->{original}{data_type} = 'number';
217             $info->{data_type}           = 'numeric';
218
219             if (try { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
220                 $info->{original}{size} = $info->{size};
221
222                 $info->{data_type} = 'integer';
223                 delete $info->{size};
224             }
225         }
226         elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
227             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
228
229             if ($precision == 6) {
230                 delete $info->{size};
231             }
232             else {
233                 $info->{size} = $precision;
234             }
235         }
236         elsif ($info->{data_type} =~ /timestamp/i && ref $info->{size} && $info->{size}[0] == 0) {
237             my $size = $info->{size}[1];
238             delete $info->{size};
239             $info->{size} = $size unless $size == 6;
240         }
241         elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
242             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
243
244             if ($precision == 2) {
245                 delete $info->{size};
246             }
247             else {
248                 $info->{size} = $precision;
249             }
250         }
251         elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
252             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
253
254             if ($day_precision == 2 && $second_precision == 6) {
255                 delete $info->{size};
256             }
257             else {
258                 $info->{size} = [ $day_precision, $second_precision ];
259             }
260         }
261         elsif ($info->{data_type} =~ /^interval year to month\z/i && ref $info->{size}) {
262             my $precision = $info->{size}[0];
263
264             if ($precision == 2) {
265                 delete $info->{size};
266             }
267             else {
268                 $info->{size} = $precision;
269             }
270         }
271         elsif ($info->{data_type} =~ /^interval day to second\z/i && ref $info->{size}) {
272             if ($info->{size}[0] == 2 && $info->{size}[1] == 6) {
273                 delete $info->{size};
274             }
275         }
276         elsif (lc($info->{data_type}) eq 'float') {
277             $info->{original}{data_type} = 'float';
278             $info->{original}{size}      = $info->{size};
279
280             if ($info->{size} <= 63) {
281                 $info->{data_type} = 'real';
282             }
283             else {
284                 $info->{data_type} = 'double precision';
285             }
286             delete $info->{size};
287         }
288         elsif (lc($info->{data_type}) eq 'double precision') {
289             $info->{original}{data_type} = 'float';
290
291             my $size = try { $info->{size}[0] };
292
293             $info->{original}{size} = $size;
294
295             if ($size <= 63) {
296                 $info->{data_type} = 'real';
297             }
298             delete $info->{size};
299         }
300         elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
301             delete $info->{size};
302         }
303         elsif ($info->{data_type} eq '-9104') {
304             $info->{data_type} = 'rowid';
305             delete $info->{size};
306         }
307         elsif ($info->{data_type} eq '-2') {
308             $info->{data_type} = 'raw';
309             $info->{size} = try { $info->{size}[0] / 2 };
310         }
311         elsif (lc($info->{data_type}) eq 'date') {
312             $info->{data_type}           = 'datetime';
313             $info->{original}{data_type} = 'date';
314         }
315         elsif (lc($info->{data_type}) eq 'binary_float') {
316             $info->{data_type}           = 'real';
317             $info->{original}{data_type} = 'binary_float';
318         } 
319         elsif (lc($info->{data_type}) eq 'binary_double') {
320             $info->{data_type}           = 'double precision';
321             $info->{original}{data_type} = 'binary_double';
322         }
323
324         # DEFAULT could be missed by ::DBI because of ORA-24345
325         if (not defined $info->{default_value}) {
326             local $self->dbh->{LongReadLen} = 1_000_000;
327             local $self->dbh->{LongTruncOk} = 1;
328             my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
329 SELECT data_default
330 FROM all_tab_columns
331 WHERE column_name = ? AND table_name = ? AND owner = ?
332 EOF
333             $sth->execute($self->_uc($col), $table->name, $table->schema);
334             my ($default) = $sth->fetchrow_array;
335             $sth->finish;
336
337             # this is mostly copied from ::DBI::QuotedDefault
338             if (defined $default) {
339                 s/^\s+//, s/\s+\z// for $default;
340
341                 if ($default =~ /^'(.*?)'\z/) {
342                     $info->{default_value} = $1;
343                 }
344                 elsif ($default =~ /^(-?\d.*?)\z/) {
345                     $info->{default_value} = $1;
346                 }
347                 elsif ($default =~ /^NULL\z/i) {
348                     my $null = 'null';
349                     $info->{default_value} = \$null;
350                 }
351                 elsif ($default ne '') {
352                     my $val = $default;
353                     $info->{default_value} = \$val;
354                 }
355             }
356         }
357
358         if ((try { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
359             my $current_timestamp  = 'current_timestamp';
360             $info->{default_value} = \$current_timestamp;
361
362             my $sysdate = 'sysdate';
363             $info->{original}{default_value} = \$sysdate;
364         }
365     }
366
367     return $result;
368 }
369
370 sub _dbh_column_info {
371     my $self  = shift;
372     my ($dbh) = @_;
373
374     # try to avoid ORA-24345
375     local $dbh->{LongReadLen} = 1_000_000;
376     local $dbh->{LongTruncOk} = 1;
377
378     return $self->next::method(@_);
379 }
380
381 =head1 SEE ALSO
382
383 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
384 L<DBIx::Class::Schema::Loader::DBI>
385
386 =head1 AUTHOR
387
388 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
389
390 =head1 LICENSE
391
392 This library is free software; you can redistribute it and/or modify it under
393 the same terms as Perl itself.
394
395 =cut
396
397 1;
398 # vim:et sts=4 sw=4 tw=0: