1 package SQL::Translator::Parser::Oracle;
3 # -------------------------------------------------------------------
4 # $Id: Oracle.pm,v 1.14 2003-10-15 18:59:53 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
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.14 $ =~ /(\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, $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 { \%tables }
140 alter : /alter/i WORD /[^;]+/ ';'
141 { @table_comments = () }
143 drop : /drop/i TABLE ';'
145 drop : /drop/i WORD(s) ';'
146 { @table_comments = () }
148 prompt : /prompt/i create_table table_name
150 create : prompt(?) create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
152 my $table_name = $item{'table_name'};
153 $tables{ $table_name }{'order'} = ++$table_order;
154 $tables{ $table_name }{'table_name'} = $table_name;
156 if ( @table_comments ) {
157 $tables{ $table_name }{'comments'} = [ @table_comments ];
158 @table_comments = ();
163 for my $definition ( @{ $item[5] } ) {
164 if ( $definition->{'type'} eq 'field' ) {
165 my $field_name = $definition->{'name'};
166 $tables{ $table_name }{'fields'}{ $field_name } =
167 { %$definition, order => $i };
170 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
171 $constraint->{'fields'} = [ $field_name ];
172 push @{ $tables{ $table_name }{'constraints'} },
176 elsif ( $definition->{'type'} eq 'constraint' ) {
177 $definition->{'type'} = $definition->{'constraint_type'};
178 push @{ $tables{ $table_name }{'constraints'} }, $definition;
181 push @{ $tables{ $table_name }{'indices'} }, $definition;
185 for my $option ( @{ $item[7] } ) {
186 push @{ $tables{ $table_name }{'table_options'} }, $option;
192 create : /create/i /index/i WORD /on/i table_name parens_word_list ';'
194 my $table_name = $item[5];
195 push @{ $tables{ $table_name }{'indices'} }, {
198 fields => $item[6][0],
202 # Create anything else (e.g., domain, function, etc.)
203 create : /create/i WORD /[^;]+/ ';'
204 { @table_comments = () }
206 global_temporary: /global/i /temporary/i
208 table_name : NAME '.' NAME
213 create_definition : field
217 table_comment : comment
219 my $comment = $item[1];
221 push @table_comments, $comment;
224 comment : /^\s*(?:#|-{2}).*\n/
226 my $comment = $item[1];
227 $comment =~ s/^\s*(#|-{2})\s*//;
228 $comment =~ s/\s*$//;
232 comment : /\/\*/ /[^\*]+/ /\*\//
234 my $comment = $item[2];
235 $comment =~ s/^\s*|\s*$//g;
239 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
241 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
244 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
246 my $table_name = $item[4]->{'table'};
247 my $field_name = $item[4]->{'field'};
248 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
249 $item{'comment_phrase'};
252 column_name : NAME '.' NAME
253 { $return = { table => $item[1], field => $item[3] } }
255 comment_phrase : /'.*?'/
262 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
264 my ( $is_pk, $default, @constraints );
266 for my $meta ( @{ $item[4] } ) {
267 if ( $meta->{'type'} eq 'default' ) {
271 elsif ( $meta->{'type'} eq 'not_null' ) {
275 elsif ( $meta->{'type'} eq 'primary_key' ) {
279 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
282 my @comments = ( @{ $item[1] }, @{ $item[5] } );
286 name => $item{'field_name'},
287 data_type => $item{'data_type'}{'type'},
288 size => $item{'data_type'}{'size'},
290 default => $default->{'value'},
291 is_primary_key => $is_pk,
292 constraints => [ @constraints ],
293 comments => [ @comments ],
300 data_type : ora_data_type parens_value_list(?)
304 size => $item[2][0] || '',
308 column_constraint : constraint_name(?) column_constraint_type
309 #constraint_state(s /,/)
311 my $desc = $item{'column_constraint_type'};
312 my $type = $desc->{'type'};
313 my $fields = $desc->{'fields'} || [];
314 my $expression = $desc->{'expression'} || '';
317 supertype => 'constraint',
318 name => $item{'constraint_name(?)'}[0] || '',
320 expression => $type eq 'check' ? $expression : '',
321 deferrable => $item{'deferrable'},
322 deferred => $item{'deferred'},
323 reference_table => $desc->{'reference_table'},
324 reference_fields => $desc->{'reference_fields'},
325 # match_type => $desc->{'match_type'},
326 # on_update_do => $desc->{'on_update_do'},
330 constraint_name : /constraint/i NAME { $item[2] }
332 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
334 { $return = { type => 'null' } }
336 { $return = { type => 'unique' } }
338 { $return = { type => 'primary_key' } }
339 | /check/i '(' /[^)]+/ ')'
340 { $return = { type => 'check', expression => $item[2] } }
341 | /references/i table_name parens_word_list(?) on_delete_do(?)
344 type => 'foreign_key',
345 reference_table => $item[2],
346 reference_fields => $item[3][0],
347 # match_type => $item[4][0],
348 on_delete_do => $item[5][0],
352 #constraint_state : deferrable { $return = { type => $item[1] } }
353 # | deferred { $return = { type => $item[1] } }
354 # | /(no)?rely/ { $return = { type => $item[1] } }
355 # | /using/i /index/i using_index_clause
356 # { $return = { type => 'using_index', index => $item[3] }
357 # | (dis)?enable { $return = { type => $item[1] } }
358 # | (no)?validate { $return = { type => $item[1] } }
359 # | /exceptions/i /into/i table_name
360 # { $return = { type => 'exceptions_into', table => $item[3] } }
362 deferrable : /not/i /deferrable/i
363 { $return = 'not_deferrable' }
365 { $return = 'deferrable' }
367 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
370 /(n?varchar2|varchar)/i { $return = 'varchar2' }
372 /n?char/i { $return = 'character' }
374 /n?dec/i { $return = 'decimal' }
376 /number/i { $return = 'number' }
378 /(pls_integer|binary_integer)/i { $return = 'integer' }
380 /interval\s+day/i { $return = 'interval_day' }
382 /interval\s+year/i { $return = 'interval_year' }
384 /long\s+raw/i { $return = 'long_raw' }
386 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile)/i { $item[1] }
388 parens_value_list : '(' VALUE(s /,/) ')'
391 parens_word_list : '(' WORD(s /,/) ')'
394 field_meta : default_val
397 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
400 $val =~ s/'//g if defined $val;
402 supertype => 'constraint',
408 create_table : /create/i global_temporary(?) /table/i
410 table_option : /organization/i WORD
412 $return = { 'ORGANIZATION' => $item[2] }
415 table_option : /nomonitoring/i
417 $return = { 'NOMONITORING' => undef }
420 table_option : /parallel/i '(' key_value(s) ')'
422 $return = { 'PARALLEL' => $item[3] }
425 key_value : WORD VALUE
427 $return = { $item[1], $item[2] }
430 table_option : /[^;]+/
432 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
434 my $desc = $item{'table_constraint_type'};
435 my $type = $desc->{'type'};
436 my $fields = $desc->{'fields'};
437 my $expression = $desc->{'expression'};
438 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
441 name => $item{'constraint_name(?)'}[0] || '',
442 type => 'constraint',
443 constraint_type => $type,
444 fields => $type ne 'check' ? $fields : [],
445 expression => $type eq 'check' ? $expression : '',
446 deferrable => $item{'deferrable(?)'},
447 deferred => $item{'deferred(?)'},
448 reference_table => $desc->{'reference_table'},
449 reference_fields => $desc->{'reference_fields'},
450 # match_type => $desc->{'match_type'}[0],
451 on_delete_do => $desc->{'on_delete_do'},
452 on_update_do => $desc->{'on_update_do'},
453 comments => [ @comments ],
457 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
460 type => 'primary_key',
465 /unique/i '(' NAME(s /,/) ')'
473 /check/ '(' /(.+)/ ')'
477 expression => $item[3],
481 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete_do(?)
484 type => 'foreign_key',
486 reference_table => $item[6],
487 reference_fields => $item[7][0],
488 match_type => $item[8][0],
489 on_delete_do => $item[9][0],
490 on_update_do => $item[10][0],
494 on_delete_do : /on delete/i WORD(s)
499 NAME : /\w+/ { $item[1] }
503 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
505 | /'.*?'/ # XXX doesn't handle embedded quotes
512 # -------------------------------------------------------------------
514 my ( $translator, $data ) = @_;
515 $parser ||= Parse::RecDescent->new($GRAMMAR);
517 local $::RD_TRACE = $translator->trace ? 1 : undef;
518 local $DEBUG = $translator->debug;
520 unless (defined $parser) {
521 return $translator->error("Error instantiating Parse::RecDescent ".
522 "instance: Bad grammer");
525 my $result = $parser->startrule($data);
526 die "Parse failed.\n" unless defined $result;
527 warn Dumper($result) if $DEBUG;
529 my $schema = $translator->schema;
531 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
534 for my $table_name ( @tables ) {
535 my $tdata = $result->{ $table_name };
536 my $table = $schema->add_table(
537 name => $tdata->{'table_name'},
538 comments => $tdata->{'comments'},
539 ) or die $schema->error;
541 $table->options( $tdata->{'table_options'} );
544 $tdata->{'fields'}->{$a}->{'order'}
546 $tdata->{'fields'}->{$b}->{'order'}
547 } keys %{ $tdata->{'fields'} };
549 for my $fname ( @fields ) {
550 my $fdata = $tdata->{'fields'}{ $fname };
551 my $field = $table->add_field(
552 name => $fdata->{'name'},
553 data_type => $fdata->{'data_type'},
554 size => $fdata->{'size'},
555 default_value => $fdata->{'default'},
556 is_auto_increment => $fdata->{'is_auto_inc'},
557 is_nullable => $fdata->{'null'},
558 comments => $fdata->{'comments'},
559 ) or die $table->error;
561 for my $cdata ( @{ $fdata->{'constraints'} } ) {
562 next unless $cdata->{'type'} eq 'foreign_key';
563 $cdata->{'fields'} ||= [ $field->name ];
564 push @{ $tdata->{'constraints'} }, $cdata;
568 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
569 my $index = $table->add_index(
570 name => $idata->{'name'},
571 type => uc $idata->{'type'},
572 fields => $idata->{'fields'},
573 ) or die $table->error;
576 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
577 my $constraint = $table->add_constraint(
578 name => $cdata->{'name'},
579 type => $cdata->{'type'},
580 fields => $cdata->{'fields'},
581 reference_table => $cdata->{'reference_table'},
582 reference_fields => $cdata->{'reference_fields'},
583 match_type => $cdata->{'match_type'} || '',
584 on_delete => $cdata->{'on_delete_do'},
585 on_update => $cdata->{'on_update_do'},
586 ) or die $table->error;
595 # -------------------------------------------------------------------
596 # Something there is that doesn't love a wall.
598 # -------------------------------------------------------------------
604 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
608 SQL::Translator, Parse::RecDescent, DDL::Oracle.