1 package DBIx::Class::Schema::Loader::DBI::Oracle;
5 use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault';
8 use DBIx::Class::Schema::Loader::Utils qw/sigwarn_silencer/;
11 our $VERSION = '0.07037';
15 DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI
16 Oracle Implementation.
20 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
27 $self->next::method(@_);
29 my ($current_schema) = $self->dbh->selectrow_array('SELECT USER FROM DUAL');
31 $self->db_schema([ $current_schema ]) unless $self->db_schema;
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]);
38 if (not defined $self->preserve_case) {
39 $self->preserve_case(0);
41 elsif ($self->preserve_case) {
42 $self->schema->storage->sql_maker->quote_char('"');
43 $self->schema->storage->sql_maker->name_sep('.');
47 sub _build_name_sep { '.' }
52 # From http://www.adp-gmbh.ch/ora/misc/known_schemas.html
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/);
60 return ($self->next::method(@_), 'PLAN_TABLE');
64 my ($self, $schema) = @_;
66 return $self->dbh->tables(undef, $schema, '%', 'TABLE,VIEW');
72 # silence a warning from older DBD::Oracles in tests
73 local $SIG{__WARN__} = sigwarn_silencer(
74 qr/^Field \d+ has an Oracle type \(\d+\) which is not explicitly supported/
77 return $self->next::method(@_);
84 my $rels = $self->next::method(@_);
86 my $deferrable_sth = $self->dbh->prepare_cached(<<'EOF');
87 select deferrable from all_constraints
88 where owner = ? and table_name = ? and constraint_name = ?
91 foreach my $rel (@$rels) {
92 # Oracle does not have update rules
93 $rel->{attrs}{on_update} = 'NO ACTION';;
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}
100 $rel->{attrs}{is_deferrable} = $deferrable && $deferrable =~ /^DEFERRABLE/i ? 1 : 0;
106 sub _table_uniq_info {
107 my ($self, $table) = @_;
109 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
110 SELECT ac.constraint_name, acc.column_name
111 FROM all_constraints ac, all_cons_columns acc
112 WHERE 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'
116 ORDER BY acc.position
119 $sth->execute($table->name, $table->schema);
123 while(my $constr = $sth->fetchrow_arrayref) {
124 my $constr_name = $self->_lc($constr->[0]);
125 my $constr_col = $self->_lc($constr->[1]);
126 push @{$constr_names{$constr_name}}, $constr_col;
129 my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
137 my $table_comment = $self->next::method(@_);
139 return $table_comment if $table_comment;
141 ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name);
142 SELECT comments FROM all_tab_comments
145 AND (table_type = 'TABLE' OR table_type = 'VIEW')
148 return $table_comment
151 sub _column_comment {
153 my ($table, $column_number, $column_name) = @_;
155 my $column_comment = $self->next::method(@_);
157 return $column_comment if $column_comment;
159 ($column_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $self->_uc($column_name));
160 SELECT comments FROM all_col_comments
166 return $column_comment
169 sub _columns_info_for {
173 my $result = $self->next::method(@_);
175 local $self->dbh->{LongReadLen} = 1_000_000;
176 local $self->dbh->{LongTruncOk} = 1;
178 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
181 WHERE table_name = ? AND table_owner = ?
182 AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
185 $sth->execute($table->name, $table->schema);
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);
192 $result->{$col_name}{is_auto_increment} = 1;
194 $seq_schema = $self->_lc($seq_schema || $table->schema);
195 $seq_name = $self->_lc($seq_name);
197 $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
202 while (my ($col, $info) = each %$result) {
203 no warnings 'uninitialized';
205 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
206 SELECT data_type, data_length
208 WHERE column_name = ? AND table_name = ? AND owner = ?
210 $sth->execute($self->_uc($col), $table->name, $table->schema);
211 my ($data_type, $data_length) = $sth->fetchrow_array;
213 $data_type = lc $data_type;
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;
218 if ($data_type =~ /^u?rowid\z/i) {
219 $info->{size} = $data_length;
223 if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
224 delete $info->{size};
227 if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
228 if (ref $info->{size}) {
229 $info->{size} = $info->{size}[0] / 8;
232 $info->{size} = $info->{size} / 2;
235 elsif ($info->{data_type} =~ /^(?:var)?char2?\z/i) {
236 if (ref $info->{size}) {
237 $info->{size} = $info->{size}[0];
240 elsif (lc($info->{data_type}) =~ /^(?:number|decimal)\z/i) {
241 $info->{original}{data_type} = 'number';
242 $info->{data_type} = 'numeric';
244 if (try { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
245 $info->{original}{size} = $info->{size};
247 $info->{data_type} = 'integer';
248 delete $info->{size};
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;
254 if ($precision == 6) {
255 delete $info->{size};
258 $info->{size} = $precision;
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;
266 elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
267 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
269 if ($precision == 2) {
270 delete $info->{size};
273 $info->{size} = $precision;
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;
279 if ($day_precision == 2 && $second_precision == 6) {
280 delete $info->{size};
283 $info->{size} = [ $day_precision, $second_precision ];
286 elsif ($info->{data_type} =~ /^interval year to month\z/i && ref $info->{size}) {
287 my $precision = $info->{size}[0];
289 if ($precision == 2) {
290 delete $info->{size};
293 $info->{size} = $precision;
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};
301 elsif (lc($info->{data_type}) eq 'float') {
302 $info->{original}{data_type} = 'float';
303 $info->{original}{size} = $info->{size};
305 if ($info->{size} <= 63) {
306 $info->{data_type} = 'real';
309 $info->{data_type} = 'double precision';
311 delete $info->{size};
313 elsif (lc($info->{data_type}) eq 'double precision') {
314 $info->{original}{data_type} = 'float';
316 my $size = try { $info->{size}[0] };
318 $info->{original}{size} = $size;
321 $info->{data_type} = 'real';
323 delete $info->{size};
325 elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
326 delete $info->{size};
328 elsif ($info->{data_type} eq '-9104') {
329 $info->{data_type} = 'rowid';
330 delete $info->{size};
332 elsif ($info->{data_type} eq '-2') {
333 $info->{data_type} = 'raw';
334 $info->{size} = try { $info->{size}[0] / 2 };
336 elsif (lc($info->{data_type}) eq 'date') {
337 $info->{data_type} = 'datetime';
338 $info->{original}{data_type} = 'date';
340 elsif (lc($info->{data_type}) eq 'binary_float') {
341 $info->{data_type} = 'real';
342 $info->{original}{data_type} = 'binary_float';
344 elsif (lc($info->{data_type}) eq 'binary_double') {
345 $info->{data_type} = 'double precision';
346 $info->{original}{data_type} = 'binary_double';
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);
356 WHERE column_name = ? AND table_name = ? AND owner = ?
358 $sth->execute($self->_uc($col), $table->name, $table->schema);
359 my ($default) = $sth->fetchrow_array;
362 # this is mostly copied from ::DBI::QuotedDefault
363 if (defined $default) {
364 s/^\s+//, s/\s+\z// for $default;
366 if ($default =~ /^'(.*?)'\z/) {
367 $info->{default_value} = $1;
369 elsif ($default =~ /^(-?\d.*?)\z/) {
370 $info->{default_value} = $1;
372 elsif ($default =~ /^NULL\z/i) {
374 $info->{default_value} = \$null;
376 elsif ($default ne '') {
378 $info->{default_value} = \$val;
383 if ((try { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
384 my $current_timestamp = 'current_timestamp';
385 $info->{default_value} = \$current_timestamp;
387 my $sysdate = 'sysdate';
388 $info->{original}{default_value} = \$sysdate;
395 sub _dbh_column_info {
399 # try to avoid ORA-24345
400 local $dbh->{LongReadLen} = 1_000_000;
401 local $dbh->{LongTruncOk} = 1;
403 return $self->next::method(@_);
408 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
409 L<DBIx::Class::Schema::Loader::DBI>
413 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
417 This library is free software; you can redistribute it and/or modify it under
418 the same terms as Perl itself.
423 # vim:et sts=4 sw=4 tw=0: