release 0.07005
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Oracle.pm
1 package DBIx::Class::Schema::Loader::DBI::Oracle;
2
3 use strict;
4 use warnings;
5 use base qw/
6     DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7     DBIx::Class::Schema::Loader::DBI
8 /;
9 use Carp::Clan qw/^DBIx::Class/;
10 use mro 'c3';
11
12 our $VERSION = '0.07005';
13
14 =head1 NAME
15
16 DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI 
17 Oracle Implementation.
18
19 =head1 DESCRIPTION
20
21 See L<DBIx::Class::Schema::Loader::Base>.
22
23 =cut
24
25 sub _setup {
26     my $self = shift;
27
28     $self->next::method(@_);
29
30     my $dbh = $self->schema->storage->dbh;
31
32     my ($current_schema) = $dbh->selectrow_array('SELECT USER FROM DUAL', {});
33
34     $self->{db_schema} ||= $current_schema;
35
36     if (lc($self->db_schema) ne lc($current_schema)) {
37         $dbh->do('ALTER SESSION SET current_schema=' . $self->db_schema);
38     }
39
40     if (not defined $self->preserve_case) {
41         $self->preserve_case(0);
42     }
43     elsif ($self->preserve_case) {
44         $self->schema->storage->sql_maker->quote_char('"');
45         $self->schema->storage->sql_maker->name_sep('.');
46     }
47 }
48
49 sub _table_as_sql {
50     my ($self, $table) = @_;
51
52     return $self->_quote_table_name($table);
53 }
54
55 sub _tables_list { 
56     my ($self, $opts) = @_;
57
58     my $dbh = $self->schema->storage->dbh;
59
60     my @tables;
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;
64
65         # remove "user." (schema) prefixes
66         $table =~ s/\w+\.//;
67
68         next if $table eq 'PLAN_TABLE';
69         $table = $self->_lc($table);
70         push @tables, $1
71           if $table =~ /\A(\w+)\z/;
72     }
73
74     {
75         # silence a warning from older DBD::Oracles in tests
76         my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
77         local $SIG{__WARN__} = sub {
78             $warn_handler->(@_)
79             unless $_[0] =~ /^Field \d+ has an Oracle type \(\d+\) which is not explicitly supported/;
80         };
81
82         return $self->_filter_tables(\@tables, $opts);
83     }
84 }
85
86 sub _table_columns {
87     my ($self, $table) = @_;
88
89     my $dbh = $self->schema->storage->dbh;
90
91     my $sth = $dbh->column_info(undef, $self->db_schema, $self->_uc($table), '%');
92
93     return [ map $self->_lc($_->{COLUMN_NAME}), @{ $sth->fetchall_arrayref({ COLUMN_NAME => 1 }) || [] } ];
94 }
95
96 sub _table_uniq_info {
97     my ($self, $table) = @_;
98
99     my $dbh = $self->schema->storage->dbh;
100
101     my $sth = $dbh->prepare_cached(
102         q{
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
107         },
108         {}, 1);
109
110     $sth->execute($self->_uc($table),$self->{db_schema} );
111     my %constr_names;
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;
118     }
119     
120     my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
121     return \@uniqs;
122 }
123
124 sub _table_comment {
125     my ( $self, $table ) = @_;
126      my ($table_comment) = $self->schema->storage->dbh->selectrow_array(
127         q{
128             SELECT comments FROM all_tab_comments
129             WHERE owner = ? 
130               AND table_name = ?
131               AND table_type = 'TABLE'
132         }, undef, $self->db_schema, $self->_uc($table)
133     );
134
135     return $table_comment
136 }
137
138 sub _column_comment {
139     my ( $self, $table, $column_number, $column_name ) = @_;
140     my ($column_comment) = $self->schema->storage->dbh->selectrow_array(
141         q{
142             SELECT comments FROM all_col_comments
143             WHERE owner = ? 
144               AND table_name = ?
145               AND column_name = ?
146         }, undef, $self->db_schema, $self->_uc( $table ), $self->_uc( $column_name )
147     );
148     return $column_comment
149 }
150
151 sub _table_pk_info {
152     my ($self, $table) = (shift, shift);
153
154     return $self->next::method($self->_uc($table), @_);
155 }
156
157 sub _table_fk_info {
158     my ($self, $table) = (shift, shift);
159
160     my $rels = $self->next::method($self->_uc($table), @_);
161
162     foreach my $rel (@$rels) {
163         $rel->{remote_table} = $self->_lc($rel->{remote_table});
164     }
165
166     return $rels;
167 }
168
169 sub _columns_info_for {
170     my ($self, $table) = (shift, shift);
171
172     my $result = $self->next::method($self->_uc($table), @_);
173
174     my $dbh = $self->schema->storage->dbh;
175
176     local $dbh->{LongReadLen} = 100000;
177     local $dbh->{LongTruncOk} = 1;
178
179     my $sth = $dbh->prepare_cached(q{
180 SELECT atc.column_name, ut.trigger_body
181 FROM all_triggers ut
182 JOIN all_trigger_cols atc USING (trigger_name)
183 WHERE atc.table_name = ?
184 AND lower(column_usage) LIKE '%new%' AND lower(column_usage) LIKE '%out%'
185 AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
186     }, {}, 1);
187
188     $sth->execute($self->_uc($table));
189
190     while (my ($col_name, $trigger_body) = $sth->fetchrow_array) {
191         $col_name = $self->_lc($col_name);
192
193         $result->{$col_name}{is_auto_increment} = 1;
194
195         if (my ($seq_schema, $seq_name) = $trigger_body =~ /(?:\."?(\w+)"?)?"?(\w+)"?\.nextval/i) {
196             $seq_schema = $self->_lc($seq_schema || $self->db_schema);
197             $seq_name   = $self->_lc($seq_name);
198
199             $result->{$col_name}{sequence} = ($self->qualify_objects ? ($seq_schema . '.') : '') . $seq_name;
200         }
201     }
202
203     while (my ($col, $info) = each %$result) {
204         no warnings 'uninitialized';
205
206         if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
207             delete $info->{size};
208         }
209
210         if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
211             $info->{size} = $info->{size} / 2;
212         }
213         elsif (lc($info->{data_type}) eq 'number') {
214             $info->{original}{data_type} = 'number';
215             $info->{data_type}           = 'numeric';
216
217             if (eval { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
218                 $info->{original}{size} = $info->{size};
219
220                 $info->{data_type} = 'integer';
221                 delete $info->{size};
222             }
223         }
224         elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
225             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
226
227             if ($precision == 6) {
228                 delete $info->{size};
229             }
230             else {
231                 $info->{size} = $precision;
232             }
233         }
234         elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
235             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
236
237             if ($precision == 2) {
238                 delete $info->{size};
239             }
240             else {
241                 $info->{size} = $precision;
242             }
243         }
244         elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
245             $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
246
247             if ($day_precision == 2 && $second_precision == 6) {
248                 delete $info->{size};
249             }
250             else {
251                 $info->{size} = [ $day_precision, $second_precision ];
252             }
253         }
254         elsif (lc($info->{data_type}) eq 'float') {
255             $info->{original}{data_type} = 'float';
256             $info->{original}{size}      = $info->{size};
257
258             if ($info->{size} <= 63) {
259                 $info->{data_type} = 'real';
260             }
261             else {
262                 $info->{data_type} = 'double precision';
263             }
264             delete $info->{size};
265         }
266         elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
267             delete $info->{size};
268         }
269         elsif (lc($info->{data_type}) eq 'date') {
270             $info->{data_type}           = 'datetime';
271             $info->{original}{data_type} = 'date';
272         }
273         elsif (lc($info->{data_type}) eq 'binary_float') {
274             $info->{data_type}           = 'real';
275             $info->{original}{data_type} = 'binary_float';
276         } 
277         elsif (lc($info->{data_type}) eq 'binary_double') {
278             $info->{data_type}           = 'double precision';
279             $info->{original}{data_type} = 'binary_double';
280         } 
281
282         if ((eval { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
283             my $current_timestamp  = 'current_timestamp';
284             $info->{default_value} = \$current_timestamp;
285
286             my $sysdate = 'sysdate';
287             $info->{original}{default_value} = \$sysdate;
288         }
289     }
290
291     return $result;
292 }
293
294 =head1 SEE ALSO
295
296 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
297 L<DBIx::Class::Schema::Loader::DBI>
298
299 =head1 AUTHOR
300
301 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
302
303 =head1 LICENSE
304
305 This library is free software; you can redistribute it and/or modify it under
306 the same terms as Perl itself.
307
308 =cut
309
310 1;
311 # vim:et sts=4 sw=4 tw=0: