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