1 package DBIx::Class::Schema::Loader::DBI::Oracle;
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
11 our $VERSION = '0.07010';
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 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
74 local $SIG{__WARN__} = sub {
76 unless $_[0] =~ /^Field \d+ has an Oracle type \(\d+\) which is not explicitly supported/;
79 return $self->next::method(@_);
83 my ($self, $table) = @_;
85 my $sth = $self->dbh->column_info(undef, $table->schema, $table, '%');
87 return [ map $self->_lc($_->{COLUMN_NAME}), @{ $sth->fetchall_arrayref({ COLUMN_NAME => 1 }) || [] } ];
90 sub _table_uniq_info {
91 my ($self, $table) = @_;
93 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
94 SELECT constraint_name, acc.column_name
96 JOIN all_cons_columns acc USING (constraint_name)
97 WHERE acc.table_name=? and acc.owner = ? AND constraint_type='U'
101 $sth->execute($table->name, $table->schema);
105 while(my $constr = $sth->fetchrow_arrayref) {
106 my $constr_name = $self->_lc($constr->[0]);
107 my $constr_col = $self->_lc($constr->[1]);
108 push @{$constr_names{$constr_name}}, $constr_col;
111 my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
119 my $table_comment = $self->next::method(@_);
121 return $table_comment if $table_comment;
123 ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name);
124 SELECT comments FROM all_tab_comments
127 AND (table_type = 'TABLE' OR table_type = 'VIEW')
130 return $table_comment
133 sub _column_comment {
135 my ($table, $column_number, $column_name) = @_;
137 my $column_comment = $self->next::method(@_);
139 return $column_comment if $column_comment;
141 ($column_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->schema, $table->name, $self->_uc($column_name));
142 SELECT comments FROM all_col_comments
148 return $column_comment
151 sub _columns_info_for {
155 my $result = $self->next::method(@_);
157 local $self->dbh->{LongReadLen} = 100000;
158 local $self->dbh->{LongTruncOk} = 1;
160 my $sth = $self->dbh->prepare_cached(<<'EOF', {}, 1);
161 SELECT atc.column_name, ut.trigger_body
163 JOIN all_trigger_cols atc USING (trigger_name)
164 WHERE atc.table_name = ?
165 AND lower(column_usage) LIKE '%new%' AND lower(column_usage) LIKE '%out%'
166 AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
169 $sth->execute($table->name);
171 while (my ($col_name, $trigger_body) = $sth->fetchrow_array) {
172 $col_name = $self->_lc($col_name);
174 $result->{$col_name}{is_auto_increment} = 1;
176 if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) {
177 $seq_schema = $self->_lc($seq_schema || $table->schema);
178 $seq_name = $self->_lc($seq_name);
180 $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
184 while (my ($col, $info) = each %$result) {
185 no warnings 'uninitialized';
187 if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
188 delete $info->{size};
191 if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
192 $info->{size} = $info->{size} / 2;
194 elsif (lc($info->{data_type}) eq 'number') {
195 $info->{original}{data_type} = 'number';
196 $info->{data_type} = 'numeric';
198 if (eval { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
199 $info->{original}{size} = $info->{size};
201 $info->{data_type} = 'integer';
202 delete $info->{size};
205 elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
206 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
208 if ($precision == 6) {
209 delete $info->{size};
212 $info->{size} = $precision;
215 elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
216 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
218 if ($precision == 2) {
219 delete $info->{size};
222 $info->{size} = $precision;
225 elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
226 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
228 if ($day_precision == 2 && $second_precision == 6) {
229 delete $info->{size};
232 $info->{size} = [ $day_precision, $second_precision ];
235 elsif (lc($info->{data_type}) eq 'float') {
236 $info->{original}{data_type} = 'float';
237 $info->{original}{size} = $info->{size};
239 if ($info->{size} <= 63) {
240 $info->{data_type} = 'real';
243 $info->{data_type} = 'double precision';
245 delete $info->{size};
247 elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
248 delete $info->{size};
250 elsif (lc($info->{data_type}) eq 'date') {
251 $info->{data_type} = 'datetime';
252 $info->{original}{data_type} = 'date';
254 elsif (lc($info->{data_type}) eq 'binary_float') {
255 $info->{data_type} = 'real';
256 $info->{original}{data_type} = 'binary_float';
258 elsif (lc($info->{data_type}) eq 'binary_double') {
259 $info->{data_type} = 'double precision';
260 $info->{original}{data_type} = 'binary_double';
263 if ((eval { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
264 my $current_timestamp = 'current_timestamp';
265 $info->{default_value} = \$current_timestamp;
267 my $sysdate = 'sysdate';
268 $info->{original}{default_value} = \$sysdate;
277 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
278 L<DBIx::Class::Schema::Loader::DBI>
282 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
286 This library is free software; you can redistribute it and/or modify it under
287 the same terms as Perl itself.
292 # vim:et sts=4 sw=4 tw=0: