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