1 package DBIx::Class::Schema::Loader::DBI::Oracle;
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
13 our $VERSION = '0.07018';
17 DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI
18 Oracle Implementation.
22 See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
29 $self->next::method(@_);
31 my ($current_schema) = $self->dbh->selectrow_array('SELECT USER FROM DUAL');
33 $self->db_schema([ $current_schema ]) unless $self->db_schema;
35 if (@{ $self->db_schema } == 1 && $self->db_schema->[0] ne '%'
36 && lc($self->db_schema->[0]) ne lc($current_schema)) {
37 $self->dbh->do('ALTER SESSION SET current_schema=' . $self->db_schema->[0]);
40 if (not defined $self->preserve_case) {
41 $self->preserve_case(0);
43 elsif ($self->preserve_case) {
44 $self->schema->storage->sql_maker->quote_char('"');
45 $self->schema->storage->sql_maker->name_sep('.');
49 sub _build_name_sep { '.' }
54 # From http://www.adp-gmbh.ch/ora/misc/known_schemas.html
56 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/);
62 return ($self->next::method(@_), 'PLAN_TABLE');
66 my ($self, $schema) = @_;
68 return $self->dbh->tables(undef, $schema, '%', 'TABLE,VIEW');
74 # silence a warning from older DBD::Oracles in tests
75 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
76 local $SIG{__WARN__} = sub {
78 unless $_[0] =~ /^Field \d+ has an Oracle type \(\d+\) which is not explicitly supported/;
81 return $self->next::method(@_);
84 sub _table_uniq_info {
85 my ($self, $table) = @_;
87 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
88 SELECT ac.constraint_name, acc.column_name
89 FROM all_constraints ac, all_cons_columns acc
90 WHERE acc.table_name=? AND acc.owner = ?
91 AND ac.table_name = acc.table_name AND ac.owner = acc.owner
92 AND acc.constraint_name = ac.constraint_name
93 AND ac.constraint_type='U'
97 $sth->execute($table->name, $table->schema);
101 while(my $constr = $sth->fetchrow_arrayref) {
102 my $constr_name = $self->_lc($constr->[0]);
103 my $constr_col = $self->_lc($constr->[1]);
104 push @{$constr_names{$constr_name}}, $constr_col;
107 my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
115 my $table_comment = $self->next::method(@_);
117 return $table_comment if $table_comment;
119 ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name);
120 SELECT comments FROM all_tab_comments
123 AND (table_type = 'TABLE' OR table_type = 'VIEW')
126 return $table_comment
129 sub _column_comment {
131 my ($table, $column_number, $column_name) = @_;
133 my $column_comment = $self->next::method(@_);
135 return $column_comment if $column_comment;
137 ($column_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $self->_uc($column_name));
138 SELECT comments FROM all_col_comments
144 return $column_comment
147 sub _columns_info_for {
151 my $result = $self->next::method(@_);
153 local $self->dbh->{LongReadLen} = 1_000_000;
154 local $self->dbh->{LongTruncOk} = 1;
156 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
159 WHERE table_name = ? AND table_owner = ?
160 AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
163 $sth->execute($table->name, $table->schema);
165 while (my ($trigger_body) = $sth->fetchrow_array) {
166 if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) {
167 if (my ($col_name) = $trigger_body =~ /:new\.(\w+)/i) {
168 $col_name = $self->_lc($col_name);
170 $result->{$col_name}{is_auto_increment} = 1;
172 $seq_schema = $self->_lc($seq_schema || $table->schema);
173 $seq_name = $self->_lc($seq_name);
175 $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
180 while (my ($col, $info) = each %$result) {
181 no warnings 'uninitialized';
183 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
184 SELECT data_type, data_length
186 WHERE column_name = ? AND table_name = ? AND owner = ?
188 $sth->execute($self->_uc($col), $table->name, $table->schema);
189 my ($data_type, $data_length) = $sth->fetchrow_array;
191 $data_type = lc $data_type;
193 if ($data_type =~ /^(?:n(?:var)?char2?|u?rowid|nclob|timestamp\(\d+\)(?: with(?: local)? time zone)?|binary_(?:float|double))\z/i) {
194 $info->{data_type} = $data_type;
196 if ($data_type =~ /^u?rowid\z/i) {
197 $info->{size} = $data_length;
201 if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
202 delete $info->{size};
205 if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
206 if (ref $info->{size}) {
207 $info->{size} = $info->{size}[0] / 8;
210 $info->{size} = $info->{size} / 2;
213 elsif ($info->{data_type} =~ /^(?:var)?char2?\z/i) {
214 if (ref $info->{size}) {
215 $info->{size} = $info->{size}[0];
218 elsif (lc($info->{data_type}) =~ /^(?:number|decimal)\z/i) {
219 $info->{original}{data_type} = 'number';
220 $info->{data_type} = 'numeric';
222 if (try { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
223 $info->{original}{size} = $info->{size};
225 $info->{data_type} = 'integer';
226 delete $info->{size};
229 elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
230 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
232 if ($precision == 6) {
233 delete $info->{size};
236 $info->{size} = $precision;
239 elsif ($info->{data_type} =~ /timestamp/i && ref $info->{size} && $info->{size}[0] == 0) {
240 my $size = $info->{size}[1];
241 delete $info->{size};
242 $info->{size} = $size unless $size == 6;
244 elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
245 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
247 if ($precision == 2) {
248 delete $info->{size};
251 $info->{size} = $precision;
254 elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
255 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
257 if ($day_precision == 2 && $second_precision == 6) {
258 delete $info->{size};
261 $info->{size} = [ $day_precision, $second_precision ];
264 elsif ($info->{data_type} =~ /^interval year to month\z/i && ref $info->{size}) {
265 my $precision = $info->{size}[0];
267 if ($precision == 2) {
268 delete $info->{size};
271 $info->{size} = $precision;
274 elsif ($info->{data_type} =~ /^interval day to second\z/i && ref $info->{size}) {
275 if ($info->{size}[0] == 2 && $info->{size}[1] == 6) {
276 delete $info->{size};
279 elsif (lc($info->{data_type}) eq 'float') {
280 $info->{original}{data_type} = 'float';
281 $info->{original}{size} = $info->{size};
283 if ($info->{size} <= 63) {
284 $info->{data_type} = 'real';
287 $info->{data_type} = 'double precision';
289 delete $info->{size};
291 elsif (lc($info->{data_type}) eq 'double precision') {
292 $info->{original}{data_type} = 'float';
294 my $size = try { $info->{size}[0] };
296 $info->{original}{size} = $size;
299 $info->{data_type} = 'real';
301 delete $info->{size};
303 elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
304 delete $info->{size};
306 elsif ($info->{data_type} eq '-9104') {
307 $info->{data_type} = 'rowid';
308 delete $info->{size};
310 elsif ($info->{data_type} eq '-2') {
311 $info->{data_type} = 'raw';
312 $info->{size} = try { $info->{size}[0] / 2 };
314 elsif (lc($info->{data_type}) eq 'date') {
315 $info->{data_type} = 'datetime';
316 $info->{original}{data_type} = 'date';
318 elsif (lc($info->{data_type}) eq 'binary_float') {
319 $info->{data_type} = 'real';
320 $info->{original}{data_type} = 'binary_float';
322 elsif (lc($info->{data_type}) eq 'binary_double') {
323 $info->{data_type} = 'double precision';
324 $info->{original}{data_type} = 'binary_double';
327 # DEFAULT could be missed by ::DBI because of ORA-24345
328 if (not defined $info->{default_value}) {
329 local $self->dbh->{LongReadLen} = 1_000_000;
330 local $self->dbh->{LongTruncOk} = 1;
331 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
334 WHERE column_name = ? AND table_name = ? AND owner = ?
336 $sth->execute($self->_uc($col), $table->name, $table->schema);
337 my ($default) = $sth->fetchrow_array;
340 # this is mostly copied from ::DBI::QuotedDefault
341 if (defined $default) {
342 s/^\s+//, s/\s+\z// for $default;
344 if ($default =~ /^'(.*?)'\z/) {
345 $info->{default_value} = $1;
347 elsif ($default =~ /^(-?\d.*?)\z/) {
348 $info->{default_value} = $1;
350 elsif ($default =~ /^NULL\z/i) {
352 $info->{default_value} = \$null;
354 elsif ($default ne '') {
356 $info->{default_value} = \$val;
361 if ((try { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
362 my $current_timestamp = 'current_timestamp';
363 $info->{default_value} = \$current_timestamp;
365 my $sysdate = 'sysdate';
366 $info->{original}{default_value} = \$sysdate;
373 sub _dbh_column_info {
377 # try to avoid ORA-24345
378 local $dbh->{LongReadLen} = 1_000_000;
379 local $dbh->{LongTruncOk} = 1;
381 return $self->next::method(@_);
386 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
387 L<DBIx::Class::Schema::Loader::DBI>
391 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
395 This library is free software; you can redistribute it and/or modify it under
396 the same terms as Perl itself.
401 # vim:et sts=4 sw=4 tw=0: