1 package DBIx::Class::Schema::Loader::DBI::Oracle;
5 use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault';
10 our $VERSION = '0.07027';
14 DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI
15 Oracle Implementation.
19 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
26 $self->next::method(@_);
28 my ($current_schema) = $self->dbh->selectrow_array('SELECT USER FROM DUAL');
30 $self->db_schema([ $current_schema ]) unless $self->db_schema;
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]);
37 if (not defined $self->preserve_case) {
38 $self->preserve_case(0);
40 elsif ($self->preserve_case) {
41 $self->schema->storage->sql_maker->quote_char('"');
42 $self->schema->storage->sql_maker->name_sep('.');
46 sub _build_name_sep { '.' }
51 # From http://www.adp-gmbh.ch/ora/misc/known_schemas.html
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/);
59 return ($self->next::method(@_), 'PLAN_TABLE');
63 my ($self, $schema) = @_;
65 return $self->dbh->tables(undef, $schema, '%', 'TABLE,VIEW');
71 # silence a warning from older DBD::Oracles in tests
72 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
73 local $SIG{__WARN__} = sub {
75 unless $_[0] =~ /^Field \d+ has an Oracle type \(\d+\) which is not explicitly supported/;
78 return $self->next::method(@_);
81 sub _table_uniq_info {
82 my ($self, $table) = @_;
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'
94 $sth->execute($table->name, $table->schema);
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;
104 my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
112 my $table_comment = $self->next::method(@_);
114 return $table_comment if $table_comment;
116 ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name);
117 SELECT comments FROM all_tab_comments
120 AND (table_type = 'TABLE' OR table_type = 'VIEW')
123 return $table_comment
126 sub _column_comment {
128 my ($table, $column_number, $column_name) = @_;
130 my $column_comment = $self->next::method(@_);
132 return $column_comment if $column_comment;
134 ($column_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $self->_uc($column_name));
135 SELECT comments FROM all_col_comments
141 return $column_comment
144 sub _columns_info_for {
148 my $result = $self->next::method(@_);
150 local $self->dbh->{LongReadLen} = 1_000_000;
151 local $self->dbh->{LongTruncOk} = 1;
153 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
156 WHERE table_name = ? AND table_owner = ?
157 AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
160 $sth->execute($table->name, $table->schema);
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);
167 $result->{$col_name}{is_auto_increment} = 1;
169 $seq_schema = $self->_lc($seq_schema || $table->schema);
170 $seq_name = $self->_lc($seq_name);
172 $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
177 while (my ($col, $info) = each %$result) {
178 no warnings 'uninitialized';
180 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
181 SELECT data_type, data_length
183 WHERE column_name = ? AND table_name = ? AND owner = ?
185 $sth->execute($self->_uc($col), $table->name, $table->schema);
186 my ($data_type, $data_length) = $sth->fetchrow_array;
188 $data_type = lc $data_type;
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;
193 if ($data_type =~ /^u?rowid\z/i) {
194 $info->{size} = $data_length;
198 if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
199 delete $info->{size};
202 if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
203 if (ref $info->{size}) {
204 $info->{size} = $info->{size}[0] / 8;
207 $info->{size} = $info->{size} / 2;
210 elsif ($info->{data_type} =~ /^(?:var)?char2?\z/i) {
211 if (ref $info->{size}) {
212 $info->{size} = $info->{size}[0];
215 elsif (lc($info->{data_type}) =~ /^(?:number|decimal)\z/i) {
216 $info->{original}{data_type} = 'number';
217 $info->{data_type} = 'numeric';
219 if (try { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
220 $info->{original}{size} = $info->{size};
222 $info->{data_type} = 'integer';
223 delete $info->{size};
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;
229 if ($precision == 6) {
230 delete $info->{size};
233 $info->{size} = $precision;
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;
241 elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
242 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
244 if ($precision == 2) {
245 delete $info->{size};
248 $info->{size} = $precision;
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;
254 if ($day_precision == 2 && $second_precision == 6) {
255 delete $info->{size};
258 $info->{size} = [ $day_precision, $second_precision ];
261 elsif ($info->{data_type} =~ /^interval year to month\z/i && ref $info->{size}) {
262 my $precision = $info->{size}[0];
264 if ($precision == 2) {
265 delete $info->{size};
268 $info->{size} = $precision;
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};
276 elsif (lc($info->{data_type}) eq 'float') {
277 $info->{original}{data_type} = 'float';
278 $info->{original}{size} = $info->{size};
280 if ($info->{size} <= 63) {
281 $info->{data_type} = 'real';
284 $info->{data_type} = 'double precision';
286 delete $info->{size};
288 elsif (lc($info->{data_type}) eq 'double precision') {
289 $info->{original}{data_type} = 'float';
291 my $size = try { $info->{size}[0] };
293 $info->{original}{size} = $size;
296 $info->{data_type} = 'real';
298 delete $info->{size};
300 elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
301 delete $info->{size};
303 elsif ($info->{data_type} eq '-9104') {
304 $info->{data_type} = 'rowid';
305 delete $info->{size};
307 elsif ($info->{data_type} eq '-2') {
308 $info->{data_type} = 'raw';
309 $info->{size} = try { $info->{size}[0] / 2 };
311 elsif (lc($info->{data_type}) eq 'date') {
312 $info->{data_type} = 'datetime';
313 $info->{original}{data_type} = 'date';
315 elsif (lc($info->{data_type}) eq 'binary_float') {
316 $info->{data_type} = 'real';
317 $info->{original}{data_type} = 'binary_float';
319 elsif (lc($info->{data_type}) eq 'binary_double') {
320 $info->{data_type} = 'double precision';
321 $info->{original}{data_type} = 'binary_double';
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);
331 WHERE column_name = ? AND table_name = ? AND owner = ?
333 $sth->execute($self->_uc($col), $table->name, $table->schema);
334 my ($default) = $sth->fetchrow_array;
337 # this is mostly copied from ::DBI::QuotedDefault
338 if (defined $default) {
339 s/^\s+//, s/\s+\z// for $default;
341 if ($default =~ /^'(.*?)'\z/) {
342 $info->{default_value} = $1;
344 elsif ($default =~ /^(-?\d.*?)\z/) {
345 $info->{default_value} = $1;
347 elsif ($default =~ /^NULL\z/i) {
349 $info->{default_value} = \$null;
351 elsif ($default ne '') {
353 $info->{default_value} = \$val;
358 if ((try { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
359 my $current_timestamp = 'current_timestamp';
360 $info->{default_value} = \$current_timestamp;
362 my $sysdate = 'sysdate';
363 $info->{original}{default_value} = \$sysdate;
370 sub _dbh_column_info {
374 # try to avoid ORA-24345
375 local $dbh->{LongReadLen} = 1_000_000;
376 local $dbh->{LongTruncOk} = 1;
378 return $self->next::method(@_);
383 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
384 L<DBIx::Class::Schema::Loader::DBI>
388 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
392 This library is free software; you can redistribute it and/or modify it under
393 the same terms as Perl itself.
398 # vim:et sts=4 sw=4 tw=0: