1 package SQL::Translator::Parser::Oracle;
3 # -------------------------------------------------------------------
4 # $Id: Oracle.pm,v 1.21 2005-08-10 15:17:48 duality72 Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::Oracle - parser for Oracle
30 use SQL::Translator::Parser::Oracle;
32 my $translator = SQL::Translator->new;
33 $translator->parser("SQL::Translator::Parser::Oracle");
37 From http://www.ss64.com/ora/table_c.html:
39 CREATE [GLOBAL TEMPORARY] TABLE [schema.]table (tbl_defs,...)
40 [ON COMMIT {DELETE|PRESERVE} ROWS]
41 [storage_options | CLUSTER cluster_name (col1, col2,... )
42 | ORGANIZATION {HEAP [storage_options]
43 | INDEX idx_organized_tbl_clause}]
44 [LOB_storage_clause][varray_clause][nested_storage_clause]
46 [[NO]CACHE] [[NO]MONITORING] [PARALLEL parallel_clause]
47 [ENABLE enable_clause | DISABLE disable_clause]
51 column datatype [DEFAULT expr] [column_constraint(s)]
59 STORAGE storage_clause
63 idx_organized_tbl_clause:
64 storage_option(s) [PCTTHRESHOLD int]
65 [COMPRESS int|NOCOMPRESS]
66 [ [INCLUDING column_name] OVERFLOW [storage_option(s)] ]
68 nested_storage_clause:
69 NESTED TABLE nested_item STORE AS storage_table
70 [RETURN AS {LOCATOR|VALUE} ]
73 Partition_clause {ENABLE|DISABLE} ROW MOVEMENT
76 (http://www.ss64.com/ora/clause_constraint_col.html)
78 CONSTRAINT constrnt_name {UNIQUE|PRIMARY KEY} constrnt_state
80 CONSTRAINT constrnt_name CHECK(condition) constrnt_state
82 CONSTRAINT constrnt_name [NOT] NULL constrnt_state
84 CONSTRAINT constrnt_name REFERENCES [schema.]table[(column)]
85 [ON DELETE {CASCADE|SET NULL}] constrnt_state
88 [[NOT] DEFERRABLE] [INITIALLY {IMMEDIATE|DEFERRED}]
89 [RELY | NORELY] [USING INDEX using_index_clause]
90 [ENABLE|DISABLE] [VALIDATE|NOVALIDATE]
91 [EXCEPTIONS INTO [schema.]table]
93 Note that probably not all of the above syntax is supported, but the grammar
94 was altered to better handle the syntax created by DDL::Oracle.
99 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
100 $VERSION = sprintf "%d.%02d", q$Revision: 1.21 $ =~ /(\d+)\.(\d+)/;
101 $DEBUG = 0 unless defined $DEBUG;
104 use Parse::RecDescent;
106 use base qw(Exporter);
108 @EXPORT_OK = qw(parse);
110 # Enable warnings within the Parse::RecDescent module.
111 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
112 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
113 $::RD_HINT = 1; # Give out hints to help fix problems.
119 { my ( %tables, %indices, %constraints, $table_order, @table_comments ) }
122 # The "eofile" rule makes the parser fail if any "statement" rule
123 # fails. Otherwise, the first successful match by a "statement"
124 # won't cause the failure needed to know that the parse, as a whole,
127 startrule : statement(s) eofile
131 indices => \%indices,
132 constraints => \%constraints,
149 alter : /alter/i WORD /[^;]+/ ';'
150 { @table_comments = () }
152 drop : /drop/i TABLE ';'
154 drop : /drop/i WORD(s) ';'
155 { @table_comments = () }
157 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
159 my $table_name = $item{'table_name'};
160 $tables{ $table_name }{'order'} = ++$table_order;
161 $tables{ $table_name }{'table_name'} = $table_name;
163 if ( @table_comments ) {
164 $tables{ $table_name }{'comments'} = [ @table_comments ];
165 @table_comments = ();
170 for my $definition ( @{ $item[4] } ) {
171 if ( $definition->{'type'} eq 'field' ) {
172 my $field_name = $definition->{'name'};
173 $tables{ $table_name }{'fields'}{ $field_name } =
174 { %$definition, order => $i };
177 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
178 $constraint->{'fields'} = [ $field_name ];
179 push @{ $tables{ $table_name }{'constraints'} },
183 elsif ( $definition->{'type'} eq 'constraint' ) {
184 $definition->{'type'} = $definition->{'constraint_type'};
185 push @{ $tables{ $table_name }{'constraints'} }, $definition;
188 push @{ $tables{ $table_name }{'indices'} }, $definition;
192 for my $option ( @{ $item[6] } ) {
193 push @{ $tables{ $table_name }{'table_options'} }, $option;
199 create : create_index index_name /on/i table_name parens_word_list table_option(?) ';'
201 my $table_name = $item[4];
203 push @{ $constraints{ $table_name } }, {
210 push @{ $indices{ $table_name } }, {
218 # Create anything else (e.g., domain, function, etc.)
219 create : ...!create_table ...!create_index /create/i WORD /[^;]+/ ';'
220 { @table_comments = () }
222 create_index : /create/i UNIQUE(?) /index/i
223 { $return = $item[2] }
225 index_name : NAME '.' NAME
230 global_temporary: /global/i /temporary/i
232 table_name : NAME '.' NAME
237 create_definition : field
241 table_comment : comment
243 my $comment = $item[1];
245 push @table_comments, $comment;
248 comment : /^\s*(?:#|-{2}).*\n/
250 my $comment = $item[1];
251 $comment =~ s/^\s*(#|-{2})\s*//;
252 $comment =~ s/\s*$//;
256 comment : /\/\*/ /[^\*]+/ /\*\//
258 my $comment = $item[2];
259 $comment =~ s/^\s*|\s*$//g;
263 remark : /^REM\s+.*\n/
265 run : /^(RUN|\/)\s+.*\n/
267 prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
269 prompt : /prompt\s+create\s+.*\n/i
271 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
273 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
276 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
278 my $table_name = $item[4]->{'table'};
279 my $field_name = $item[4]->{'field'};
280 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
281 $item{'comment_phrase'};
284 column_name : NAME '.' NAME
285 { $return = { table => $item[1], field => $item[3] } }
287 comment_phrase : /'.*?'/
294 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
296 my ( $is_pk, $default, @constraints );
298 for my $meta ( @{ $item[4] } ) {
299 if ( $meta->{'type'} eq 'default' ) {
303 elsif ( $meta->{'type'} eq 'not_null' ) {
307 elsif ( $meta->{'type'} eq 'primary_key' ) {
311 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
314 my @comments = ( @{ $item[1] }, @{ $item[5] } );
318 name => $item{'field_name'},
319 data_type => $item{'data_type'}{'type'},
320 size => $item{'data_type'}{'size'},
322 default => $default->{'value'},
323 is_primary_key => $is_pk,
324 constraints => [ @constraints ],
325 comments => [ @comments ],
332 data_type : ora_data_type parens_value_list(?)
336 size => $item[2][0] || '',
340 column_constraint : constraint_name(?) column_constraint_type constraint_state(s?)
342 my $desc = $item{'column_constraint_type'};
343 my $type = $desc->{'type'};
344 my $fields = $desc->{'fields'} || [];
345 my $expression = $desc->{'expression'} || '';
348 supertype => 'constraint',
349 name => $item{'constraint_name(?)'}[0] || '',
351 expression => $type eq 'check' ? $expression : '',
352 deferrable => $item{'deferrable'},
353 deferred => $item{'deferred'},
354 reference_table => $desc->{'reference_table'},
355 reference_fields => $desc->{'reference_fields'},
356 # match_type => $desc->{'match_type'},
357 # on_update => $desc->{'on_update'},
361 constraint_name : /constraint/i NAME { $item[2] }
363 column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } }
365 { $return = { type => 'null' } }
367 { $return = { type => 'unique' } }
369 { $return = { type => 'primary_key' } }
370 | /check/i '(' /[^)]+/ ')'
371 { $return = { type => 'check', expression => $item[3] } }
372 | /references/i table_name parens_word_list(?) on_delete(?)
375 type => 'foreign_key',
376 reference_table => $item[2],
377 reference_fields => $item[3][0],
378 # match_type => $item[4][0],
379 on_delete => $item[5][0],
383 constraint_state : deferrable { $return = { type => $item[1] } }
384 | deferred { $return = { type => $item[1] } }
385 | /(no)?rely/i { $return = { type => $item[1] } }
386 # | /using/i /index/i using_index_clause
387 # { $return = { type => 'using_index', index => $item[3] } }
388 | /(dis|en)able/i { $return = { type => $item[1] } }
389 | /(no)?validate/i { $return = { type => $item[1] } }
390 | /exceptions/i /into/i table_name
391 { $return = { type => 'exceptions_into', table => $item[3] } }
393 deferrable : /not/i /deferrable/i
394 { $return = 'not_deferrable' }
396 { $return = 'deferrable' }
398 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
401 /(n?varchar2|varchar)/i { $return = 'varchar2' }
403 /n?char/i { $return = 'character' }
405 /n?dec/i { $return = 'decimal' }
407 /number/i { $return = 'number' }
409 /integer/i { $return = 'integer' }
411 /(pls_integer|binary_integer)/i { $return = 'integer' }
413 /interval\s+day/i { $return = 'interval day' }
415 /interval\s+year/i { $return = 'interval year' }
417 /long\s+raw/i { $return = 'long raw' }
419 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile|float)/i { $item[1] }
421 parens_value_list : '(' VALUE(s /,/) ')'
424 parens_word_list : '(' WORD(s /,/) ')'
427 field_meta : default_val
430 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
433 $val =~ s/'//g if defined $val;
435 supertype => 'constraint',
441 create_table : /create/i global_temporary(?) /table/i
443 table_option : /organization/i WORD
445 $return = { 'ORGANIZATION' => $item[2] }
448 table_option : /nomonitoring/i
450 $return = { 'NOMONITORING' => undef }
453 table_option : /parallel/i '(' key_value(s) ')'
455 $return = { 'PARALLEL' => $item[3] }
458 key_value : WORD VALUE
460 $return = { $item[1], $item[2] }
463 table_option : /[^;]+/
465 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) constraint_state(s?) comment(s?)
467 my $desc = $item{'table_constraint_type'};
468 my $type = $desc->{'type'};
469 my $fields = $desc->{'fields'};
470 my $expression = $desc->{'expression'};
471 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
474 name => $item{'constraint_name(?)'}[0] || '',
475 type => 'constraint',
476 constraint_type => $type,
477 fields => $type ne 'check' ? $fields : [],
478 expression => $type eq 'check' ? $expression : '',
479 deferrable => $item{'deferrable(?)'},
480 deferred => $item{'deferred(?)'},
481 reference_table => $desc->{'reference_table'},
482 reference_fields => $desc->{'reference_fields'},
483 # match_type => $desc->{'match_type'}[0],
484 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
485 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
486 comments => [ @comments ],
490 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
493 type => 'primary_key',
498 /unique/i '(' NAME(s /,/) ')'
506 /check/ '(' /(.+)/ ')'
510 expression => $item[3],
514 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete(?)
517 type => 'foreign_key',
519 reference_table => $item[6],
520 reference_fields => $item[7][0],
521 match_type => $item[8][0],
522 on_delete => $item[9][0],
523 on_update => $item[10][0],
527 on_delete : /on delete/i WORD(s)
530 UNIQUE : /unique/i { $return = 1 }
534 NAME : /\w+/ { $item[1] }
538 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
540 | /'.*?'/ # XXX doesn't handle embedded quotes
547 # -------------------------------------------------------------------
549 my ( $translator, $data ) = @_;
550 $parser ||= Parse::RecDescent->new($GRAMMAR);
552 local $::RD_TRACE = $translator->trace ? 1 : undef;
553 local $DEBUG = $translator->debug;
555 unless (defined $parser) {
556 return $translator->error("Error instantiating Parse::RecDescent ".
557 "instance: Bad grammer");
560 my $result = $parser->startrule( $data );
561 die "Parse failed.\n" unless defined $result;
563 warn "Parser results =\n", Dumper($result), "\n";
566 my $schema = $translator->schema;
567 my $indices = $result->{'indices'};
568 my $constraints = $result->{'constraints'};
570 $result->{'tables'}{ $a }{'order'}
572 $result->{'tables'}{ $b }{'order'}
573 } keys %{ $result->{'tables'} };
575 for my $table_name ( @tables ) {
576 my $tdata = $result->{'tables'}{ $table_name };
577 next unless $tdata->{'table_name'};
578 my $table = $schema->add_table(
579 name => $tdata->{'table_name'},
580 comments => $tdata->{'comments'},
581 ) or die $schema->error;
583 $table->options( $tdata->{'table_options'} );
586 $tdata->{'fields'}->{$a}->{'order'}
588 $tdata->{'fields'}->{$b}->{'order'}
589 } keys %{ $tdata->{'fields'} };
591 for my $fname ( @fields ) {
592 my $fdata = $tdata->{'fields'}{ $fname };
593 my $field = $table->add_field(
594 name => $fdata->{'name'},
595 data_type => $fdata->{'data_type'},
596 size => $fdata->{'size'},
597 default_value => $fdata->{'default'},
598 is_auto_increment => $fdata->{'is_auto_inc'},
599 is_nullable => $fdata->{'null'},
600 comments => $fdata->{'comments'},
601 ) or die $table->error;
604 push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
605 push @{ $tdata->{'constraints'} },
606 @{ $constraints->{ $table_name } || [] };
608 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
609 my $index = $table->add_index(
610 name => $idata->{'name'},
611 type => uc $idata->{'type'},
612 fields => $idata->{'fields'},
613 ) or die $table->error;
616 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
617 my $constraint = $table->add_constraint(
618 name => $cdata->{'name'},
619 type => $cdata->{'type'},
620 fields => $cdata->{'fields'},
621 reference_table => $cdata->{'reference_table'},
622 reference_fields => $cdata->{'reference_fields'},
623 match_type => $cdata->{'match_type'} || '',
624 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
625 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
626 ) or die $table->error;
635 # -------------------------------------------------------------------
636 # Something there is that doesn't love a wall.
638 # -------------------------------------------------------------------
644 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
648 SQL::Translator, Parse::RecDescent, DDL::Oracle.