Fixed up ON DELETE parsing for FKs
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / Oracle.pm
index 46bcaec..8ed34e9 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Parser::Oracle;
 
 # -------------------------------------------------------------------
-# $Id: Oracle.pm,v 1.16 2004-02-09 22:23:40 kycl4rk Exp $
+# $Id: Oracle.pm,v 1.24 2006-05-05 16:42:17 duality72 Exp $
 # -------------------------------------------------------------------
 # Copyright (C) 2002-4 SQLFairy Authors
 #
@@ -49,7 +49,6 @@ From http://www.ss64.com/ora/table_c.html:
 
 tbl_defs:
    column datatype [DEFAULT expr] [column_constraint(s)]
-   table_constraint
    table_ref_constraint
 
 storage_options:
@@ -98,7 +97,7 @@ was altered to better handle the syntax created by DDL::Oracle.
 
 use strict;
 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -115,9 +114,9 @@ $::RD_HINT   = 1; # Give out hints to help fix problems.
 
 my $parser; 
 
-$GRAMMAR = q!
+$GRAMMAR = q`
 
-{ my ( %tables, $table_order, @table_comments ) }
+{ my ( %tables, %indices, %constraints, $table_order, @table_comments ) }
 
 #
 # The "eofile" rule makes the parser fail if any "statement" rule
@@ -125,11 +124,21 @@ $GRAMMAR = q!
 # won't cause the failure needed to know that the parse, as a whole,
 # failed. -ky
 #
-startrule : statement(s) eofile { \%tables }
+startrule : statement(s) eofile 
+    { 
+        $return = {
+            tables      => \%tables,
+            indices     => \%indices,
+            constraints => \%constraints,
+        };
+    }
 
 eofile : /^\Z/
 
-statement : create
+statement : remark
+       | run
+    | prompt
+    | create
     | table_comment
     | comment_on_table
     | comment_on_column
@@ -145,9 +154,7 @@ drop : /drop/i TABLE ';'
 drop : /drop/i WORD(s) ';'
     { @table_comments = () }
 
-prompt : /prompt/i create_table table_name
-
-create : prompt(?) create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
+create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
     {
         my $table_name                       = $item{'table_name'};
         $tables{ $table_name }{'order'}      = ++$table_order;
@@ -160,7 +167,7 @@ create : prompt(?) create_table table_name '(' create_definition(s /,/) ')' tabl
 
         my $i = 1;
         my @constraints;
-        for my $definition ( @{ $item[5] } ) {
+        for my $definition ( @{ $item[4] } ) {
             if ( $definition->{'type'} eq 'field' ) {
                 my $field_name = $definition->{'name'};
                 $tables{ $table_name }{'fields'}{ $field_name } = 
@@ -182,27 +189,52 @@ create : prompt(?) create_table table_name '(' create_definition(s /,/) ')' tabl
             }
         }
 
-        for my $option ( @{ $item[7] } ) {
+        for my $option ( @{ $item[6] } ) {
             push @{ $tables{ $table_name }{'table_options'} }, $option;
         }
 
         1;
     }
 
-create : /create/i /index/i WORD /on/i table_name parens_word_list table_option(?) ';'
+create : create_index index_name /on/i table_name index_expr table_option(?) ';'
     {
-        my $table_name = $item[5];
-        push @{ $tables{ $table_name }{'indices'} }, {
-            name   => $item[3],
-            type   => 'normal',
-            fields => $item[6][0],
-        };
+        my $table_name = $item[4];
+        if ( $item[1] ) {
+            push @{ $constraints{ $table_name } }, {
+                name   => $item[2],
+                type   => 'unique',
+                fields => $item[5],
+            };
+        }
+        else {
+            push @{ $indices{ $table_name } }, {
+                name   => $item[2],
+                type   => 'normal',
+                fields => $item[5],
+            };
+        }
     }
 
+index_expr: parens_word_list
+       { $item[1] }
+       | '(' WORD parens_word_list ')'
+       {
+               my $arg_list = join(",", @{$item[3]});
+               $return = "$item[2]($arg_list)";
+       }
+
 # Create anything else (e.g., domain, function, etc.)
-create : /create/i WORD /[^;]+/ ';'
+create : ...!create_table ...!create_index /create/i WORD /[^;]+/ ';'
     { @table_comments = () }
 
+create_index : /create/i UNIQUE(?) /index/i
+       { $return = $item[2] }
+
+index_name : NAME '.' NAME
+    { $item[3] }
+    | NAME 
+    { $item[1] }
+
 global_temporary: /global/i /temporary/i
 
 table_name : NAME '.' NAME
@@ -236,6 +268,14 @@ comment : /\/\*/ /[^\*]+/ /\*\//
         $return = $comment;
     }
 
+remark : /^REM\s+.*\n/
+
+run : /^(RUN|\/)\s+.*\n/
+
+prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
+
+prompt : /prompt\s+create\s+.*\n/i
+
 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
     {
         push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
@@ -297,16 +337,21 @@ field : comment(s?) field_name data_type field_meta(s?) comment(s?)
 
 field_name : NAME
 
-data_type : ora_data_type parens_value_list(?)
+data_type : ora_data_type data_size(?)
     { 
         $return  = { 
             type => $item[1],
             size => $item[2][0] || '',
         } 
     }
+    
+data_size : '(' VALUE(s /,/) data_size_modifier(?) ')'
+    { $item[2] } 
+
+data_size_modifier: /byte/i
+       | /char/i
 
-column_constraint : constraint_name(?) column_constraint_type 
-#constraint_state(s /,/)
+column_constraint : constraint_name(?) column_constraint_type constraint_state(s?)
     {
         my $desc       = $item{'column_constraint_type'};
         my $type       = $desc->{'type'};
@@ -323,41 +368,41 @@ column_constraint : constraint_name(?) column_constraint_type
             reference_table  => $desc->{'reference_table'},
             reference_fields => $desc->{'reference_fields'},
 #            match_type       => $desc->{'match_type'},
-#            on_update_do     => $desc->{'on_update_do'},
+#            on_update        => $desc->{'on_update'},
         } 
     }
 
 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] } }
-    | /references/i table_name parens_word_list(?) on_delete_do(?) 
+        { $return = { type => 'check', expression => $item[3] } }
+    | /references/i table_name parens_word_list(?) on_delete(?) 
     {
         $return              =  {
             type             => 'foreign_key',
             reference_table  => $item[2],
             reference_fields => $item[3][0],
 #            match_type       => $item[4][0],
-            on_delete_do     => $item[5][0],
+            on_delete     => $item[5][0],
         }
     }
 
-#constraint_state : deferrable { $return = { type => $item[1] } }
-#    | deferred { $return = { type => $item[1] } }
-#    | /(no)?rely/ { $return = { type => $item[1] } }
+constraint_state : deferrable { $return = { type => $item[1] } }
+    | deferred { $return = { type => $item[1] } }
+    | /(no)?rely/i { $return = { type => $item[1] } }
 #    | /using/i /index/i using_index_clause 
-#        { $return = { type => 'using_index', index => $item[3] }
-#    | (dis)?enable { $return = { type => $item[1] } }
-#    | (no)?validate { $return = { type => $item[1] } }
-#    | /exceptions/i /into/i table_name 
-#        { $return = { type => 'exceptions_into', table => $item[3] } }
+#        { $return = { type => 'using_index', index => $item[3] } }
+    | /(dis|en)able/i { $return = { type => $item[1] } }
+    | /(no)?validate/i { $return = { type => $item[1] } }
+    | /exceptions/i /into/i table_name 
+        { $return = { type => 'exceptions_into', table => $item[3] } }
 
 deferrable : /not/i /deferrable/i 
     { $return = 'not_deferrable' }
@@ -375,15 +420,17 @@ ora_data_type :
        |
     /number/i { $return = 'number' }
     |
+    /integer/i { $return = 'integer' }
+    |
     /(pls_integer|binary_integer)/i { $return = 'integer' }
     |
-    /interval\s+day/i { $return = 'interval_day' }
+    /interval\s+day/i { $return = 'interval day' }
     |
-    /interval\s+year/i { $return = 'interval_year' }
+    /interval\s+year/i { $return = 'interval year' }
     |
-    /long\s+raw/i { $return = 'long_raw' }
+    /long\s+raw/i { $return = 'long raw' }
     |
-    /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile)/i { $item[1] }
+    /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile|float)/i { $item[1] }
 
 parens_value_list : '(' VALUE(s /,/) ')'
     { $item[2] }
@@ -429,7 +476,7 @@ key_value : WORD VALUE
 
 table_option : /[^;]+/
 
-table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
+table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) constraint_state(s?) comment(s?)
     {
         my $desc       = $item{'table_constraint_type'};
         my $type       = $desc->{'type'};
@@ -448,13 +495,13 @@ table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrab
             reference_table  => $desc->{'reference_table'},
             reference_fields => $desc->{'reference_fields'},
 #            match_type       => $desc->{'match_type'}[0],
-            on_delete_do     => $desc->{'on_delete_do'},
-            on_update_do     => $desc->{'on_update_do'},
+            on_delete        => $desc->{'on_delete'} || $desc->{'on_delete_do'},
+            on_update        => $desc->{'on_update'} || $desc->{'on_update_do'},
             comments         => [ @comments ],
         } 
     }
 
-table_constraint_type : /primary key/i '(' NAME(s /,/) ')' 
+table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
     { 
         $return = {
             type   => 'primary_key',
@@ -478,21 +525,23 @@ table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
         }
     }
     |
-    /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete_do(?)
+    /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete(?)
     {
         $return              =  {
             type             => 'foreign_key',
             fields           => $item[3],
             reference_table  => $item[6],
             reference_fields => $item[7][0],
-            match_type       => $item[8][0],
-            on_delete_do     => $item[9][0],
-            on_update_do     => $item[10][0],
+#            match_type       => $item[8][0],
+            on_delete     => $item[8][0],
+#            on_update     => $item[9][0],
         }
     }
 
-on_delete_do : /on delete/i WORD(s)
-    { $item[2] }
+on_delete : /on delete/i WORD(s)
+    { join(' ', @{$item[2]}) }
+
+UNIQUE : /unique/i { $return = 1 }
 
 WORD : /\w+/
 
@@ -507,7 +556,7 @@ VALUE   : /[-+]?\.?\d+(?:[eE]\d+)?/
     | /NULL/
     { 'NULL' }
 
-!;
+`;
 
 # -------------------------------------------------------------------
 sub parse {
@@ -522,17 +571,24 @@ sub parse {
             "instance: Bad grammer");
     }
 
-    my $result = $parser->startrule($data);
+    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 @tables = sort { 
-        $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
-    } keys %{ $result };
+    my $schema      = $translator->schema;
+    my $indices     = $result->{'indices'};
+    my $constraints = $result->{'constraints'};
+    my @tables      = sort { 
+        $result->{'tables'}{ $a }{'order'} 
+        <=> 
+        $result->{'tables'}{ $b }{'order'}
+    } keys %{ $result->{'tables'} };
 
     for my $table_name ( @tables ) {
-        my $tdata    =  $result->{ $table_name };
+        my $tdata    =  $result->{'tables'}{ $table_name };
+        next unless $tdata->{'table_name'};
         my $table    =  $schema->add_table( 
             name     => $tdata->{'table_name'},
             comments => $tdata->{'comments'},
@@ -557,14 +613,12 @@ sub parse {
                 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(
                 name   => $idata->{'name'},
@@ -581,8 +635,8 @@ sub parse {
                 reference_table  => $cdata->{'reference_table'},
                 reference_fields => $cdata->{'reference_fields'},
                 match_type       => $cdata->{'match_type'} || '',
-                on_delete        => $cdata->{'on_delete_do'},
-                on_update        => $cdata->{'on_update_do'},
+                on_delete        => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
+                on_update        => $cdata->{'on_update'} || $cdata->{'on_update_do'},
             ) or die $table->error;
         }
     }