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