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 WORD /[^;]+/ ';'
127 { @table_comments = () }
129 drop : /drop/i TABLE ';'
131 drop : /drop/i WORD(s) ';'
132 { @table_comments = () }
134 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
136 my $table_name = $item{'table_name'};
137 $tables{ $table_name }{'order'} = ++$table_order;
138 $tables{ $table_name }{'table_name'} = $table_name;
140 if ( @table_comments ) {
141 $tables{ $table_name }{'comments'} = [ @table_comments ];
142 @table_comments = ();
147 for my $definition ( @{ $item[4] } ) {
148 if ( $definition->{'type'} eq 'field' ) {
149 my $field_name = $definition->{'name'};
150 $tables{ $table_name }{'fields'}{ $field_name } =
151 { %$definition, order => $i };
154 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
155 $constraint->{'fields'} = [ $field_name ];
156 push @{ $tables{ $table_name }{'constraints'} },
160 elsif ( $definition->{'type'} eq 'constraint' ) {
161 $definition->{'type'} = $definition->{'constraint_type'};
162 push @{ $tables{ $table_name }{'constraints'} }, $definition;
165 push @{ $tables{ $table_name }{'indices'} }, $definition;
169 for my $option ( @{ $item[6] } ) {
170 push @{ $tables{ $table_name }{'table_options'} }, $option;
176 create : create_index index_name /on/i table_name index_expr table_option(?) ';'
178 my $table_name = $item[4];
180 push @{ $constraints{ $table_name } }, {
187 push @{ $indices{ $table_name } }, {
195 index_expr: parens_word_list
197 | '(' WORD parens_word_list ')'
199 my $arg_list = join(",", @{$item[3]});
200 $return = "$item[2]($arg_list)";
203 create : /create/i /or replace/i /procedure/i table_name not_end m#^/$#im
205 @table_comments = ();
206 my $proc_name = $item[4];
207 # Hack to strip owner from procedure name
208 $proc_name =~ s#.*\.##;
210 my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5]";
212 $procedures{ $proc_name }{'order'} = ++$proc_order;
213 $procedures{ $proc_name }{'name'} = $proc_name;
214 $procedures{ $proc_name }{'owner'} = $owner;
215 $procedures{ $proc_name }{'sql'} = $sql;
218 not_end: m#.*?(?=^/$)#ism
220 create : /create/i /or replace/i /force/i /view/i table_name not_delimiter ';'
222 @table_comments = ();
223 my $view_name = $item[5];
224 # Hack to strip owner from view name
225 $view_name =~ s#.*\.##;
226 my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5] $item[6] $item[7]";
228 $views{ $view_name }{'order'} = ++$view_order;
229 $views{ $view_name }{'name'} = $view_name;
230 $views{ $view_name }{'sql'} = $sql;
233 not_delimiter: /.*?(?=;)/is
235 # Create anything else (e.g., domain, function, etc.)
236 create : ...!create_table ...!create_index /create/i WORD /[^;]+/ ';'
237 { @table_comments = () }
239 create_index : /create/i UNIQUE(?) /index/i
240 { $return = @{$item[2]} }
242 index_name : NAME '.' NAME
247 global_temporary: /global/i /temporary/i
249 table_name : NAME '.' NAME
254 create_definition : table_constraint
258 table_comment : comment
260 my $comment = $item[1];
262 push @table_comments, $comment;
265 comment : /^\s*(?:#|-{2}).*\n/
267 my $comment = $item[1];
268 $comment =~ s/^\s*(#|-{2})\s*//;
269 $comment =~ s/\s*$//;
273 comment : /\/\*/ /[^\*]+/ /\*\//
275 my $comment = $item[2];
276 $comment =~ s/^\s*|\s*$//g;
280 remark : /^REM\s+.*\n/
282 run : /^(RUN|\/)\s+.*\n/
284 prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
286 prompt : /prompt\s+create\s+.*\n/i
288 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
290 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
293 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
295 my $table_name = $item[4]->{'table'};
296 my $field_name = $item[4]->{'field'};
297 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
298 $item{'comment_phrase'};
301 column_name : NAME '.' NAME
302 { $return = { table => $item[1], field => $item[3] } }
304 comment_phrase : /'.*?'/
311 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
313 my ( $is_pk, $default, @constraints );
315 for my $meta ( @{ $item[4] } ) {
316 if ( $meta->{'type'} eq 'default' ) {
320 elsif ( $meta->{'type'} eq 'not_null' ) {
324 elsif ( $meta->{'type'} eq 'primary_key' ) {
328 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
331 my @comments = ( @{ $item[1] }, @{ $item[5] } );
335 name => $item{'field_name'},
336 data_type => $item{'data_type'}{'type'},
337 size => $item{'data_type'}{'size'},
339 default => $default->{'value'},
340 is_primary_key => $is_pk,
341 constraints => [ @constraints ],
342 comments => [ @comments ],
349 data_type : ora_data_type data_size(?)
353 size => $item[2][0] || '',
357 data_size : '(' VALUE(s /,/) data_size_modifier(?) ')'
360 data_size_modifier: /byte/i
363 column_constraint : constraint_name(?) column_constraint_type constraint_state(s?)
365 my $desc = $item{'column_constraint_type'};
366 my $type = $desc->{'type'};
367 my $fields = $desc->{'fields'} || [];
368 my $expression = $desc->{'expression'} || '';
371 supertype => 'constraint',
372 name => $item{'constraint_name(?)'}[0] || '',
374 expression => $type eq 'check' ? $expression : '',
375 deferrable => $desc->{'deferrable'},
376 deferred => $desc->{'deferred'},
377 reference_table => $desc->{'reference_table'},
378 reference_fields => $desc->{'reference_fields'},
379 # match_type => $desc->{'match_type'},
380 # on_update => $desc->{'on_update'},
384 constraint_name : /constraint/i NAME { $item[2] }
386 column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } }
388 { $return = { type => 'unique' } }
390 { $return = { type => 'primary_key' } }
391 | /check/i check_expression
395 expression => $item[2],
398 | /references/i table_name parens_word_list(?) on_delete(?)
401 type => 'foreign_key',
402 reference_table => $item[2],
403 reference_fields => $item[3][0],
404 # match_type => $item[4][0],
405 on_delete => $item[5][0],
413 check_condition_text : /.+\s+in\s+\([^)]+\)/i
416 check_expression : LPAREN check_condition_text RPAREN
417 { $return = join( ' ', map { $_ || () }
418 $item[1], $item[2], $item[3], $item[4][0] )
421 constraint_state : deferrable { $return = { type => $item[1] } }
422 | deferred { $return = { type => $item[1] } }
423 | /(no)?rely/i { $return = { type => $item[1] } }
424 # | /using/i /index/i using_index_clause
425 # { $return = { type => 'using_index', index => $item[3] } }
426 | /(dis|en)able/i { $return = { type => $item[1] } }
427 | /(no)?validate/i { $return = { type => $item[1] } }
428 | /exceptions/i /into/i table_name
429 { $return = { type => 'exceptions_into', table => $item[3] } }
431 deferrable : /not/i /deferrable/i
432 { $return = 'not_deferrable' }
434 { $return = 'deferrable' }
436 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
439 /(n?varchar2|varchar)/i { $return = 'varchar2' }
441 /n?char/i { $return = 'character' }
443 /n?dec/i { $return = 'decimal' }
445 /number/i { $return = 'number' }
447 /integer/i { $return = 'integer' }
449 /(pls_integer|binary_integer)/i { $return = 'integer' }
451 /interval\s+day/i { $return = 'interval day' }
453 /interval\s+year/i { $return = 'interval year' }
455 /long\s+raw/i { $return = 'long raw' }
457 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile|float|double)/i { $item[1] }
459 parens_value_list : '(' VALUE(s /,/) ')'
462 parens_word_list : '(' WORD(s /,/) ')'
465 field_meta : default_val
468 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
471 $val =~ s/'//g if defined $val;
473 supertype => 'constraint',
481 supertype => 'constraint',
487 create_table : /create/i global_temporary(?) /table/i
489 table_option : /organization/i WORD
491 $return = { 'ORGANIZATION' => $item[2] }
494 table_option : /nomonitoring/i
496 $return = { 'NOMONITORING' => undef }
499 table_option : /parallel/i '(' key_value(s) ')'
501 $return = { 'PARALLEL' => $item[3] }
504 key_value : WORD VALUE
506 $return = { $item[1], $item[2] }
509 table_option : /[^;]+/
511 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) constraint_state(s?) comment(s?)
513 my $desc = $item{'table_constraint_type'};
514 my $type = $desc->{'type'};
515 my $fields = $desc->{'fields'};
516 my $expression = $desc->{'expression'};
517 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
520 name => $item{'constraint_name(?)'}[0] || '',
521 type => 'constraint',
522 constraint_type => $type,
523 fields => $type ne 'check' ? $fields : [],
524 expression => $type eq 'check' ? $expression : '',
525 deferrable => $item{'deferrable(?)'},
526 deferred => $item{'deferred(?)'},
527 reference_table => $desc->{'reference_table'},
528 reference_fields => $desc->{'reference_fields'},
529 # match_type => $desc->{'match_type'}[0],
530 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
531 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
532 comments => [ @comments ],
536 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
539 type => 'primary_key',
544 /unique/i '(' NAME(s /,/) ')'
552 /check/i check_expression /^(en|dis)able/i
556 expression => join(' ', $item[2], $item[3]),
560 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete(?)
563 type => 'foreign_key',
565 reference_table => $item[6],
566 reference_fields => $item[7][0],
567 # match_type => $item[8][0],
568 on_delete => $item[8][0],
569 # on_update => $item[9][0],
573 on_delete : /on delete/i WORD(s)
574 { join(' ', @{$item[2]}) }
576 UNIQUE : /unique/i { $return = 1 }
580 NAME : /\w+/ { $item[1] }
584 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
586 | /'.*?'/ # XXX doesn't handle embedded quotes
594 my ( $translator, $data ) = @_;
596 # Enable warnings within the Parse::RecDescent module.
597 local $::RD_ERRORS = 1 unless defined $::RD_ERRORS; # Make sure the parser dies when it encounters an error
598 local $::RD_WARN = 1 unless defined $::RD_WARN; # Enable warnings. This will warn on unused rules &c.
599 local $::RD_HINT = 1 unless defined $::RD_HINT; # Give out hints to help fix problems.
601 local $::RD_TRACE = $translator->trace ? 1 : undef;
602 local $DEBUG = $translator->debug;
604 my $parser = ddl_parser_instance('Oracle');
606 my $result = $parser->startrule( $data );
607 die "Parse failed.\n" unless defined $result;
609 warn "Parser results =\n", Dumper($result), "\n";
612 my $schema = $translator->schema;
613 my $indices = $result->{'indices'};
614 my $constraints = $result->{'constraints'};
616 $result->{'tables'}{ $a }{'order'}
618 $result->{'tables'}{ $b }{'order'}
619 } keys %{ $result->{'tables'} };
621 for my $table_name ( @tables ) {
622 my $tdata = $result->{'tables'}{ $table_name };
623 next unless $tdata->{'table_name'};
624 my $table = $schema->add_table(
625 name => $tdata->{'table_name'},
626 comments => $tdata->{'comments'},
627 ) or die $schema->error;
629 $table->options( $tdata->{'table_options'} );
632 $tdata->{'fields'}->{$a}->{'order'}
634 $tdata->{'fields'}->{$b}->{'order'}
635 } keys %{ $tdata->{'fields'} };
637 for my $fname ( @fields ) {
638 my $fdata = $tdata->{'fields'}{ $fname };
639 my $field = $table->add_field(
640 name => $fdata->{'name'},
641 data_type => $fdata->{'data_type'},
642 size => $fdata->{'size'},
643 default_value => $fdata->{'default'},
644 is_auto_increment => $fdata->{'is_auto_inc'},
645 is_nullable => $fdata->{'null'},
646 comments => $fdata->{'comments'},
647 ) or die $table->error;
650 push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
651 push @{ $tdata->{'constraints'} },
652 @{ $constraints->{ $table_name } || [] };
654 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
655 my $index = $table->add_index(
656 name => $idata->{'name'},
657 type => uc $idata->{'type'},
658 fields => $idata->{'fields'},
659 ) or die $table->error;
662 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
663 my $constraint = $table->add_constraint(
664 name => $cdata->{'name'},
665 type => $cdata->{'type'},
666 fields => $cdata->{'fields'},
667 expression => $cdata->{'expression'},
668 reference_table => $cdata->{'reference_table'},
669 reference_fields => $cdata->{'reference_fields'},
670 match_type => $cdata->{'match_type'} || '',
671 on_delete => $cdata->{'on_delete'}
672 || $cdata->{'on_delete_do'},
673 on_update => $cdata->{'on_update'}
674 || $cdata->{'on_update_do'},
675 ) or die $table->error;
679 my @procedures = sort {
680 $result->{procedures}->{ $a }->{'order'} <=> $result->{procedures}->{ $b }->{'order'}
681 } keys %{ $result->{procedures} };
682 foreach my $proc_name (@procedures) {
683 $schema->add_procedure(
685 owner => $result->{procedures}->{$proc_name}->{owner},
686 sql => $result->{procedures}->{$proc_name}->{sql},
691 $result->{views}->{ $a }->{'order'} <=> $result->{views}->{ $b }->{'order'}
692 } keys %{ $result->{views} };
693 foreach my $view_name (keys %{ $result->{views} }) {
696 sql => $result->{views}->{$view_name}->{sql},
705 # -------------------------------------------------------------------
706 # Something there is that doesn't love a wall.
708 # -------------------------------------------------------------------
714 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
718 SQL::Translator, Parse::RecDescent, DDL::Oracle.