1 package SQL::Translator::Parser::Oracle;
3 # -------------------------------------------------------------------
4 # $Id: Oracle.pm,v 1.17 2004-02-11 21:36:00 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.17 $ =~ /(\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, $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,
148 alter : /alter/i WORD /[^;]+/ ';'
149 { @table_comments = () }
151 drop : /drop/i TABLE ';'
153 drop : /drop/i WORD(s) ';'
154 { @table_comments = () }
156 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
158 my $table_name = $item{'table_name'};
159 $tables{ $table_name }{'order'} = ++$table_order;
160 $tables{ $table_name }{'table_name'} = $table_name;
162 if ( @table_comments ) {
163 $tables{ $table_name }{'comments'} = [ @table_comments ];
164 @table_comments = ();
169 for my $definition ( @{ $item[4] } ) {
170 if ( $definition->{'type'} eq 'field' ) {
171 my $field_name = $definition->{'name'};
172 $tables{ $table_name }{'fields'}{ $field_name } =
173 { %$definition, order => $i };
176 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
177 $constraint->{'fields'} = [ $field_name ];
178 push @{ $tables{ $table_name }{'constraints'} },
182 elsif ( $definition->{'type'} eq 'constraint' ) {
183 $definition->{'type'} = $definition->{'constraint_type'};
184 push @{ $tables{ $table_name }{'constraints'} }, $definition;
187 push @{ $tables{ $table_name }{'indices'} }, $definition;
191 for my $option ( @{ $item[6] } ) {
192 push @{ $tables{ $table_name }{'table_options'} }, $option;
198 create : /create/i /index/i WORD /on/i table_name parens_word_list table_option(?) ';'
200 my $table_name = $item[5];
201 push @{ $indices{ $table_name } }, {
204 fields => $item[6][0],
208 # Create anything else (e.g., domain, function, etc.)
209 create : /create/i WORD /[^;]+/ ';'
210 { @table_comments = () }
212 global_temporary: /global/i /temporary/i
214 table_name : NAME '.' NAME
219 create_definition : field
223 table_comment : comment
225 my $comment = $item[1];
227 push @table_comments, $comment;
230 comment : /^\s*(?:#|-{2}).*\n/
232 my $comment = $item[1];
233 $comment =~ s/^\s*(#|-{2})\s*//;
234 $comment =~ s/\s*$//;
238 comment : /\/\*/ /[^\*]+/ /\*\//
240 my $comment = $item[2];
241 $comment =~ s/^\s*|\s*$//g;
245 remark : /^REM\s+.*\n/
247 prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
249 prompt : /prompt\s+create\s+.*\n/i
251 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
253 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
256 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
258 my $table_name = $item[4]->{'table'};
259 my $field_name = $item[4]->{'field'};
260 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
261 $item{'comment_phrase'};
264 column_name : NAME '.' NAME
265 { $return = { table => $item[1], field => $item[3] } }
267 comment_phrase : /'.*?'/
274 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
276 my ( $is_pk, $default, @constraints );
278 for my $meta ( @{ $item[4] } ) {
279 if ( $meta->{'type'} eq 'default' ) {
283 elsif ( $meta->{'type'} eq 'not_null' ) {
287 elsif ( $meta->{'type'} eq 'primary_key' ) {
291 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
294 my @comments = ( @{ $item[1] }, @{ $item[5] } );
298 name => $item{'field_name'},
299 data_type => $item{'data_type'}{'type'},
300 size => $item{'data_type'}{'size'},
302 default => $default->{'value'},
303 is_primary_key => $is_pk,
304 constraints => [ @constraints ],
305 comments => [ @comments ],
312 data_type : ora_data_type parens_value_list(?)
316 size => $item[2][0] || '',
320 column_constraint : constraint_name(?) column_constraint_type
322 my $desc = $item{'column_constraint_type'};
323 my $type = $desc->{'type'};
324 my $fields = $desc->{'fields'} || [];
325 my $expression = $desc->{'expression'} || '';
328 supertype => 'constraint',
329 name => $item{'constraint_name(?)'}[0] || '',
331 expression => $type eq 'check' ? $expression : '',
332 deferrable => $item{'deferrable'},
333 deferred => $item{'deferred'},
334 reference_table => $desc->{'reference_table'},
335 reference_fields => $desc->{'reference_fields'},
336 # match_type => $desc->{'match_type'},
337 # on_update_do => $desc->{'on_update_do'},
341 constraint_name : /constraint/i NAME { $item[2] }
343 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
345 { $return = { type => 'null' } }
347 { $return = { type => 'unique' } }
349 { $return = { type => 'primary_key' } }
350 | /check/i '(' /[^)]+/ ')'
351 { $return = { type => 'check', expression => $item[2] } }
352 | /references/i table_name parens_word_list(?) on_delete_do(?)
355 type => 'foreign_key',
356 reference_table => $item[2],
357 reference_fields => $item[3][0],
358 # match_type => $item[4][0],
359 on_delete_do => $item[5][0],
363 #constraint_state : deferrable { $return = { type => $item[1] } }
364 # | deferred { $return = { type => $item[1] } }
365 # | /(no)?rely/ { $return = { type => $item[1] } }
366 # | /using/i /index/i using_index_clause
367 # { $return = { type => 'using_index', index => $item[3] }
368 # | (dis)?enable { $return = { type => $item[1] } }
369 # | (no)?validate { $return = { type => $item[1] } }
370 # | /exceptions/i /into/i table_name
371 # { $return = { type => 'exceptions_into', table => $item[3] } }
373 deferrable : /not/i /deferrable/i
374 { $return = 'not_deferrable' }
376 { $return = 'deferrable' }
378 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
381 /(n?varchar2|varchar)/i { $return = 'varchar2' }
383 /n?char/i { $return = 'character' }
385 /n?dec/i { $return = 'decimal' }
387 /number/i { $return = 'number' }
389 /integer/i { $return = 'integer' }
391 /(pls_integer|binary_integer)/i { $return = 'integer' }
393 /interval\s+day/i { $return = 'interval_day' }
395 /interval\s+year/i { $return = 'interval_year' }
397 /long\s+raw/i { $return = 'long_raw' }
399 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile)/i { $item[1] }
401 parens_value_list : '(' VALUE(s /,/) ')'
404 parens_word_list : '(' WORD(s /,/) ')'
407 field_meta : default_val
410 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
413 $val =~ s/'//g if defined $val;
415 supertype => 'constraint',
421 create_table : /create/i global_temporary(?) /table/i
423 table_option : /organization/i WORD
425 $return = { 'ORGANIZATION' => $item[2] }
428 table_option : /nomonitoring/i
430 $return = { 'NOMONITORING' => undef }
433 table_option : /parallel/i '(' key_value(s) ')'
435 $return = { 'PARALLEL' => $item[3] }
438 key_value : WORD VALUE
440 $return = { $item[1], $item[2] }
443 table_option : /[^;]+/
445 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
447 my $desc = $item{'table_constraint_type'};
448 my $type = $desc->{'type'};
449 my $fields = $desc->{'fields'};
450 my $expression = $desc->{'expression'};
451 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
454 name => $item{'constraint_name(?)'}[0] || '',
455 type => 'constraint',
456 constraint_type => $type,
457 fields => $type ne 'check' ? $fields : [],
458 expression => $type eq 'check' ? $expression : '',
459 deferrable => $item{'deferrable(?)'},
460 deferred => $item{'deferred(?)'},
461 reference_table => $desc->{'reference_table'},
462 reference_fields => $desc->{'reference_fields'},
463 # match_type => $desc->{'match_type'}[0],
464 on_delete_do => $desc->{'on_delete_do'},
465 on_update_do => $desc->{'on_update_do'},
466 comments => [ @comments ],
470 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
473 type => 'primary_key',
478 /unique/i '(' NAME(s /,/) ')'
486 /check/ '(' /(.+)/ ')'
490 expression => $item[3],
494 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete_do(?)
497 type => 'foreign_key',
499 reference_table => $item[6],
500 reference_fields => $item[7][0],
501 match_type => $item[8][0],
502 on_delete_do => $item[9][0],
503 on_update_do => $item[10][0],
507 on_delete_do : /on delete/i WORD(s)
512 NAME : /\w+/ { $item[1] }
516 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
518 | /'.*?'/ # XXX doesn't handle embedded quotes
525 # -------------------------------------------------------------------
527 my ( $translator, $data ) = @_;
528 $parser ||= Parse::RecDescent->new($GRAMMAR);
530 local $::RD_TRACE = $translator->trace ? 1 : undef;
531 local $DEBUG = $translator->debug;
533 unless (defined $parser) {
534 return $translator->error("Error instantiating Parse::RecDescent ".
535 "instance: Bad grammer");
538 my $result = $parser->startrule( $data );
539 die "Parse failed.\n" unless defined $result;
540 warn Dumper($result) if $DEBUG;
542 my $schema = $translator->schema;
543 my $indices = $result->{'indices'};
545 $result->{'tables'}{ $a }{'order'}
547 $result->{'tables'}{ $b }{'order'}
548 } keys %{ $result->{'tables'} };
550 for my $table_name ( @tables ) {
551 my $tdata = $result->{'tables'}{ $table_name };
552 next unless $tdata->{'table_name'};
553 my $table = $schema->add_table(
554 name => $tdata->{'table_name'},
555 comments => $tdata->{'comments'},
556 ) or die $schema->error;
558 $table->options( $tdata->{'table_options'} );
561 $tdata->{'fields'}->{$a}->{'order'}
563 $tdata->{'fields'}->{$b}->{'order'}
564 } keys %{ $tdata->{'fields'} };
566 for my $fname ( @fields ) {
567 my $fdata = $tdata->{'fields'}{ $fname };
568 my $field = $table->add_field(
569 name => $fdata->{'name'},
570 data_type => $fdata->{'data_type'},
571 size => $fdata->{'size'},
572 default_value => $fdata->{'default'},
573 is_auto_increment => $fdata->{'is_auto_inc'},
574 is_nullable => $fdata->{'null'},
575 comments => $fdata->{'comments'},
576 ) or die $table->error;
578 for my $cdata ( @{ $fdata->{'constraints'} } ) {
579 next unless $cdata->{'type'} eq 'foreign_key';
580 $cdata->{'fields'} ||= [ $field->name ];
581 push @{ $tdata->{'constraints'} }, $cdata;
585 push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
587 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
588 my $index = $table->add_index(
589 name => $idata->{'name'},
590 type => uc $idata->{'type'},
591 fields => $idata->{'fields'},
592 ) or die $table->error;
595 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
596 my $constraint = $table->add_constraint(
597 name => $cdata->{'name'},
598 type => $cdata->{'type'},
599 fields => $cdata->{'fields'},
600 reference_table => $cdata->{'reference_table'},
601 reference_fields => $cdata->{'reference_fields'},
602 match_type => $cdata->{'match_type'} || '',
603 on_delete => $cdata->{'on_delete_do'},
604 on_update => $cdata->{'on_update_do'},
605 ) or die $table->error;
614 # -------------------------------------------------------------------
615 # Something there is that doesn't love a wall.
617 # -------------------------------------------------------------------
623 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
627 SQL::Translator, Parse::RecDescent, DDL::Oracle.