Fixed a problem with the trigger_steps where it used to expect a string when it was...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / Oracle.pm
CommitLineData
17cae8ab 1package SQL::Translator::Parser::Oracle;
2
3# -------------------------------------------------------------------
a74de73b 4# $Id: Oracle.pm,v 1.25 2006-05-24 18:10:30 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 ];
a74de73b 100$VERSION = sprintf "%d.%02d", q$Revision: 1.25 $ =~ /(\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
3b668c81 115$GRAMMAR = q`
17cae8ab 116
6a36a3d8 117{ my ( %tables, %indices, %constraints, $table_order, @table_comments ) }
17cae8ab 118
119#
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,
123# failed. -ky
124#
87053733 125startrule : statement(s) eofile
126 {
127 $return = {
6a36a3d8 128 tables => \%tables,
129 indices => \%indices,
130 constraints => \%constraints,
87053733 131 };
132 }
17cae8ab 133
134eofile : /^\Z/
135
87053733 136statement : remark
3b668c81 137 | run
87053733 138 | prompt
139 | create
f77a985b 140 | table_comment
067e3cf4 141 | comment_on_table
142 | comment_on_column
143 | alter
144 | drop
145 | <error>
17cae8ab 146
44567b47 147alter : /alter/i WORD /[^;]+/ ';'
067e3cf4 148 { @table_comments = () }
149
150drop : /drop/i TABLE ';'
151
152drop : /drop/i WORD(s) ';'
153 { @table_comments = () }
44567b47 154
87053733 155create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
17cae8ab 156 {
157 my $table_name = $item{'table_name'};
158 $tables{ $table_name }{'order'} = ++$table_order;
159 $tables{ $table_name }{'table_name'} = $table_name;
160
067e3cf4 161 if ( @table_comments ) {
162 $tables{ $table_name }{'comments'} = [ @table_comments ];
163 @table_comments = ();
164 }
165
17cae8ab 166 my $i = 1;
167 my @constraints;
87053733 168 for my $definition ( @{ $item[4] } ) {
17cae8ab 169 if ( $definition->{'type'} eq 'field' ) {
170 my $field_name = $definition->{'name'};
171 $tables{ $table_name }{'fields'}{ $field_name } =
172 { %$definition, order => $i };
173 $i++;
174
17cae8ab 175 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
176 $constraint->{'fields'} = [ $field_name ];
177 push @{ $tables{ $table_name }{'constraints'} },
178 $constraint;
179 }
180 }
181 elsif ( $definition->{'type'} eq 'constraint' ) {
182 $definition->{'type'} = $definition->{'constraint_type'};
f77a985b 183 push @{ $tables{ $table_name }{'constraints'} }, $definition;
17cae8ab 184 }
185 else {
186 push @{ $tables{ $table_name }{'indices'} }, $definition;
187 }
188 }
189
87053733 190 for my $option ( @{ $item[6] } ) {
7c4cf567 191 push @{ $tables{ $table_name }{'table_options'} }, $option;
17cae8ab 192 }
193
194 1;
195 }
196
7f6504d3 197create : create_index index_name /on/i table_name index_expr table_option(?) ';'
b85a95ec 198 {
3b668c81 199 my $table_name = $item[4];
200 if ( $item[1] ) {
6a36a3d8 201 push @{ $constraints{ $table_name } }, {
3b668c81 202 name => $item[2],
6a36a3d8 203 type => 'unique',
3b668c81 204 fields => $item[5],
6a36a3d8 205 };
206 }
207 else {
208 push @{ $indices{ $table_name } }, {
3b668c81 209 name => $item[2],
6a36a3d8 210 type => 'normal',
3b668c81 211 fields => $item[5],
6a36a3d8 212 };
213 }
b85a95ec 214 }
215
7f6504d3 216index_expr: parens_word_list
217 { $item[1] }
218 | '(' WORD parens_word_list ')'
c4222efd 219 {
220 my $arg_list = join(",", @{$item[3]});
221 $return = "$item[2]($arg_list)";
222 }
7f6504d3 223
17cae8ab 224# Create anything else (e.g., domain, function, etc.)
3b668c81 225create : ...!create_table ...!create_index /create/i WORD /[^;]+/ ';'
067e3cf4 226 { @table_comments = () }
17cae8ab 227
3b668c81 228create_index : /create/i UNIQUE(?) /index/i
229 { $return = $item[2] }
230
231index_name : NAME '.' NAME
232 { $item[3] }
233 | NAME
234 { $item[1] }
235
17cae8ab 236global_temporary: /global/i /temporary/i
237
238table_name : NAME '.' NAME
239 { $item[3] }
240 | NAME
241 { $item[1] }
242
243create_definition : field
244 | table_constraint
245 | <error>
246
f77a985b 247table_comment : comment
248 {
249 my $comment = $item[1];
250 $return = $comment;
251 push @table_comments, $comment;
252 }
253
17cae8ab 254comment : /^\s*(?:#|-{2}).*\n/
067e3cf4 255 {
39129b47 256 my $comment = $item[1];
257 $comment =~ s/^\s*(#|-{2})\s*//;
258 $comment =~ s/\s*$//;
259 $return = $comment;
f77a985b 260 }
261
262comment : /\/\*/ /[^\*]+/ /\*\//
263 {
264 my $comment = $item[2];
265 $comment =~ s/^\s*|\s*$//g;
266 $return = $comment;
067e3cf4 267 }
17cae8ab 268
87053733 269remark : /^REM\s+.*\n/
270
3b668c81 271run : /^(RUN|\/)\s+.*\n/
272
87053733 273prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
274
275prompt : /prompt\s+create\s+.*\n/i
276
947dfd86 277comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
17cae8ab 278 {
947dfd86 279 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
17cae8ab 280 }
281
947dfd86 282comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
17cae8ab 283 {
284 my $table_name = $item[4]->{'table'};
285 my $field_name = $item[4]->{'field'};
286 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
947dfd86 287 $item{'comment_phrase'};
17cae8ab 288 }
289
290column_name : NAME '.' NAME
291 { $return = { table => $item[1], field => $item[3] } }
292
947dfd86 293comment_phrase : /'.*?'/
294 {
295 my $val = $item[1];
296 $val =~ s/^'|'$//g;
297 $return = $val;
298 }
17cae8ab 299
300field : comment(s?) field_name data_type field_meta(s?) comment(s?)
301 {
947dfd86 302 my ( $is_pk, $default, @constraints );
303 my $null = 1;
17cae8ab 304 for my $meta ( @{ $item[4] } ) {
947dfd86 305 if ( $meta->{'type'} eq 'default' ) {
306 $default = $meta;
307 next;
308 }
309 elsif ( $meta->{'type'} eq 'not_null' ) {
310 $null = 0;
311 next;
312 }
313 elsif ( $meta->{'type'} eq 'primary_key' ) {
314 $is_pk = 1;
315 }
316
317 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
17cae8ab 318 }
319
947dfd86 320 my @comments = ( @{ $item[1] }, @{ $item[5] } );
17cae8ab 321
322 $return = {
323 type => 'field',
324 name => $item{'field_name'},
325 data_type => $item{'data_type'}{'type'},
326 size => $item{'data_type'}{'size'},
17cae8ab 327 null => $null,
328 default => $default->{'value'},
947dfd86 329 is_primary_key => $is_pk,
17cae8ab 330 constraints => [ @constraints ],
947dfd86 331 comments => [ @comments ],
17cae8ab 332 }
333 }
334 | <error>
335
336field_name : NAME
337
7f6504d3 338data_type : ora_data_type data_size(?)
17cae8ab 339 {
340 $return = {
341 type => $item[1],
342 size => $item[2][0] || '',
343 }
344 }
7f6504d3 345
346data_size : '(' VALUE(s /,/) data_size_modifier(?) ')'
347 { $item[2] }
348
349data_size_modifier: /byte/i
350 | /char/i
17cae8ab 351
3b668c81 352column_constraint : constraint_name(?) column_constraint_type constraint_state(s?)
17cae8ab 353 {
354 my $desc = $item{'column_constraint_type'};
355 my $type = $desc->{'type'};
356 my $fields = $desc->{'fields'} || [];
357 my $expression = $desc->{'expression'} || '';
358
359 $return = {
947dfd86 360 supertype => 'constraint',
361 name => $item{'constraint_name(?)'}[0] || '',
17cae8ab 362 type => $type,
363 expression => $type eq 'check' ? $expression : '',
40c5e2f4 364 deferrable => $item{'deferrable'},
17cae8ab 365 deferred => $item{'deferred'},
366 reference_table => $desc->{'reference_table'},
367 reference_fields => $desc->{'reference_fields'},
368# match_type => $desc->{'match_type'},
100684f3 369# on_update => $desc->{'on_update'},
17cae8ab 370 }
371 }
372
373constraint_name : /constraint/i NAME { $item[2] }
374
c72b8f32 375column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } }
947dfd86 376 | /null/
17cae8ab 377 { $return = { type => 'null' } }
947dfd86 378 | /unique/
17cae8ab 379 { $return = { type => 'unique' } }
c72b8f32 380 | /primary\s+key/i
17cae8ab 381 { $return = { type => 'primary_key' } }
947dfd86 382 | /check/i '(' /[^)]+/ ')'
c72b8f32 383 { $return = { type => 'check', expression => $item[3] } }
100684f3 384 | /references/i table_name parens_word_list(?) on_delete(?)
17cae8ab 385 {
386 $return = {
387 type => 'foreign_key',
388 reference_table => $item[2],
389 reference_fields => $item[3][0],
390# match_type => $item[4][0],
100684f3 391 on_delete => $item[5][0],
17cae8ab 392 }
393 }
394
3b668c81 395constraint_state : deferrable { $return = { type => $item[1] } }
396 | deferred { $return = { type => $item[1] } }
397 | /(no)?rely/i { $return = { type => $item[1] } }
17cae8ab 398# | /using/i /index/i using_index_clause
3b668c81 399# { $return = { type => 'using_index', index => $item[3] } }
400 | /(dis|en)able/i { $return = { type => $item[1] } }
401 | /(no)?validate/i { $return = { type => $item[1] } }
402 | /exceptions/i /into/i table_name
403 { $return = { type => 'exceptions_into', table => $item[3] } }
17cae8ab 404
405deferrable : /not/i /deferrable/i
406 { $return = 'not_deferrable' }
407 | /deferrable/i
408 { $return = 'deferrable' }
409
410deferred : /initially/i /(deferred|immediate)/i { $item[2] }
411
412ora_data_type :
947dfd86 413 /(n?varchar2|varchar)/i { $return = 'varchar2' }
17cae8ab 414 |
415 /n?char/i { $return = 'character' }
416 |
00e41431 417 /n?dec/i { $return = 'decimal' }
418 |
17cae8ab 419 /number/i { $return = 'number' }
420 |
87053733 421 /integer/i { $return = 'integer' }
422 |
17cae8ab 423 /(pls_integer|binary_integer)/i { $return = 'integer' }
424 |
3b668c81 425 /interval\s+day/i { $return = 'interval day' }
17cae8ab 426 |
3b668c81 427 /interval\s+year/i { $return = 'interval year' }
17cae8ab 428 |
3b668c81 429 /long\s+raw/i { $return = 'long raw' }
17cae8ab 430 |
3b668c81 431 /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile|float)/i { $item[1] }
17cae8ab 432
433parens_value_list : '(' VALUE(s /,/) ')'
434 { $item[2] }
435
436parens_word_list : '(' WORD(s /,/) ')'
437 { $item[2] }
438
439field_meta : default_val
947dfd86 440 | column_constraint
17cae8ab 441
442default_val : /default/i /(?:')?[\w\d.-]*(?:')?/
443 {
b85a95ec 444 my $val = $item[2];
445 $val =~ s/'//g if defined $val;
17cae8ab 446 $return = {
947dfd86 447 supertype => 'constraint',
448 type => 'default',
17cae8ab 449 value => $val,
450 }
451 }
452
453create_table : /create/i global_temporary(?) /table/i
454
7c4cf567 455table_option : /organization/i WORD
456 {
457 $return = { 'ORGANIZATION' => $item[2] }
458 }
459
460table_option : /nomonitoring/i
461 {
462 $return = { 'NOMONITORING' => undef }
463 }
464
465table_option : /parallel/i '(' key_value(s) ')'
466 {
467 $return = { 'PARALLEL' => $item[3] }
468 }
469
470key_value : WORD VALUE
471 {
472 $return = { $item[1], $item[2] }
473 }
474
17cae8ab 475table_option : /[^;]+/
476
3b668c81 477table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) constraint_state(s?) comment(s?)
17cae8ab 478 {
479 my $desc = $item{'table_constraint_type'};
480 my $type = $desc->{'type'};
481 my $fields = $desc->{'fields'};
482 my $expression = $desc->{'expression'};
483 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
484
485 $return = {
947dfd86 486 name => $item{'constraint_name(?)'}[0] || '',
17cae8ab 487 type => 'constraint',
488 constraint_type => $type,
489 fields => $type ne 'check' ? $fields : [],
490 expression => $type eq 'check' ? $expression : '',
40c5e2f4 491 deferrable => $item{'deferrable(?)'},
947dfd86 492 deferred => $item{'deferred(?)'},
17cae8ab 493 reference_table => $desc->{'reference_table'},
494 reference_fields => $desc->{'reference_fields'},
495# match_type => $desc->{'match_type'}[0],
100684f3 496 on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'},
497 on_update => $desc->{'on_update'} || $desc->{'on_update_do'},
17cae8ab 498 comments => [ @comments ],
499 }
500 }
501
3b668c81 502table_constraint_type : /primary key/i '(' NAME(s /,/) ')'
17cae8ab 503 {
504 $return = {
505 type => 'primary_key',
506 fields => $item[3],
507 }
508 }
509 |
510 /unique/i '(' NAME(s /,/) ')'
511 {
512 $return = {
513 type => 'unique',
514 fields => $item[3],
515 }
516 }
517 |
518 /check/ '(' /(.+)/ ')'
519 {
520 $return = {
521 type => 'check',
522 expression => $item[3],
523 }
524 }
525 |
100684f3 526 /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete(?)
17cae8ab 527 {
528 $return = {
529 type => 'foreign_key',
530 fields => $item[3],
531 reference_table => $item[6],
532 reference_fields => $item[7][0],
188a97b5 533# match_type => $item[8][0],
534 on_delete => $item[8][0],
535# on_update => $item[9][0],
17cae8ab 536 }
537 }
538
100684f3 539on_delete : /on delete/i WORD(s)
188a97b5 540 { join(' ', @{$item[2]}) }
17cae8ab 541
6a36a3d8 542UNIQUE : /unique/i { $return = 1 }
543
17cae8ab 544WORD : /\w+/
545
546NAME : /\w+/ { $item[1] }
547
067e3cf4 548TABLE : /table/i
549
17cae8ab 550VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
551 { $item[1] }
552 | /'.*?'/ # XXX doesn't handle embedded quotes
553 { $item[1] }
554 | /NULL/
555 { 'NULL' }
556
3b668c81 557`;
17cae8ab 558
559# -------------------------------------------------------------------
560sub parse {
561 my ( $translator, $data ) = @_;
a74de73b 562 my $parser = Parse::RecDescent->new($GRAMMAR);
17cae8ab 563
564 local $::RD_TRACE = $translator->trace ? 1 : undef;
565 local $DEBUG = $translator->debug;
566
567 unless (defined $parser) {
568 return $translator->error("Error instantiating Parse::RecDescent ".
569 "instance: Bad grammer");
570 }
571
87053733 572 my $result = $parser->startrule( $data );
17cae8ab 573 die "Parse failed.\n" unless defined $result;
c72b8f32 574 if ( $DEBUG ) {
575 warn "Parser results =\n", Dumper($result), "\n";
576 }
947dfd86 577
6a36a3d8 578 my $schema = $translator->schema;
579 my $indices = $result->{'indices'};
580 my $constraints = $result->{'constraints'};
581 my @tables = sort {
87053733 582 $result->{'tables'}{ $a }{'order'}
583 <=>
584 $result->{'tables'}{ $b }{'order'}
585 } keys %{ $result->{'tables'} };
947dfd86 586
587 for my $table_name ( @tables ) {
87053733 588 my $tdata = $result->{'tables'}{ $table_name };
589 next unless $tdata->{'table_name'};
947dfd86 590 my $table = $schema->add_table(
591 name => $tdata->{'table_name'},
44567b47 592 comments => $tdata->{'comments'},
947dfd86 593 ) or die $schema->error;
594
7c4cf567 595 $table->options( $tdata->{'table_options'} );
596
947dfd86 597 my @fields = sort {
598 $tdata->{'fields'}->{$a}->{'order'}
599 <=>
600 $tdata->{'fields'}->{$b}->{'order'}
601 } keys %{ $tdata->{'fields'} };
602
603 for my $fname ( @fields ) {
604 my $fdata = $tdata->{'fields'}{ $fname };
605 my $field = $table->add_field(
606 name => $fdata->{'name'},
607 data_type => $fdata->{'data_type'},
608 size => $fdata->{'size'},
609 default_value => $fdata->{'default'},
610 is_auto_increment => $fdata->{'is_auto_inc'},
611 is_nullable => $fdata->{'null'},
44567b47 612 comments => $fdata->{'comments'},
947dfd86 613 ) or die $table->error;
947dfd86 614 }
615
87053733 616 push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
6a36a3d8 617 push @{ $tdata->{'constraints'} },
618 @{ $constraints->{ $table_name } || [] };
87053733 619
947dfd86 620 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
621 my $index = $table->add_index(
622 name => $idata->{'name'},
623 type => uc $idata->{'type'},
624 fields => $idata->{'fields'},
625 ) or die $table->error;
626 }
627
628 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
629 my $constraint = $table->add_constraint(
630 name => $cdata->{'name'},
631 type => $cdata->{'type'},
632 fields => $cdata->{'fields'},
633 reference_table => $cdata->{'reference_table'},
634 reference_fields => $cdata->{'reference_fields'},
635 match_type => $cdata->{'match_type'} || '',
100684f3 636 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
637 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
947dfd86 638 ) or die $table->error;
639 }
640 }
641
f62bd16c 642 return 1;
17cae8ab 643}
644
6451;
646
947dfd86 647# -------------------------------------------------------------------
648# Something there is that doesn't love a wall.
649# Robert Frost
650# -------------------------------------------------------------------
651
17cae8ab 652=pod
653
654=head1 AUTHOR
655
656Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
657
658=head1 SEE ALSO
659
3b2be65a 660SQL::Translator, Parse::RecDescent, DDL::Oracle.
17cae8ab 661
662=cut