1 package SQL::Translator::Parser::Oracle;
3 # -------------------------------------------------------------------
4 # $Id: Oracle.pm,v 1.4 2003-06-11 03:59:49 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]
97 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
98 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
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.
117 { our ( %tables, $table_order ) }
120 # The "eofile" rule makes the parser fail if any "statement" rule
121 # fails. Otherwise, the first successful match by a "statement"
122 # won't cause the failure needed to know that the parse, as a whole,
125 startrule : statement(s) eofile { \%tables }
136 alter : /alter/i WORD /[^;]+/ ';'
138 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
140 my $table_name = $item{'table_name'};
141 $tables{ $table_name }{'order'} = ++$table_order;
142 $tables{ $table_name }{'table_name'} = $table_name;
146 for my $definition ( @{ $item[4] } ) {
147 if ( $definition->{'type'} eq 'field' ) {
148 my $field_name = $definition->{'name'};
149 $tables{ $table_name }{'fields'}{ $field_name } =
150 { %$definition, order => $i };
153 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
154 $constraint->{'fields'} = [ $field_name ];
155 push @{ $tables{ $table_name }{'constraints'} },
159 elsif ( $definition->{'type'} eq 'constraint' ) {
160 $definition->{'type'} = $definition->{'constraint_type'};
161 # group FKs at the field level
162 # if ( $definition->{'type'} eq 'foreign_key' ) {
163 # for my $fld ( @{ $definition->{'fields'} || [] } ) {
165 # $tables{$table_name}{'fields'}{$fld}{'constraints'}
170 push @{ $tables{ $table_name }{'constraints'} },
175 push @{ $tables{ $table_name }{'indices'} }, $definition;
179 for my $option ( @{ $item[6] } ) {
180 $tables{ $table_name }{'table_options'}{ $option->{'type'} } =
187 # Create anything else (e.g., domain, function, etc.)
188 create : /create/i WORD /[^;]+/ ';'
190 global_temporary: /global/i /temporary/i
192 table_name : NAME '.' NAME
197 create_definition : field
201 comment : /^\s*(?:#|-{2}).*\n/
203 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
205 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
208 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
210 my $table_name = $item[4]->{'table'};
211 my $field_name = $item[4]->{'field'};
212 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
213 $item{'comment_phrase'};
216 column_name : NAME '.' NAME
217 { $return = { table => $item[1], field => $item[3] } }
219 comment_phrase : /'.*?'/
226 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
228 my ( $is_pk, $default, @constraints );
230 for my $meta ( @{ $item[4] } ) {
231 if ( $meta->{'type'} eq 'default' ) {
235 elsif ( $meta->{'type'} eq 'not_null' ) {
239 elsif ( $meta->{'type'} eq 'primary_key' ) {
243 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
246 my @comments = ( @{ $item[1] }, @{ $item[5] } );
250 name => $item{'field_name'},
251 data_type => $item{'data_type'}{'type'},
252 size => $item{'data_type'}{'size'},
254 default => $default->{'value'},
255 is_primary_key => $is_pk,
256 constraints => [ @constraints ],
257 comments => [ @comments ],
264 data_type : ora_data_type parens_value_list(?)
268 size => $item[2][0] || '',
272 column_constraint : constraint_name(?) column_constraint_type
273 #constraint_state(s /,/)
275 my $desc = $item{'column_constraint_type'};
276 my $type = $desc->{'type'};
277 my $fields = $desc->{'fields'} || [];
278 my $expression = $desc->{'expression'} || '';
281 supertype => 'constraint',
282 name => $item{'constraint_name(?)'}[0] || '',
284 expression => $type eq 'check' ? $expression : '',
285 deferreable => $item{'deferrable'},
286 deferred => $item{'deferred'},
287 reference_table => $desc->{'reference_table'},
288 reference_fields => $desc->{'reference_fields'},
289 # match_type => $desc->{'match_type'},
290 # on_update_do => $desc->{'on_update_do'},
294 constraint_name : /constraint/i NAME { $item[2] }
296 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
298 { $return = { type => 'null' } }
300 { $return = { type => 'unique' } }
302 { $return = { type => 'primary_key' } }
303 | /check/i '(' /[^)]+/ ')'
304 { $return = { type => 'check', expression => $item[2] } }
305 | /references/i table_name parens_word_list(?) on_delete_do(?)
308 type => 'foreign_key',
309 reference_table => $item[2],
310 reference_fields => $item[3][0],
311 # match_type => $item[4][0],
312 on_delete_do => $item[5][0],
316 #constraint_state : deferrable { $return = { type => $item[1] } }
317 # | deferred { $return = { type => $item[1] } }
318 # | /(no)?rely/ { $return = { type => $item[1] } }
319 # | /using/i /index/i using_index_clause
320 # { $return = { type => 'using_index', index => $item[3] }
321 # | (dis)?enable { $return = { type => $item[1] } }
322 # | (no)?validate { $return = { type => $item[1] } }
323 # | /exceptions/i /into/i table_name
324 # { $return = { type => 'exceptions_into', table => $item[3] } }
326 deferrable : /not/i /deferrable/i
327 { $return = 'not_deferrable' }
329 { $return = 'deferrable' }
331 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
334 /(n?varchar2|varchar)/i { $return = 'varchar2' }
336 /n?char/i { $return = 'character' }
338 /number/i { $return = 'number' }
340 /(pls_integer|binary_integer)/i { $return = 'integer' }
342 /interval\s+day/i { $return = 'interval_day' }
344 /interval\s+year/i { $return = 'interval_year' }
346 /long\s+raw/i { $return = 'long_raw' }
348 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile)/i { $item[1] }
350 parens_value_list : '(' VALUE(s /,/) ')'
353 parens_word_list : '(' WORD(s /,/) ')'
356 field_meta : default_val
359 default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
361 my $val = $item[2] || '';
364 supertype => 'constraint',
370 create_table : /create/i global_temporary(?) /table/i
372 table_option : /[^;]+/
374 table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
376 my $desc = $item{'table_constraint_type'};
377 my $type = $desc->{'type'};
378 my $fields = $desc->{'fields'};
379 my $expression = $desc->{'expression'};
380 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
383 name => $item{'constraint_name(?)'}[0] || '',
384 type => 'constraint',
385 constraint_type => $type,
386 fields => $type ne 'check' ? $fields : [],
387 expression => $type eq 'check' ? $expression : '',
388 deferreable => $item{'deferrable(?)'},
389 deferred => $item{'deferred(?)'},
390 reference_table => $desc->{'reference_table'},
391 reference_fields => $desc->{'reference_fields'},
392 # match_type => $desc->{'match_type'}[0],
393 on_delete_do => $desc->{'on_delete_do'},
394 on_update_do => $desc->{'on_update_do'},
395 comments => [ @comments ],
399 table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
402 type => 'primary_key',
407 /unique/i '(' NAME(s /,/) ')'
415 /check/ '(' /(.+)/ ')'
419 expression => $item[3],
423 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete_do(?)
426 type => 'foreign_key',
428 reference_table => $item[6],
429 reference_fields => $item[7][0],
430 match_type => $item[8][0],
431 on_delete_do => $item[9][0],
432 on_update_do => $item[10][0],
436 on_delete_do : /on delete/i WORD(s)
441 NAME : /\w+/ { $item[1] }
443 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
445 | /'.*?'/ # XXX doesn't handle embedded quotes
452 # -------------------------------------------------------------------
454 my ( $translator, $data ) = @_;
455 $parser ||= Parse::RecDescent->new($GRAMMAR);
457 local $::RD_TRACE = $translator->trace ? 1 : undef;
458 local $DEBUG = $translator->debug;
460 unless (defined $parser) {
461 return $translator->error("Error instantiating Parse::RecDescent ".
462 "instance: Bad grammer");
465 my $result = $parser->startrule($data);
466 die "Parse failed.\n" unless defined $result;
467 warn Dumper($result) if $DEBUG;
469 my $schema = $translator->schema;
471 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
474 for my $table_name ( @tables ) {
475 my $tdata = $result->{ $table_name };
476 my $table = $schema->add_table(
477 name => $tdata->{'table_name'},
478 comments => $tdata->{'comments'},
479 ) or die $schema->error;
482 $tdata->{'fields'}->{$a}->{'order'}
484 $tdata->{'fields'}->{$b}->{'order'}
485 } keys %{ $tdata->{'fields'} };
487 for my $fname ( @fields ) {
488 my $fdata = $tdata->{'fields'}{ $fname };
489 my $field = $table->add_field(
490 name => $fdata->{'name'},
491 data_type => $fdata->{'data_type'},
492 size => $fdata->{'size'},
493 default_value => $fdata->{'default'},
494 is_auto_increment => $fdata->{'is_auto_inc'},
495 is_nullable => $fdata->{'null'},
496 comments => $fdata->{'comments'},
497 ) or die $table->error;
499 for my $cdata ( @{ $fdata->{'constraints'} } ) {
500 next unless $cdata->{'type'} eq 'foreign_key';
501 $cdata->{'fields'} ||= [ $field->name ];
502 push @{ $tdata->{'constraints'} }, $cdata;
506 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
507 my $index = $table->add_index(
508 name => $idata->{'name'},
509 type => uc $idata->{'type'},
510 fields => $idata->{'fields'},
511 ) or die $table->error;
514 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
515 my $constraint = $table->add_constraint(
516 name => $cdata->{'name'},
517 type => $cdata->{'type'},
518 fields => $cdata->{'fields'},
519 reference_table => $cdata->{'reference_table'},
520 reference_fields => $cdata->{'reference_fields'},
521 match_type => $cdata->{'match_type'} || '',
522 on_delete => $cdata->{'on_delete_do'},
523 on_update => $cdata->{'on_update_do'},
524 ) or die $table->error;
533 # -------------------------------------------------------------------
534 # Something there is that doesn't love a wall.
536 # -------------------------------------------------------------------
542 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
546 perl(1), Parse::RecDescent.