Added rules (REM, prompt) to help parse output of DDL::Oracle; changed
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / Oracle.pm
1 package SQL::Translator::Parser::Oracle;
2
3 # -------------------------------------------------------------------
4 # $Id: Oracle.pm,v 1.17 2004-02-11 21:36:00 kycl4rk Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
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
25 SQL::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
37 From 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
50 tbl_defs:
51    column datatype [DEFAULT expr] [column_constraint(s)]
52    table_constraint
53    table_ref_constraint
54
55 storage_options:
56    PCTFREE int
57    PCTUSED int
58    INITTRANS int
59    MAXTRANS int
60    STORAGE storage_clause
61    TABLESPACE tablespace
62    [LOGGING|NOLOGGING]
63
64 idx_organized_tbl_clause:
65    storage_option(s) [PCTTHRESHOLD int]
66      [COMPRESS int|NOCOMPRESS]
67          [ [INCLUDING column_name] OVERFLOW [storage_option(s)] ]
68
69 nested_storage_clause:
70    NESTED TABLE nested_item STORE AS storage_table
71       [RETURN AS {LOCATOR|VALUE} ]
72
73 partitioning_options:
74    Partition_clause {ENABLE|DISABLE} ROW MOVEMENT
75
76 Column 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
88 constrnt_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 Note that probably not all of the above syntax is supported, but the grammar 
95 was altered to better handle the syntax created by DDL::Oracle.
96
97 =cut
98
99 use strict;
100 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
101 $VERSION = sprintf "%d.%02d", q$Revision: 1.17 $ =~ /(\d+)\.(\d+)/;
102 $DEBUG   = 0 unless defined $DEBUG;
103
104 use Data::Dumper;
105 use Parse::RecDescent;
106 use Exporter;
107 use 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
116 my $parser; 
117
118 $GRAMMAR = q!
119
120 { my ( %tables, %indices, $table_order, @table_comments ) }
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 #
128 startrule : statement(s) eofile 
129     { 
130         $return = {
131             tables  => \%tables,
132             indices => \%indices,
133         };
134     }
135
136 eofile : /^\Z/
137
138 statement : remark
139     | prompt
140     | create
141     | table_comment
142     | comment_on_table
143     | comment_on_column
144     | alter
145     | drop
146     | <error>
147
148 alter : /alter/i WORD /[^;]+/ ';'
149     { @table_comments = () }
150
151 drop : /drop/i TABLE ';'
152
153 drop : /drop/i WORD(s) ';'
154     { @table_comments = () }
155
156 create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
157     {
158         my $table_name                       = $item{'table_name'};
159         $tables{ $table_name }{'order'}      = ++$table_order;
160         $tables{ $table_name }{'table_name'} = $table_name;
161
162         if ( @table_comments ) {
163             $tables{ $table_name }{'comments'} = [ @table_comments ];
164             @table_comments = ();
165         }
166
167         my $i = 1;
168         my @constraints;
169         for my $definition ( @{ $item[4] } ) {
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                                 
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'};
184                 push @{ $tables{ $table_name }{'constraints'} }, $definition;
185             }
186             else {
187                 push @{ $tables{ $table_name }{'indices'} }, $definition;
188             }
189         }
190
191         for my $option ( @{ $item[6] } ) {
192             push @{ $tables{ $table_name }{'table_options'} }, $option;
193         }
194
195         1;
196     }
197
198 create : /create/i /index/i WORD /on/i table_name parens_word_list table_option(?) ';'
199     {
200         my $table_name = $item[5];
201         push @{ $indices{ $table_name } }, {
202             name   => $item[3],
203             type   => 'normal',
204             fields => $item[6][0],
205         };
206     }
207
208 # Create anything else (e.g., domain, function, etc.)
209 create : /create/i WORD /[^;]+/ ';'
210     { @table_comments = () }
211
212 global_temporary: /global/i /temporary/i
213
214 table_name : NAME '.' NAME
215     { $item[3] }
216     | NAME 
217     { $item[1] }
218
219 create_definition : field
220     | table_constraint
221     | <error>
222
223 table_comment : comment
224     {
225         my $comment = $item[1];
226         $return     = $comment;
227         push @table_comments, $comment;
228     }
229
230 comment : /^\s*(?:#|-{2}).*\n/
231     {
232         my $comment =  $item[1];
233         $comment    =~ s/^\s*(#|-{2})\s*//;
234         $comment    =~ s/\s*$//;
235         $return     = $comment;
236     }
237
238 comment : /\/\*/ /[^\*]+/ /\*\// 
239     {
240         my $comment = $item[2];
241         $comment    =~ s/^\s*|\s*$//g;
242         $return = $comment;
243     }
244
245 remark : /^REM\s+.*\n/
246
247 prompt : /prompt/i /(table|index|sequence|trigger)/i ';'
248
249 prompt : /prompt\s+create\s+.*\n/i
250
251 comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
252     {
253         push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
254     }
255
256 comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
257     {
258         my $table_name = $item[4]->{'table'};
259         my $field_name = $item[4]->{'field'};
260         push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} }, 
261             $item{'comment_phrase'};
262     }
263
264 column_name : NAME '.' NAME
265     { $return = { table => $item[1], field => $item[3] } }
266
267 comment_phrase : /'.*?'/ 
268     { 
269         my $val = $item[1];
270         $val =~ s/^'|'$//g;
271         $return = $val;
272     }
273
274 field : comment(s?) field_name data_type field_meta(s?) comment(s?)
275     {
276         my ( $is_pk, $default, @constraints );
277         my $null = 1;
278         for my $meta ( @{ $item[4] } ) {
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';
292         }
293
294         my @comments = ( @{ $item[1] }, @{ $item[5] } );
295
296         $return = { 
297             type           => 'field',
298             name           => $item{'field_name'}, 
299             data_type      => $item{'data_type'}{'type'},
300             size           => $item{'data_type'}{'size'},
301             null           => $null,
302             default        => $default->{'value'},
303             is_primary_key => $is_pk,
304             constraints    => [ @constraints ],
305             comments       => [ @comments ],
306         } 
307     }
308     | <error>
309
310 field_name : NAME
311
312 data_type : ora_data_type parens_value_list(?)
313     { 
314         $return  = { 
315             type => $item[1],
316             size => $item[2][0] || '',
317         } 
318     }
319
320 column_constraint : constraint_name(?) column_constraint_type 
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              =  {
328             supertype        => 'constraint',
329             name             => $item{'constraint_name(?)'}[0] || '',
330             type             => $type,
331             expression       => $type eq 'check' ? $expression : '',
332             deferrable       => $item{'deferrable'},
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
341 constraint_name : /constraint/i NAME { $item[2] }
342
343 column_constraint_type : /not null/i { $return = { type => 'not_null' } }
344     | /null/ 
345         { $return = { type => 'null' } }
346     | /unique/ 
347         { $return = { type => 'unique' } }
348     | /primary key/i 
349         { $return = { type => 'primary_key' } }
350     | /check/i '(' /[^)]+/ ')' 
351         { $return = { type => 'check', expression => $item[2] } }
352     | /references/i table_name parens_word_list(?) on_delete_do(?) 
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
373 deferrable : /not/i /deferrable/i 
374     { $return = 'not_deferrable' }
375     | /deferrable/i 
376     { $return = 'deferrable' }
377
378 deferred : /initially/i /(deferred|immediate)/i { $item[2] }
379
380 ora_data_type :
381     /(n?varchar2|varchar)/i { $return = 'varchar2' }
382     |
383     /n?char/i { $return = 'character' }
384     |
385         /n?dec/i { $return = 'decimal' }
386         |
387     /number/i { $return = 'number' }
388     |
389     /integer/i { $return = 'integer' }
390     |
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
401 parens_value_list : '(' VALUE(s /,/) ')'
402     { $item[2] }
403
404 parens_word_list : '(' WORD(s /,/) ')'
405     { $item[2] }
406
407 field_meta : default_val
408     | column_constraint
409
410 default_val  : /default/i /(?:')?[\w\d.-]*(?:')?/ 
411     { 
412         my $val =  $item[2];
413         $val    =~ s/'//g if defined $val; 
414         $return =  {
415             supertype => 'constraint',
416             type      => 'default',
417             value     => $val,
418         }
419     }
420
421 create_table : /create/i global_temporary(?) /table/i
422
423 table_option : /organization/i WORD
424     {
425         $return = { 'ORGANIZATION' => $item[2] }
426     }
427
428 table_option : /nomonitoring/i
429     {
430         $return = { 'NOMONITORING' => undef }
431     }
432
433 table_option : /parallel/i '(' key_value(s) ')'
434     {
435         $return = { 'PARALLEL' => $item[3] }
436     }
437
438 key_value : WORD VALUE
439     {
440         $return = { $item[1], $item[2] }
441     }
442
443 table_option : /[^;]+/
444
445 table_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              =  {
454             name             => $item{'constraint_name(?)'}[0] || '',
455             type             => 'constraint',
456             constraint_type  => $type,
457             fields           => $type ne 'check' ? $fields : [],
458             expression       => $type eq 'check' ? $expression : '',
459             deferrable       => $item{'deferrable(?)'},
460             deferred         => $item{'deferred(?)'},
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
470 table_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
507 on_delete_do : /on delete/i WORD(s)
508     { $item[2] }
509
510 WORD : /\w+/
511
512 NAME : /\w+/ { $item[1] }
513
514 TABLE : /table/i
515
516 VALUE   : /[-+]?\.?\d+(?:[eE]\d+)?/
517     { $item[1] }
518     | /'.*?'/   # XXX doesn't handle embedded quotes
519     { $item[1] }
520     | /NULL/
521     { 'NULL' }
522
523 !;
524
525 # -------------------------------------------------------------------
526 sub 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
538     my $result = $parser->startrule( $data );
539     die "Parse failed.\n" unless defined $result;
540     warn Dumper($result) if $DEBUG;
541
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'} };
549
550     for my $table_name ( @tables ) {
551         my $tdata    =  $result->{'tables'}{ $table_name };
552         next unless $tdata->{'table_name'};
553         my $table    =  $schema->add_table( 
554             name     => $tdata->{'table_name'},
555             comments => $tdata->{'comments'},
556         ) or die $schema->error;
557
558         $table->options( $tdata->{'table_options'} );
559
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'},
575                 comments          => $fdata->{'comments'},
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
585         push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] };
586
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
609     return 1;
610 }
611
612 1;
613
614 # -------------------------------------------------------------------
615 # Something there is that doesn't love a wall.
616 # Robert Frost
617 # -------------------------------------------------------------------
618
619 =pod
620
621 =head1 AUTHOR
622
623 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
624
625 =head1 SEE ALSO
626
627 SQL::Translator, Parse::RecDescent, DDL::Oracle.
628
629 =cut