Commit | Line | Data |
17cae8ab |
1 | package SQL::Translator::Parser::Oracle; |
2 | |
3 | # ------------------------------------------------------------------- |
dc7506f7 |
4 | # $Id: Oracle.pm,v 1.29 2007-03-19 22:32:31 duality72 Exp $ |
17cae8ab |
5 | # ------------------------------------------------------------------- |
90075866 |
6 | # Copyright (C) 2002-4 SQLFairy Authors |
17cae8ab |
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)] |
17cae8ab |
52 | table_ref_constraint |
53 | |
54 | storage_options: |
55 | PCTFREE int |
56 | PCTUSED int |
57 | INITTRANS int |
58 | MAXTRANS int |
59 | STORAGE storage_clause |
60 | TABLESPACE tablespace |
61 | [LOGGING|NOLOGGING] |
62 | |
63 | idx_organized_tbl_clause: |
64 | storage_option(s) [PCTTHRESHOLD int] |
65 | [COMPRESS int|NOCOMPRESS] |
66 | [ [INCLUDING column_name] OVERFLOW [storage_option(s)] ] |
67 | |
68 | nested_storage_clause: |
69 | NESTED TABLE nested_item STORE AS storage_table |
70 | [RETURN AS {LOCATOR|VALUE} ] |
71 | |
72 | partitioning_options: |
73 | Partition_clause {ENABLE|DISABLE} ROW MOVEMENT |
74 | |
75 | Column Constraints |
76 | (http://www.ss64.com/ora/clause_constraint_col.html) |
77 | |
78 | CONSTRAINT constrnt_name {UNIQUE|PRIMARY KEY} constrnt_state |
79 | |
80 | CONSTRAINT constrnt_name CHECK(condition) constrnt_state |
81 | |
82 | CONSTRAINT constrnt_name [NOT] NULL constrnt_state |
83 | |
84 | CONSTRAINT constrnt_name REFERENCES [schema.]table[(column)] |
85 | [ON DELETE {CASCADE|SET NULL}] constrnt_state |
86 | |
87 | constrnt_state |
88 | [[NOT] DEFERRABLE] [INITIALLY {IMMEDIATE|DEFERRED}] |
89 | [RELY | NORELY] [USING INDEX using_index_clause] |
90 | [ENABLE|DISABLE] [VALIDATE|NOVALIDATE] |
91 | [EXCEPTIONS INTO [schema.]table] |
92 | |
3b2be65a |
93 | Note that probably not all of the above syntax is supported, but the grammar |
94 | was altered to better handle the syntax created by DDL::Oracle. |
95 | |
17cae8ab |
96 | =cut |
97 | |
98 | use strict; |
99 | use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ]; |
dc7506f7 |
100 | $VERSION = sprintf "%d.%02d", q$Revision: 1.29 $ =~ /(\d+)\.(\d+)/; |
17cae8ab |
101 | $DEBUG = 0 unless defined $DEBUG; |
102 | |
103 | use Data::Dumper; |
104 | use Parse::RecDescent; |
105 | use Exporter; |
106 | use base qw(Exporter); |
107 | |
108 | @EXPORT_OK = qw(parse); |
109 | |
110 | # Enable warnings within the Parse::RecDescent module. |
111 | $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error |
112 | $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c. |
113 | $::RD_HINT = 1; # Give out hints to help fix problems. |
114 | |
3b668c81 |
115 | $GRAMMAR = q` |
17cae8ab |
116 | |
dc7506f7 |
117 | { my ( %tables, %indices, %constraints, $table_order, @table_comments, %views, $view_order, %procedures, $proc_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 | # |
87053733 |
125 | startrule : statement(s) eofile |
126 | { |
127 | $return = { |
6a36a3d8 |
128 | tables => \%tables, |
129 | indices => \%indices, |
130 | constraints => \%constraints, |
dc7506f7 |
131 | views => \%views, |
132 | procedures => \%procedures, |
87053733 |
133 | }; |
134 | } |
17cae8ab |
135 | |
136 | eofile : /^\Z/ |
137 | |
87053733 |
138 | statement : remark |
3b668c81 |
139 | | run |
87053733 |
140 | | prompt |
141 | | create |
f77a985b |
142 | | table_comment |
067e3cf4 |
143 | | comment_on_table |
144 | | comment_on_column |
145 | | alter |
146 | | drop |
147 | | <error> |
17cae8ab |
148 | |
44567b47 |
149 | alter : /alter/i WORD /[^;]+/ ';' |
067e3cf4 |
150 | { @table_comments = () } |
151 | |
152 | drop : /drop/i TABLE ';' |
153 | |
154 | drop : /drop/i WORD(s) ';' |
155 | { @table_comments = () } |
44567b47 |
156 | |
87053733 |
157 | create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';' |
17cae8ab |
158 | { |
159 | my $table_name = $item{'table_name'}; |
160 | $tables{ $table_name }{'order'} = ++$table_order; |
161 | $tables{ $table_name }{'table_name'} = $table_name; |
162 | |
067e3cf4 |
163 | if ( @table_comments ) { |
164 | $tables{ $table_name }{'comments'} = [ @table_comments ]; |
165 | @table_comments = (); |
166 | } |
167 | |
17cae8ab |
168 | my $i = 1; |
169 | my @constraints; |
87053733 |
170 | for my $definition ( @{ $item[4] } ) { |
17cae8ab |
171 | if ( $definition->{'type'} eq 'field' ) { |
172 | my $field_name = $definition->{'name'}; |
173 | $tables{ $table_name }{'fields'}{ $field_name } = |
174 | { %$definition, order => $i }; |
175 | $i++; |
176 | |
17cae8ab |
177 | for my $constraint ( @{ $definition->{'constraints'} || [] } ) { |
178 | $constraint->{'fields'} = [ $field_name ]; |
179 | push @{ $tables{ $table_name }{'constraints'} }, |
180 | $constraint; |
181 | } |
182 | } |
183 | elsif ( $definition->{'type'} eq 'constraint' ) { |
184 | $definition->{'type'} = $definition->{'constraint_type'}; |
f77a985b |
185 | push @{ $tables{ $table_name }{'constraints'} }, $definition; |
17cae8ab |
186 | } |
187 | else { |
188 | push @{ $tables{ $table_name }{'indices'} }, $definition; |
189 | } |
190 | } |
191 | |
87053733 |
192 | for my $option ( @{ $item[6] } ) { |
7c4cf567 |
193 | push @{ $tables{ $table_name }{'table_options'} }, $option; |
17cae8ab |
194 | } |
195 | |
196 | 1; |
197 | } |
198 | |
7f6504d3 |
199 | create : create_index index_name /on/i table_name index_expr table_option(?) ';' |
b85a95ec |
200 | { |
3b668c81 |
201 | my $table_name = $item[4]; |
202 | if ( $item[1] ) { |
6a36a3d8 |
203 | push @{ $constraints{ $table_name } }, { |
3b668c81 |
204 | name => $item[2], |
6a36a3d8 |
205 | type => 'unique', |
3b668c81 |
206 | fields => $item[5], |
6a36a3d8 |
207 | }; |
208 | } |
209 | else { |
210 | push @{ $indices{ $table_name } }, { |
3b668c81 |
211 | name => $item[2], |
6a36a3d8 |
212 | type => 'normal', |
3b668c81 |
213 | fields => $item[5], |
6a36a3d8 |
214 | }; |
215 | } |
b85a95ec |
216 | } |
217 | |
7f6504d3 |
218 | index_expr: parens_word_list |
219 | { $item[1] } |
220 | | '(' WORD parens_word_list ')' |
c4222efd |
221 | { |
222 | my $arg_list = join(",", @{$item[3]}); |
223 | $return = "$item[2]($arg_list)"; |
224 | } |
7f6504d3 |
225 | |
dc7506f7 |
226 | create : /create/i /or replace/i /procedure/i table_name not_end m#^/$#im |
227 | { |
228 | @table_comments = (); |
229 | my $proc_name = $item[4]; |
230 | # Hack to strip owner from procedure name |
231 | $proc_name =~ s#.*\.##; |
232 | my $owner = ''; |
233 | my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5]"; |
234 | |
235 | $procedures{ $proc_name }{'order'} = ++$proc_order; |
236 | $procedures{ $proc_name }{'name'} = $proc_name; |
237 | $procedures{ $proc_name }{'owner'} = $owner; |
238 | $procedures{ $proc_name }{'sql'} = $sql; |
239 | } |
240 | |
241 | not_end: m#.*?(?=^/$)#ism |
242 | |
243 | create : /create/i /or replace/i /force/i /view/i table_name not_delimiter ';' |
244 | { |
245 | @table_comments = (); |
246 | my $view_name = $item[5]; |
247 | # Hack to strip owner from view name |
248 | $view_name =~ s#.*\.##; |
249 | my $sql = "$item[1] $item[2] $item[3] $item[4] $item[5] $item[6] $item[7]"; |
250 | |
251 | $views{ $view_name }{'order'} = ++$view_order; |
252 | $views{ $view_name }{'name'} = $view_name; |
253 | $views{ $view_name }{'sql'} = $sql; |
254 | } |
255 | |
256 | not_delimiter: /.*?(?=;)/is |
257 | |
17cae8ab |
258 | # Create anything else (e.g., domain, function, etc.) |
3b668c81 |
259 | create : ...!create_table ...!create_index /create/i WORD /[^;]+/ ';' |
067e3cf4 |
260 | { @table_comments = () } |
17cae8ab |
261 | |
3b668c81 |
262 | create_index : /create/i UNIQUE(?) /index/i |
ae8a4ce3 |
263 | { $return = @{$item[2]} } |
3b668c81 |
264 | |
265 | index_name : NAME '.' NAME |
266 | { $item[3] } |
267 | | NAME |
268 | { $item[1] } |
269 | |
17cae8ab |
270 | global_temporary: /global/i /temporary/i |
271 | |
272 | table_name : NAME '.' NAME |
273 | { $item[3] } |
274 | | NAME |
275 | { $item[1] } |
276 | |
277 | create_definition : field |
278 | | table_constraint |
279 | | <error> |
280 | |
f77a985b |
281 | table_comment : comment |
282 | { |
283 | my $comment = $item[1]; |
284 | $return = $comment; |
285 | push @table_comments, $comment; |
286 | } |
287 | |
17cae8ab |
288 | comment : /^\s*(?:#|-{2}).*\n/ |
067e3cf4 |
289 | { |
39129b47 |
290 | my $comment = $item[1]; |
291 | $comment =~ s/^\s*(#|-{2})\s*//; |
292 | $comment =~ s/\s*$//; |
293 | $return = $comment; |
f77a985b |
294 | } |
295 | |
296 | comment : /\/\*/ /[^\*]+/ /\*\// |
297 | { |
298 | my $comment = $item[2]; |
299 | $comment =~ s/^\s*|\s*$//g; |
300 | $return = $comment; |
067e3cf4 |
301 | } |
17cae8ab |
302 | |
87053733 |
303 | remark : /^REM\s+.*\n/ |
304 | |
3b668c81 |
305 | run : /^(RUN|\/)\s+.*\n/ |
306 | |
87053733 |
307 | prompt : /prompt/i /(table|index|sequence|trigger)/i ';' |
308 | |
309 | prompt : /prompt\s+create\s+.*\n/i |
310 | |
947dfd86 |
311 | comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';' |
17cae8ab |
312 | { |
947dfd86 |
313 | push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'}; |
17cae8ab |
314 | } |
315 | |
947dfd86 |
316 | comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';' |
17cae8ab |
317 | { |
318 | my $table_name = $item[4]->{'table'}; |
319 | my $field_name = $item[4]->{'field'}; |
320 | push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} }, |
947dfd86 |
321 | $item{'comment_phrase'}; |
17cae8ab |
322 | } |
323 | |
324 | column_name : NAME '.' NAME |
325 | { $return = { table => $item[1], field => $item[3] } } |
326 | |
947dfd86 |
327 | comment_phrase : /'.*?'/ |
328 | { |
329 | my $val = $item[1]; |
330 | $val =~ s/^'|'$//g; |
331 | $return = $val; |
332 | } |
17cae8ab |
333 | |
334 | field : comment(s?) field_name data_type field_meta(s?) comment(s?) |
335 | { |
947dfd86 |
336 | my ( $is_pk, $default, @constraints ); |
337 | my $null = 1; |
17cae8ab |
338 | for my $meta ( @{ $item[4] } ) { |
947dfd86 |
339 | if ( $meta->{'type'} eq 'default' ) { |
340 | $default = $meta; |
341 | next; |
342 | } |
343 | elsif ( $meta->{'type'} eq 'not_null' ) { |
344 | $null = 0; |
345 | next; |
346 | } |
347 | elsif ( $meta->{'type'} eq 'primary_key' ) { |
348 | $is_pk = 1; |
349 | } |
350 | |
351 | push @constraints, $meta if $meta->{'supertype'} eq 'constraint'; |
17cae8ab |
352 | } |
353 | |
947dfd86 |
354 | my @comments = ( @{ $item[1] }, @{ $item[5] } ); |
17cae8ab |
355 | |
356 | $return = { |
357 | type => 'field', |
358 | name => $item{'field_name'}, |
359 | data_type => $item{'data_type'}{'type'}, |
360 | size => $item{'data_type'}{'size'}, |
17cae8ab |
361 | null => $null, |
362 | default => $default->{'value'}, |
947dfd86 |
363 | is_primary_key => $is_pk, |
17cae8ab |
364 | constraints => [ @constraints ], |
947dfd86 |
365 | comments => [ @comments ], |
17cae8ab |
366 | } |
367 | } |
368 | | <error> |
369 | |
370 | field_name : NAME |
371 | |
7f6504d3 |
372 | data_type : ora_data_type data_size(?) |
17cae8ab |
373 | { |
374 | $return = { |
375 | type => $item[1], |
376 | size => $item[2][0] || '', |
377 | } |
378 | } |
7f6504d3 |
379 | |
380 | data_size : '(' VALUE(s /,/) data_size_modifier(?) ')' |
381 | { $item[2] } |
382 | |
383 | data_size_modifier: /byte/i |
384 | | /char/i |
17cae8ab |
385 | |
3b668c81 |
386 | column_constraint : constraint_name(?) column_constraint_type constraint_state(s?) |
17cae8ab |
387 | { |
388 | my $desc = $item{'column_constraint_type'}; |
389 | my $type = $desc->{'type'}; |
390 | my $fields = $desc->{'fields'} || []; |
391 | my $expression = $desc->{'expression'} || ''; |
392 | |
393 | $return = { |
947dfd86 |
394 | supertype => 'constraint', |
395 | name => $item{'constraint_name(?)'}[0] || '', |
17cae8ab |
396 | type => $type, |
397 | expression => $type eq 'check' ? $expression : '', |
40c5e2f4 |
398 | deferrable => $item{'deferrable'}, |
17cae8ab |
399 | deferred => $item{'deferred'}, |
400 | reference_table => $desc->{'reference_table'}, |
401 | reference_fields => $desc->{'reference_fields'}, |
402 | # match_type => $desc->{'match_type'}, |
100684f3 |
403 | # on_update => $desc->{'on_update'}, |
17cae8ab |
404 | } |
405 | } |
406 | |
407 | constraint_name : /constraint/i NAME { $item[2] } |
408 | |
c72b8f32 |
409 | column_constraint_type : /not\s+null/i { $return = { type => 'not_null' } } |
3294d624 |
410 | | /unique/i |
17cae8ab |
411 | { $return = { type => 'unique' } } |
c72b8f32 |
412 | | /primary\s+key/i |
17cae8ab |
413 | { $return = { type => 'primary_key' } } |
947dfd86 |
414 | | /check/i '(' /[^)]+/ ')' |
c72b8f32 |
415 | { $return = { type => 'check', expression => $item[3] } } |
100684f3 |
416 | | /references/i table_name parens_word_list(?) on_delete(?) |
17cae8ab |
417 | { |
418 | $return = { |
419 | type => 'foreign_key', |
420 | reference_table => $item[2], |
421 | reference_fields => $item[3][0], |
422 | # match_type => $item[4][0], |
100684f3 |
423 | on_delete => $item[5][0], |
17cae8ab |
424 | } |
425 | } |
426 | |
3b668c81 |
427 | constraint_state : deferrable { $return = { type => $item[1] } } |
428 | | deferred { $return = { type => $item[1] } } |
429 | | /(no)?rely/i { $return = { type => $item[1] } } |
17cae8ab |
430 | # | /using/i /index/i using_index_clause |
3b668c81 |
431 | # { $return = { type => 'using_index', index => $item[3] } } |
432 | | /(dis|en)able/i { $return = { type => $item[1] } } |
433 | | /(no)?validate/i { $return = { type => $item[1] } } |
434 | | /exceptions/i /into/i table_name |
435 | { $return = { type => 'exceptions_into', table => $item[3] } } |
17cae8ab |
436 | |
437 | deferrable : /not/i /deferrable/i |
438 | { $return = 'not_deferrable' } |
439 | | /deferrable/i |
440 | { $return = 'deferrable' } |
441 | |
442 | deferred : /initially/i /(deferred|immediate)/i { $item[2] } |
443 | |
444 | ora_data_type : |
947dfd86 |
445 | /(n?varchar2|varchar)/i { $return = 'varchar2' } |
17cae8ab |
446 | | |
447 | /n?char/i { $return = 'character' } |
448 | | |
00e41431 |
449 | /n?dec/i { $return = 'decimal' } |
450 | | |
17cae8ab |
451 | /number/i { $return = 'number' } |
452 | | |
87053733 |
453 | /integer/i { $return = 'integer' } |
454 | | |
17cae8ab |
455 | /(pls_integer|binary_integer)/i { $return = 'integer' } |
456 | | |
3b668c81 |
457 | /interval\s+day/i { $return = 'interval day' } |
17cae8ab |
458 | | |
3b668c81 |
459 | /interval\s+year/i { $return = 'interval year' } |
17cae8ab |
460 | | |
3b668c81 |
461 | /long\s+raw/i { $return = 'long raw' } |
17cae8ab |
462 | | |
3b668c81 |
463 | /(long|date|timestamp|raw|rowid|urowid|mlslabel|clob|nclob|blob|bfile|float)/i { $item[1] } |
17cae8ab |
464 | |
465 | parens_value_list : '(' VALUE(s /,/) ')' |
466 | { $item[2] } |
467 | |
468 | parens_word_list : '(' WORD(s /,/) ')' |
469 | { $item[2] } |
470 | |
471 | field_meta : default_val |
947dfd86 |
472 | | column_constraint |
17cae8ab |
473 | |
474 | default_val : /default/i /(?:')?[\w\d.-]*(?:')?/ |
475 | { |
b85a95ec |
476 | my $val = $item[2]; |
477 | $val =~ s/'//g if defined $val; |
17cae8ab |
478 | $return = { |
947dfd86 |
479 | supertype => 'constraint', |
480 | type => 'default', |
17cae8ab |
481 | value => $val, |
482 | } |
483 | } |
3294d624 |
484 | | /null/i |
485 | { |
486 | $return = { |
487 | supertype => 'constraint', |
488 | type => 'default', |
489 | value => 'NULL', |
490 | } |
491 | } |
17cae8ab |
492 | |
493 | create_table : /create/i global_temporary(?) /table/i |
494 | |
7c4cf567 |
495 | table_option : /organization/i WORD |
496 | { |
497 | $return = { 'ORGANIZATION' => $item[2] } |
498 | } |
499 | |
500 | table_option : /nomonitoring/i |
501 | { |
502 | $return = { 'NOMONITORING' => undef } |
503 | } |
504 | |
505 | table_option : /parallel/i '(' key_value(s) ')' |
506 | { |
507 | $return = { 'PARALLEL' => $item[3] } |
508 | } |
509 | |
510 | key_value : WORD VALUE |
511 | { |
512 | $return = { $item[1], $item[2] } |
513 | } |
514 | |
17cae8ab |
515 | table_option : /[^;]+/ |
516 | |
3b668c81 |
517 | table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) constraint_state(s?) comment(s?) |
17cae8ab |
518 | { |
519 | my $desc = $item{'table_constraint_type'}; |
520 | my $type = $desc->{'type'}; |
521 | my $fields = $desc->{'fields'}; |
522 | my $expression = $desc->{'expression'}; |
523 | my @comments = ( @{ $item[1] }, @{ $item[-1] } ); |
524 | |
525 | $return = { |
947dfd86 |
526 | name => $item{'constraint_name(?)'}[0] || '', |
17cae8ab |
527 | type => 'constraint', |
528 | constraint_type => $type, |
529 | fields => $type ne 'check' ? $fields : [], |
530 | expression => $type eq 'check' ? $expression : '', |
40c5e2f4 |
531 | deferrable => $item{'deferrable(?)'}, |
947dfd86 |
532 | deferred => $item{'deferred(?)'}, |
17cae8ab |
533 | reference_table => $desc->{'reference_table'}, |
534 | reference_fields => $desc->{'reference_fields'}, |
535 | # match_type => $desc->{'match_type'}[0], |
100684f3 |
536 | on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'}, |
537 | on_update => $desc->{'on_update'} || $desc->{'on_update_do'}, |
17cae8ab |
538 | comments => [ @comments ], |
539 | } |
540 | } |
541 | |
3b668c81 |
542 | table_constraint_type : /primary key/i '(' NAME(s /,/) ')' |
17cae8ab |
543 | { |
544 | $return = { |
545 | type => 'primary_key', |
546 | fields => $item[3], |
547 | } |
548 | } |
549 | | |
550 | /unique/i '(' NAME(s /,/) ')' |
551 | { |
552 | $return = { |
553 | type => 'unique', |
554 | fields => $item[3], |
555 | } |
556 | } |
557 | | |
558 | /check/ '(' /(.+)/ ')' |
559 | { |
560 | $return = { |
561 | type => 'check', |
562 | expression => $item[3], |
563 | } |
564 | } |
565 | | |
100684f3 |
566 | /foreign key/i '(' NAME(s /,/) ')' /references/i table_name parens_word_list(?) on_delete(?) |
17cae8ab |
567 | { |
568 | $return = { |
569 | type => 'foreign_key', |
570 | fields => $item[3], |
571 | reference_table => $item[6], |
572 | reference_fields => $item[7][0], |
188a97b5 |
573 | # match_type => $item[8][0], |
574 | on_delete => $item[8][0], |
575 | # on_update => $item[9][0], |
17cae8ab |
576 | } |
577 | } |
578 | |
100684f3 |
579 | on_delete : /on delete/i WORD(s) |
188a97b5 |
580 | { join(' ', @{$item[2]}) } |
17cae8ab |
581 | |
6a36a3d8 |
582 | UNIQUE : /unique/i { $return = 1 } |
583 | |
17cae8ab |
584 | WORD : /\w+/ |
585 | |
586 | NAME : /\w+/ { $item[1] } |
587 | |
067e3cf4 |
588 | TABLE : /table/i |
589 | |
17cae8ab |
590 | VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ |
591 | { $item[1] } |
592 | | /'.*?'/ # XXX doesn't handle embedded quotes |
593 | { $item[1] } |
594 | | /NULL/ |
595 | { 'NULL' } |
596 | |
3b668c81 |
597 | `; |
17cae8ab |
598 | |
599 | # ------------------------------------------------------------------- |
600 | sub parse { |
601 | my ( $translator, $data ) = @_; |
a74de73b |
602 | my $parser = Parse::RecDescent->new($GRAMMAR); |
17cae8ab |
603 | |
604 | local $::RD_TRACE = $translator->trace ? 1 : undef; |
605 | local $DEBUG = $translator->debug; |
606 | |
607 | unless (defined $parser) { |
608 | return $translator->error("Error instantiating Parse::RecDescent ". |
609 | "instance: Bad grammer"); |
610 | } |
611 | |
87053733 |
612 | my $result = $parser->startrule( $data ); |
17cae8ab |
613 | die "Parse failed.\n" unless defined $result; |
c72b8f32 |
614 | if ( $DEBUG ) { |
615 | warn "Parser results =\n", Dumper($result), "\n"; |
616 | } |
947dfd86 |
617 | |
6a36a3d8 |
618 | my $schema = $translator->schema; |
619 | my $indices = $result->{'indices'}; |
620 | my $constraints = $result->{'constraints'}; |
621 | my @tables = sort { |
87053733 |
622 | $result->{'tables'}{ $a }{'order'} |
623 | <=> |
624 | $result->{'tables'}{ $b }{'order'} |
625 | } keys %{ $result->{'tables'} }; |
947dfd86 |
626 | |
627 | for my $table_name ( @tables ) { |
87053733 |
628 | my $tdata = $result->{'tables'}{ $table_name }; |
629 | next unless $tdata->{'table_name'}; |
947dfd86 |
630 | my $table = $schema->add_table( |
631 | name => $tdata->{'table_name'}, |
44567b47 |
632 | comments => $tdata->{'comments'}, |
947dfd86 |
633 | ) or die $schema->error; |
634 | |
7c4cf567 |
635 | $table->options( $tdata->{'table_options'} ); |
636 | |
947dfd86 |
637 | my @fields = sort { |
638 | $tdata->{'fields'}->{$a}->{'order'} |
639 | <=> |
640 | $tdata->{'fields'}->{$b}->{'order'} |
641 | } keys %{ $tdata->{'fields'} }; |
642 | |
643 | for my $fname ( @fields ) { |
644 | my $fdata = $tdata->{'fields'}{ $fname }; |
645 | my $field = $table->add_field( |
646 | name => $fdata->{'name'}, |
647 | data_type => $fdata->{'data_type'}, |
648 | size => $fdata->{'size'}, |
649 | default_value => $fdata->{'default'}, |
650 | is_auto_increment => $fdata->{'is_auto_inc'}, |
651 | is_nullable => $fdata->{'null'}, |
44567b47 |
652 | comments => $fdata->{'comments'}, |
947dfd86 |
653 | ) or die $table->error; |
947dfd86 |
654 | } |
655 | |
87053733 |
656 | push @{ $tdata->{'indices'} }, @{ $indices->{ $table_name } || [] }; |
6a36a3d8 |
657 | push @{ $tdata->{'constraints'} }, |
658 | @{ $constraints->{ $table_name } || [] }; |
87053733 |
659 | |
947dfd86 |
660 | for my $idata ( @{ $tdata->{'indices'} || [] } ) { |
661 | my $index = $table->add_index( |
662 | name => $idata->{'name'}, |
663 | type => uc $idata->{'type'}, |
664 | fields => $idata->{'fields'}, |
665 | ) or die $table->error; |
666 | } |
667 | |
668 | for my $cdata ( @{ $tdata->{'constraints'} || [] } ) { |
669 | my $constraint = $table->add_constraint( |
670 | name => $cdata->{'name'}, |
671 | type => $cdata->{'type'}, |
672 | fields => $cdata->{'fields'}, |
673 | reference_table => $cdata->{'reference_table'}, |
674 | reference_fields => $cdata->{'reference_fields'}, |
675 | match_type => $cdata->{'match_type'} || '', |
100684f3 |
676 | on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'}, |
677 | on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'}, |
947dfd86 |
678 | ) or die $table->error; |
679 | } |
680 | } |
dc7506f7 |
681 | |
682 | my @procedures = sort { |
683 | $result->{procedures}->{ $a }->{'order'} <=> $result->{procedures}->{ $b }->{'order'} |
684 | } keys %{ $result->{procedures} }; |
685 | foreach my $proc_name (@procedures) { |
686 | $schema->add_procedure( |
687 | name => $proc_name, |
688 | owner => $result->{procedures}->{$proc_name}->{owner}, |
689 | sql => $result->{procedures}->{$proc_name}->{sql}, |
690 | ); |
691 | } |
692 | |
693 | my @views = sort { |
694 | $result->{views}->{ $a }->{'order'} <=> $result->{views}->{ $b }->{'order'} |
695 | } keys %{ $result->{views} }; |
696 | foreach my $view_name (keys %{ $result->{views} }) { |
697 | $schema->add_view( |
698 | name => $view_name, |
699 | sql => $result->{views}->{$view_name}->{sql}, |
700 | ); |
701 | } |
947dfd86 |
702 | |
f62bd16c |
703 | return 1; |
17cae8ab |
704 | } |
705 | |
706 | 1; |
707 | |
947dfd86 |
708 | # ------------------------------------------------------------------- |
709 | # Something there is that doesn't love a wall. |
710 | # Robert Frost |
711 | # ------------------------------------------------------------------- |
712 | |
17cae8ab |
713 | =pod |
714 | |
715 | =head1 AUTHOR |
716 | |
717 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. |
718 | |
719 | =head1 SEE ALSO |
720 | |
3b2be65a |
721 | SQL::Translator, Parse::RecDescent, DDL::Oracle. |
17cae8ab |
722 | |
723 | =cut |