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