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