1 package SQL::Translator::Parser::Oracle;
3 # -------------------------------------------------------------------
4 # $Id: Oracle.pm,v 1.25 2006-05-24 18:10:30 duality72 Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::Oracle - parser for Oracle
30 use SQL::Translator::Parser::Oracle;
32 my $translator = SQL::Translator->new;
33 $translator->parser("SQL::Translator::Parser::Oracle");
37 From http://www.ss64.com/ora/table_c.html:
39 CREATE [GLOBAL TEMPORARY] TABLE [schema.]table (tbl_defs,...)
40 [ON COMMIT {DELETE|PRESERVE} ROWS]
41 [storage_options | CLUSTER cluster_name (col1, col2,... )
42 | ORGANIZATION {HEAP [storage_options]
43 | INDEX idx_organized_tbl_clause}]
44 [LOB_storage_clause][varray_clause][nested_storage_clause]
46 [[NO]CACHE] [[NO]MONITORING] [PARALLEL parallel_clause]
47 [ENABLE enable_clause | DISABLE disable_clause]
51 column datatype [DEFAULT expr] [column_constraint(s)]
59 STORAGE storage_clause
63 idx_organized_tbl_clause:
64 storage_option(s) [PCTTHRESHOLD int]
65 [COMPRESS int|NOCOMPRESS]
66 [ [INCLUDING column_name] OVERFLOW [storage_option(s)] ]
68 nested_storage_clause:
69 NESTED TABLE nested_item STORE AS storage_table
70 [RETURN AS {LOCATOR|VALUE} ]
73 Partition_clause {ENABLE|DISABLE} ROW MOVEMENT
76 (http://www.ss64.com/ora/clause_constraint_col.html)
78 CONSTRAINT constrnt_name {UNIQUE|PRIMARY KEY} constrnt_state
80 CONSTRAINT constrnt_name CHECK(condition) constrnt_state
82 CONSTRAINT constrnt_name [NOT] NULL constrnt_state
84 CONSTRAINT constrnt_name REFERENCES [schema.]table[(column)]
85 [ON DELETE {CASCADE|SET NULL}] constrnt_state
88 [[NOT] DEFERRABLE] [INITIALLY {IMMEDIATE|DEFERRED}]
89 [RELY | NORELY] [USING INDEX using_index_clause]
90 [ENABLE|DISABLE] [VALIDATE|NOVALIDATE]
91 [EXCEPTIONS INTO [schema.]table]
93 Note that probably not all of the above syntax is supported, but the grammar
94 was altered to better handle the syntax created by DDL::Oracle.
99 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
100 $VERSION = sprintf "%d.%02d", q$Revision: 1.25 $ =~ /(\d+)\.(\d+)/;
101 $DEBUG = 0 unless defined $DEBUG;
104 use Parse::RecDescent;
106 use base qw(Exporter);
108 @EXPORT_OK = qw(parse);
110 # Enable warnings within the Parse::RecDescent module.
111 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
112 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
113 $::RD_HINT = 1; # Give out hints to help fix problems.
117 { my ( %tables, %indices, %constraints, $table_order, @table_comments ) }
120 # The "eofile" rule makes the parser fail if any "statement" rule
121 # fails. Otherwise, the first successful match by a "statement"
122 # won't cause the failure needed to know that the parse, as a whole,
125 startrule : statement(s) eofile
129 indices => \%indices,
130 constraints => \%constraints,
147 alter : /alter/i WORD /[^;]+/ ';'
148 { @table_comments = () }
150 drop : /drop/i TABLE ';'
152 drop : /drop/i WORD(s) ';'
153 { @table_comments = () }
155 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
157 my $table_name = $item{'table_name'};
158 $tables{ $table_name }{'order'} = ++$table_order;
159 $tables{ $table_name }{'table_name'} = $table_name;
161 if ( @table_comments ) {
162 $tables{ $table_name }{'comments'} = [ @table_comments ];
163 @table_comments = ();
168 for my $definition ( @{ $item[4] } ) {
169 if ( $definition->{'type'} eq 'field' ) {
170 my $field_name = $definition->{'name'};
171 $tables{ $table_name }{'fields'}{ $field_name } =
172 { %$definition, order => $i };
175 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
176 $constraint->{'fields'} = [ $field_name ];
177 push @{ $tables{ $table_name }{'constraints'} },
181 elsif ( $definition->{'type'} eq 'constraint' ) {
182 $definition->{'type'} = $definition->{'constraint_type'};
183 push @{ $tables{ $table_name }{'constraints'} }, $definition;
186 push @{ $tables{ $table_name }{'indices'} }, $definition;
190 for my $option ( @{ $item[6] } ) {
191 push @{ $tables{ $table_name }{'table_options'} }, $option;
197 create : create_index index_name /on/i table_name index_expr table_option(?) ';'
199 my $table_name = $item[4];
201 push @{ $constraints{ $table_name } }, {
208 push @{ $indices{ $table_name } }, {
216 index_expr: parens_word_list
218 | '(' WORD parens_word_list ')'
220 my $arg_list = join(",", @{$item[3]});
221 $return = "$item[2]($arg_list)";
224 # Create anything else (e.g., domain, function, etc.)
225 create : ...!create_table ...!create_index /create/i WORD /[^;]+/ ';'
226 { @table_comments = () }
228 create_index : /create/i UNIQUE(?) /index/i
229 { $return = $item[2] }
231 index_name : NAME '.' NAME
236 global_temporary: /global/i /temporary/i
238 table_name : NAME '.' NAME
243 create_definition : field
247 table_comment : comment
249 my $comment = $item[1];
251 push @table_comments, $comment;
254 comment : /^\s*(?:#|-{2}).*\n/
256 my $comment = $item[1];
257 $comment =~ s/^\s*(#|-{2})\s*//;
258 $comment =~ s/\s*$//;
262 comment : /\/\*/ /[^\*]+/ /\*\//
264 my $comment = $item[2];
265 $comment =~ s/^\s*|\s*$//g;
269 remark : /^REM\s+.*\n/
271 run : /^(RUN|\/)\s+.*\n/
273 prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
275 prompt : /prompt\s+create\s+.*\n/i
277 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
279 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
282 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
284 my $table_name = $item[4]->{'table'};
285 my $field_name = $item[4]->{'field'};
286 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
287 $item{'comment_phrase'};
290 column_name : NAME '.' NAME
291 { $return = { table => $item[1], field => $item[3] } }
293 comment_phrase : /'.*?'/
300 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
302 my ( $is_pk, $default, @constraints );
304 for my $meta ( @{ $item[4] } ) {
305 if ( $meta->{'type'} eq 'default' ) {
309 elsif ( $meta->{'type'} eq 'not_null' ) {
313 elsif ( $meta->{'type'} eq 'primary_key' ) {
317 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
320 my @comments = ( @{ $item[1] }, @{ $item[5] } );
324 name => $item{'field_name'},
325 data_type => $item{'data_type'}{'type'},
326 size => $item{'data_type'}{'size'},
328 default => $default->{'value'},
329 is_primary_key => $is_pk,
330 constraints => [ @constraints ],
331 comments => [ @comments ],
338 data_type : ora_data_type data_size(?)
342 size => $item[2][0] || '',
346 data_size : '(' VALUE(s /,/) data_size_modifier(?) ')'
349 data_size_modifier: /byte/i
352 column_constraint : constraint_name(?) column_constraint_type constraint_state(s?)
354 my $desc = $item{'column_constraint_type'};
355 my $type = $desc->{'type'};
356 my $fields = $desc->{'fields'} || [];
357 my $expression = $desc->{'expression'} || '';
360 supertype => 'constraint',
361 name => $item{'constraint_name(?)'}[0] || '',
363 expression => $type eq 'check' ? $expression : '',
364 deferrable => $item{'deferrable'},
365 deferred => $item{'deferred'},
366 reference_table => $desc->{'reference_table'},
367 reference_fields => $desc->{'reference_fields'},
368 # match_type => $desc->{'match_type'},
369 # on_update => $desc->{'on_update'},
373 constraint_name : /constraint/i NAME { $item[2] }
375 column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } }
377 { $return = { type => 'null' } }
379 { $return = { type => 'unique' } }
381 { $return = { type => 'primary_key' } }
382 | /check/i '(' /[^)]+/ ')'
383 { $return = { type => 'check', expression => $item[3] } }
384 | /references/i table_name parens_word_list(?) on_delete(?)
387 type => 'foreign_key',
388 reference_table => $item[2],
389 reference_fields => $item[3][0],
390 # match_type => $item[4][0],
391 on_delete => $item[5][0],
395 constraint_state : deferrable { $return = { type => $item[1] } }
396 | deferred { $return = { type => $item[1] } }
397 | /(no)?rely/i { $return = { type => $item[1] } }
398 # | /using/i /index/i using_index_clause
399 # { $return = { type => 'using_index', index => $item[3] } }
400 | /(dis|en)able/i { $return = { type => $item[1] } }
401 | /(no)?validate/i { $return = { type => $item[1] } }
402 | /exceptions/i /into/i table_name
403 { $return = { type => 'exceptions_into', table => $item[3] } }
405 deferrable : /not/i /deferrable/i
406 { $return = 'not_deferrable' }
408 { $return = 'deferrable' }
410 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
413 /(n?varchar2|varchar)/i { $return = 'varchar2' }
415 /n?char/i { $return = 'character' }
417 /n?dec/i { $return = 'decimal' }
419 /number/i { $return = 'number' }
421 /integer/i { $return = 'integer' }
423 /(pls_integer|binary_integer)/i { $return = 'integer' }
425 /interval\s+day/i { $return = 'interval day' }
427 /interval\s+year/i { $return = 'interval year' }
429 /long\s+raw/i { $return = 'long raw' }
431 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile|float)/i { $item[1] }
433 parens_value_list : '(' VALUE(s /,/) ')'
436 parens_word_list : '(' WORD(s /,/) ')'
439 field_meta : default_val
442 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
445 $val =~ s/'//g if defined $val;
447 supertype => 'constraint',
453 create_table : /create/i global_temporary(?) /table/i
455 table_option : /organization/i WORD
457 $return = { 'ORGANIZATION' => $item[2] }
460 table_option : /nomonitoring/i
462 $return = { 'NOMONITORING' => undef }
465 table_option : /parallel/i '(' key_value(s) ')'
467 $return = { 'PARALLEL' => $item[3] }
470 key_value : WORD VALUE
472 $return = { $item[1], $item[2] }
475 table_option : /[^;]+/
477 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) constraint_state(s?) comment(s?)
479 my $desc = $item{'table_constraint_type'};
480 my $type = $desc->{'type'};
481 my $fields = $desc->{'fields'};
482 my $expression = $desc->{'expression'};
483 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
486 name => $item{'constraint_name(?)'}[0] || '',
487 type => 'constraint',
488 constraint_type => $type,
489 fields => $type ne 'check' ? $fields : [],
490 expression => $type eq 'check' ? $expression : '',
491 deferrable => $item{'deferrable(?)'},
492 deferred => $item{'deferred(?)'},
493 reference_table => $desc->{'reference_table'},
494 reference_fields => $desc->{'reference_fields'},
495 # match_type => $desc->{'match_type'}[0],
496 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
497 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
498 comments => [ @comments ],
502 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
505 type => 'primary_key',
510 /unique/i '(' NAME(s /,/) ')'
518 /check/ '(' /(.+)/ ')'
522 expression => $item[3],
526 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete(?)
529 type => 'foreign_key',
531 reference_table => $item[6],
532 reference_fields => $item[7][0],
533 # match_type => $item[8][0],
534 on_delete => $item[8][0],
535 # on_update => $item[9][0],
539 on_delete : /on delete/i WORD(s)
540 { join(' ', @{$item[2]}) }
542 UNIQUE : /unique/i { $return = 1 }
546 NAME : /\w+/ { $item[1] }
550 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
552 | /'.*?'/ # XXX doesn't handle embedded quotes
559 # -------------------------------------------------------------------
561 my ( $translator, $data ) = @_;
562 my $parser = Parse::RecDescent->new($GRAMMAR);
564 local $::RD_TRACE = $translator->trace ? 1 : undef;
565 local $DEBUG = $translator->debug;
567 unless (defined $parser) {
568 return $translator->error("Error instantiating Parse::RecDescent ".
569 "instance: Bad grammer");
572 my $result = $parser->startrule( $data );
573 die "Parse failed.\n" unless defined $result;
575 warn "Parser results =\n", Dumper($result), "\n";
578 my $schema = $translator->schema;
579 my $indices = $result->{'indices'};
580 my $constraints = $result->{'constraints'};
582 $result->{'tables'}{ $a }{'order'}
584 $result->{'tables'}{ $b }{'order'}
585 } keys %{ $result->{'tables'} };
587 for my $table_name ( @tables ) {
588 my $tdata = $result->{'tables'}{ $table_name };
589 next unless $tdata->{'table_name'};
590 my $table = $schema->add_table(
591 name => $tdata->{'table_name'},
592 comments => $tdata->{'comments'},
593 ) or die $schema->error;
595 $table->options( $tdata->{'table_options'} );
598 $tdata->{'fields'}->{$a}->{'order'}
600 $tdata->{'fields'}->{$b}->{'order'}
601 } keys %{ $tdata->{'fields'} };
603 for my $fname ( @fields ) {
604 my $fdata = $tdata->{'fields'}{ $fname };
605 my $field = $table->add_field(
606 name => $fdata->{'name'},
607 data_type => $fdata->{'data_type'},
608 size => $fdata->{'size'},
609 default_value => $fdata->{'default'},
610 is_auto_increment => $fdata->{'is_auto_inc'},
611 is_nullable => $fdata->{'null'},
612 comments => $fdata->{'comments'},
613 ) or die $table->error;
616 push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
617 push @{ $tdata->{'constraints'} },
618 @{ $constraints->{ $table_name } || [] };
620 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
621 my $index = $table->add_index(
622 name => $idata->{'name'},
623 type => uc $idata->{'type'},
624 fields => $idata->{'fields'},
625 ) or die $table->error;
628 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
629 my $constraint = $table->add_constraint(
630 name => $cdata->{'name'},
631 type => $cdata->{'type'},
632 fields => $cdata->{'fields'},
633 reference_table => $cdata->{'reference_table'},
634 reference_fields => $cdata->{'reference_fields'},
635 match_type => $cdata->{'match_type'} || '',
636 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
637 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
638 ) or die $table->error;
647 # -------------------------------------------------------------------
648 # Something there is that doesn't love a wall.
650 # -------------------------------------------------------------------
656 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
660 SQL::Translator, Parse::RecDescent, DDL::Oracle.