package SQL::Translator::Parser::Oracle;
# -------------------------------------------------------------------
-# $Id: Oracle.pm,v 1.4 2003-06-11 03:59:49 kycl4rk Exp $
+# $Id: Oracle.pm,v 1.13 2003-09-26 21:04:07 kycl4rk Exp $
# -------------------------------------------------------------------
# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
#
use strict;
use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/;
$DEBUG = 0 unless defined $DEBUG;
use Data::Dumper;
$GRAMMAR = q!
-{ our ( %tables, $table_order ) }
+{ my ( %tables, $table_order, @table_comments ) }
#
# The "eofile" rule makes the parser fail if any "statement" rule
eofile : /^\Z/
statement : create
- | comment
- | comment_on_table
- | comment_on_column
- | alter
- | <error>
+ | table_comment
+ | comment_on_table
+ | comment_on_column
+ | alter
+ | drop
+ | <error>
alter : /alter/i WORD /[^;]+/ ';'
+ { @table_comments = () }
+
+drop : /drop/i TABLE ';'
+
+drop : /drop/i WORD(s) ';'
+ { @table_comments = () }
+
+prompt : /prompt/i create_table table_name
-create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
+create : prompt(?) create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
{
my $table_name = $item{'table_name'};
$tables{ $table_name }{'order'} = ++$table_order;
$tables{ $table_name }{'table_name'} = $table_name;
+ if ( @table_comments ) {
+ $tables{ $table_name }{'comments'} = [ @table_comments ];
+ @table_comments = ();
+ }
+
my $i = 1;
my @constraints;
- for my $definition ( @{ $item[4] } ) {
+ for my $definition ( @{ $item[5] } ) {
if ( $definition->{'type'} eq 'field' ) {
my $field_name = $definition->{'name'};
$tables{ $table_name }{'fields'}{ $field_name } =
}
elsif ( $definition->{'type'} eq 'constraint' ) {
$definition->{'type'} = $definition->{'constraint_type'};
- # group FKs at the field level
-# if ( $definition->{'type'} eq 'foreign_key' ) {
-# for my $fld ( @{ $definition->{'fields'} || [] } ) {
-# push @{
-# $tables{$table_name}{'fields'}{$fld}{'constraints'}
-# }, $definition;
-# }
-# }
-# else {
- push @{ $tables{ $table_name }{'constraints'} },
- $definition;
-# }
+ push @{ $tables{ $table_name }{'constraints'} }, $definition;
}
else {
push @{ $tables{ $table_name }{'indices'} }, $definition;
}
}
- for my $option ( @{ $item[6] } ) {
- $tables{ $table_name }{'table_options'}{ $option->{'type'} } =
- $option;
+ for my $option ( @{ $item[7] } ) {
+ push @{ $tables{ $table_name }{'table_options'} }, $option;
}
1;
}
+create : /create/i /index/i WORD /on/i table_name parens_word_list ';'
+ {
+ my $table_name = $item[5];
+ push @{ $tables{ $table_name }{'indices'} }, {
+ name => $item[3],
+ type => 'normal',
+ fields => $item[6][0],
+ };
+ }
+
# Create anything else (e.g., domain, function, etc.)
create : /create/i WORD /[^;]+/ ';'
+ { @table_comments = () }
global_temporary: /global/i /temporary/i
| table_constraint
| <error>
+table_comment : comment
+ {
+ my $comment = $item[1];
+ $return = $comment;
+ push @table_comments, $comment;
+ }
+
comment : /^\s*(?:#|-{2}).*\n/
+ {
+ my $comment = $item[1];
+ $comment =~ s/^\s*(#|-{2})\s*//;
+ $comment =~ s/\s*$//;
+ $return = $comment;
+ }
+
+comment : /\/\*/ /[^\*]+/ /\*\//
+ {
+ my $comment = $item[2];
+ $comment =~ s/^\s*|\s*$//g;
+ $return = $comment;
+ }
comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
{
name => $item{'constraint_name(?)'}[0] || '',
type => $type,
expression => $type eq 'check' ? $expression : '',
- deferreable => $item{'deferrable'},
+ deferrable => $item{'deferrable'},
deferred => $item{'deferred'},
reference_table => $desc->{'reference_table'},
reference_fields => $desc->{'reference_fields'},
|
/n?char/i { $return = 'character' }
|
+ /n?dec/i { $return = 'decimal' }
+ |
/number/i { $return = 'number' }
|
/(pls_integer|binary_integer)/i { $return = 'integer' }
default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
{
- my $val = $item[2] || '';
- $val =~ s/'//g;
+ my $val = $item[2];
+ $val =~ s/'//g if defined $val;
$return = {
supertype => 'constraint',
type => 'default',
create_table : /create/i global_temporary(?) /table/i
+table_option : /organization/i WORD
+ {
+ $return = { 'ORGANIZATION' => $item[2] }
+ }
+
+table_option : /nomonitoring/i
+ {
+ $return = { 'NOMONITORING' => undef }
+ }
+
+table_option : /parallel/i '(' key_value(s) ')'
+ {
+ $return = { 'PARALLEL' => $item[3] }
+ }
+
+key_value : WORD VALUE
+ {
+ $return = { $item[1], $item[2] }
+ }
+
table_option : /[^;]+/
table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
constraint_type => $type,
fields => $type ne 'check' ? $fields : [],
expression => $type eq 'check' ? $expression : '',
- deferreable => $item{'deferrable(?)'},
+ deferrable => $item{'deferrable(?)'},
deferred => $item{'deferred(?)'},
reference_table => $desc->{'reference_table'},
reference_fields => $desc->{'reference_fields'},
NAME : /\w+/ { $item[1] }
+TABLE : /table/i
+
VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
{ $item[1] }
| /'.*?'/ # XXX doesn't handle embedded quotes
comments => $tdata->{'comments'},
) or die $schema->error;
+ $table->options( $tdata->{'table_options'} );
+
my @fields = sort {
$tdata->{'fields'}->{$a}->{'order'}
<=>