1 package DBIx::Class::Schema::Loader::DBI::Oracle;
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
9 use Carp::Clan qw/^DBIx::Class/;
12 our $VERSION = '0.07010';
16 DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI
17 Oracle Implementation.
21 See L<DBIx::Class::Schema::Loader::Base>.
28 $self->next::method(@_);
30 my $dbh = $self->schema->storage->dbh;
32 my ($current_schema) = $dbh->selectrow_array('SELECT USER FROM DUAL', {});
34 $self->{db_schema} ||= $current_schema;
36 if (lc($self->db_schema) ne lc($current_schema)) {
37 $dbh->do('ALTER SESSION SET current_schema=' . $self->db_schema);
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('.');
50 my ($self, $table) = @_;
52 return $self->_quote($table);
56 my ($self, $opts) = @_;
58 my $dbh = $self->schema->storage->dbh;
61 for my $table ( $dbh->tables(undef, $self->db_schema, '%', 'TABLE,VIEW') ) { #catalog, schema, table, type
62 my $quoter = $dbh->get_info(29);
63 $table =~ s/$quoter//g;
65 # remove "user." (schema) prefixes
68 next if $table eq 'PLAN_TABLE';
69 $table = $self->_lc($table);
71 if $table =~ /\A(\w+)\z/;
75 # silence a warning from older DBD::Oracles in tests
76 my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
77 local $SIG{__WARN__} = sub {
79 unless $_[0] =~ /^Field \d+ has an Oracle type \(\d+\) which is not explicitly supported/;
82 return $self->_filter_tables(\@tables, $opts);
87 my ($self, $table) = @_;
89 my $dbh = $self->schema->storage->dbh;
91 my $sth = $dbh->column_info(undef, $self->db_schema, $self->_uc($table), '%');
93 return [ map $self->_lc($_->{COLUMN_NAME}), @{ $sth->fetchall_arrayref({ COLUMN_NAME => 1 }) || [] } ];
96 sub _table_uniq_info {
97 my ($self, $table) = @_;
99 my $dbh = $self->schema->storage->dbh;
101 my $sth = $dbh->prepare_cached(
103 SELECT constraint_name, acc.column_name
104 FROM all_constraints JOIN all_cons_columns acc USING (constraint_name)
105 WHERE acc.table_name=? and acc.owner = ? AND constraint_type='U'
106 ORDER BY acc.position
110 $sth->execute($self->_uc($table),$self->{db_schema} );
112 while(my $constr = $sth->fetchrow_arrayref) {
113 my $constr_name = $self->_lc($constr->[0]);
114 my $constr_col = $self->_lc($constr->[1]);
115 $constr_name =~ s/\Q$self->{_quoter}\E//;
116 $constr_col =~ s/\Q$self->{_quoter}\E//;
117 push @{$constr_names{$constr_name}}, $constr_col;
120 my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
128 my $table_comment = $self->next::method(@_);
130 return $table_comment if $table_comment;
132 ($table_comment) = $self->schema->storage->dbh->selectrow_array(
134 SELECT comments FROM all_tab_comments
137 AND table_type = 'TABLE'
138 }, undef, $self->db_schema, $self->_uc($table)
141 return $table_comment
144 sub _column_comment {
146 my ($table, $column_number, $column_name) = @_;
148 my $column_comment = $self->next::method(@_);
150 return $column_comment if $column_comment;
152 ($column_comment) = $self->schema->storage->dbh->selectrow_array(
154 SELECT comments FROM all_col_comments
158 }, undef, $self->db_schema, $self->_uc( $table ), $self->_uc( $column_name )
160 return $column_comment
164 my ($self, $table) = (shift, shift);
166 return $self->next::method($self->_uc($table), @_);
170 my ($self, $table) = (shift, shift);
172 my $rels = $self->next::method($self->_uc($table), @_);
174 foreach my $rel (@$rels) {
175 $rel->{remote_table} = $self->_lc($rel->{remote_table});
181 sub _columns_info_for {
182 my ($self, $table) = (shift, shift);
184 my $result = $self->next::method($self->_uc($table), @_);
186 my $dbh = $self->schema->storage->dbh;
188 local $dbh->{LongReadLen} = 100000;
189 local $dbh->{LongTruncOk} = 1;
191 my $sth = $dbh->prepare_cached(q{
192 SELECT atc.column_name, ut.trigger_body
194 JOIN all_trigger_cols atc USING (trigger_name)
195 WHERE atc.table_name = ?
196 AND lower(column_usage) LIKE '%new%' AND lower(column_usage) LIKE '%out%'
197 AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
200 $sth->execute($self->_uc($table));
202 while (my ($col_name, $trigger_body) = $sth->fetchrow_array) {
203 $col_name = $self->_lc($col_name);
205 $result->{$col_name}{is_auto_increment} = 1;
207 if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) {
208 $seq_schema = $self->_lc($seq_schema || $self->db_schema);
209 $seq_name = $self->_lc($seq_name);
211 $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
215 while (my ($col, $info) = each %$result) {
216 no warnings 'uninitialized';
218 if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
219 delete $info->{size};
222 if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
223 $info->{size} = $info->{size} / 2;
225 elsif (lc($info->{data_type}) eq 'number') {
226 $info->{original}{data_type} = 'number';
227 $info->{data_type} = 'numeric';
229 if (eval { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
230 $info->{original}{size} = $info->{size};
232 $info->{data_type} = 'integer';
233 delete $info->{size};
236 elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
237 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
239 if ($precision == 6) {
240 delete $info->{size};
243 $info->{size} = $precision;
246 elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
247 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
249 if ($precision == 2) {
250 delete $info->{size};
253 $info->{size} = $precision;
256 elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
257 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
259 if ($day_precision == 2 && $second_precision == 6) {
260 delete $info->{size};
263 $info->{size} = [ $day_precision, $second_precision ];
266 elsif (lc($info->{data_type}) eq 'float') {
267 $info->{original}{data_type} = 'float';
268 $info->{original}{size} = $info->{size};
270 if ($info->{size} <= 63) {
271 $info->{data_type} = 'real';
274 $info->{data_type} = 'double precision';
276 delete $info->{size};
278 elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
279 delete $info->{size};
281 elsif (lc($info->{data_type}) eq 'date') {
282 $info->{data_type} = 'datetime';
283 $info->{original}{data_type} = 'date';
285 elsif (lc($info->{data_type}) eq 'binary_float') {
286 $info->{data_type} = 'real';
287 $info->{original}{data_type} = 'binary_float';
289 elsif (lc($info->{data_type}) eq 'binary_double') {
290 $info->{data_type} = 'double precision';
291 $info->{original}{data_type} = 'binary_double';
294 if ((eval { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
295 my $current_timestamp = 'current_timestamp';
296 $info->{default_value} = \$current_timestamp;
298 my $sysdate = 'sysdate';
299 $info->{original}{default_value} = \$sysdate;
308 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
309 L<DBIx::Class::Schema::Loader::DBI>
313 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
317 This library is free software; you can redistribute it and/or modify it under
318 the same terms as Perl itself.
323 # vim:et sts=4 sw=4 tw=0: