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