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