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