release 0.07015
[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.07015';
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 ac.constraint_name, acc.column_name
95 FROM all_constraints ac, all_cons_columns acc
96 WHERE acc.table_name=? AND acc.owner = ?
97     AND ac.table_name = acc.table_name AND ac.owner = acc.owner
98     AND acc.constraint_name = ac.constraint_name
99     AND ac.constraint_type='U'
100 ORDER BY acc.position
101 EOF
102
103     $sth->execute($table->name, $table->schema);
104
105     my %constr_names;
106
107     while(my $constr = $sth->fetchrow_arrayref) {
108         my $constr_name = $self->_lc($constr->[0]);
109         my $constr_col  = $self->_lc($constr->[1]);
110         push @{$constr_names{$constr_name}}, $constr_col;
111     }
112     
113     my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
114     return \@uniqs;
115 }
116
117 sub _table_comment {
118     my $self = shift;
119     my ($table) = @_;
120
121     my $table_comment = $self->next::method(@_);
122
123     return $table_comment if $table_comment;
124
125     ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name);
126 SELECT comments FROM all_tab_comments
127 WHERE owner = ? 
128   AND table_name = ?
129   AND (table_type = 'TABLE' OR table_type = 'VIEW')
130 EOF
131
132     return $table_comment
133 }
134
135 sub _column_comment {
136     my $self = shift;
137     my ($table, $column_number, $column_name) = @_;
138
139     my $column_comment = $self->next::method(@_);
140
141     return $column_comment if $column_comment;
142
143     ($column_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $self->_uc($column_name));
144 SELECT comments FROM all_col_comments
145 WHERE owner = ? 
146   AND table_name = ?
147   AND column_name = ?
148 EOF
149
150     return $column_comment
151 }
152
153 sub _columns_info_for {
154     my $self = shift;
155     my ($table) = @_;
156
157     my $result = $self->next::method(@_);
158
159     local $self->dbh->{LongReadLen} = 100000;
160     local $self->dbh->{LongTruncOk} = 1;
161
162     my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
163 SELECT trigger_body
164 FROM all_triggers
165 WHERE table_name = ? AND table_owner = ?
166 AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
167 EOF
168
169     $sth->execute($table->name, $table->schema);
170
171     while (my ($trigger_body) = $sth->fetchrow_array) {
172         if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) {
173             if (my ($col_name) = $trigger_body =~ /:new\.(\w+)/i) {
174                 $col_name = $self->_lc($col_name);
175
176                 $result->{$col_name}{is_auto_increment} = 1;
177
178                 $seq_schema = $self->_lc($seq_schema || $table->schema);
179                 $seq_name   = $self->_lc($seq_name);
180
181                 $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
182             }
183         }
184     }
185
186     while (my ($col, $info) = each %$result) {
187         no warnings 'uninitialized';
188
189         if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
190             delete $info->{size};
191         }
192
193         if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
194             $info->{size} = $info->{size} / 2;
195         }
196         elsif (lc($info->{data_type}) eq 'number') {
197             $info->{original}{data_type} = 'number';
198             $info->{data_type}           = 'numeric';
199
200             if (eval { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
201                 $info->{original}{size} = $info->{size};
202
203                 $info->{data_type} = 'integer';
204                 delete $info->{size};
205             }
206         }
207         elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
208             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
209
210             if ($precision == 6) {
211                 delete $info->{size};
212             }
213             else {
214                 $info->{size} = $precision;
215             }
216         }
217         elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
218             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
219
220             if ($precision == 2) {
221                 delete $info->{size};
222             }
223             else {
224                 $info->{size} = $precision;
225             }
226         }
227         elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
228             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
229
230             if ($day_precision == 2 && $second_precision == 6) {
231                 delete $info->{size};
232             }
233             else {
234                 $info->{size} = [ $day_precision, $second_precision ];
235             }
236         }
237         elsif (lc($info->{data_type}) eq 'float') {
238             $info->{original}{data_type} = 'float';
239             $info->{original}{size}      = $info->{size};
240
241             if ($info->{size} <= 63) {
242                 $info->{data_type} = 'real';
243             }
244             else {
245                 $info->{data_type} = 'double precision';
246             }
247             delete $info->{size};
248         }
249         elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
250             delete $info->{size};
251         }
252         elsif (lc($info->{data_type}) eq 'date') {
253             $info->{data_type}           = 'datetime';
254             $info->{original}{data_type} = 'date';
255         }
256         elsif (lc($info->{data_type}) eq 'binary_float') {
257             $info->{data_type}           = 'real';
258             $info->{original}{data_type} = 'binary_float';
259         } 
260         elsif (lc($info->{data_type}) eq 'binary_double') {
261             $info->{data_type}           = 'double precision';
262             $info->{original}{data_type} = 'binary_double';
263         } 
264
265         if ((eval { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
266             my $current_timestamp  = 'current_timestamp';
267             $info->{default_value} = \$current_timestamp;
268
269             my $sysdate = 'sysdate';
270             $info->{original}{default_value} = \$sysdate;
271         }
272     }
273
274     return $result;
275 }
276
277 =head1 SEE ALSO
278
279 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
280 L<DBIx::Class::Schema::Loader::DBI>
281
282 =head1 AUTHOR
283
284 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
285
286 =head1 LICENSE
287
288 This library is free software; you can redistribute it and/or modify it under
289 the same terms as Perl itself.
290
291 =cut
292
293 1;
294 # vim:et sts=4 sw=4 tw=0: