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.07001';
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_name($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/;
74 return $self->_filter_tables(\@tables, $opts);
78 my ($self, $table) = @_;
80 my $dbh = $self->schema->storage->dbh;
82 my $sth = $dbh->column_info(undef, $self->db_schema, $self->_uc($table), '%');
84 return [ map $self->_lc($_->{COLUMN_NAME}), @{ $sth->fetchall_arrayref({ COLUMN_NAME => 1 }) || [] } ];
87 sub _table_uniq_info {
88 my ($self, $table) = @_;
90 my $dbh = $self->schema->storage->dbh;
92 my $sth = $dbh->prepare_cached(
94 SELECT constraint_name, acc.column_name
95 FROM all_constraints JOIN all_cons_columns acc USING (constraint_name)
96 WHERE acc.table_name=? and acc.owner = ? AND constraint_type='U'
101 $sth->execute($self->_uc($table),$self->{db_schema} );
103 while(my $constr = $sth->fetchrow_arrayref) {
104 my $constr_name = $self->_lc($constr->[0]);
105 my $constr_col = $self->_lc($constr->[1]);
106 $constr_name =~ s/\Q$self->{_quoter}\E//;
107 $constr_col =~ s/\Q$self->{_quoter}\E//;
108 push @{$constr_names{$constr_name}}, $constr_col;
111 my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
116 my ($self, $table) = (shift, shift);
118 return $self->next::method($self->_uc($table), @_);
122 my ($self, $table) = (shift, shift);
124 my $rels = $self->next::method($self->_uc($table), @_);
126 foreach my $rel (@$rels) {
127 $rel->{remote_table} = $self->_lc($rel->{remote_table});
133 sub _columns_info_for {
134 my ($self, $table) = (shift, shift);
136 my $result = $self->next::method($self->_uc($table), @_);
138 my $dbh = $self->schema->storage->dbh;
140 local $dbh->{LongReadLen} = 100000;
141 local $dbh->{LongTruncOk} = 1;
143 my $sth = $dbh->prepare_cached(q{
144 SELECT atc.column_name, ut.trigger_body
146 JOIN all_trigger_cols atc USING (trigger_name)
147 WHERE atc.table_name = ?
148 AND lower(column_usage) LIKE '%new%' AND lower(column_usage) LIKE '%out%'
149 AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
152 $sth->execute($self->_uc($table));
154 while (my ($col_name, $trigger_body) = $sth->fetchrow_array) {
155 $col_name = $self->_lc($col_name);
157 $result->{$col_name}{is_auto_increment} = 1;
159 if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) {
160 $seq_schema = $self->_lc($seq_schema) || $self->db_schema;
161 $seq_name = $self->_lc($seq_name);
163 $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
167 while (my ($col, $info) = each %$result) {
168 no warnings 'uninitialized';
170 if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
171 delete $info->{size};
174 if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
175 $info->{size} = $info->{size} / 2;
177 elsif (lc($info->{data_type}) eq 'number') {
178 $info->{original}{data_type} = 'number';
179 $info->{data_type} = 'numeric';
181 if (eval { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
182 $info->{original}{size} = $info->{size};
184 $info->{data_type} = 'integer';
185 delete $info->{size};
188 elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
189 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
191 if ($precision == 6) {
192 delete $info->{size};
195 $info->{size} = $precision;
198 elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
199 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
201 if ($precision == 2) {
202 delete $info->{size};
205 $info->{size} = $precision;
208 elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
209 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
211 if ($day_precision == 2 && $second_precision == 6) {
212 delete $info->{size};
215 $info->{size} = [ $day_precision, $second_precision ];
218 elsif (lc($info->{data_type}) eq 'float') {
219 $info->{original}{data_type} = 'float';
220 $info->{original}{size} = $info->{size};
222 if ($info->{size} <= 63) {
223 $info->{data_type} = 'real';
226 $info->{data_type} = 'double precision';
228 delete $info->{size};
230 elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
231 delete $info->{size};
233 elsif (lc($info->{data_type}) eq 'date') {
234 $info->{data_type} = 'datetime';
235 $info->{original}{data_type} = 'date';
237 elsif (lc($info->{data_type}) eq 'binary_float') {
238 $info->{data_type} = 'real';
239 $info->{original}{data_type} = 'binary_float';
241 elsif (lc($info->{data_type}) eq 'binary_double') {
242 $info->{data_type} = 'double precision';
243 $info->{original}{data_type} = 'binary_double';
246 if ((eval { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
247 my $current_timestamp = 'current_timestamp';
248 $info->{default_value} = \$current_timestamp;
250 my $sysdate = 'sysdate';
251 $info->{original}{default_value} = \$sysdate;
260 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
261 L<DBIx::Class::Schema::Loader::DBI>
265 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
269 This library is free software; you can redistribute it and/or modify it under
270 the same terms as Perl itself.
275 # vim:et sts=4 sw=4 tw=0: