1 package SQL::Translator::Parser::Oracle;
3 # -------------------------------------------------------------------
4 # $Id: Oracle.pm,v 1.18 2004-04-22 19:56:28 kycl4rk 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)]
60 STORAGE storage_clause
64 idx_organized_tbl_clause:
65 storage_option(s) [PCTTHRESHOLD int]
66 [COMPRESS int|NOCOMPRESS]
67 [ [INCLUDING column_name] OVERFLOW [storage_option(s)] ]
69 nested_storage_clause:
70 NESTED TABLE nested_item STORE AS storage_table
71 [RETURN AS {LOCATOR|VALUE} ]
74 Partition_clause {ENABLE|DISABLE} ROW MOVEMENT
77 (http://www.ss64.com/ora/clause_constraint_col.html)
79 CONSTRAINT constrnt_name {UNIQUE|PRIMARY KEY} constrnt_state
81 CONSTRAINT constrnt_name CHECK(condition) constrnt_state
83 CONSTRAINT constrnt_name [NOT] NULL constrnt_state
85 CONSTRAINT constrnt_name REFERENCES [schema.]table[(column)]
86 [ON DELETE {CASCADE|SET NULL}] constrnt_state
89 [[NOT] DEFERRABLE] [INITIALLY {IMMEDIATE|DEFERRED}]
90 [RELY | NORELY] [USING INDEX using_index_clause]
91 [ENABLE|DISABLE] [VALIDATE|NOVALIDATE]
92 [EXCEPTIONS INTO [schema.]table]
94 Note that probably not all of the above syntax is supported, but the grammar
95 was altered to better handle the syntax created by DDL::Oracle.
100 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
101 $VERSION = sprintf "%d.%02d", q$Revision: 1.18 $ =~ /(\d+)\.(\d+)/;
102 $DEBUG = 0 unless defined $DEBUG;
105 use Parse::RecDescent;
107 use base qw(Exporter);
109 @EXPORT_OK = qw(parse);
111 # Enable warnings within the Parse::RecDescent module.
112 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
113 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
114 $::RD_HINT = 1; # Give out hints to help fix problems.
120 { my ( %tables, %indices, %constraints, $table_order, @table_comments ) }
123 # The "eofile" rule makes the parser fail if any "statement" rule
124 # fails. Otherwise, the first successful match by a "statement"
125 # won't cause the failure needed to know that the parse, as a whole,
128 startrule : statement(s) eofile
132 indices => \%indices,
133 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/i UNIQUE(?) /index/i WORD /on/i table_name parens_word_list table_option(?) ';'
201 my $table_name = $item[6];
203 push @{ $constraints{ $table_name } }, {
206 fields => $item[7][0],
210 push @{ $indices{ $table_name } }, {
213 fields => $item[7][0],
218 # Create anything else (e.g., domain, function, etc.)
219 create : /create/i WORD /[^;]+/ ';'
220 { @table_comments = () }
222 global_temporary: /global/i /temporary/i
224 table_name : NAME '.' NAME
229 create_definition : field
233 table_comment : comment
235 my $comment = $item[1];
237 push @table_comments, $comment;
240 comment : /^\s*(?:#|-{2}).*\n/
242 my $comment = $item[1];
243 $comment =~ s/^\s*(#|-{2})\s*//;
244 $comment =~ s/\s*$//;
248 comment : /\/\*/ /[^\*]+/ /\*\//
250 my $comment = $item[2];
251 $comment =~ s/^\s*|\s*$//g;
255 remark : /^REM\s+.*\n/
257 prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
259 prompt : /prompt\s+create\s+.*\n/i
261 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
263 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
266 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
268 my $table_name = $item[4]->{'table'};
269 my $field_name = $item[4]->{'field'};
270 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
271 $item{'comment_phrase'};
274 column_name : NAME '.' NAME
275 { $return = { table => $item[1], field => $item[3] } }
277 comment_phrase : /'.*?'/
284 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
286 my ( $is_pk, $default, @constraints );
288 for my $meta ( @{ $item[4] } ) {
289 if ( $meta->{'type'} eq 'default' ) {
293 elsif ( $meta->{'type'} eq 'not_null' ) {
297 elsif ( $meta->{'type'} eq 'primary_key' ) {
301 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
304 my @comments = ( @{ $item[1] }, @{ $item[5] } );
308 name => $item{'field_name'},
309 data_type => $item{'data_type'}{'type'},
310 size => $item{'data_type'}{'size'},
312 default => $default->{'value'},
313 is_primary_key => $is_pk,
314 constraints => [ @constraints ],
315 comments => [ @comments ],
322 data_type : ora_data_type parens_value_list(?)
326 size => $item[2][0] || '',
330 column_constraint : constraint_name(?) column_constraint_type
332 my $desc = $item{'column_constraint_type'};
333 my $type = $desc->{'type'};
334 my $fields = $desc->{'fields'} || [];
335 my $expression = $desc->{'expression'} || '';
338 supertype => 'constraint',
339 name => $item{'constraint_name(?)'}[0] || '',
341 expression => $type eq 'check' ? $expression : '',
342 deferrable => $item{'deferrable'},
343 deferred => $item{'deferred'},
344 reference_table => $desc->{'reference_table'},
345 reference_fields => $desc->{'reference_fields'},
346 # match_type => $desc->{'match_type'},
347 # on_update_do => $desc->{'on_update_do'},
351 constraint_name : /constraint/i NAME { $item[2] }
353 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
355 { $return = { type => 'null' } }
357 { $return = { type => 'unique' } }
359 { $return = { type => 'primary_key' } }
360 | /check/i '(' /[^)]+/ ')'
361 { $return = { type => 'check', expression => $item[2] } }
362 | /references/i table_name parens_word_list(?) on_delete_do(?)
365 type => 'foreign_key',
366 reference_table => $item[2],
367 reference_fields => $item[3][0],
368 # match_type => $item[4][0],
369 on_delete_do => $item[5][0],
373 #constraint_state : deferrable { $return = { type => $item[1] } }
374 # | deferred { $return = { type => $item[1] } }
375 # | /(no)?rely/ { $return = { type => $item[1] } }
376 # | /using/i /index/i using_index_clause
377 # { $return = { type => 'using_index', index => $item[3] }
378 # | (dis)?enable { $return = { type => $item[1] } }
379 # | (no)?validate { $return = { type => $item[1] } }
380 # | /exceptions/i /into/i table_name
381 # { $return = { type => 'exceptions_into', table => $item[3] } }
383 deferrable : /not/i /deferrable/i
384 { $return = 'not_deferrable' }
386 { $return = 'deferrable' }
388 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
391 /(n?varchar2|varchar)/i { $return = 'varchar2' }
393 /n?char/i { $return = 'character' }
395 /n?dec/i { $return = 'decimal' }
397 /number/i { $return = 'number' }
399 /integer/i { $return = 'integer' }
401 /(pls_integer|binary_integer)/i { $return = 'integer' }
403 /interval\s+day/i { $return = 'interval_day' }
405 /interval\s+year/i { $return = 'interval_year' }
407 /long\s+raw/i { $return = 'long_raw' }
409 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile)/i { $item[1] }
411 parens_value_list : '(' VALUE(s /,/) ')'
414 parens_word_list : '(' WORD(s /,/) ')'
417 field_meta : default_val
420 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
423 $val =~ s/'//g if defined $val;
425 supertype => 'constraint',
431 create_table : /create/i global_temporary(?) /table/i
433 table_option : /organization/i WORD
435 $return = { 'ORGANIZATION' => $item[2] }
438 table_option : /nomonitoring/i
440 $return = { 'NOMONITORING' => undef }
443 table_option : /parallel/i '(' key_value(s) ')'
445 $return = { 'PARALLEL' => $item[3] }
448 key_value : WORD VALUE
450 $return = { $item[1], $item[2] }
453 table_option : /[^;]+/
455 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
457 my $desc = $item{'table_constraint_type'};
458 my $type = $desc->{'type'};
459 my $fields = $desc->{'fields'};
460 my $expression = $desc->{'expression'};
461 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
464 name => $item{'constraint_name(?)'}[0] || '',
465 type => 'constraint',
466 constraint_type => $type,
467 fields => $type ne 'check' ? $fields : [],
468 expression => $type eq 'check' ? $expression : '',
469 deferrable => $item{'deferrable(?)'},
470 deferred => $item{'deferred(?)'},
471 reference_table => $desc->{'reference_table'},
472 reference_fields => $desc->{'reference_fields'},
473 # match_type => $desc->{'match_type'}[0],
474 on_delete_do => $desc->{'on_delete_do'},
475 on_update_do => $desc->{'on_update_do'},
476 comments => [ @comments ],
480 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
483 type => 'primary_key',
488 /unique/i '(' NAME(s /,/) ')'
496 /check/ '(' /(.+)/ ')'
500 expression => $item[3],
504 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete_do(?)
507 type => 'foreign_key',
509 reference_table => $item[6],
510 reference_fields => $item[7][0],
511 match_type => $item[8][0],
512 on_delete_do => $item[9][0],
513 on_update_do => $item[10][0],
517 on_delete_do : /on delete/i WORD(s)
520 UNIQUE : /unique/i { $return = 1 }
524 NAME : /\w+/ { $item[1] }
528 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
530 | /'.*?'/ # XXX doesn't handle embedded quotes
537 # -------------------------------------------------------------------
539 my ( $translator, $data ) = @_;
540 $parser ||= Parse::RecDescent->new($GRAMMAR);
542 local $::RD_TRACE = $translator->trace ? 1 : undef;
543 local $DEBUG = $translator->debug;
545 unless (defined $parser) {
546 return $translator->error("Error instantiating Parse::RecDescent ".
547 "instance: Bad grammer");
550 my $result = $parser->startrule( $data );
551 die "Parse failed.\n" unless defined $result;
552 warn Dumper($result) if $DEBUG;
554 my $schema = $translator->schema;
555 my $indices = $result->{'indices'};
556 my $constraints = $result->{'constraints'};
558 $result->{'tables'}{ $a }{'order'}
560 $result->{'tables'}{ $b }{'order'}
561 } keys %{ $result->{'tables'} };
563 for my $table_name ( @tables ) {
564 my $tdata = $result->{'tables'}{ $table_name };
565 next unless $tdata->{'table_name'};
566 my $table = $schema->add_table(
567 name => $tdata->{'table_name'},
568 comments => $tdata->{'comments'},
569 ) or die $schema->error;
571 $table->options( $tdata->{'table_options'} );
574 $tdata->{'fields'}->{$a}->{'order'}
576 $tdata->{'fields'}->{$b}->{'order'}
577 } keys %{ $tdata->{'fields'} };
579 for my $fname ( @fields ) {
580 my $fdata = $tdata->{'fields'}{ $fname };
581 my $field = $table->add_field(
582 name => $fdata->{'name'},
583 data_type => $fdata->{'data_type'},
584 size => $fdata->{'size'},
585 default_value => $fdata->{'default'},
586 is_auto_increment => $fdata->{'is_auto_inc'},
587 is_nullable => $fdata->{'null'},
588 comments => $fdata->{'comments'},
589 ) or die $table->error;
591 for my $cdata ( @{ $fdata->{'constraints'} } ) {
592 next unless $cdata->{'type'} eq 'foreign_key';
593 $cdata->{'fields'} ||= [ $field->name ];
594 push @{ $tdata->{'constraints'} }, $cdata;
598 push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
599 push @{ $tdata->{'constraints'} },
600 @{ $constraints->{ $table_name } || [] };
602 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
603 my $index = $table->add_index(
604 name => $idata->{'name'},
605 type => uc $idata->{'type'},
606 fields => $idata->{'fields'},
607 ) or die $table->error;
610 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
611 my $constraint = $table->add_constraint(
612 name => $cdata->{'name'},
613 type => $cdata->{'type'},
614 fields => $cdata->{'fields'},
615 reference_table => $cdata->{'reference_table'},
616 reference_fields => $cdata->{'reference_fields'},
617 match_type => $cdata->{'match_type'} || '',
618 on_delete => $cdata->{'on_delete_do'},
619 on_update => $cdata->{'on_update_do'},
620 ) or die $table->error;
629 # -------------------------------------------------------------------
630 # Something there is that doesn't love a wall.
632 # -------------------------------------------------------------------
638 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
642 SQL::Translator, Parse::RecDescent, DDL::Oracle.