1 package SQL::Translator::Parser::DBI::Oracle;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2006-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
23 SQL::Translator::Parser::DBI::Oracle - parser for DBD::Oracle
27 See SQL::Translator::Parser::DBI.
31 Uses DBI introspection methods to determine schema details.
38 use SQL::Translator::Schema::Constants;
39 use SQL::Translator::Schema::Table;
40 use SQL::Translator::Schema::Field;
41 use SQL::Translator::Schema::Constraint;
43 our $VERSION = '1.59';
45 # -------------------------------------------------------------------
47 my ( $tr, $dbh ) = @_;
49 my $schema = $tr->schema;
51 my $sth = $dbh->table_info();
53 while(my $table_info = $sth->fetchrow_hashref('NAME_uc')) {
54 next unless ($table_info->{TABLE_TYPE} eq 'TABLE');
55 next if ($table_info->{TABLE_NAME} =~ /\$/);
59 my $table = $schema->add_table(
60 name => $table_info->{TABLE_NAME},
61 type => $table_info->{TABLE_TYPE},
64 # add the fields (columns) for this table
68 $sth = $dbh->column_info(
70 $table_info->{TABLE_SCHEM},
71 $table_info->{TABLE_NAME},
75 while(my $column = $sth->fetchrow_hashref('NAME_uc')) {
76 my $f = $table->add_field(
77 name => $column->{COLUMN_NAME},
78 default_value => $column->{COLUMN_DEF},
79 data_type => $column->{TYPE_NAME},
80 order => $column->{ORDINAL_POSITION},
81 size => $column->{COLUMN_SIZE},
82 ) || die $table->error;
84 $f->is_nullable( $column->{NULLABLE} == 1 );
87 # add the primary key info
89 $sth = $dbh->primary_key_info(
91 $table_info->{TABLE_SCHEM},
92 $table_info->{TABLE_NAME},
95 while(my $primary_key = $sth->fetchrow_hashref('NAME_uc')) {
96 my $f = $table->get_field( $primary_key->{COLUMN_NAME} );
97 $f->is_primary_key(1);
100 # add the foreign key info (constraints)
102 $sth = $dbh->foreign_key_info(
107 $table_info->{TABLE_SCHEM},
108 $table_info->{TABLE_NAME},
112 while(my $foreign_key = $sth->fetchrow_hashref('NAME_uc')) {
113 my $name = $foreign_key->{FK_NAME};
114 $cons->{$name}->{reference_table} = $foreign_key->{UK_TABLE_NAME};
115 push @{ $cons->{$name}->{fields} },
116 $foreign_key->{FK_COLUMN_NAME};
117 push @{ $cons->{$name}->{reference_fields} },
118 $foreign_key->{UK_COLUMN_NAME};
121 for my $name ( keys %$cons ) {
122 my $c = $table->add_constraint(
125 fields => $cons->{$name}->{fields},
126 reference_fields => $cons->{$name}->{reference_fields},
127 reference_table => $cons->{$name}->{reference_table},
128 ) || die $table->error;
141 Earl Cahill E<lt>cpan@spack.netE<gt>.
143 =head1 ACKNOWLEDGEMENT
145 Initial revision of this module came almost entirely from work done by
146 Todd Hepler E<lt>thepler@freeshell.orgE<gt>. My changes were
147 quite minor (ensuring NAME_uc, changing a couple variable names,
148 skipping tables with a $ in them).
150 Todd claimed his work to be an almost verbatim copy of
151 SQL::Translator::Parser::DBI::PostgreSQL revision 1.7
153 For me, the real work happens in DBD::Oracle and DBI, which, also
154 for me, that is the beauty of having introspection methods in DBI.
158 SQL::Translator, DBD::Oracle.