package SQL::Translator::Parser::Oracle;
# -------------------------------------------------------------------
-# $Id: Oracle.pm,v 1.17 2004-02-11 21:36:00 kycl4rk Exp $
+# $Id: Oracle.pm,v 1.19 2004-09-17 21:52:46 kycl4rk Exp $
# -------------------------------------------------------------------
# Copyright (C) 2002-4 SQLFairy Authors
#
tbl_defs:
column datatype [DEFAULT expr] [column_constraint(s)]
- table_constraint
table_ref_constraint
storage_options:
use strict;
use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.17 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.19 $ =~ /(\d+)\.(\d+)/;
$DEBUG = 0 unless defined $DEBUG;
use Data::Dumper;
$GRAMMAR = q!
-{ my ( %tables, %indices, $table_order, @table_comments ) }
+{ my ( %tables, %indices, %constraints, $table_order, @table_comments ) }
#
# The "eofile" rule makes the parser fail if any "statement" rule
startrule : statement(s) eofile
{
$return = {
- tables => \%tables,
- indices => \%indices,
+ tables => \%tables,
+ indices => \%indices,
+ constraints => \%constraints,
};
}
1;
}
-create : /create/i /index/i WORD /on/i table_name parens_word_list table_option(?) ';'
+create : /create/i UNIQUE(?) /index/i WORD /on/i table_name parens_word_list table_option(?) ';'
{
- my $table_name = $item[5];
- push @{ $indices{ $table_name } }, {
- name => $item[3],
- type => 'normal',
- fields => $item[6][0],
- };
+ my $table_name = $item[6];
+ if ( $item[2] ) {
+ push @{ $constraints{ $table_name } }, {
+ name => $item[4],
+ type => 'unique',
+ fields => $item[7][0],
+ };
+ }
+ else {
+ push @{ $indices{ $table_name } }, {
+ name => $item[4],
+ type => 'normal',
+ fields => $item[7][0],
+ };
+ }
}
# Create anything else (e.g., domain, function, etc.)
constraint_name : /constraint/i NAME { $item[2] }
-column_constraint_type : /not null/i { $return = { type => 'not_null' } }
+column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } }
| /null/
{ $return = { type => 'null' } }
| /unique/
{ $return = { type => 'unique' } }
- | /primary key/i
+ | /primary\s+key/i
{ $return = { type => 'primary_key' } }
| /check/i '(' /[^)]+/ ')'
- { $return = { type => 'check', expression => $item[2] } }
+ { $return = { type => 'check', expression => $item[3] } }
| /references/i table_name parens_word_list(?) on_delete_do(?)
{
$return = {
on_delete_do : /on delete/i WORD(s)
{ $item[2] }
+UNIQUE : /unique/i { $return = 1 }
+
WORD : /\w+/
NAME : /\w+/ { $item[1] }
my $result = $parser->startrule( $data );
die "Parse failed.\n" unless defined $result;
- warn Dumper($result) if $DEBUG;
+ if ( $DEBUG ) {
+ warn "Parser results =\n", Dumper($result), "\n";
+ }
- my $schema = $translator->schema;
- my $indices = $result->{'indices'};
- my @tables = sort {
+ my $schema = $translator->schema;
+ my $indices = $result->{'indices'};
+ my $constraints = $result->{'constraints'};
+ my @tables = sort {
$result->{'tables'}{ $a }{'order'}
<=>
$result->{'tables'}{ $b }{'order'}
is_nullable => $fdata->{'null'},
comments => $fdata->{'comments'},
) or die $table->error;
-
- for my $cdata ( @{ $fdata->{'constraints'} } ) {
- next unless $cdata->{'type'} eq 'foreign_key';
- $cdata->{'fields'} ||= [ $field->name ];
- push @{ $tdata->{'constraints'} }, $cdata;
- }
}
push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
+ push @{ $tdata->{'constraints'} },
+ @{ $constraints->{ $table_name } || [] };
for my $idata ( @{ $tdata->{'indices'} || [] } ) {
my $index = $table->add_index(