Changes to handle a constraint like:
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / Oracle.pm
index e8e4b64..ddb6b3f 100644 (file)
@@ -1,9 +1,7 @@
 package SQL::Translator::Parser::Oracle;
 
 # -------------------------------------------------------------------
-# $Id: Oracle.pm,v 1.3 2003-06-09 02:16:02 kycl4rk Exp $
-# -------------------------------------------------------------------
-# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
+# Copyright (C) 2002-2009 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 +47,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 +88,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.3 $ =~ /(\d+)\.(\d+)/;
+$VERSION = '1.59';
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -110,11 +110,9 @@ $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
 $::RD_WARN   = 1; # Enable warnings. This will warn on unused rules &c.
 $::RD_HINT   = 1; # Give out hints to help fix problems.
 
-my $parser; 
-
-$GRAMMAR = q!
+$GRAMMAR = q`
 
-{ our ( %tables, $table_order ) }
+{ my ( %tables, %indices, %constraints, $table_order, @table_comments, %views, $view_order, %procedures, $proc_order ) }
 
 #
 # The "eofile" rule makes the parser fail if any "statement" rule
@@ -122,18 +120,37 @@ $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,
+            views       => \%views,
+            procedures  => \%procedures,
+        };
+    }
 
 eofile : /^\Z/
 
-statement : create
-  | comment
-  | comment_on_table
-  | comment_on_column
-  | alter
-  | <error>
+statement : remark
+       | run
+    | 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?) ';'
     {
@@ -141,6 +158,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] } ) {
@@ -158,18 +180,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;
@@ -177,15 +188,82 @@ 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_index index_name /on/i table_name index_expr table_option(?) ';'
+    {
+        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 : /create/i /or replace/i /procedure/i table_name not_end m#^/$#im
+       {
+               @table_comments = ();
+        my $proc_name = $item[4];
+        # Hack to strip owner from procedure name
+        $proc_name =~ s#.*\.##;
+        my $owner = '';
+        my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5]";
+        
+        $procedures{ $proc_name }{'order'}  = ++$proc_order;
+        $procedures{ $proc_name }{'name'}   = $proc_name;
+        $procedures{ $proc_name }{'owner'}  = $owner;
+        $procedures{ $proc_name }{'sql'}    = $sql;
+       }
+
+not_end: m#.*?(?=^/$)#ism
+
+create : /create/i /or replace/i /force/i /view/i table_name not_delimiter ';'
+       {
+               @table_comments = ();
+        my $view_name = $item[5];
+        # Hack to strip owner from view name
+        $view_name =~ s#.*\.##;
+        my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5] $item[6] $item[7]";
+        
+        $views{ $view_name }{'order'}  = ++$view_order;
+        $views{ $view_name }{'name'}   = $view_name;
+        $views{ $view_name }{'sql'}    = $sql;
+       }
+
+not_delimiter: /.*?(?=;)/is
+
 # 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
 
@@ -194,11 +272,39 @@ table_name : NAME '.' NAME
     | NAME 
     { $item[1] }
 
-create_definition : field
-    | table_constraint
+create_definition : table_constraint
+    | field
     | <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/
+
+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 ';'
     {
@@ -261,16 +367,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'};
@@ -282,46 +393,61 @@ column_constraint : constraint_name(?) column_constraint_type
             name             => $item{'constraint_name(?)'}[0] || '',
             type             => $type,
             expression       => $type eq 'check' ? $expression : '',
-            deferreable      => $item{'deferrable'},
-            deferred         => $item{'deferred'},
+            deferrable       => $desc->{'deferrable'},
+            deferred         => $desc->{'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/ 
-        { $return = { type => 'null' } }
-    | /unique/ 
+column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } }
+    | /unique/i
         { $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 check_expression
+        { 
+            $return = { 
+                type       => 'check', 
+                expression => $item[2], 
+            };
+        }
+    | /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] } }
+LPAREN : '('
+
+RPAREN : ')'
+
+check_condition_text : /.+\s+in\s+\([^)]+\)/i
+    | /[^)]+/ 
+
+check_expression : LPAREN check_condition_text RPAREN 
+    { $return = join( ' ', map { $_ || () } 
+        $item[1], $item[2], $item[3], $item[4][0] ) 
+    }
+
+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' }
@@ -335,17 +461,21 @@ ora_data_type :
     |
     /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' }
+    /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|double)/i { $item[1] }
 
 parens_value_list : '(' VALUE(s /,/) ')'
     { $item[2] }
@@ -358,20 +488,48 @@ field_meta : default_val
 
 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',
             value     => $val,
         }
     }
+    | /null/i
+    {
+        $return =  {
+            supertype => 'constraint',
+            type      => 'default',
+            value     => 'NULL',
+        }
+    }
 
 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?)
+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'};
@@ -385,18 +543,18 @@ table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrab
             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'},
 #            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',
@@ -412,34 +570,38 @@ table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
         }
     }
     |
-    /check/ '(' /(.+)/ ')'
+    /check/i check_expression /^(en|dis)able/i
     {
         $return        =  {
             type       => 'check',
-            expression => $item[3],
+            expression => join(' ', $item[2], $item[3]),
         }
     }
     |
-    /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+/
 
 NAME : /\w+/ { $item[1] }
 
+TABLE : /table/i
+
 VALUE   : /[-+]?\.?\d+(?:[eE]\d+)?/
     { $item[1] }
     | /'.*?'/   # XXX doesn't handle embedded quotes
@@ -447,12 +609,12 @@ VALUE   : /[-+]?\.?\d+(?:[eE]\d+)?/
     | /NULL/
     { 'NULL' }
 
-!;
+`;
 
 # -------------------------------------------------------------------
 sub parse {
     my ( $translator, $data ) = @_;
-    $parser ||= Parse::RecDescent->new($GRAMMAR);
+    my $parser = Parse::RecDescent->new($GRAMMAR);
 
     local $::RD_TRACE = $translator->trace ? 1 : undef;
     local $DEBUG      = $translator->debug;
@@ -462,22 +624,31 @@ 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'},
         ) or die $schema->error;
 
+        $table->options( $tdata->{'table_options'} );
+
         my @fields = sort { 
             $tdata->{'fields'}->{$a}->{'order'} 
             <=>
@@ -495,14 +666,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'},
@@ -516,16 +685,40 @@ sub parse {
                 name             => $cdata->{'name'},
                 type             => $cdata->{'type'},
                 fields           => $cdata->{'fields'},
+                expression       => $cdata->{'expression'},
                 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;
         }
     }
+    
+    my @procedures = sort { 
+        $result->{procedures}->{ $a }->{'order'} <=> $result->{procedures}->{ $b }->{'order'}
+    } keys %{ $result->{procedures} };
+    foreach my $proc_name (@procedures) {
+       $schema->add_procedure(
+               name  => $proc_name,
+               owner => $result->{procedures}->{$proc_name}->{owner},
+               sql   => $result->{procedures}->{$proc_name}->{sql},
+               );
+    }
+
+    my @views = sort { 
+        $result->{views}->{ $a }->{'order'} <=> $result->{views}->{ $b }->{'order'}
+    } keys %{ $result->{views} };
+    foreach my $view_name (keys %{ $result->{views} }) {
+       $schema->add_view(
+               name => $view_name,
+               sql  => $result->{views}->{$view_name}->{sql},
+               );
+    }
 
-    return $result;
+    return 1;
 }
 
 1;
@@ -539,10 +732,10 @@ sub parse {
 
 =head1 AUTHOR
 
-Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
+Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
 
 =head1 SEE ALSO
 
-perl(1), Parse::RecDescent.
+SQL::Translator, Parse::RecDescent, DDL::Oracle.
 
 =cut