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, %triggers, $trigger_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,
110 triggers => \%triggers,
127 alter: /alter/i TABLE table_name /add/i table_constraint ';'
129 my $constraint = $item{table_constraint};
130 $constraint->{type} = $constraint->{constraint_type};
131 push @{$tables{$item{table_name}}{constraints}}, $constraint;
134 alter : /alter/i WORD /[^;]+/ ';'
135 { @table_comments = () }
137 drop : /drop/i WORD(s) NAME WORD(s?) ';'
138 { @table_comments = () }
140 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
142 my $table_name = $item{'table_name'};
143 $tables{ $table_name }{'order'} = ++$table_order;
144 $tables{ $table_name }{'table_name'} = $table_name;
146 if ( @table_comments ) {
147 $tables{ $table_name }{'comments'} = [ @table_comments ];
148 @table_comments = ();
153 for my $definition ( @{ $item[4] } ) {
154 if ( $definition->{'type'} eq 'field' ) {
155 my $field_name = $definition->{'name'};
156 $tables{ $table_name }{'fields'}{ $field_name } =
157 { %$definition, order => $i };
160 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
161 $constraint->{'fields'} = [ $field_name ];
162 push @{ $tables{ $table_name }{'constraints'} },
166 elsif ( $definition->{'type'} eq 'constraint' ) {
167 $definition->{'type'} = $definition->{'constraint_type'};
168 push @{ $tables{ $table_name }{'constraints'} }, $definition;
171 push @{ $tables{ $table_name }{'indices'} }, $definition;
175 for my $option ( @{ $item[6] } ) {
176 push @{ $tables{ $table_name }{'table_options'} }, $option;
182 create : create_index index_name /on/i table_name index_expr table_option(?) ';'
184 my $table_name = $item[4];
186 push @{ $constraints{ $table_name } }, {
193 push @{ $indices{ $table_name } }, {
201 index_expr: parens_name_list
203 | '(' WORD parens_name_list ')'
205 my $arg_list = join(",", @{$item[3]});
206 $return = "$item[2]($arg_list)";
209 create : /create/i /or replace/i /trigger/i table_name not_end m#^/$#im
211 @table_comments = ();
212 my $trigger_name = $item[4];
213 # Hack to strip owner from trigger name
214 $trigger_name =~ s#.*\.##;
216 my $action = "$item[1] $item[2] $item[3] $item[4] $item[5]";
218 $triggers{ $trigger_name }{'order'} = ++$trigger_order;
219 $triggers{ $trigger_name }{'name'} = $trigger_name;
220 $triggers{ $trigger_name }{'owner'} = $owner;
221 $triggers{ $trigger_name }{'action'} = $action;
224 create : /create/i /or replace/i /procedure/i table_name not_end m#^/$#im
226 @table_comments = ();
227 my $proc_name = $item[4];
228 # Hack to strip owner from procedure name
229 $proc_name =~ s#.*\.##;
231 my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5]";
233 $procedures{ $proc_name }{'order'} = ++$proc_order;
234 $procedures{ $proc_name }{'name'} = $proc_name;
235 $procedures{ $proc_name }{'owner'} = $owner;
236 $procedures{ $proc_name }{'sql'} = $sql;
239 not_end: m#.*?(?=^/$)#ism
241 create : /create/i /or replace/i /force/i /view/i table_name not_delimiter ';'
243 @table_comments = ();
244 my $view_name = $item[5];
245 # Hack to strip owner from view name
246 $view_name =~ s#.*\.##;
247 my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5] $item[6] $item[7]";
249 $views{ $view_name }{'order'} = ++$view_order;
250 $views{ $view_name }{'name'} = $view_name;
251 $views{ $view_name }{'sql'} = $sql;
254 not_delimiter: /.*?(?=;)/is
256 # Create anything else (e.g., domain, function, etc.)
257 create : ...!create_table ...!create_index /create/i WORD /[^;]+/ ';'
258 { @table_comments = () }
260 create_index : /create/i UNIQUE(?) /index/i
261 { $return = @{$item[2]} }
263 index_name : NAME '.' NAME
268 global_temporary: /global/i /temporary/i
270 table_name : NAME '.' NAME
275 create_definition : table_constraint
279 table_comment : comment
281 my $comment = $item[1];
283 push @table_comments, $comment;
286 comment : /^\s*(?:#|-{2}).*\n/
288 my $comment = $item[1];
289 $comment =~ s/^\s*(#|-{2})\s*//;
290 $comment =~ s/\s*$//;
294 comment : /\/\*/ /[^\*]+/ /\*\//
296 my $comment = $item[2];
297 $comment =~ s/^\s*|\s*$//g;
301 remark : /^REM\s+.*\n/
303 run : /^(RUN|\/)\s+.*\n/
305 prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
307 prompt : /prompt\s+create\s+.*\n/i
309 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
311 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
314 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
316 my $table_name = $item[4]->{'table'};
317 my $field_name = $item[4]->{'field'};
318 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
319 $item{'comment_phrase'};
322 column_name : NAME '.' NAME
323 { $return = { table => $item[1], field => $item[3] } }
325 comment_phrase : /'.*?'/
332 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
334 my ( $is_pk, $default, @constraints );
336 for my $meta ( @{ $item[4] } ) {
337 if ( $meta->{'type'} eq 'default' ) {
341 elsif ( $meta->{'type'} eq 'not_null' ) {
345 elsif ( $meta->{'type'} eq 'primary_key' ) {
349 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
352 my @comments = ( @{ $item[1] }, @{ $item[5] } );
356 name => $item{'field_name'},
357 data_type => $item{'data_type'}{'type'},
358 size => $item{'data_type'}{'size'},
360 default => $default->{'value'},
361 is_primary_key => $is_pk,
362 constraints => [ @constraints ],
363 comments => [ @comments ],
370 data_type : ora_data_type data_size(?)
374 size => $item[2][0] || '',
378 data_size : '(' VALUE(s /,/) data_size_modifier(?) ')'
381 data_size_modifier: /byte/i
384 column_constraint : constraint_name(?) column_constraint_type constraint_state(s?)
386 my $desc = $item{'column_constraint_type'};
387 my $type = $desc->{'type'};
388 my $fields = $desc->{'fields'} || [];
389 my $expression = $desc->{'expression'} || '';
392 supertype => 'constraint',
393 name => $item{'constraint_name(?)'}[0] || '',
395 expression => $type eq 'check' ? $expression : '',
396 deferrable => $desc->{'deferrable'},
397 deferred => $desc->{'deferred'},
398 reference_table => $desc->{'reference_table'},
399 reference_fields => $desc->{'reference_fields'},
400 # match_type => $desc->{'match_type'},
401 # on_update => $desc->{'on_update'},
405 constraint_name : /constraint/i NAME { $item[2] }
407 column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } }
409 { $return = { type => 'unique' } }
411 { $return = { type => 'primary_key' } }
412 | /check/i check_expression
416 expression => $item[2],
419 | /references/i table_name parens_name_list(?) on_delete(?)
422 type => 'foreign_key',
423 reference_table => $item[2],
424 reference_fields => $item[3][0],
425 # match_type => $item[4][0],
426 on_delete => $item[5][0],
434 check_condition_text : /.+\s+in\s+\([^)]+\)/i
437 check_expression : LPAREN check_condition_text RPAREN
438 { $return = join( ' ', map { $_ || () }
439 $item[1], $item[2], $item[3], $item[4][0] )
442 constraint_state : deferrable { $return = { type => $item[1] } }
443 | deferred { $return = { type => $item[1] } }
444 | /(no)?rely/i { $return = { type => $item[1] } }
445 # | /using/i /index/i using_index_clause
446 # { $return = { type => 'using_index', index => $item[3] } }
447 | /(dis|en)able/i { $return = { type => $item[1] } }
448 | /(no)?validate/i { $return = { type => $item[1] } }
449 | /exceptions/i /into/i table_name
450 { $return = { type => 'exceptions_into', table => $item[3] } }
452 deferrable : /not/i /deferrable/i
453 { $return = 'not_deferrable' }
455 { $return = 'deferrable' }
457 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
460 /(n?varchar2|varchar)/i { $return = 'varchar2' }
462 /n?char/i { $return = 'character' }
464 /n?dec/i { $return = 'decimal' }
466 /number/i { $return = 'number' }
468 /integer/i { $return = 'integer' }
470 /(pls_integer|binary_integer)/i { $return = 'integer' }
472 /interval\s+day/i { $return = 'interval day' }
474 /interval\s+year/i { $return = 'interval year' }
476 /long\s+raw/i { $return = 'long raw' }
478 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile|float|double)/i { $item[1] }
480 parens_value_list : '(' VALUE(s /,/) ')'
483 parens_word_list : '(' WORD(s /,/) ')'
486 parens_name_list : '(' NAME(s /,/) ')'
489 field_meta : default_val
492 default_val : /default/i VALUE
496 supertype => 'constraint',
504 supertype => 'constraint',
510 create_table : /create/i global_temporary(?) /table/i
512 table_option : /organization/i WORD
514 $return = { 'ORGANIZATION' => $item[2] }
517 table_option : /nomonitoring/i
519 $return = { 'NOMONITORING' => undef }
522 table_option : /parallel/i '(' key_value(s) ')'
524 $return = { 'PARALLEL' => $item[3] }
527 key_value : WORD VALUE
529 $return = { $item[1], $item[2] }
532 table_option : /[^;]+/
534 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) constraint_state(s?) comment(s?)
536 my $desc = $item{'table_constraint_type'};
537 my $type = $desc->{'type'};
538 my $fields = $desc->{'fields'};
539 my $expression = $desc->{'expression'};
540 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
543 name => $item{'constraint_name(?)'}[0] || '',
544 type => 'constraint',
545 constraint_type => $type,
546 fields => $type ne 'check' ? $fields : [],
547 expression => $type eq 'check' ? $expression : '',
548 deferrable => $item{'deferrable(?)'},
549 deferred => $item{'deferred(?)'},
550 reference_table => $desc->{'reference_table'},
551 reference_fields => $desc->{'reference_fields'},
552 # match_type => $desc->{'match_type'}[0],
553 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
554 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
555 comments => [ @comments ],
559 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
562 type => 'primary_key',
567 /unique/i '(' NAME(s /,/) ')'
575 /check/i check_expression /^(en|dis)able/i
579 expression => join(' ', $item[2], $item[3]),
583 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_name_list(?) on_delete(?)
586 type => 'foreign_key',
588 reference_table => $item[6],
589 reference_fields => $item[7][0],
590 # match_type => $item[8][0],
591 on_delete => $item[8][0],
592 # on_update => $item[9][0],
596 on_delete : /on delete/i WORD(s)
597 { join(' ', @{$item[2]}) }
599 UNIQUE : /unique/i { $return = 1 }
603 NAME : /\w+/ { $item[1] }
608 DQSTRING : '"' /((?:[^"]|"")+)/ '"'
609 { ($return = $item[2]) =~ s/""/"/g; }
611 SQSTRING : "'" /((?:[^']|'')*)/ "'"
612 { ($return = $item[2]) =~ s/''/'/g }
614 VALUE : /[-+]?\d*\.?\d+(?:[eE]\d+)?/
622 my ( $translator, $data ) = @_;
624 # Enable warnings within the Parse::RecDescent module.
625 local $::RD_ERRORS = 1 unless defined $::RD_ERRORS; # Make sure the parser dies when it encounters an error
626 local $::RD_WARN = 1 unless defined $::RD_WARN; # Enable warnings. This will warn on unused rules &c.
627 local $::RD_HINT = 1 unless defined $::RD_HINT; # Give out hints to help fix problems.
629 local $::RD_TRACE = $translator->trace ? 1 : undef;
630 local $DEBUG = $translator->debug;
632 my $parser = ddl_parser_instance('Oracle');
634 my $result = $parser->startrule( $data );
635 die "Parse failed.\n" unless defined $result;
637 warn "Parser results =\n", Dumper($result), "\n";
640 my $schema = $translator->schema;
641 my $indices = $result->{'indices'};
642 my $constraints = $result->{'constraints'};
644 $result->{'tables'}{ $a }{'order'}
646 $result->{'tables'}{ $b }{'order'}
647 } keys %{ $result->{'tables'} };
649 for my $table_name ( @tables ) {
650 my $tdata = $result->{'tables'}{ $table_name };
651 next unless $tdata->{'table_name'};
652 my $table = $schema->add_table(
653 name => $tdata->{'table_name'},
654 comments => $tdata->{'comments'},
655 ) or die $schema->error;
657 $table->options( $tdata->{'table_options'} );
660 $tdata->{'fields'}->{$a}->{'order'}
662 $tdata->{'fields'}->{$b}->{'order'}
663 } keys %{ $tdata->{'fields'} };
665 for my $fname ( @fields ) {
666 my $fdata = $tdata->{'fields'}{ $fname };
667 my $field = $table->add_field(
668 name => $fdata->{'name'},
669 data_type => $fdata->{'data_type'},
670 size => $fdata->{'size'},
671 default_value => $fdata->{'default'},
672 is_auto_increment => $fdata->{'is_auto_inc'},
673 is_nullable => $fdata->{'null'},
674 comments => $fdata->{'comments'},
675 ) or die $table->error;
678 push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
679 push @{ $tdata->{'constraints'} },
680 @{ $constraints->{ $table_name } || [] };
682 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
683 my $index = $table->add_index(
684 name => $idata->{'name'},
685 type => uc $idata->{'type'},
686 fields => $idata->{'fields'},
687 ) or die $table->error;
690 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
691 my $constraint = $table->add_constraint(
692 name => $cdata->{'name'},
693 type => $cdata->{'type'},
694 fields => $cdata->{'fields'},
695 expression => $cdata->{'expression'},
696 reference_table => $cdata->{'reference_table'},
697 reference_fields => $cdata->{'reference_fields'},
698 match_type => $cdata->{'match_type'} || '',
699 on_delete => $cdata->{'on_delete'}
700 || $cdata->{'on_delete_do'},
701 on_update => $cdata->{'on_update'}
702 || $cdata->{'on_update_do'},
703 ) or die $table->error;
707 my @procedures = sort {
708 $result->{procedures}->{ $a }->{'order'} <=> $result->{procedures}->{ $b }->{'order'}
709 } keys %{ $result->{procedures} };
710 foreach my $proc_name (@procedures) {
711 $schema->add_procedure(
713 owner => $result->{procedures}->{$proc_name}->{owner},
714 sql => $result->{procedures}->{$proc_name}->{sql},
719 $result->{views}->{ $a }->{'order'} <=> $result->{views}->{ $b }->{'order'}
720 } keys %{ $result->{views} };
721 foreach my $view_name (keys %{ $result->{views} }) {
724 sql => $result->{views}->{$view_name}->{sql},
728 my @triggers = sort {
729 $result->{triggers}->{ $a }->{'order'} <=> $result->{triggers}->{ $b }->{'order'}
730 } keys %{ $result->{triggers} };
731 foreach my $trigger_name (@triggers) {
732 $schema->add_trigger(
733 name => $trigger_name,
734 action => $result->{triggers}->{$trigger_name}->{action},
743 # -------------------------------------------------------------------
744 # Something there is that doesn't love a wall.
746 # -------------------------------------------------------------------
752 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
756 SQL::Translator, Parse::RecDescent, DDL::Oracle.