add ->{original}{default_value} when rewriting to current_timestamp
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Oracle.pm
CommitLineData
d87d939a 1package DBIx::Class::Schema::Loader::DBI::Oracle;
e7262300 2
3use strict;
4use warnings;
6b0e47fc 5use base qw/
6 DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
7 DBIx::Class::Schema::Loader::DBI
8/;
e7262300 9use Carp::Clan qw/^DBIx::Class/;
10use Class::C3;
11
9990e58f 12our $VERSION = '0.07000';
e7262300 13
14=head1 NAME
15
16DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI
17Oracle Implementation.
18
e7262300 19=head1 DESCRIPTION
20
21See L<DBIx::Class::Schema::Loader::Base>.
22
e7262300 23=cut
24
d0e184e9 25sub _setup {
26 my $self = shift;
27
28 $self->next::method(@_);
29
30 my $dbh = $self->schema->storage->dbh;
46065bcb 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 }
bc1cb85e 39
40 if (not defined $self->preserve_case) {
41 $self->preserve_case(0);
42 }
3785bc2e 43 elsif ($self->preserve_case) {
44 $self->schema->storage->quote_char('"');
45 $self->schema->storage->name_sep('.');
46 }
d0e184e9 47}
48
075aff97 49sub _table_as_sql {
e7262300 50 my ($self, $table) = @_;
51
075aff97 52 return $self->_quote_table_name($table);
e7262300 53}
54
55sub _tables_list {
bfb43060 56 my ($self, $opts) = @_;
e7262300 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';
3785bc2e 69 $table = $self->_lc($table);
e7262300 70 push @tables, $1
71 if $table =~ /\A(\w+)\z/;
72 }
ffb03c96 73
bfb43060 74 return $self->_filter_tables(\@tables, $opts);
e7262300 75}
76
4337bddf 77sub _table_columns {
78 my ($self, $table) = @_;
79
80 my $dbh = $self->schema->storage->dbh;
81
3785bc2e 82 my $sth = $dbh->column_info(undef, $self->db_schema, $self->_uc($table), '%');
83
4337bddf 84 return [ map lc($_->{COLUMN_NAME}), @{ $sth->fetchall_arrayref({ COLUMN_NAME => 1 }) || [] } ];
85}
86
e7262300 87sub _table_uniq_info {
88 my ($self, $table) = @_;
89
e7262300 90 my $dbh = $self->schema->storage->dbh;
91
92 my $sth = $dbh->prepare_cached(
65ab592d 93 q{
c7bf4194 94 SELECT constraint_name, acc.column_name
95 FROM all_constraints JOIN all_cons_columns acc USING (constraint_name)
8803e4ed 96 WHERE acc.table_name=? and acc.owner = ? AND constraint_type='U'
c7bf4194 97 ORDER BY acc.position
65ab592d 98 },
99 {}, 1);
e7262300 100
3785bc2e 101 $sth->execute($self->_uc($table),$self->{db_schema} );
e7262300 102 my %constr_names;
103 while(my $constr = $sth->fetchrow_arrayref) {
3785bc2e 104 my $constr_name = $constr->[0];
105 my $constr_col = $self->_lc($constr->[1]);
e7262300 106 $constr_name =~ s/\Q$self->{_quoter}\E//;
3785bc2e 107 $constr_col =~ s/\Q$self->{_quoter}\E//;
108 push @{$constr_names{$constr_name}}, $constr_col;
e7262300 109 }
65ab592d 110
111 my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
e7262300 112 return \@uniqs;
113}
114
115sub _table_pk_info {
3785bc2e 116 my ($self, $table) = (shift, shift);
117
118 return $self->next::method($self->_uc($table), @_);
e7262300 119}
120
121sub _table_fk_info {
3785bc2e 122 my ($self, $table) = (shift, shift);
e7262300 123
3785bc2e 124 my $rels = $self->next::method($self->_uc($table), @_);
e7262300 125
65ab592d 126 foreach my $rel (@$rels) {
3785bc2e 127 $rel->{remote_table} = $self->_lc($rel->{remote_table});
e7262300 128 }
129
65ab592d 130 return $rels;
131}
132
133sub _columns_info_for {
760fd65c 134 my ($self, $table) = (shift, shift);
e7262300 135
3785bc2e 136 my $result = $self->next::method($self->_uc($table), @_);
fb328d1a 137
fb328d1a 138 my $dbh = $self->schema->storage->dbh;
fb328d1a 139
760fd65c 140 my $sth = $dbh->prepare_cached(q{
141SELECT atc.column_name
142FROM all_triggers ut
143JOIN all_trigger_cols atc USING (trigger_name)
144WHERE atc.table_name = ?
145AND lower(column_usage) LIKE '%new%' AND lower(column_usage) LIKE '%out%'
146AND upper(trigger_type) LIKE '%BEFORE EACH ROW%' AND lower(triggering_event) LIKE '%insert%'
147 }, {}, 1);
148
3785bc2e 149 $sth->execute($self->_uc($table));
760fd65c 150
151 while (my ($col_name) = $sth->fetchrow_array) {
3785bc2e 152 $result->{$self->_lc($col_name)}{is_auto_increment} = 1;
760fd65c 153 }
154
155 while (my ($col, $info) = each %$result) {
156 no warnings 'uninitialized';
157
158 if ($info->{data_type} =~ /^(?:n?[cb]lob|long(?: raw)?|bfile|date|binary_(?:float|double)|rowid)\z/i) {
159 delete $info->{size};
160 }
161
162 if ($info->{data_type} =~ /^n(?:var)?char2?\z/i) {
163 $info->{size} = $info->{size} / 2;
164 }
165 elsif (lc($info->{data_type}) eq 'number') {
4ea15dfe 166 $info->{original}{data_type} = 'number';
167 $info->{data_type} = 'numeric';
760fd65c 168
169 if (eval { $info->{size}[0] == 38 && $info->{size}[1] == 0 }) {
4ea15dfe 170 $info->{original}{size} = $info->{size};
171
760fd65c 172 $info->{data_type} = 'integer';
173 delete $info->{size};
174 }
175 }
176 elsif (my ($precision) = $info->{data_type} =~ /^timestamp\((\d+)\)(?: with (?:local )?time zone)?\z/i) {
177 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
178
179 if ($precision == 6) {
180 delete $info->{size};
181 }
182 else {
183 $info->{size} = $precision;
184 }
185 }
186 elsif (($precision) = $info->{data_type} =~ /^interval year\((\d+)\) to month\z/i) {
187 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
188
189 if ($precision == 2) {
190 delete $info->{size};
191 }
192 else {
193 $info->{size} = $precision;
194 }
195 }
196 elsif (my ($day_precision, $second_precision) = $info->{data_type} =~ /^interval day\((\d+)\) to second\((\d+)\)\z/i) {
197 $info->{data_type} = join ' ', $info->{data_type} =~ /[a-z]+/ig;
198
199 if ($day_precision == 2 && $second_precision == 6) {
200 delete $info->{size};
201 }
202 else {
203 $info->{size} = [ $day_precision, $second_precision ];
204 }
205 }
1c22571b 206 elsif (lc($info->{data_type}) eq 'float') {
207 $info->{original}{data_type} = 'float';
208 $info->{original}{size} = $info->{size};
209
210 if ($info->{size} <= 63) {
211 $info->{data_type} = 'real';
212 }
213 else {
214 $info->{data_type} = 'double precision';
215 }
216 delete $info->{size};
217 }
760fd65c 218 elsif (lc($info->{data_type}) eq 'urowid' && $info->{size} == 4000) {
219 delete $info->{size};
220 }
3785bc2e 221 elsif (lc($info->{data_type}) eq 'date') {
222 $info->{data_type} = 'datetime';
223 $info->{original}{data_type} = 'date';
224 }
1c22571b 225 elsif (lc($info->{data_type}) eq 'binary_float') {
226 $info->{data_type} = 'real';
227 $info->{original}{data_type} = 'binary_float';
228 }
229 elsif (lc($info->{data_type}) eq 'binary_double') {
230 $info->{data_type} = 'double precision';
231 $info->{original}{data_type} = 'binary_double';
232 }
760fd65c 233
268cc246 234 if ((eval { lc(${ $info->{default_value} }) }||'') eq 'sysdate') {
3785bc2e 235 my $current_timestamp = 'current_timestamp';
236 $info->{default_value} = \$current_timestamp;
701cd3e3 237
238 my $sysdate = 'sysdate';
239 $info->{original}{default_value} = \$sysdate;
760fd65c 240 }
fb328d1a 241 }
242
760fd65c 243 return $result;
fb328d1a 244}
245
e7262300 246=head1 SEE ALSO
247
248L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
249L<DBIx::Class::Schema::Loader::DBI>
250
251=head1 AUTHOR
252
9cc8e7e1 253See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
e7262300 254
be80bba7 255=head1 LICENSE
256
257This library is free software; you can redistribute it and/or modify it under
258the same terms as Perl itself.
fb328d1a 259
e7262300 260=cut
261
2621;
760fd65c 263# vim:et sts=4 sw=4 tw=0: