1 package SQL::Translator::Parser::Oracle;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
23 SQL::Translator::Parser::Oracle - parser for Oracle
28 use SQL::Translator::Parser::Oracle;
30 my $translator = SQL::Translator->new;
31 $translator->parser("SQL::Translator::Parser::Oracle");
35 From http://www.ss64.com/ora/table_c.html:
37 CREATE [GLOBAL TEMPORARY] TABLE [schema.]table (tbl_defs,...)
38 [ON COMMIT {DELETE|PRESERVE} ROWS]
39 [storage_options | CLUSTER cluster_name (col1, col2,... )
40 | ORGANIZATION {HEAP [storage_options]
41 | INDEX idx_organized_tbl_clause}]
42 [LOB_storage_clause][varray_clause][nested_storage_clause]
44 [[NO]CACHE] [[NO]MONITORING] [PARALLEL parallel_clause]
45 [ENABLE enable_clause | DISABLE disable_clause]
49 column datatype [DEFAULT expr] [column_constraint(s)]
57 STORAGE storage_clause
61 idx_organized_tbl_clause:
62 storage_option(s) [PCTTHRESHOLD int]
63 [COMPRESS int|NOCOMPRESS]
64 [ [INCLUDING column_name] OVERFLOW [storage_option(s)] ]
66 nested_storage_clause:
67 NESTED TABLE nested_item STORE AS storage_table
68 [RETURN AS {LOCATOR|VALUE} ]
71 Partition_clause {ENABLE|DISABLE} ROW MOVEMENT
74 (http://www.ss64.com/ora/clause_constraint_col.html)
76 CONSTRAINT constrnt_name {UNIQUE|PRIMARY KEY} constrnt_state
78 CONSTRAINT constrnt_name CHECK(condition) constrnt_state
80 CONSTRAINT constrnt_name [NOT] NULL constrnt_state
82 CONSTRAINT constrnt_name REFERENCES [schema.]table[(column)]
83 [ON DELETE {CASCADE|SET NULL}] constrnt_state
86 [[NOT] DEFERRABLE] [INITIALLY {IMMEDIATE|DEFERRED}]
87 [RELY | NORELY] [USING INDEX using_index_clause]
88 [ENABLE|DISABLE] [VALIDATE|NOVALIDATE]
89 [EXCEPTIONS INTO [schema.]table]
91 Note that probably not all of the above syntax is supported, but the grammar
92 was altered to better handle the syntax created by DDL::Oracle.
97 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
99 $DEBUG = 0 unless defined $DEBUG;
102 use Parse::RecDescent;
104 use base qw(Exporter);
106 @EXPORT_OK = qw(parse);
108 # Enable warnings within the Parse::RecDescent module.
109 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
110 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
111 $::RD_HINT = 1; # Give out hints to help fix problems.
115 { my ( %tables, %indices, %constraints, $table_order, @table_comments, %views, $view_order, %procedures, $proc_order ) }
118 # The "eofile" rule makes the parser fail if any "statement" rule
119 # fails. Otherwise, the first successful match by a "statement"
120 # won't cause the failure needed to know that the parse, as a whole,
123 startrule : statement(s) eofile
127 indices => \%indices,
128 constraints => \%constraints,
130 procedures => \%procedures,
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 : /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 : field
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 => $item{'deferrable'},
397 deferred => $item{'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 '(' /[^)]+/ ')'
413 { $return = { type => 'check', expression => $item[3] } }
414 | /references/i table_name parens_word_list(?) on_delete(?)
417 type => 'foreign_key',
418 reference_table => $item[2],
419 reference_fields => $item[3][0],
420 # match_type => $item[4][0],
421 on_delete => $item[5][0],
425 constraint_state : deferrable { $return = { type => $item[1] } }
426 | deferred { $return = { type => $item[1] } }
427 | /(no)?rely/i { $return = { type => $item[1] } }
428 # | /using/i /index/i using_index_clause
429 # { $return = { type => 'using_index', index => $item[3] } }
430 | /(dis|en)able/i { $return = { type => $item[1] } }
431 | /(no)?validate/i { $return = { type => $item[1] } }
432 | /exceptions/i /into/i table_name
433 { $return = { type => 'exceptions_into', table => $item[3] } }
435 deferrable : /not/i /deferrable/i
436 { $return = 'not_deferrable' }
438 { $return = 'deferrable' }
440 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
443 /(n?varchar2|varchar)/i { $return = 'varchar2' }
445 /n?char/i { $return = 'character' }
447 /n?dec/i { $return = 'decimal' }
449 /number/i { $return = 'number' }
451 /integer/i { $return = 'integer' }
453 /(pls_integer|binary_integer)/i { $return = 'integer' }
455 /interval\s+day/i { $return = 'interval day' }
457 /interval\s+year/i { $return = 'interval year' }
459 /long\s+raw/i { $return = 'long raw' }
461 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile|float|double)/i { $item[1] }
463 parens_value_list : '(' VALUE(s /,/) ')'
466 parens_word_list : '(' WORD(s /,/) ')'
469 field_meta : default_val
472 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
475 $val =~ s/'//g if defined $val;
477 supertype => 'constraint',
485 supertype => 'constraint',
491 create_table : /create/i global_temporary(?) /table/i
493 table_option : /organization/i WORD
495 $return = { 'ORGANIZATION' => $item[2] }
498 table_option : /nomonitoring/i
500 $return = { 'NOMONITORING' => undef }
503 table_option : /parallel/i '(' key_value(s) ')'
505 $return = { 'PARALLEL' => $item[3] }
508 key_value : WORD VALUE
510 $return = { $item[1], $item[2] }
513 table_option : /[^;]+/
515 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) constraint_state(s?) comment(s?)
517 my $desc = $item{'table_constraint_type'};
518 my $type = $desc->{'type'};
519 my $fields = $desc->{'fields'};
520 my $expression = $desc->{'expression'};
521 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
524 name => $item{'constraint_name(?)'}[0] || '',
525 type => 'constraint',
526 constraint_type => $type,
527 fields => $type ne 'check' ? $fields : [],
528 expression => $type eq 'check' ? $expression : '',
529 deferrable => $item{'deferrable(?)'},
530 deferred => $item{'deferred(?)'},
531 reference_table => $desc->{'reference_table'},
532 reference_fields => $desc->{'reference_fields'},
533 # match_type => $desc->{'match_type'}[0],
534 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
535 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
536 comments => [ @comments ],
540 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
543 type => 'primary_key',
548 /unique/i '(' NAME(s /,/) ')'
556 /check/ '(' /(.+)/ ')'
560 expression => $item[3],
564 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete(?)
567 type => 'foreign_key',
569 reference_table => $item[6],
570 reference_fields => $item[7][0],
571 # match_type => $item[8][0],
572 on_delete => $item[8][0],
573 # on_update => $item[9][0],
577 on_delete : /on delete/i WORD(s)
578 { join(' ', @{$item[2]}) }
580 UNIQUE : /unique/i { $return = 1 }
584 NAME : /\w+/ { $item[1] }
588 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
590 | /'.*?'/ # XXX doesn't handle embedded quotes
597 # -------------------------------------------------------------------
599 my ( $translator, $data ) = @_;
600 my $parser = Parse::RecDescent->new($GRAMMAR);
602 local $::RD_TRACE = $translator->trace ? 1 : undef;
603 local $DEBUG = $translator->debug;
605 unless (defined $parser) {
606 return $translator->error("Error instantiating Parse::RecDescent ".
607 "instance: Bad grammer");
610 my $result = $parser->startrule( $data );
611 die "Parse failed.\n" unless defined $result;
613 warn "Parser results =\n", Dumper($result), "\n";
616 my $schema = $translator->schema;
617 my $indices = $result->{'indices'};
618 my $constraints = $result->{'constraints'};
620 $result->{'tables'}{ $a }{'order'}
622 $result->{'tables'}{ $b }{'order'}
623 } keys %{ $result->{'tables'} };
625 for my $table_name ( @tables ) {
626 my $tdata = $result->{'tables'}{ $table_name };
627 next unless $tdata->{'table_name'};
628 my $table = $schema->add_table(
629 name => $tdata->{'table_name'},
630 comments => $tdata->{'comments'},
631 ) or die $schema->error;
633 $table->options( $tdata->{'table_options'} );
636 $tdata->{'fields'}->{$a}->{'order'}
638 $tdata->{'fields'}->{$b}->{'order'}
639 } keys %{ $tdata->{'fields'} };
641 for my $fname ( @fields ) {
642 my $fdata = $tdata->{'fields'}{ $fname };
643 my $field = $table->add_field(
644 name => $fdata->{'name'},
645 data_type => $fdata->{'data_type'},
646 size => $fdata->{'size'},
647 default_value => $fdata->{'default'},
648 is_auto_increment => $fdata->{'is_auto_inc'},
649 is_nullable => $fdata->{'null'},
650 comments => $fdata->{'comments'},
651 ) or die $table->error;
654 push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
655 push @{ $tdata->{'constraints'} },
656 @{ $constraints->{ $table_name } || [] };
658 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
659 my $index = $table->add_index(
660 name => $idata->{'name'},
661 type => uc $idata->{'type'},
662 fields => $idata->{'fields'},
663 ) or die $table->error;
666 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
667 my $constraint = $table->add_constraint(
668 name => $cdata->{'name'},
669 type => $cdata->{'type'},
670 fields => $cdata->{'fields'},
671 reference_table => $cdata->{'reference_table'},
672 reference_fields => $cdata->{'reference_fields'},
673 match_type => $cdata->{'match_type'} || '',
674 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
675 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
676 ) or die $table->error;
680 my @procedures = sort {
681 $result->{procedures}->{ $a }->{'order'} <=> $result->{procedures}->{ $b }->{'order'}
682 } keys %{ $result->{procedures} };
683 foreach my $proc_name (@procedures) {
684 $schema->add_procedure(
686 owner => $result->{procedures}->{$proc_name}->{owner},
687 sql => $result->{procedures}->{$proc_name}->{sql},
692 $result->{views}->{ $a }->{'order'} <=> $result->{views}->{ $b }->{'order'}
693 } keys %{ $result->{views} };
694 foreach my $view_name (keys %{ $result->{views} }) {
697 sql => $result->{views}->{$view_name}->{sql},
706 # -------------------------------------------------------------------
707 # Something there is that doesn't love a wall.
709 # -------------------------------------------------------------------
715 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
719 SQL::Translator, Parse::RecDescent, DDL::Oracle.