new dev release
[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;
5use base 'DBIx::Class::Schema::Loader::DBI';
6use Carp::Clan qw/^DBIx::Class/;
7use Class::C3;
8
1fa18849 9our $VERSION = '0.04999_13';
e7262300 10
11=head1 NAME
12
13DBIx::Class::Schema::Loader::DBI::Oracle - DBIx::Class::Schema::Loader::DBI
14Oracle Implementation.
15
16=head1 SYNOPSIS
17
18 package My::Schema;
19 use base qw/DBIx::Class::Schema::Loader/;
20
21 __PACKAGE__->loader_options( debug => 1 );
22
23 1;
24
25=head1 DESCRIPTION
26
27See L<DBIx::Class::Schema::Loader::Base>.
28
29This module is considered experimental and not well tested yet.
30
31=cut
32
d0e184e9 33sub _setup {
34 my $self = shift;
35
36 $self->next::method(@_);
37
38 my $dbh = $self->schema->storage->dbh;
46065bcb 39
40 my ($current_schema) = $dbh->selectrow_array('SELECT USER FROM DUAL', {});
41
42 $self->{db_schema} ||= $current_schema;
43
44 if (lc($self->db_schema) ne lc($current_schema)) {
45 $dbh->do('ALTER SESSION SET current_schema=' . $self->db_schema);
46 }
d0e184e9 47}
48
49
e7262300 50sub _table_columns {
51 my ($self, $table) = @_;
52
53 my $dbh = $self->schema->storage->dbh;
54
55 my $sth = $dbh->prepare($self->schema->storage->sql_maker->select($table, undef, \'1 = 0'));
56 $sth->execute;
57 return \@{$sth->{NAME_lc}};
58}
59
60sub _tables_list {
61 my $self = shift;
62
63 my $dbh = $self->schema->storage->dbh;
64
65 my @tables;
66 for my $table ( $dbh->tables(undef, $self->db_schema, '%', 'TABLE,VIEW') ) { #catalog, schema, table, type
67 my $quoter = $dbh->get_info(29);
68 $table =~ s/$quoter//g;
69
70 # remove "user." (schema) prefixes
71 $table =~ s/\w+\.//;
72
73 next if $table eq 'PLAN_TABLE';
74 $table = lc $table;
75 push @tables, $1
76 if $table =~ /\A(\w+)\z/;
77 }
78 return @tables;
79}
80
81sub _table_uniq_info {
82 my ($self, $table) = @_;
83
e7262300 84 my $dbh = $self->schema->storage->dbh;
85
86 my $sth = $dbh->prepare_cached(
65ab592d 87 q{
c7bf4194 88 SELECT constraint_name, acc.column_name
89 FROM all_constraints JOIN all_cons_columns acc USING (constraint_name)
8803e4ed 90 WHERE acc.table_name=? and acc.owner = ? AND constraint_type='U'
c7bf4194 91 ORDER BY acc.position
65ab592d 92 },
93 {}, 1);
e7262300 94
8803e4ed 95 $sth->execute(uc $table,$self->{db_schema} );
e7262300 96 my %constr_names;
97 while(my $constr = $sth->fetchrow_arrayref) {
65ab592d 98 my $constr_name = lc $constr->[0];
99 my $constr_def = lc $constr->[1];
e7262300 100 $constr_name =~ s/\Q$self->{_quoter}\E//;
101 $constr_def =~ s/\Q$self->{_quoter}\E//;
65ab592d 102 push @{$constr_names{$constr_name}}, $constr_def;
e7262300 103 }
65ab592d 104
105 my @uniqs = map { [ $_ => $constr_names{$_} ] } keys %constr_names;
e7262300 106 return \@uniqs;
107}
108
109sub _table_pk_info {
65ab592d 110 my ($self, $table) = @_;
111 return $self->next::method(uc $table);
e7262300 112}
113
114sub _table_fk_info {
115 my ($self, $table) = @_;
116
65ab592d 117 my $rels = $self->next::method(uc $table);
e7262300 118
65ab592d 119 foreach my $rel (@$rels) {
120 $rel->{remote_table} = lc $rel->{remote_table};
e7262300 121 }
122
65ab592d 123 return $rels;
124}
125
126sub _columns_info_for {
127 my ($self, $table) = @_;
128 return $self->next::method(uc $table);
e7262300 129}
130
fb328d1a 131sub _extra_column_info {
132 my ($self, $info) = @_;
133 my %extra_info;
134
135 my ($table, $column) = @$info{qw/TABLE_NAME COLUMN_NAME/};
136
137 my $dbh = $self->schema->storage->dbh;
138 my $sth = $dbh->prepare_cached(
139 q{
140 SELECT COUNT(*)
c7bf4194 141 FROM all_triggers ut JOIN all_trigger_cols atc USING (trigger_name)
142 WHERE atc.table_name = ? AND atc.column_name = ?
fb328d1a 143 AND column_usage LIKE '%NEW%' AND column_usage LIKE '%OUT%'
144 AND trigger_type = 'BEFORE EACH ROW' AND triggering_event LIKE '%INSERT%'
145 },
146 {}, 1);
147
148 $sth->execute($table, $column);
149 if ($sth->fetchrow_array) {
150 $extra_info{is_auto_increment} = 1;
151 }
152
153 return \%extra_info;
154}
155
e7262300 156=head1 SEE ALSO
157
158L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
159L<DBIx::Class::Schema::Loader::DBI>
160
161=head1 AUTHOR
162
9cc8e7e1 163See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
e7262300 164
be80bba7 165=head1 LICENSE
166
167This library is free software; you can redistribute it and/or modify it under
168the same terms as Perl itself.
fb328d1a 169
e7262300 170=cut
171
1721;