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