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.
79 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
81 $DEBUG = 0 unless defined $DEBUG;
84 use Parse::RecDescent;
86 use base qw(Exporter);
88 @EXPORT_OK = qw(parse);
90 # Enable warnings within the Parse::RecDescent module.
91 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
92 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
93 $::RD_HINT = 1; # Give out hints to help fix problems.
97 { my ( %tables, %indices, %constraints, $table_order, @table_comments, %views, $view_order, %procedures, $proc_order ) }
100 # The "eofile" rule makes the parser fail if any "statement" rule
101 # fails. Otherwise, the first successful match by a "statement"
102 # won't cause the failure needed to know that the parse, as a whole,
105 startrule : statement(s) eofile
109 indices => \%indices,
110 constraints => \%constraints,
112 procedures => \%procedures,
129 alter : /alter/i WORD /[^;]+/ ';'
130 { @table_comments = () }
132 drop : /drop/i TABLE ';'
134 drop : /drop/i WORD(s) ';'
135 { @table_comments = () }
137 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
139 my $table_name = $item{'table_name'};
140 $tables{ $table_name }{'order'} = ++$table_order;
141 $tables{ $table_name }{'table_name'} = $table_name;
143 if ( @table_comments ) {
144 $tables{ $table_name }{'comments'} = [ @table_comments ];
145 @table_comments = ();
150 for my $definition ( @{ $item[4] } ) {
151 if ( $definition->{'type'} eq 'field' ) {
152 my $field_name = $definition->{'name'};
153 $tables{ $table_name }{'fields'}{ $field_name } =
154 { %$definition, order => $i };
157 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
158 $constraint->{'fields'} = [ $field_name ];
159 push @{ $tables{ $table_name }{'constraints'} },
163 elsif ( $definition->{'type'} eq 'constraint' ) {
164 $definition->{'type'} = $definition->{'constraint_type'};
165 push @{ $tables{ $table_name }{'constraints'} }, $definition;
168 push @{ $tables{ $table_name }{'indices'} }, $definition;
172 for my $option ( @{ $item[6] } ) {
173 push @{ $tables{ $table_name }{'table_options'} }, $option;
179 create : create_index index_name /on/i table_name index_expr table_option(?) ';'
181 my $table_name = $item[4];
183 push @{ $constraints{ $table_name } }, {
190 push @{ $indices{ $table_name } }, {
198 index_expr: parens_word_list
200 | '(' WORD parens_word_list ')'
202 my $arg_list = join(",", @{$item[3]});
203 $return = "$item[2]($arg_list)";
206 create : /create/i /or replace/i /procedure/i table_name not_end m#^/$#im
208 @table_comments = ();
209 my $proc_name = $item[4];
210 # Hack to strip owner from procedure name
211 $proc_name =~ s#.*\.##;
213 my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5]";
215 $procedures{ $proc_name }{'order'} = ++$proc_order;
216 $procedures{ $proc_name }{'name'} = $proc_name;
217 $procedures{ $proc_name }{'owner'} = $owner;
218 $procedures{ $proc_name }{'sql'} = $sql;
221 not_end: m#.*?(?=^/$)#ism
223 create : /create/i /or replace/i /force/i /view/i table_name not_delimiter ';'
225 @table_comments = ();
226 my $view_name = $item[5];
227 # Hack to strip owner from view name
228 $view_name =~ s#.*\.##;
229 my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5] $item[6] $item[7]";
231 $views{ $view_name }{'order'} = ++$view_order;
232 $views{ $view_name }{'name'} = $view_name;
233 $views{ $view_name }{'sql'} = $sql;
236 not_delimiter: /.*?(?=;)/is
238 # Create anything else (e.g., domain, function, etc.)
239 create : ...!create_table ...!create_index /create/i WORD /[^;]+/ ';'
240 { @table_comments = () }
242 create_index : /create/i UNIQUE(?) /index/i
243 { $return = @{$item[2]} }
245 index_name : NAME '.' NAME
250 global_temporary: /global/i /temporary/i
252 table_name : NAME '.' NAME
257 create_definition : table_constraint
261 table_comment : comment
263 my $comment = $item[1];
265 push @table_comments, $comment;
268 comment : /^\s*(?:#|-{2}).*\n/
270 my $comment = $item[1];
271 $comment =~ s/^\s*(#|-{2})\s*//;
272 $comment =~ s/\s*$//;
276 comment : /\/\*/ /[^\*]+/ /\*\//
278 my $comment = $item[2];
279 $comment =~ s/^\s*|\s*$//g;
283 remark : /^REM\s+.*\n/
285 run : /^(RUN|\/)\s+.*\n/
287 prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
289 prompt : /prompt\s+create\s+.*\n/i
291 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
293 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
296 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
298 my $table_name = $item[4]->{'table'};
299 my $field_name = $item[4]->{'field'};
300 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
301 $item{'comment_phrase'};
304 column_name : NAME '.' NAME
305 { $return = { table => $item[1], field => $item[3] } }
307 comment_phrase : /'.*?'/
314 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
316 my ( $is_pk, $default, @constraints );
318 for my $meta ( @{ $item[4] } ) {
319 if ( $meta->{'type'} eq 'default' ) {
323 elsif ( $meta->{'type'} eq 'not_null' ) {
327 elsif ( $meta->{'type'} eq 'primary_key' ) {
331 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
334 my @comments = ( @{ $item[1] }, @{ $item[5] } );
338 name => $item{'field_name'},
339 data_type => $item{'data_type'}{'type'},
340 size => $item{'data_type'}{'size'},
342 default => $default->{'value'},
343 is_primary_key => $is_pk,
344 constraints => [ @constraints ],
345 comments => [ @comments ],
352 data_type : ora_data_type data_size(?)
356 size => $item[2][0] || '',
360 data_size : '(' VALUE(s /,/) data_size_modifier(?) ')'
363 data_size_modifier: /byte/i
366 column_constraint : constraint_name(?) column_constraint_type constraint_state(s?)
368 my $desc = $item{'column_constraint_type'};
369 my $type = $desc->{'type'};
370 my $fields = $desc->{'fields'} || [];
371 my $expression = $desc->{'expression'} || '';
374 supertype => 'constraint',
375 name => $item{'constraint_name(?)'}[0] || '',
377 expression => $type eq 'check' ? $expression : '',
378 deferrable => $desc->{'deferrable'},
379 deferred => $desc->{'deferred'},
380 reference_table => $desc->{'reference_table'},
381 reference_fields => $desc->{'reference_fields'},
382 # match_type => $desc->{'match_type'},
383 # on_update => $desc->{'on_update'},
387 constraint_name : /constraint/i NAME { $item[2] }
389 column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } }
391 { $return = { type => 'unique' } }
393 { $return = { type => 'primary_key' } }
394 | /check/i check_expression
398 expression => $item[2],
401 | /references/i table_name parens_word_list(?) on_delete(?)
404 type => 'foreign_key',
405 reference_table => $item[2],
406 reference_fields => $item[3][0],
407 # match_type => $item[4][0],
408 on_delete => $item[5][0],
416 check_condition_text : /.+\s+in\s+\([^)]+\)/i
419 check_expression : LPAREN check_condition_text RPAREN
420 { $return = join( ' ', map { $_ || () }
421 $item[1], $item[2], $item[3], $item[4][0] )
424 constraint_state : deferrable { $return = { type => $item[1] } }
425 | deferred { $return = { type => $item[1] } }
426 | /(no)?rely/i { $return = { type => $item[1] } }
427 # | /using/i /index/i using_index_clause
428 # { $return = { type => 'using_index', index => $item[3] } }
429 | /(dis|en)able/i { $return = { type => $item[1] } }
430 | /(no)?validate/i { $return = { type => $item[1] } }
431 | /exceptions/i /into/i table_name
432 { $return = { type => 'exceptions_into', table => $item[3] } }
434 deferrable : /not/i /deferrable/i
435 { $return = 'not_deferrable' }
437 { $return = 'deferrable' }
439 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
442 /(n?varchar2|varchar)/i { $return = 'varchar2' }
444 /n?char/i { $return = 'character' }
446 /n?dec/i { $return = 'decimal' }
448 /number/i { $return = 'number' }
450 /integer/i { $return = 'integer' }
452 /(pls_integer|binary_integer)/i { $return = 'integer' }
454 /interval\s+day/i { $return = 'interval day' }
456 /interval\s+year/i { $return = 'interval year' }
458 /long\s+raw/i { $return = 'long raw' }
460 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile|float|double)/i { $item[1] }
462 parens_value_list : '(' VALUE(s /,/) ')'
465 parens_word_list : '(' WORD(s /,/) ')'
468 field_meta : default_val
471 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
474 $val =~ s/'//g if defined $val;
476 supertype => 'constraint',
484 supertype => 'constraint',
490 create_table : /create/i global_temporary(?) /table/i
492 table_option : /organization/i WORD
494 $return = { 'ORGANIZATION' => $item[2] }
497 table_option : /nomonitoring/i
499 $return = { 'NOMONITORING' => undef }
502 table_option : /parallel/i '(' key_value(s) ')'
504 $return = { 'PARALLEL' => $item[3] }
507 key_value : WORD VALUE
509 $return = { $item[1], $item[2] }
512 table_option : /[^;]+/
514 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) constraint_state(s?) comment(s?)
516 my $desc = $item{'table_constraint_type'};
517 my $type = $desc->{'type'};
518 my $fields = $desc->{'fields'};
519 my $expression = $desc->{'expression'};
520 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
523 name => $item{'constraint_name(?)'}[0] || '',
524 type => 'constraint',
525 constraint_type => $type,
526 fields => $type ne 'check' ? $fields : [],
527 expression => $type eq 'check' ? $expression : '',
528 deferrable => $item{'deferrable(?)'},
529 deferred => $item{'deferred(?)'},
530 reference_table => $desc->{'reference_table'},
531 reference_fields => $desc->{'reference_fields'},
532 # match_type => $desc->{'match_type'}[0],
533 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
534 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
535 comments => [ @comments ],
539 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
542 type => 'primary_key',
547 /unique/i '(' NAME(s /,/) ')'
555 /check/i check_expression /^(en|dis)able/i
559 expression => join(' ', $item[2], $item[3]),
563 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete(?)
566 type => 'foreign_key',
568 reference_table => $item[6],
569 reference_fields => $item[7][0],
570 # match_type => $item[8][0],
571 on_delete => $item[8][0],
572 # on_update => $item[9][0],
576 on_delete : /on delete/i WORD(s)
577 { join(' ', @{$item[2]}) }
579 UNIQUE : /unique/i { $return = 1 }
583 NAME : /\w+/ { $item[1] }
587 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
589 | /'.*?'/ # XXX doesn't handle embedded quotes
597 my ( $translator, $data ) = @_;
598 my $parser = Parse::RecDescent->new($GRAMMAR);
600 local $::RD_TRACE = $translator->trace ? 1 : undef;
601 local $DEBUG = $translator->debug;
603 unless (defined $parser) {
604 return $translator->error("Error instantiating Parse::RecDescent ".
605 "instance: Bad grammer");
608 my $result = $parser->startrule( $data );
609 die "Parse failed.\n" unless defined $result;
611 warn "Parser results =\n", Dumper($result), "\n";
614 my $schema = $translator->schema;
615 my $indices = $result->{'indices'};
616 my $constraints = $result->{'constraints'};
618 $result->{'tables'}{ $a }{'order'}
620 $result->{'tables'}{ $b }{'order'}
621 } keys %{ $result->{'tables'} };
623 for my $table_name ( @tables ) {
624 my $tdata = $result->{'tables'}{ $table_name };
625 next unless $tdata->{'table_name'};
626 my $table = $schema->add_table(
627 name => $tdata->{'table_name'},
628 comments => $tdata->{'comments'},
629 ) or die $schema->error;
631 $table->options( $tdata->{'table_options'} );
634 $tdata->{'fields'}->{$a}->{'order'}
636 $tdata->{'fields'}->{$b}->{'order'}
637 } keys %{ $tdata->{'fields'} };
639 for my $fname ( @fields ) {
640 my $fdata = $tdata->{'fields'}{ $fname };
641 my $field = $table->add_field(
642 name => $fdata->{'name'},
643 data_type => $fdata->{'data_type'},
644 size => $fdata->{'size'},
645 default_value => $fdata->{'default'},
646 is_auto_increment => $fdata->{'is_auto_inc'},
647 is_nullable => $fdata->{'null'},
648 comments => $fdata->{'comments'},
649 ) or die $table->error;
652 push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
653 push @{ $tdata->{'constraints'} },
654 @{ $constraints->{ $table_name } || [] };
656 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
657 my $index = $table->add_index(
658 name => $idata->{'name'},
659 type => uc $idata->{'type'},
660 fields => $idata->{'fields'},
661 ) or die $table->error;
664 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
665 my $constraint = $table->add_constraint(
666 name => $cdata->{'name'},
667 type => $cdata->{'type'},
668 fields => $cdata->{'fields'},
669 expression => $cdata->{'expression'},
670 reference_table => $cdata->{'reference_table'},
671 reference_fields => $cdata->{'reference_fields'},
672 match_type => $cdata->{'match_type'} || '',
673 on_delete => $cdata->{'on_delete'}
674 || $cdata->{'on_delete_do'},
675 on_update => $cdata->{'on_update'}
676 || $cdata->{'on_update_do'},
677 ) or die $table->error;
681 my @procedures = sort {
682 $result->{procedures}->{ $a }->{'order'} <=> $result->{procedures}->{ $b }->{'order'}
683 } keys %{ $result->{procedures} };
684 foreach my $proc_name (@procedures) {
685 $schema->add_procedure(
687 owner => $result->{procedures}->{$proc_name}->{owner},
688 sql => $result->{procedures}->{$proc_name}->{sql},
693 $result->{views}->{ $a }->{'order'} <=> $result->{views}->{ $b }->{'order'}
694 } keys %{ $result->{views} };
695 foreach my $view_name (keys %{ $result->{views} }) {
698 sql => $result->{views}->{$view_name}->{sql},
707 # -------------------------------------------------------------------
708 # Something there is that doesn't love a wall.
710 # -------------------------------------------------------------------
716 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
720 SQL::Translator, Parse::RecDescent, DDL::Oracle.