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