1 package SQL::Translator::Parser::DBI::Oracle;
3 # -------------------------------------------------------------------
4 # $Id: Oracle.pm,v 1.2 2006-03-09 20:18:05 cahille Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2006 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::DBI::Oracle - parser for DBD::Oracle
29 See SQL::Translator::Parser::DBI.
33 Uses DBI introspection methods to determine schema details.
40 use SQL::Translator::Schema::Constants;
41 use SQL::Translator::Schema::Table;
42 use SQL::Translator::Schema::Field;
43 use SQL::Translator::Schema::Constraint;
45 our $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
47 # -------------------------------------------------------------------
49 my ( $tr, $dbh ) = @_;
51 my $schema = $tr->schema;
53 my $sth = $dbh->table_info();
55 while(my $table_info = $sth->fetchrow_hashref('NAME_uc')) {
56 next unless ($table_info->{TABLE_TYPE} eq 'TABLE');
57 next if ($table_info->{TABLE_NAME} =~ /\$/);
61 my $table = $schema->add_table(
62 name => $table_info->{TABLE_NAME},
63 type => $table_info->{TABLE_TYPE},
66 # add the fields (columns) for this table
70 $sth = $dbh->column_info(
72 $table_info->{TABLE_SCHEM},
73 $table_info->{TABLE_NAME},
77 while(my $column = $sth->fetchrow_hashref('NAME_uc')) {
78 my $f = $table->add_field(
79 name => $column->{COLUMN_NAME},
80 default_value => $column->{COLUMN_DEF},
81 data_type => $column->{TYPE_NAME},
82 order => $column->{ORDINAL_POSITION},
83 size => $column->{COLUMN_SIZE},
84 ) || die $table->error;
86 $f->is_nullable( $column->{NULLABLE} == 1 );
89 # add the primary key info
91 $sth = $dbh->primary_key_info(
93 $table_info->{TABLE_SCHEM},
94 $table_info->{TABLE_NAME},
97 while(my $primary_key = $sth->fetchrow_hashref('NAME_uc')) {
98 my $f = $table->get_field( $primary_key->{COLUMN_NAME} );
99 $f->is_primary_key(1);
102 # add the foreign key info (constraints)
104 $sth = $dbh->foreign_key_info(
109 $table_info->{TABLE_SCHEM},
110 $table_info->{TABLE_NAME},
114 while(my $foreign_key = $sth->fetchrow_hashref('NAME_uc')) {
115 my $name = $foreign_key->{FK_NAME};
116 $cons->{$name}->{reference_table} = $foreign_key->{UK_TABLE_NAME};
117 push @{ $cons->{$name}->{fields} },
118 $foreign_key->{FK_COLUMN_NAME};
119 push @{ $cons->{$name}->{reference_fields} },
120 $foreign_key->{UK_COLUMN_NAME};
123 for my $name ( keys %$cons ) {
124 my $c = $table->add_constraint(
127 fields => $cons->{$name}->{fields},
128 reference_fields => $cons->{$name}->{reference_fields},
129 reference_table => $cons->{$name}->{reference_table},
130 ) || die $table->error;
143 Earl Cahill E<lt>cpan@spack.netE<gt>.
145 =head1 ACKNOWLEDGEMENT
147 Initial revision of this module came almost entirely from work done by
148 Todd Hepler E<lt>thepler@freeshell.orgE<gt>. My changes were
149 quite minor (ensuring NAME_uc, changing a couple variable names,
150 skipping tables with a $ in them).
152 Todd claimed his work to be an almost verbatim copy of
153 SQL::Translator::Parser::DBI::PostgreSQL revision 1.7
155 For me, the real work happens in DBD::Oracle and DBI, which, also
156 for me, that is the beauty of having introspection methods in DBI.
160 SQL::Translator, DBD::Oracle.