Changed references to on_delete_do to on_delete and on_update_do to on_update. I...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / Oracle.pm
index 1979450..9868d08 100644 (file)
@@ -1,9 +1,9 @@
 package SQL::Translator::Parser::Oracle;
 
 # -------------------------------------------------------------------
-# $Id: Oracle.pm,v 1.1 2003-04-10 03:09:28 kycl4rk Exp $
+# $Id: Oracle.pm,v 1.20 2005-06-28 16:39:41 mwz444 Exp $
 # -------------------------------------------------------------------
-# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
+# Copyright (C) 2002-4 SQLFairy Authors
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License as
@@ -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:
@@ -91,11 +90,14 @@ constrnt_state
           [ENABLE|DISABLE] [VALIDATE|NOVALIDATE]
               [EXCEPTIONS INTO [schema.]table]
 
+Note that probably not all of the above syntax is supported, but the grammar 
+was altered to better handle the syntax created by DDL::Oracle.
+
 =cut
 
 use strict;
 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.20 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -114,7 +116,7 @@ my $parser;
 
 $GRAMMAR = q!
 
-{ our ( %tables, $table_order ) }
+{ my ( %tables, %indices, %constraints, $table_order, @table_comments ) }
 
 #
 # The "eofile" rule makes the parser fail if any "statement" rule
@@ -122,15 +124,34 @@ $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
-  | comment
-  | comment_on_table
-  | comment_on_column
-  | <error>
+statement : remark
+    | prompt
+    | create
+    | 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 = () }
 
 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
     {
@@ -138,6 +159,11 @@ create : create_table table_name '(' create_definition(s /,/) ')' table_option(s
         $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] } ) {
@@ -147,13 +173,6 @@ create : create_table table_name '(' create_definition(s /,/) ')' table_option(s
                     { %$definition, order => $i };
                 $i++;
                                
-                if ( $definition->{'is_primary_key'} ) {
-                    push @{ $tables{ $table_name }{'indices'} }, {
-                        type   => 'primary_key',
-                        fields => [ $field_name ],
-                    };
-                }
-
                 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
                     $constraint->{'fields'} = [ $field_name ];
                     push @{ $tables{ $table_name }{'constraints'} }, 
@@ -162,18 +181,7 @@ create : create_table table_name '(' create_definition(s /,/) ')' table_option(s
             }
             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;
@@ -181,15 +189,34 @@ create : create_table table_name '(' create_definition(s /,/) ')' table_option(s
         }
 
         for my $option ( @{ $item[6] } ) {
-            $tables{ $table_name }{'table_options'}{ $option->{'type'} } = 
-                $option;
+            push @{ $tables{ $table_name }{'table_options'} }, $option;
         }
 
         1;
     }
 
+create : /create/i UNIQUE(?) /index/i WORD /on/i table_name parens_word_list table_option(?) ';'
+    {
+        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.)
 create : /create/i WORD /[^;]+/ ';'
+    { @table_comments = () }
 
 global_temporary: /global/i /temporary/i
 
@@ -202,45 +229,89 @@ create_definition : field
     | 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;
+    }
+
+remark : /^REM\s+.*\n/
+
+prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
 
-comment_on_table : /comment/i /on/i /table/i table_name /is/i phrase ';'
+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[7];
+        push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
     }
 
-comment_on_column : /comment/i /on/i /column/i column_name /is/i phrase ';'
+comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
     {
         my $table_name = $item[4]->{'table'};
         my $field_name = $item[4]->{'field'};
         push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} }, 
-            $item[7];
+            $item{'comment_phrase'};
     }
 
 column_name : NAME '.' NAME
     { $return = { table => $item[1], field => $item[3] } }
 
-phrase : /'.*?'/ { $item[1] }
+comment_phrase : /'.*?'/ 
+    { 
+        my $val = $item[1];
+        $val =~ s/^'|'$//g;
+        $return = $val;
+    }
 
 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
     {
-        my ( $default, @constraints );
+        my ( $is_pk, $default, @constraints );
+        my $null = 1;
         for my $meta ( @{ $item[4] } ) {
-            $default = $meta if $meta->{'meta_type'} eq 'default';
-            push @constraints, $meta if $meta->{'meta_type'} eq 'constraint';
+            if ( $meta->{'type'} eq 'default' ) {
+                $default = $meta;
+                next;
+            }
+            elsif ( $meta->{'type'} eq 'not_null' ) {
+                $null = 0;
+                next;
+            }
+            elsif ( $meta->{'type'} eq 'primary_key' ) {
+                $is_pk = 1;
+            }
+
+            push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
         }
 
-        my $null = ( grep { $_->{'type'} eq 'not_null' } @constraints ) ? 0 : 1;
+        my @comments = ( @{ $item[1] }, @{ $item[5] } );
 
         $return = { 
             type           => 'field',
             name           => $item{'field_name'}, 
             data_type      => $item{'data_type'}{'type'},
             size           => $item{'data_type'}{'size'},
-            list           => $item{'data_type'}{'list'},
             null           => $null,
             default        => $default->{'value'},
+            is_primary_key => $is_pk,
             constraints    => [ @constraints ],
+            comments       => [ @comments ],
         } 
     }
     | <error>
@@ -256,7 +327,6 @@ data_type : ora_data_type parens_value_list(?)
     }
 
 column_constraint : constraint_name(?) column_constraint_type 
-#constraint_state(s /,/)
     {
         my $desc       = $item{'column_constraint_type'};
         my $type       = $desc->{'type'};
@@ -264,43 +334,38 @@ column_constraint : constraint_name(?) column_constraint_type
         my $expression = $desc->{'expression'} || '';
 
         $return              =  {
-            meta_type        => 'constraint',
-            name             => $item{'constraint_name'}[0] || '',
+            supertype        => 'constraint',
+            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'},
 #            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' } }
-    |
-    /null/ 
+column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } }
+    | /null/ 
         { $return = { type => 'null' } }
-    |
-    /unique/ 
+    | /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(?) 
+    | /check/i '(' /[^)]+/ ')' 
+        { $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],
         }
     }
 
@@ -322,12 +387,16 @@ deferrable : /not/i /deferrable/i
 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
 
 ora_data_type :
-    /(n?varchar2|varchar)/i { $return = 'varchar' }
+    /(n?varchar2|varchar)/i { $return = 'varchar2' }
     |
     /n?char/i { $return = 'character' }
     |
+       /n?dec/i { $return = 'decimal' }
+       |
     /number/i { $return = 'number' }
     |
+    /integer/i { $return = 'integer' }
+    |
     /(pls_integer|binary_integer)/i { $return = 'integer' }
     |
     /interval\s+day/i { $return = 'interval_day' }
@@ -345,21 +414,41 @@ parens_word_list : '(' WORD(s /,/) ')'
     { $item[2] }
 
 field_meta : default_val
-    |
-    column_constraint
+    | column_constraint
 
 default_val  : /default/i /(?:')?[\w\d.-]*(?:')?/ 
     { 
-        my $val =  $item[2] || '';
-        $val    =~ s/'//g; 
+        my $val =  $item[2];
+        $val    =~ s/'//g if defined $val; 
         $return =  {
-            meta_type => 'default',
+            supertype => 'constraint',
+            type      => 'default',
             value     => $val,
         }
     }
 
 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?)
@@ -371,18 +460,18 @@ table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrab
         my @comments   = ( @{ $item[1] }, @{ $item[-1] } );
 
         $return              =  {
-            name             => $item{'constraint_name'}[0] || '',
+            name             => $item{'constraint_name(?)'}[0] || '',
             type             => 'constraint',
             constraint_type  => $type,
             fields           => $type ne 'check' ? $fields : [],
             expression       => $type eq 'check' ? $expression : '',
-            deferreable      => $item{'deferrable'},
-            deferred         => $item{'deferred'},
+            deferrable       => $item{'deferrable(?)'},
+            deferred         => $item{'deferred(?)'},
             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 ],
         } 
     }
@@ -411,7 +500,7 @@ 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',
@@ -419,18 +508,22 @@ table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
             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],
+            on_delete     => $item[9][0],
+            on_update     => $item[10][0],
         }
     }
 
-on_delete_do : /on delete/i WORD(s)
+on_delete : /on delete/i WORD(s)
     { $item[2] }
 
+UNIQUE : /unique/i { $return = 1 }
+
 WORD : /\w+/
 
 NAME : /\w+/ { $item[1] }
 
+TABLE : /table/i
+
 VALUE   : /[-+]?\.?\d+(?:[eE]\d+)?/
     { $item[1] }
     | /'.*?'/   # XXX doesn't handle embedded quotes
@@ -453,14 +546,86 @@ 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;
-    return $result;
+    if ( $DEBUG ) {
+        warn "Parser results =\n", Dumper($result), "\n";
+    }
+
+    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->{'tables'}{ $table_name };
+        next unless $tdata->{'table_name'};
+        my $table    =  $schema->add_table( 
+            name     => $tdata->{'table_name'},
+            comments => $tdata->{'comments'},
+        ) or die $schema->error;
+
+        $table->options( $tdata->{'table_options'} );
+
+        my @fields = sort { 
+            $tdata->{'fields'}->{$a}->{'order'} 
+            <=>
+            $tdata->{'fields'}->{$b}->{'order'}
+        } keys %{ $tdata->{'fields'} };
+
+        for my $fname ( @fields ) {
+            my $fdata = $tdata->{'fields'}{ $fname };
+            my $field = $table->add_field(
+                name              => $fdata->{'name'},
+                data_type         => $fdata->{'data_type'},
+                size              => $fdata->{'size'},
+                default_value     => $fdata->{'default'},
+                is_auto_increment => $fdata->{'is_auto_inc'},
+                is_nullable       => $fdata->{'null'},
+                comments          => $fdata->{'comments'},
+            ) or die $table->error;
+        }
+
+        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'},
+                type   => uc $idata->{'type'},
+                fields => $idata->{'fields'},
+            ) or die $table->error;
+        }
+
+        for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
+            my $constraint       =  $table->add_constraint(
+                name             => $cdata->{'name'},
+                type             => $cdata->{'type'},
+                fields           => $cdata->{'fields'},
+                reference_table  => $cdata->{'reference_table'},
+                reference_fields => $cdata->{'reference_fields'},
+                match_type       => $cdata->{'match_type'} || '',
+                on_delete        => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
+                on_update        => $cdata->{'on_update'} || $cdata->{'on_update_do'},
+            ) or die $table->error;
+        }
+    }
+
+    return 1;
 }
 
 1;
 
+# -------------------------------------------------------------------
+# Something there is that doesn't love a wall.
+# Robert Frost
+# -------------------------------------------------------------------
+
 =pod
 
 =head1 AUTHOR
@@ -469,6 +634,6 @@ Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
 
 =head1 SEE ALSO
 
-perl(1), Parse::RecDescent.
+SQL::Translator, Parse::RecDescent, DDL::Oracle.
 
 =cut