1 package SQL::Translator::Parser::Oracle;
5 SQL::Translator::Parser::Oracle - parser for Oracle
10 use SQL::Translator::Parser::Oracle;
12 my $translator = SQL::Translator->new;
13 $translator->parser("SQL::Translator::Parser::Oracle");
17 From http://www.ss64.com/ora/table_c.html:
19 CREATE [GLOBAL TEMPORARY] TABLE [schema.]table (tbl_defs,...)
20 [ON COMMIT {DELETE|PRESERVE} ROWS]
21 [storage_options | CLUSTER cluster_name (col1, col2,... )
22 | ORGANIZATION {HEAP [storage_options]
23 | INDEX idx_organized_tbl_clause}]
24 [LOB_storage_clause][varray_clause][nested_storage_clause]
26 [[NO]CACHE] [[NO]MONITORING] [PARALLEL parallel_clause]
27 [ENABLE enable_clause | DISABLE disable_clause]
31 column datatype [DEFAULT expr] [column_constraint(s)]
39 STORAGE storage_clause
43 idx_organized_tbl_clause:
44 storage_option(s) [PCTTHRESHOLD int]
45 [COMPRESS int|NOCOMPRESS]
46 [ [INCLUDING column_name] OVERFLOW [storage_option(s)] ]
48 nested_storage_clause:
49 NESTED TABLE nested_item STORE AS storage_table
50 [RETURN AS {LOCATOR|VALUE} ]
53 Partition_clause {ENABLE|DISABLE} ROW MOVEMENT
56 (http://www.ss64.com/ora/clause_constraint_col.html)
58 CONSTRAINT constrnt_name {UNIQUE|PRIMARY KEY} constrnt_state
60 CONSTRAINT constrnt_name CHECK(condition) constrnt_state
62 CONSTRAINT constrnt_name [NOT] NULL constrnt_state
64 CONSTRAINT constrnt_name REFERENCES [schema.]table[(column)]
65 [ON DELETE {CASCADE|SET NULL}] constrnt_state
68 [[NOT] DEFERRABLE] [INITIALLY {IMMEDIATE|DEFERRED}]
69 [RELY | NORELY] [USING INDEX using_index_clause]
70 [ENABLE|DISABLE] [VALIDATE|NOVALIDATE]
71 [EXCEPTIONS INTO [schema.]table]
73 Note that probably not all of the above syntax is supported, but the grammar
74 was altered to better handle the syntax created by DDL::Oracle.
81 our $VERSION = '1.59';
84 $DEBUG = 0 unless defined $DEBUG;
87 use SQL::Translator::Utils qw/ddl_parser_instance/;
89 use base qw(Exporter);
90 our @EXPORT_OK = qw(parse);
92 our $GRAMMAR = <<'END_OF_GRAMMAR';
94 { my ( %tables, %indices, %constraints, $table_order, @table_comments, %views, $view_order, %procedures, $proc_order ) }
97 # The "eofile" rule makes the parser fail if any "statement" rule
98 # fails. Otherwise, the first successful match by a "statement"
99 # won't cause the failure needed to know that the parse, as a whole,
102 startrule : statement(s) eofile
106 indices => \%indices,
107 constraints => \%constraints,
109 procedures => \%procedures,
126 alter: /alter/i TABLE table_name /add/i table_constraint ';'
128 my $constraint = $item{table_constraint};
129 $constraint->{type} = $constraint->{constraint_type};
130 push @{$tables{$item{table_name}}{constraints}}, $constraint;
133 alter : /alter/i WORD /[^;]+/ ';'
134 { @table_comments = () }
136 drop : /drop/i WORD(s) NAME WORD(s?) ';'
137 { @table_comments = () }
139 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
141 my $table_name = $item{'table_name'};
142 $tables{ $table_name }{'order'} = ++$table_order;
143 $tables{ $table_name }{'table_name'} = $table_name;
145 if ( @table_comments ) {
146 $tables{ $table_name }{'comments'} = [ @table_comments ];
147 @table_comments = ();
152 for my $definition ( @{ $item[4] } ) {
153 if ( $definition->{'type'} eq 'field' ) {
154 my $field_name = $definition->{'name'};
155 $tables{ $table_name }{'fields'}{ $field_name } =
156 { %$definition, order => $i };
159 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
160 $constraint->{'fields'} = [ $field_name ];
161 push @{ $tables{ $table_name }{'constraints'} },
165 elsif ( $definition->{'type'} eq 'constraint' ) {
166 $definition->{'type'} = $definition->{'constraint_type'};
167 push @{ $tables{ $table_name }{'constraints'} }, $definition;
170 push @{ $tables{ $table_name }{'indices'} }, $definition;
174 for my $option ( @{ $item[6] } ) {
175 push @{ $tables{ $table_name }{'table_options'} }, $option;
181 create : create_index index_name /on/i table_name index_expr table_option(?) ';'
183 my $table_name = $item[4];
185 push @{ $constraints{ $table_name } }, {
192 push @{ $indices{ $table_name } }, {
200 index_expr: parens_name_list
202 | '(' WORD parens_name_list ')'
204 my $arg_list = join(",", @{$item[3]});
205 $return = "$item[2]($arg_list)";
208 create : /create/i /or replace/i /procedure/i table_name not_end m#^/$#im
210 @table_comments = ();
211 my $proc_name = $item[4];
212 # Hack to strip owner from procedure name
213 $proc_name =~ s#.*\.##;
215 my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5]";
217 $procedures{ $proc_name }{'order'} = ++$proc_order;
218 $procedures{ $proc_name }{'name'} = $proc_name;
219 $procedures{ $proc_name }{'owner'} = $owner;
220 $procedures{ $proc_name }{'sql'} = $sql;
223 not_end: m#.*?(?=^/$)#ism
225 create : /create/i /or replace/i /force/i /view/i table_name not_delimiter ';'
227 @table_comments = ();
228 my $view_name = $item[5];
229 # Hack to strip owner from view name
230 $view_name =~ s#.*\.##;
231 my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5] $item[6] $item[7]";
233 $views{ $view_name }{'order'} = ++$view_order;
234 $views{ $view_name }{'name'} = $view_name;
235 $views{ $view_name }{'sql'} = $sql;
238 not_delimiter: /.*?(?=;)/is
240 # Create anything else (e.g., domain, function, etc.)
241 create : ...!create_table ...!create_index /create/i WORD /[^;]+/ ';'
242 { @table_comments = () }
244 create_index : /create/i UNIQUE(?) /index/i
245 { $return = @{$item[2]} }
247 index_name : NAME '.' NAME
252 global_temporary: /global/i /temporary/i
254 table_name : NAME '.' NAME
259 create_definition : table_constraint
263 table_comment : comment
265 my $comment = $item[1];
267 push @table_comments, $comment;
270 comment : /^\s*(?:#|-{2}).*\n/
272 my $comment = $item[1];
273 $comment =~ s/^\s*(#|-{2})\s*//;
274 $comment =~ s/\s*$//;
278 comment : /\/\*/ /[^\*]+/ /\*\//
280 my $comment = $item[2];
281 $comment =~ s/^\s*|\s*$//g;
285 remark : /^REM\s+.*\n/
287 run : /^(RUN|\/)\s+.*\n/
289 prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
291 prompt : /prompt\s+create\s+.*\n/i
293 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
295 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
298 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
300 my $table_name = $item[4]->{'table'};
301 my $field_name = $item[4]->{'field'};
302 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
303 $item{'comment_phrase'};
306 column_name : NAME '.' NAME
307 { $return = { table => $item[1], field => $item[3] } }
309 comment_phrase : /'.*?'/
316 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
318 my ( $is_pk, $default, @constraints );
320 for my $meta ( @{ $item[4] } ) {
321 if ( $meta->{'type'} eq 'default' ) {
325 elsif ( $meta->{'type'} eq 'not_null' ) {
329 elsif ( $meta->{'type'} eq 'primary_key' ) {
333 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
336 my @comments = ( @{ $item[1] }, @{ $item[5] } );
340 name => $item{'field_name'},
341 data_type => $item{'data_type'}{'type'},
342 size => $item{'data_type'}{'size'},
344 default => $default->{'value'},
345 is_primary_key => $is_pk,
346 constraints => [ @constraints ],
347 comments => [ @comments ],
354 data_type : ora_data_type data_size(?)
358 size => $item[2][0] || '',
362 data_size : '(' VALUE(s /,/) data_size_modifier(?) ')'
365 data_size_modifier: /byte/i
368 column_constraint : constraint_name(?) column_constraint_type constraint_state(s?)
370 my $desc = $item{'column_constraint_type'};
371 my $type = $desc->{'type'};
372 my $fields = $desc->{'fields'} || [];
373 my $expression = $desc->{'expression'} || '';
376 supertype => 'constraint',
377 name => $item{'constraint_name(?)'}[0] || '',
379 expression => $type eq 'check' ? $expression : '',
380 deferrable => $desc->{'deferrable'},
381 deferred => $desc->{'deferred'},
382 reference_table => $desc->{'reference_table'},
383 reference_fields => $desc->{'reference_fields'},
384 # match_type => $desc->{'match_type'},
385 # on_update => $desc->{'on_update'},
389 constraint_name : /constraint/i NAME { $item[2] }
391 column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } }
393 { $return = { type => 'unique' } }
395 { $return = { type => 'primary_key' } }
396 | /check/i check_expression
400 expression => $item[2],
403 | /references/i table_name parens_name_list(?) on_delete(?)
406 type => 'foreign_key',
407 reference_table => $item[2],
408 reference_fields => $item[3][0],
409 # match_type => $item[4][0],
410 on_delete => $item[5][0],
418 check_condition_text : /.+\s+in\s+\([^)]+\)/i
421 check_expression : LPAREN check_condition_text RPAREN
422 { $return = join( ' ', map { $_ || () }
423 $item[1], $item[2], $item[3], $item[4][0] )
426 constraint_state : deferrable { $return = { type => $item[1] } }
427 | deferred { $return = { type => $item[1] } }
428 | /(no)?rely/i { $return = { type => $item[1] } }
429 # | /using/i /index/i using_index_clause
430 # { $return = { type => 'using_index', index => $item[3] } }
431 | /(dis|en)able/i { $return = { type => $item[1] } }
432 | /(no)?validate/i { $return = { type => $item[1] } }
433 | /exceptions/i /into/i table_name
434 { $return = { type => 'exceptions_into', table => $item[3] } }
436 deferrable : /not/i /deferrable/i
437 { $return = 'not_deferrable' }
439 { $return = 'deferrable' }
441 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
444 /(n?varchar2|varchar)/i { $return = 'varchar2' }
446 /n?char/i { $return = 'character' }
448 /n?dec/i { $return = 'decimal' }
450 /number/i { $return = 'number' }
452 /integer/i { $return = 'integer' }
454 /(pls_integer|binary_integer)/i { $return = 'integer' }
456 /interval\s+day/i { $return = 'interval day' }
458 /interval\s+year/i { $return = 'interval year' }
460 /long\s+raw/i { $return = 'long raw' }
462 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile|float|double)/i { $item[1] }
464 parens_value_list : '(' VALUE(s /,/) ')'
467 parens_word_list : '(' WORD(s /,/) ')'
470 parens_name_list : '(' NAME(s /,/) ')'
473 field_meta : default_val
476 default_val : /default/i VALUE
480 supertype => 'constraint',
488 supertype => 'constraint',
494 create_table : /create/i global_temporary(?) /table/i
496 table_option : /organization/i WORD
498 $return = { 'ORGANIZATION' => $item[2] }
501 table_option : /nomonitoring/i
503 $return = { 'NOMONITORING' => undef }
506 table_option : /parallel/i '(' key_value(s) ')'
508 $return = { 'PARALLEL' => $item[3] }
511 key_value : WORD VALUE
513 $return = { $item[1], $item[2] }
516 table_option : /[^;]+/
518 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) constraint_state(s?) comment(s?)
520 my $desc = $item{'table_constraint_type'};
521 my $type = $desc->{'type'};
522 my $fields = $desc->{'fields'};
523 my $expression = $desc->{'expression'};
524 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
527 name => $item{'constraint_name(?)'}[0] || '',
528 type => 'constraint',
529 constraint_type => $type,
530 fields => $type ne 'check' ? $fields : [],
531 expression => $type eq 'check' ? $expression : '',
532 deferrable => $item{'deferrable(?)'},
533 deferred => $item{'deferred(?)'},
534 reference_table => $desc->{'reference_table'},
535 reference_fields => $desc->{'reference_fields'},
536 # match_type => $desc->{'match_type'}[0],
537 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
538 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
539 comments => [ @comments ],
543 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
546 type => 'primary_key',
551 /unique/i '(' NAME(s /,/) ')'
559 /check/i check_expression /^(en|dis)able/i
563 expression => join(' ', $item[2], $item[3]),
567 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_name_list(?) on_delete(?)
570 type => 'foreign_key',
572 reference_table => $item[6],
573 reference_fields => $item[7][0],
574 # match_type => $item[8][0],
575 on_delete => $item[8][0],
576 # on_update => $item[9][0],
580 on_delete : /on delete/i WORD(s)
581 { join(' ', @{$item[2]}) }
583 UNIQUE : /unique/i { $return = 1 }
587 NAME : /\w+/ { $item[1] }
592 DQSTRING : '"' /((?:[^"]|"")+)/ '"'
593 { ($return = $item[2]) =~ s/""/"/g; }
595 SQSTRING : "'" /((?:[^']|'')*)/ "'"
596 { ($return = $item[2]) =~ s/''/'/g }
598 VALUE : /[-+]?\d*\.?\d+(?:[eE]\d+)?/
606 my ( $translator, $data ) = @_;
608 # Enable warnings within the Parse::RecDescent module.
609 local $::RD_ERRORS = 1 unless defined $::RD_ERRORS; # Make sure the parser dies when it encounters an error
610 local $::RD_WARN = 1 unless defined $::RD_WARN; # Enable warnings. This will warn on unused rules &c.
611 local $::RD_HINT = 1 unless defined $::RD_HINT; # Give out hints to help fix problems.
613 local $::RD_TRACE = $translator->trace ? 1 : undef;
614 local $DEBUG = $translator->debug;
616 my $parser = ddl_parser_instance('Oracle');
618 my $result = $parser->startrule( $data );
619 die "Parse failed.\n" unless defined $result;
621 warn "Parser results =\n", Dumper($result), "\n";
624 my $schema = $translator->schema;
625 my $indices = $result->{'indices'};
626 my $constraints = $result->{'constraints'};
628 $result->{'tables'}{ $a }{'order'}
630 $result->{'tables'}{ $b }{'order'}
631 } keys %{ $result->{'tables'} };
633 for my $table_name ( @tables ) {
634 my $tdata = $result->{'tables'}{ $table_name };
635 next unless $tdata->{'table_name'};
636 my $table = $schema->add_table(
637 name => $tdata->{'table_name'},
638 comments => $tdata->{'comments'},
639 ) or die $schema->error;
641 $table->options( $tdata->{'table_options'} );
644 $tdata->{'fields'}->{$a}->{'order'}
646 $tdata->{'fields'}->{$b}->{'order'}
647 } keys %{ $tdata->{'fields'} };
649 for my $fname ( @fields ) {
650 my $fdata = $tdata->{'fields'}{ $fname };
651 my $field = $table->add_field(
652 name => $fdata->{'name'},
653 data_type => $fdata->{'data_type'},
654 size => $fdata->{'size'},
655 default_value => $fdata->{'default'},
656 is_auto_increment => $fdata->{'is_auto_inc'},
657 is_nullable => $fdata->{'null'},
658 comments => $fdata->{'comments'},
659 ) or die $table->error;
662 push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
663 push @{ $tdata->{'constraints'} },
664 @{ $constraints->{ $table_name } || [] };
666 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
667 my $index = $table->add_index(
668 name => $idata->{'name'},
669 type => uc $idata->{'type'},
670 fields => $idata->{'fields'},
671 ) or die $table->error;
674 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
675 my $constraint = $table->add_constraint(
676 name => $cdata->{'name'},
677 type => $cdata->{'type'},
678 fields => $cdata->{'fields'},
679 expression => $cdata->{'expression'},
680 reference_table => $cdata->{'reference_table'},
681 reference_fields => $cdata->{'reference_fields'},
682 match_type => $cdata->{'match_type'} || '',
683 on_delete => $cdata->{'on_delete'}
684 || $cdata->{'on_delete_do'},
685 on_update => $cdata->{'on_update'}
686 || $cdata->{'on_update_do'},
687 ) or die $table->error;
691 my @procedures = sort {
692 $result->{procedures}->{ $a }->{'order'} <=> $result->{procedures}->{ $b }->{'order'}
693 } keys %{ $result->{procedures} };
694 foreach my $proc_name (@procedures) {
695 $schema->add_procedure(
697 owner => $result->{procedures}->{$proc_name}->{owner},
698 sql => $result->{procedures}->{$proc_name}->{sql},
703 $result->{views}->{ $a }->{'order'} <=> $result->{views}->{ $b }->{'order'}
704 } keys %{ $result->{views} };
705 foreach my $view_name (keys %{ $result->{views} }) {
708 sql => $result->{views}->{$view_name}->{sql},
717 # -------------------------------------------------------------------
718 # Something there is that doesn't love a wall.
720 # -------------------------------------------------------------------
726 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
730 SQL::Translator, Parse::RecDescent, DDL::Oracle.