Modified "create anything else" rule to not include table and index creates so any...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / Oracle.pm
CommitLineData
17cae8ab 1package SQL::Translator::Parser::Oracle;
2
3# -------------------------------------------------------------------
3b668c81 4# $Id: Oracle.pm,v 1.21 2005-08-10 15:17:48 duality72 Exp $
17cae8ab 5# -------------------------------------------------------------------
90075866 6# Copyright (C) 2002-4 SQLFairy Authors
17cae8ab 7#
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.
11#
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.
16#
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
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=head1 NAME
24
25SQL::Translator::Parser::Oracle - parser for Oracle
26
27=head1 SYNOPSIS
28
29 use SQL::Translator;
30 use SQL::Translator::Parser::Oracle;
31
32 my $translator = SQL::Translator->new;
33 $translator->parser("SQL::Translator::Parser::Oracle");
34
35=head1 DESCRIPTION
36
37From http://www.ss64.com/ora/table_c.html:
38
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]
45 partitioning_options
46 [[NO]CACHE] [[NO]MONITORING] [PARALLEL parallel_clause]
47 [ENABLE enable_clause | DISABLE disable_clause]
48 [AS subquery]
49
50tbl_defs:
51 column datatype [DEFAULT expr] [column_constraint(s)]
17cae8ab 52 table_ref_constraint
53
54storage_options:
55 PCTFREE int
56 PCTUSED int
57 INITTRANS int
58 MAXTRANS int
59 STORAGE storage_clause
60 TABLESPACE tablespace
61 [LOGGING|NOLOGGING]
62
63idx_organized_tbl_clause:
64 storage_option(s) [PCTTHRESHOLD int]
65 [COMPRESS int|NOCOMPRESS]
66 [ [INCLUDING column_name] OVERFLOW [storage_option(s)] ]
67
68nested_storage_clause:
69 NESTED TABLE nested_item STORE AS storage_table
70 [RETURN AS {LOCATOR|VALUE} ]
71
72partitioning_options:
73 Partition_clause {ENABLE|DISABLE} ROW MOVEMENT
74
75Column Constraints
76(http://www.ss64.com/ora/clause_constraint_col.html)
77
78 CONSTRAINT constrnt_name {UNIQUE|PRIMARY KEY} constrnt_state
79
80 CONSTRAINT constrnt_name CHECK(condition) constrnt_state
81
82 CONSTRAINT constrnt_name [NOT] NULL constrnt_state
83
84 CONSTRAINT constrnt_name REFERENCES [schema.]table[(column)]
85 [ON DELETE {CASCADE|SET NULL}] constrnt_state
86
87constrnt_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]
92
3b2be65a 93Note that probably not all of the above syntax is supported, but the grammar
94was altered to better handle the syntax created by DDL::Oracle.
95
17cae8ab 96=cut
97
98use strict;
99use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
3b668c81 100$VERSION = sprintf "%d.%02d", q$Revision: 1.21 $ =~ /(\d+)\.(\d+)/;
17cae8ab 101$DEBUG = 0 unless defined $DEBUG;
102
103use Data::Dumper;
104use Parse::RecDescent;
105use Exporter;
106use base qw(Exporter);
107
108@EXPORT_OK = qw(parse);
109
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.
114
115my $parser;
116
3b668c81 117$GRAMMAR = q`
17cae8ab 118
6a36a3d8 119{ my ( %tables, %indices, %constraints, $table_order, @table_comments ) }
17cae8ab 120
121#
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,
125# failed. -ky
126#
87053733 127startrule : statement(s) eofile
128 {
129 $return = {
6a36a3d8 130 tables => \%tables,
131 indices => \%indices,
132 constraints => \%constraints,
87053733 133 };
134 }
17cae8ab 135
136eofile : /^\Z/
137
87053733 138statement : remark
3b668c81 139 | run
87053733 140 | prompt
141 | create
f77a985b 142 | table_comment
067e3cf4 143 | comment_on_table
144 | comment_on_column
145 | alter
146 | drop
147 | <error>
17cae8ab 148
44567b47 149alter : /alter/i WORD /[^;]+/ ';'
067e3cf4 150 { @table_comments = () }
151
152drop : /drop/i TABLE ';'
153
154drop : /drop/i WORD(s) ';'
155 { @table_comments = () }
44567b47 156
87053733 157create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
17cae8ab 158 {
159 my $table_name = $item{'table_name'};
160 $tables{ $table_name }{'order'} = ++$table_order;
161 $tables{ $table_name }{'table_name'} = $table_name;
162
067e3cf4 163 if ( @table_comments ) {
164 $tables{ $table_name }{'comments'} = [ @table_comments ];
165 @table_comments = ();
166 }
167
17cae8ab 168 my $i = 1;
169 my @constraints;
87053733 170 for my $definition ( @{ $item[4] } ) {
17cae8ab 171 if ( $definition->{'type'} eq 'field' ) {
172 my $field_name = $definition->{'name'};
173 $tables{ $table_name }{'fields'}{ $field_name } =
174 { %$definition, order => $i };
175 $i++;
176
17cae8ab 177 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
178 $constraint->{'fields'} = [ $field_name ];
179 push @{ $tables{ $table_name }{'constraints'} },
180 $constraint;
181 }
182 }
183 elsif ( $definition->{'type'} eq 'constraint' ) {
184 $definition->{'type'} = $definition->{'constraint_type'};
f77a985b 185 push @{ $tables{ $table_name }{'constraints'} }, $definition;
17cae8ab 186 }
187 else {
188 push @{ $tables{ $table_name }{'indices'} }, $definition;
189 }
190 }
191
87053733 192 for my $option ( @{ $item[6] } ) {
7c4cf567 193 push @{ $tables{ $table_name }{'table_options'} }, $option;
17cae8ab 194 }
195
196 1;
197 }
198
3b668c81 199create : create_index index_name /on/i table_name parens_word_list table_option(?) ';'
b85a95ec 200 {
3b668c81 201 my $table_name = $item[4];
202 if ( $item[1] ) {
6a36a3d8 203 push @{ $constraints{ $table_name } }, {
3b668c81 204 name => $item[2],
6a36a3d8 205 type => 'unique',
3b668c81 206 fields => $item[5],
6a36a3d8 207 };
208 }
209 else {
210 push @{ $indices{ $table_name } }, {
3b668c81 211 name => $item[2],
6a36a3d8 212 type => 'normal',
3b668c81 213 fields => $item[5],
6a36a3d8 214 };
215 }
b85a95ec 216 }
217
17cae8ab 218# Create anything else (e.g., domain, function, etc.)
3b668c81 219create : ...!create_table ...!create_index /create/i WORD /[^;]+/ ';'
067e3cf4 220 { @table_comments = () }
17cae8ab 221
3b668c81 222create_index : /create/i UNIQUE(?) /index/i
223 { $return = $item[2] }
224
225index_name : NAME '.' NAME
226 { $item[3] }
227 | NAME
228 { $item[1] }
229
17cae8ab 230global_temporary: /global/i /temporary/i
231
232table_name : NAME '.' NAME
233 { $item[3] }
234 | NAME
235 { $item[1] }
236
237create_definition : field
238 | table_constraint
239 | <error>
240
f77a985b 241table_comment : comment
242 {
243 my $comment = $item[1];
244 $return = $comment;
245 push @table_comments, $comment;
246 }
247
17cae8ab 248comment : /^\s*(?:#|-{2}).*\n/
067e3cf4 249 {
39129b47 250 my $comment = $item[1];
251 $comment =~ s/^\s*(#|-{2})\s*//;
252 $comment =~ s/\s*$//;
253 $return = $comment;
f77a985b 254 }
255
256comment : /\/\*/ /[^\*]+/ /\*\//
257 {
258 my $comment = $item[2];
259 $comment =~ s/^\s*|\s*$//g;
260 $return = $comment;
067e3cf4 261 }
17cae8ab 262
87053733 263remark : /^REM\s+.*\n/
264
3b668c81 265run : /^(RUN|\/)\s+.*\n/
266
87053733 267prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
268
269prompt : /prompt\s+create\s+.*\n/i
270
947dfd86 271comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
17cae8ab 272 {
947dfd86 273 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
17cae8ab 274 }
275
947dfd86 276comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
17cae8ab 277 {
278 my $table_name = $item[4]->{'table'};
279 my $field_name = $item[4]->{'field'};
280 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
947dfd86 281 $item{'comment_phrase'};
17cae8ab 282 }
283
284column_name : NAME '.' NAME
285 { $return = { table => $item[1], field => $item[3] } }
286
947dfd86 287comment_phrase : /'.*?'/
288 {
289 my $val = $item[1];
290 $val =~ s/^'|'$//g;
291 $return = $val;
292 }
17cae8ab 293
294field : comment(s?) field_name data_type field_meta(s?) comment(s?)
295 {
947dfd86 296 my ( $is_pk, $default, @constraints );
297 my $null = 1;
17cae8ab 298 for my $meta ( @{ $item[4] } ) {
947dfd86 299 if ( $meta->{'type'} eq 'default' ) {
300 $default = $meta;
301 next;
302 }
303 elsif ( $meta->{'type'} eq 'not_null' ) {
304 $null = 0;
305 next;
306 }
307 elsif ( $meta->{'type'} eq 'primary_key' ) {
308 $is_pk = 1;
309 }
310
311 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
17cae8ab 312 }
313
947dfd86 314 my @comments = ( @{ $item[1] }, @{ $item[5] } );
17cae8ab 315
316 $return = {
317 type => 'field',
318 name => $item{'field_name'},
319 data_type => $item{'data_type'}{'type'},
320 size => $item{'data_type'}{'size'},
17cae8ab 321 null => $null,
322 default => $default->{'value'},
947dfd86 323 is_primary_key => $is_pk,
17cae8ab 324 constraints => [ @constraints ],
947dfd86 325 comments => [ @comments ],
17cae8ab 326 }
327 }
328 | <error>
329
330field_name : NAME
331
332data_type : ora_data_type parens_value_list(?)
333 {
334 $return = {
335 type => $item[1],
336 size => $item[2][0] || '',
337 }
338 }
339
3b668c81 340column_constraint : constraint_name(?) column_constraint_type constraint_state(s?)
17cae8ab 341 {
342 my $desc = $item{'column_constraint_type'};
343 my $type = $desc->{'type'};
344 my $fields = $desc->{'fields'} || [];
345 my $expression = $desc->{'expression'} || '';
346
347 $return = {
947dfd86 348 supertype => 'constraint',
349 name => $item{'constraint_name(?)'}[0] || '',
17cae8ab 350 type => $type,
351 expression => $type eq 'check' ? $expression : '',
40c5e2f4 352 deferrable => $item{'deferrable'},
17cae8ab 353 deferred => $item{'deferred'},
354 reference_table => $desc->{'reference_table'},
355 reference_fields => $desc->{'reference_fields'},
356# match_type => $desc->{'match_type'},
100684f3 357# on_update => $desc->{'on_update'},
17cae8ab 358 }
359 }
360
361constraint_name : /constraint/i NAME { $item[2] }
362
c72b8f32 363column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } }
947dfd86 364 | /null/
17cae8ab 365 { $return = { type => 'null' } }
947dfd86 366 | /unique/
17cae8ab 367 { $return = { type => 'unique' } }
c72b8f32 368 | /primary\s+key/i
17cae8ab 369 { $return = { type => 'primary_key' } }
947dfd86 370 | /check/i '(' /[^)]+/ ')'
c72b8f32 371 { $return = { type => 'check', expression => $item[3] } }
100684f3 372 | /references/i table_name parens_word_list(?) on_delete(?)
17cae8ab 373 {
374 $return = {
375 type => 'foreign_key',
376 reference_table => $item[2],
377 reference_fields => $item[3][0],
378# match_type => $item[4][0],
100684f3 379 on_delete => $item[5][0],
17cae8ab 380 }
381 }
382
3b668c81 383constraint_state : deferrable { $return = { type => $item[1] } }
384 | deferred { $return = { type => $item[1] } }
385 | /(no)?rely/i { $return = { type => $item[1] } }
17cae8ab 386# | /using/i /index/i using_index_clause
3b668c81 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] } }
17cae8ab 392
393deferrable : /not/i /deferrable/i
394 { $return = 'not_deferrable' }
395 | /deferrable/i
396 { $return = 'deferrable' }
397
398deferred : /initially/i /(deferred|immediate)/i { $item[2] }
399
400ora_data_type :
947dfd86 401 /(n?varchar2|varchar)/i { $return = 'varchar2' }
17cae8ab 402 |
403 /n?char/i { $return = 'character' }
404 |
00e41431 405 /n?dec/i { $return = 'decimal' }
406 |
17cae8ab 407 /number/i { $return = 'number' }
408 |
87053733 409 /integer/i { $return = 'integer' }
410 |
17cae8ab 411 /(pls_integer|binary_integer)/i { $return = 'integer' }
412 |
3b668c81 413 /interval\s+day/i { $return = 'interval day' }
17cae8ab 414 |
3b668c81 415 /interval\s+year/i { $return = 'interval year' }
17cae8ab 416 |
3b668c81 417 /long\s+raw/i { $return = 'long raw' }
17cae8ab 418 |
3b668c81 419 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile|float)/i { $item[1] }
17cae8ab 420
421parens_value_list : '(' VALUE(s /,/) ')'
422 { $item[2] }
423
424parens_word_list : '(' WORD(s /,/) ')'
425 { $item[2] }
426
427field_meta : default_val
947dfd86 428 | column_constraint
17cae8ab 429
430default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
431 {
b85a95ec 432 my $val = $item[2];
433 $val =~ s/'//g if defined $val;
17cae8ab 434 $return = {
947dfd86 435 supertype => 'constraint',
436 type => 'default',
17cae8ab 437 value => $val,
438 }
439 }
440
441create_table : /create/i global_temporary(?) /table/i
442
7c4cf567 443table_option : /organization/i WORD
444 {
445 $return = { 'ORGANIZATION' => $item[2] }
446 }
447
448table_option : /nomonitoring/i
449 {
450 $return = { 'NOMONITORING' => undef }
451 }
452
453table_option : /parallel/i '(' key_value(s) ')'
454 {
455 $return = { 'PARALLEL' => $item[3] }
456 }
457
458key_value : WORD VALUE
459 {
460 $return = { $item[1], $item[2] }
461 }
462
17cae8ab 463table_option : /[^;]+/
464
3b668c81 465table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) constraint_state(s?) comment(s?)
17cae8ab 466 {
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] } );
472
473 $return = {
947dfd86 474 name => $item{'constraint_name(?)'}[0] || '',
17cae8ab 475 type => 'constraint',
476 constraint_type => $type,
477 fields => $type ne 'check' ? $fields : [],
478 expression => $type eq 'check' ? $expression : '',
40c5e2f4 479 deferrable => $item{'deferrable(?)'},
947dfd86 480 deferred => $item{'deferred(?)'},
17cae8ab 481 reference_table => $desc->{'reference_table'},
482 reference_fields => $desc->{'reference_fields'},
483# match_type => $desc->{'match_type'}[0],
100684f3 484 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
485 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
17cae8ab 486 comments => [ @comments ],
487 }
488 }
489
3b668c81 490table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
17cae8ab 491 {
492 $return = {
493 type => 'primary_key',
494 fields => $item[3],
495 }
496 }
497 |
498 /unique/i '(' NAME(s /,/) ')'
499 {
500 $return = {
501 type => 'unique',
502 fields => $item[3],
503 }
504 }
505 |
506 /check/ '(' /(.+)/ ')'
507 {
508 $return = {
509 type => 'check',
510 expression => $item[3],
511 }
512 }
513 |
100684f3 514 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete(?)
17cae8ab 515 {
516 $return = {
517 type => 'foreign_key',
518 fields => $item[3],
519 reference_table => $item[6],
520 reference_fields => $item[7][0],
521 match_type => $item[8][0],
100684f3 522 on_delete => $item[9][0],
523 on_update => $item[10][0],
17cae8ab 524 }
525 }
526
100684f3 527on_delete : /on delete/i WORD(s)
17cae8ab 528 { $item[2] }
529
6a36a3d8 530UNIQUE : /unique/i { $return = 1 }
531
17cae8ab 532WORD : /\w+/
533
534NAME : /\w+/ { $item[1] }
535
067e3cf4 536TABLE : /table/i
537
17cae8ab 538VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
539 { $item[1] }
540 | /'.*?'/ # XXX doesn't handle embedded quotes
541 { $item[1] }
542 | /NULL/
543 { 'NULL' }
544
3b668c81 545`;
17cae8ab 546
547# -------------------------------------------------------------------
548sub parse {
549 my ( $translator, $data ) = @_;
550 $parser ||= Parse::RecDescent->new($GRAMMAR);
551
552 local $::RD_TRACE = $translator->trace ? 1 : undef;
553 local $DEBUG = $translator->debug;
554
555 unless (defined $parser) {
556 return $translator->error("Error instantiating Parse::RecDescent ".
557 "instance: Bad grammer");
558 }
559
87053733 560 my $result = $parser->startrule( $data );
17cae8ab 561 die "Parse failed.\n" unless defined $result;
c72b8f32 562 if ( $DEBUG ) {
563 warn "Parser results =\n", Dumper($result), "\n";
564 }
947dfd86 565
6a36a3d8 566 my $schema = $translator->schema;
567 my $indices = $result->{'indices'};
568 my $constraints = $result->{'constraints'};
569 my @tables = sort {
87053733 570 $result->{'tables'}{ $a }{'order'}
571 <=>
572 $result->{'tables'}{ $b }{'order'}
573 } keys %{ $result->{'tables'} };
947dfd86 574
575 for my $table_name ( @tables ) {
87053733 576 my $tdata = $result->{'tables'}{ $table_name };
577 next unless $tdata->{'table_name'};
947dfd86 578 my $table = $schema->add_table(
579 name => $tdata->{'table_name'},
44567b47 580 comments => $tdata->{'comments'},
947dfd86 581 ) or die $schema->error;
582
7c4cf567 583 $table->options( $tdata->{'table_options'} );
584
947dfd86 585 my @fields = sort {
586 $tdata->{'fields'}->{$a}->{'order'}
587 <=>
588 $tdata->{'fields'}->{$b}->{'order'}
589 } keys %{ $tdata->{'fields'} };
590
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'},
44567b47 600 comments => $fdata->{'comments'},
947dfd86 601 ) or die $table->error;
947dfd86 602 }
603
87053733 604 push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
6a36a3d8 605 push @{ $tdata->{'constraints'} },
606 @{ $constraints->{ $table_name } || [] };
87053733 607
947dfd86 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;
614 }
615
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'} || '',
100684f3 624 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
625 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
947dfd86 626 ) or die $table->error;
627 }
628 }
629
f62bd16c 630 return 1;
17cae8ab 631}
632
6331;
634
947dfd86 635# -------------------------------------------------------------------
636# Something there is that doesn't love a wall.
637# Robert Frost
638# -------------------------------------------------------------------
639
17cae8ab 640=pod
641
642=head1 AUTHOR
643
644Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
645
646=head1 SEE ALSO
647
3b2be65a 648SQL::Translator, Parse::RecDescent, DDL::Oracle.
17cae8ab 649
650=cut