Commit | Line | Data |
84012a55 |
1 | package SQL::Translator::Parser::PostgreSQL; |
4422e22a |
2 | |
3 | # ------------------------------------------------------------------- |
b3384294 |
4 | # $Id: PostgreSQL.pm,v 1.31 2003-08-21 18:08:04 kycl4rk Exp $ |
4422e22a |
5 | # ------------------------------------------------------------------- |
6 | # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>, |
0efb6e1b |
7 | # Allen Day <allenday@users.sourceforge.net>, |
4422e22a |
8 | # darren chamberlain <darren@cpan.org>, |
9 | # Chris Mungall <cjm@fruitfly.org> |
10 | # |
11 | # This program is free software; you can redistribute it and/or |
12 | # modify it under the terms of the GNU General Public License as |
13 | # published by the Free Software Foundation; version 2. |
14 | # |
15 | # This program is distributed in the hope that it will be useful, but |
16 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | # General Public License for more details. |
19 | # |
20 | # You should have received a copy of the GNU General Public License |
21 | # along with this program; if not, write to the Free Software |
0efb6e1b |
22 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
4422e22a |
23 | # 02111-1307 USA |
24 | # ------------------------------------------------------------------- |
25 | |
26 | =head1 NAME |
27 | |
84012a55 |
28 | SQL::Translator::Parser::PostgreSQL - parser for PostgreSQL |
4422e22a |
29 | |
30 | =head1 SYNOPSIS |
31 | |
32 | use SQL::Translator; |
84012a55 |
33 | use SQL::Translator::Parser::PostgreSQL; |
4422e22a |
34 | |
35 | my $translator = SQL::Translator->new; |
84012a55 |
36 | $translator->parser("SQL::Translator::Parser::PostgreSQL"); |
4422e22a |
37 | |
38 | =head1 DESCRIPTION |
39 | |
0efb6e1b |
40 | The grammar was started from the MySQL parsers. Here is the description |
41 | from PostgreSQL: |
42 | |
43 | Table: |
629b76f9 |
44 | (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createtable.html) |
45 | |
46 | CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name ( |
47 | { column_name data_type [ DEFAULT default_expr ] |
48 | [ column_constraint [, ... ] ] |
49 | | table_constraint } [, ... ] |
50 | ) |
51 | [ INHERITS ( parent_table [, ... ] ) ] |
52 | [ WITH OIDS | WITHOUT OIDS ] |
53 | |
54 | where column_constraint is: |
55 | |
56 | [ CONSTRAINT constraint_name ] |
57 | { NOT NULL | NULL | UNIQUE | PRIMARY KEY | |
58 | CHECK (expression) | |
59 | REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ] |
60 | [ ON DELETE action ] [ ON UPDATE action ] } |
61 | [ DEFERRABLE | NOT DEFERRABLE ] |
62 | [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] |
63 | |
64 | and table_constraint is: |
65 | |
66 | [ CONSTRAINT constraint_name ] |
67 | { UNIQUE ( column_name [, ... ] ) | |
68 | PRIMARY KEY ( column_name [, ... ] ) | |
69 | CHECK ( expression ) | |
70 | FOREIGN KEY ( column_name [, ... ] ) |
71 | REFERENCES reftable [ ( refcolumn [, ... ] ) ] |
72 | [ MATCH FULL | MATCH PARTIAL ] |
73 | [ ON DELETE action ] [ ON UPDATE action ] } |
74 | [ DEFERRABLE | NOT DEFERRABLE ] |
75 | [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] |
0efb6e1b |
76 | |
77 | Index: |
629b76f9 |
78 | (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createindex.html) |
0efb6e1b |
79 | |
629b76f9 |
80 | CREATE [ UNIQUE ] INDEX index_name ON table |
81 | [ USING acc_method ] ( column [ ops_name ] [, ...] ) |
82 | [ WHERE predicate ] |
83 | CREATE [ UNIQUE ] INDEX index_name ON table |
84 | [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] ) |
85 | [ WHERE predicate ] |
4422e22a |
86 | |
0012a163 |
87 | Alter table: |
88 | |
89 | ALTER TABLE [ ONLY ] table [ * ] |
90 | ADD [ COLUMN ] column type [ column_constraint [ ... ] ] |
91 | ALTER TABLE [ ONLY ] table [ * ] |
92 | ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT } |
93 | ALTER TABLE [ ONLY ] table [ * ] |
94 | ALTER [ COLUMN ] column SET STATISTICS integer |
95 | ALTER TABLE [ ONLY ] table [ * ] |
96 | RENAME [ COLUMN ] column TO newcolumn |
97 | ALTER TABLE table |
98 | RENAME TO new_table |
99 | ALTER TABLE table |
100 | ADD table_constraint_definition |
101 | ALTER TABLE [ ONLY ] table |
102 | DROP CONSTRAINT constraint { RESTRICT | CASCADE } |
103 | ALTER TABLE table |
104 | OWNER TO new_owner |
105 | |
3022f45b |
106 | View table: |
107 | |
108 | CREATE [ OR REPLACE ] VIEW view [ ( column name list ) ] AS SELECT query |
109 | |
4422e22a |
110 | =cut |
111 | |
112 | use strict; |
113 | use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ]; |
b3384294 |
114 | $VERSION = sprintf "%d.%02d", q$Revision: 1.31 $ =~ /(\d+)\.(\d+)/; |
4422e22a |
115 | $DEBUG = 0 unless defined $DEBUG; |
116 | |
117 | use Data::Dumper; |
118 | use Parse::RecDescent; |
119 | use Exporter; |
120 | use base qw(Exporter); |
121 | |
122 | @EXPORT_OK = qw(parse); |
123 | |
124 | # Enable warnings within the Parse::RecDescent module. |
125 | $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error |
126 | $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c. |
127 | $::RD_HINT = 1; # Give out hints to help fix problems. |
128 | |
129 | my $parser; # should we do this? There's no programmic way to |
130 | # change the grammar, so I think this is safe. |
131 | |
132 | $GRAMMAR = q! |
133 | |
b8ea6076 |
134 | { my ( %tables, $table_order ) } |
4422e22a |
135 | |
629b76f9 |
136 | # |
137 | # The "eofile" rule makes the parser fail if any "statement" rule |
138 | # fails. Otherwise, the first successful match by a "statement" |
139 | # won't cause the failure needed to know that the parse, as a whole, |
140 | # failed. -ky |
141 | # |
0efb6e1b |
142 | startrule : statement(s) eofile { \%tables } |
4422e22a |
143 | |
0efb6e1b |
144 | eofile : /^\Z/ |
145 | |
146 | statement : create |
147 | | comment |
0012a163 |
148 | | alter |
0efb6e1b |
149 | | grant |
150 | | revoke |
4422e22a |
151 | | drop |
4d2da1f7 |
152 | | insert |
0efb6e1b |
153 | | connect |
4d2da1f7 |
154 | | update |
0012a163 |
155 | | set |
4422e22a |
156 | | <error> |
157 | |
0efb6e1b |
158 | connect : /^\s*\\\connect.*\n/ |
159 | |
f04713db |
160 | set : /set/i /[^;]*/ ';' |
0012a163 |
161 | |
0d51cd9e |
162 | revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_name /from/i name_with_opt_quotes(s /,/) ';' |
0efb6e1b |
163 | { |
164 | my $table_name = $item{'table_name'}; |
165 | push @{ $tables{ $table_name }{'permissions'} }, { |
166 | type => 'revoke', |
167 | actions => $item[2], |
91c75eab |
168 | users => $item[7], |
0efb6e1b |
169 | } |
170 | } |
171 | |
0d51cd9e |
172 | grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_name /to/i name_with_opt_quotes(s /,/) ';' |
0efb6e1b |
173 | { |
174 | my $table_name = $item{'table_name'}; |
175 | push @{ $tables{ $table_name }{'permissions'} }, { |
176 | type => 'grant', |
177 | actions => $item[2], |
91c75eab |
178 | users => $item[7], |
0efb6e1b |
179 | } |
180 | } |
181 | |
182 | drop : /drop/i /[^;]*/ ';' |
4422e22a |
183 | |
4d2da1f7 |
184 | insert : /insert/i /[^;]*/ ';' |
185 | |
186 | update : /update/i /[^;]*/ ';' |
187 | |
0012a163 |
188 | # |
189 | # Create table. |
190 | # |
ba1a1626 |
191 | create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';' |
0efb6e1b |
192 | { |
193 | my $table_name = $item{'table_name'}; |
194 | $tables{ $table_name }{'order'} = ++$table_order; |
195 | $tables{ $table_name }{'table_name'} = $table_name; |
196 | |
197 | my $i = 1; |
198 | my @constraints; |
199 | for my $definition ( @{ $item[4] } ) { |
b3384294 |
200 | if ( $definition->{'supertype'} eq 'field' ) { |
0efb6e1b |
201 | my $field_name = $definition->{'name'}; |
202 | $tables{ $table_name }{'fields'}{ $field_name } = |
203 | { %$definition, order => $i }; |
204 | $i++; |
4422e22a |
205 | |
0138f7bb |
206 | for my $constraint ( @{ $definition->{'constraints'} || [] } ) { |
207 | $constraint->{'fields'} = [ $field_name ]; |
7d5bcab8 |
208 | push @{ $tables{ $table_name }{'constraints'} }, |
0138f7bb |
209 | $constraint; |
0efb6e1b |
210 | } |
211 | } |
b3384294 |
212 | elsif ( $definition->{'supertype'} eq 'constraint' ) { |
213 | push @{ $tables{ $table_name }{'constraints'} }, $definition; |
0efb6e1b |
214 | } |
b3384294 |
215 | elsif ( $definition->{'supertype'} eq 'index' ) { |
0efb6e1b |
216 | push @{ $tables{ $table_name }{'indices'} }, $definition; |
217 | } |
218 | } |
219 | |
220 | for my $option ( @{ $item[6] } ) { |
3022f45b |
221 | $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } = |
0efb6e1b |
222 | $option; |
223 | } |
224 | |
225 | 1; |
226 | } |
227 | |
228 | create : /create/i unique(?) /(index|key)/i index_name /on/i table_name using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';' |
4422e22a |
229 | { |
230 | push @{ $tables{ $item{'table_name'} }{'indices'} }, |
231 | { |
b3384294 |
232 | name => $item{'index_name'}, |
233 | supertype => $item{'unique'}[0] ? 'constraint' : 'index', |
234 | type => $item{'unique'}[0] ? 'unique' : 'normal', |
235 | fields => $item[9], |
236 | method => $item{'using_method'}[0], |
4422e22a |
237 | } |
238 | ; |
239 | } |
240 | |
0012a163 |
241 | # |
242 | # Create anything else (e.g., domain, function, etc.) |
243 | # |
244 | create : /create/i WORD /[^;]+/ ';' |
245 | |
0efb6e1b |
246 | using_method : /using/i WORD { $item[2] } |
247 | |
248 | where_predicate : /where/i /[^;]+/ |
249 | |
250 | create_definition : field |
0efb6e1b |
251 | | table_constraint |
4422e22a |
252 | | <error> |
253 | |
254 | comment : /^\s*(?:#|-{2}).*\n/ |
255 | |
f2f71b8e |
256 | field : comment(s?) field_name data_type field_meta(s?) comment(s?) |
4422e22a |
257 | { |
3022f45b |
258 | my ( $default, @constraints, $is_pk ); |
82968eb9 |
259 | my $null = 1; |
41fc9cb3 |
260 | for my $meta ( @{ $item[4] } ) { |
82968eb9 |
261 | if ( $meta->{'type'} eq 'default' ) { |
262 | $default = $meta; |
263 | next; |
264 | } |
265 | elsif ( $meta->{'type'} eq 'not_null' ) { |
266 | $null = 0; |
f04713db |
267 | # next; |
82968eb9 |
268 | } |
269 | elsif ( $meta->{'type'} eq 'primary_key' ) { |
270 | $is_pk = 1; |
271 | } |
4422e22a |
272 | |
82968eb9 |
273 | push @constraints, $meta if $meta->{'supertype'} eq 'constraint'; |
274 | } |
0efb6e1b |
275 | |
f2f71b8e |
276 | my @comments = ( @{ $item[1] }, @{ $item[5] } ); |
277 | |
0a7fc605 |
278 | $return = { |
b3384294 |
279 | supertype => 'field', |
7eac5e12 |
280 | name => $item{'field_name'}, |
281 | data_type => $item{'data_type'}{'type'}, |
282 | size => $item{'data_type'}{'size'}, |
283 | null => $null, |
284 | default => $default->{'value'}, |
285 | constraints => [ @constraints ], |
286 | comments => [ @comments ], |
287 | is_primary_key => $is_pk || 0, |
288 | is_auto_increment => $item{'data_type'}{'is_auto_increment'}, |
4422e22a |
289 | } |
290 | } |
291 | | <error> |
292 | |
0efb6e1b |
293 | field_meta : default_val |
82968eb9 |
294 | | column_constraint |
4422e22a |
295 | |
0efb6e1b |
296 | column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?) |
297 | { |
298 | my $desc = $item{'column_constraint_type'}; |
299 | my $type = $desc->{'type'}; |
300 | my $fields = $desc->{'fields'} || []; |
301 | my $expression = $desc->{'expression'} || ''; |
302 | |
303 | $return = { |
82968eb9 |
304 | supertype => 'constraint', |
0efb6e1b |
305 | name => $item{'constraint_name'}[0] || '', |
306 | type => $type, |
307 | expression => $type eq 'check' ? $expression : '', |
f04713db |
308 | deferrable => $item{'deferrable'}, |
0efb6e1b |
309 | deferred => $item{'deferred'}, |
310 | reference_table => $desc->{'reference_table'}, |
311 | reference_fields => $desc->{'reference_fields'}, |
312 | match_type => $desc->{'match_type'}, |
313 | on_delete_do => $desc->{'on_delete_do'}, |
314 | on_update_do => $desc->{'on_update_do'}, |
4422e22a |
315 | } |
316 | } |
317 | |
0efb6e1b |
318 | constraint_name : /constraint/i name_with_opt_quotes { $item[2] } |
319 | |
320 | column_constraint_type : /not null/i { $return = { type => 'not_null' } } |
321 | | |
f04713db |
322 | /null/i |
0efb6e1b |
323 | { $return = { type => 'null' } } |
324 | | |
f04713db |
325 | /unique/i |
0efb6e1b |
326 | { $return = { type => 'unique' } } |
327 | | |
328 | /primary key/i |
329 | { $return = { type => 'primary_key' } } |
330 | | |
331 | /check/i '(' /[^)]+/ ')' |
b3384294 |
332 | { $return = { type => 'check', expression => $item[3] } } |
0efb6e1b |
333 | | |
3022f45b |
334 | /references/i table_name parens_word_list(?) match_type(?) key_action(s?) |
0efb6e1b |
335 | { |
3022f45b |
336 | my ( $on_delete, $on_update ); |
337 | for my $action ( @{ $item[5] || [] } ) { |
338 | $on_delete = $action->{'action'} if $action->{'type'} eq 'delete'; |
339 | $on_update = $action->{'action'} if $action->{'type'} eq 'update'; |
340 | } |
341 | |
0efb6e1b |
342 | $return = { |
343 | type => 'foreign_key', |
344 | reference_table => $item[2], |
0138f7bb |
345 | reference_fields => $item[3][0], |
0efb6e1b |
346 | match_type => $item[4][0], |
3022f45b |
347 | on_delete_do => $on_delete, |
348 | on_update_do => $on_update, |
ba1a1626 |
349 | } |
4422e22a |
350 | } |
351 | |
0efb6e1b |
352 | table_name : name_with_opt_quotes |
4422e22a |
353 | |
0efb6e1b |
354 | field_name : name_with_opt_quotes |
4422e22a |
355 | |
0012a163 |
356 | name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] } |
4422e22a |
357 | |
0efb6e1b |
358 | double_quote: /"/ |
4422e22a |
359 | |
0efb6e1b |
360 | index_name : WORD |
4422e22a |
361 | |
0efb6e1b |
362 | data_type : pg_data_type parens_value_list(?) |
4422e22a |
363 | { |
3022f45b |
364 | my $data_type = $item[1]; |
ba1a1626 |
365 | |
0efb6e1b |
366 | # |
367 | # We can deduce some sizes from the data type's name. |
368 | # |
82968eb9 |
369 | $data_type->{'size'} ||= $item[2][0]; |
4422e22a |
370 | |
3022f45b |
371 | $return = $data_type; |
4422e22a |
372 | } |
373 | |
0efb6e1b |
374 | pg_data_type : |
50840472 |
375 | /(bigint|int8)/i |
3022f45b |
376 | { |
377 | $return = { |
50840472 |
378 | type => 'integer', |
b8ea6076 |
379 | size => 20, |
3022f45b |
380 | }; |
381 | } |
0efb6e1b |
382 | | |
1bbd4a2b |
383 | /(smallint|int2)/i |
3022f45b |
384 | { |
385 | $return = { |
386 | type => 'integer', |
b8ea6076 |
387 | size => 5, |
3022f45b |
388 | }; |
389 | } |
0efb6e1b |
390 | | |
50840472 |
391 | /(integer|int4?)/i # interval must come before this |
3022f45b |
392 | { |
393 | $return = { |
394 | type => 'integer', |
b8ea6076 |
395 | size => 10, |
3022f45b |
396 | }; |
397 | } |
50840472 |
398 | | |
399 | /(real|float4)/i |
400 | { |
401 | $return = { |
402 | type => 'real', |
b8ea6076 |
403 | size => 10, |
50840472 |
404 | }; |
405 | } |
0efb6e1b |
406 | | |
1bbd4a2b |
407 | /(double precision|float8?)/i |
3022f45b |
408 | { |
409 | $return = { |
410 | type => 'float', |
b8ea6076 |
411 | size => 20, |
3022f45b |
412 | }; |
413 | } |
0efb6e1b |
414 | | |
50840472 |
415 | /(bigserial|serial8)/i |
3022f45b |
416 | { |
50840472 |
417 | $return = { |
7eac5e12 |
418 | type => 'integer', |
b8ea6076 |
419 | size => 20, |
7eac5e12 |
420 | is_auto_increment => 1, |
3022f45b |
421 | }; |
422 | } |
0efb6e1b |
423 | | |
1bbd4a2b |
424 | /serial4?/i |
3022f45b |
425 | { |
426 | $return = { |
7eac5e12 |
427 | type => 'integer', |
b8ea6076 |
428 | size => 11, |
7eac5e12 |
429 | is_auto_increment => 1, |
3022f45b |
430 | }; |
431 | } |
0efb6e1b |
432 | | |
1bbd4a2b |
433 | /(bit varying|varbit)/i |
3022f45b |
434 | { |
435 | $return = { type => 'varbit' }; |
436 | } |
0efb6e1b |
437 | | |
1bbd4a2b |
438 | /character varying/i |
3022f45b |
439 | { |
440 | $return = { type => 'varchar' }; |
441 | } |
0efb6e1b |
442 | | |
1bbd4a2b |
443 | /char(acter)?/i |
3022f45b |
444 | { |
445 | $return = { type => 'char' }; |
446 | } |
0efb6e1b |
447 | | |
1bbd4a2b |
448 | /bool(ean)?/i |
3022f45b |
449 | { |
450 | $return = { type => 'boolean' }; |
451 | } |
0efb6e1b |
452 | | |
1bbd4a2b |
453 | /bytea/i |
3022f45b |
454 | { |
82968eb9 |
455 | $return = { type => 'bytea' }; |
3022f45b |
456 | } |
0efb6e1b |
457 | | |
50840472 |
458 | /(timestamptz|timestamp)/i |
3022f45b |
459 | { |
460 | $return = { type => 'timestamp' }; |
461 | } |
0efb6e1b |
462 | | |
38a6a4f9 |
463 | /text/i |
464 | { |
465 | $return = { |
466 | type => 'text', |
467 | size => 64_000, |
468 | }; |
469 | } |
470 | | |
471 | /(bit|box|cidr|circle|date|inet|interval|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i |
3022f45b |
472 | { |
473 | $return = { type => $item[1] }; |
474 | } |
0efb6e1b |
475 | |
4422e22a |
476 | parens_value_list : '(' VALUE(s /,/) ')' |
477 | { $item[2] } |
478 | |
0efb6e1b |
479 | parens_word_list : '(' WORD(s /,/) ')' |
480 | { $item[2] } |
4422e22a |
481 | |
0efb6e1b |
482 | field_size : '(' num_range ')' { $item{'num_range'} } |
4422e22a |
483 | |
0efb6e1b |
484 | num_range : DIGITS ',' DIGITS |
4422e22a |
485 | { $return = $item[1].','.$item[3] } |
486 | | DIGITS |
487 | { $return = $item[1] } |
488 | |
f2f71b8e |
489 | table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?) |
0efb6e1b |
490 | { |
491 | my $desc = $item{'table_constraint_type'}; |
492 | my $type = $desc->{'type'}; |
493 | my $fields = $desc->{'fields'}; |
494 | my $expression = $desc->{'expression'}; |
f2f71b8e |
495 | my @comments = ( @{ $item[1] }, @{ $item[-1] } ); |
0efb6e1b |
496 | |
497 | $return = { |
498 | name => $item{'constraint_name'}[0] || '', |
b3384294 |
499 | supertype => 'constraint', |
500 | type => $type, |
0efb6e1b |
501 | fields => $type ne 'check' ? $fields : [], |
502 | expression => $type eq 'check' ? $expression : '', |
f04713db |
503 | deferrable => $item{'deferrable'}, |
0efb6e1b |
504 | deferred => $item{'deferred'}, |
505 | reference_table => $desc->{'reference_table'}, |
506 | reference_fields => $desc->{'reference_fields'}, |
507 | match_type => $desc->{'match_type'}[0], |
0012a163 |
508 | on_delete_do => $desc->{'on_delete_do'}, |
509 | on_update_do => $desc->{'on_update_do'}, |
f2f71b8e |
510 | comments => [ @comments ], |
0efb6e1b |
511 | } |
512 | } |
4422e22a |
513 | |
0efb6e1b |
514 | table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')' |
515 | { |
516 | $return = { |
517 | type => 'primary_key', |
518 | fields => $item[3], |
519 | } |
520 | } |
521 | | |
522 | /unique/i '(' name_with_opt_quotes(s /,/) ')' |
523 | { |
524 | $return = { |
525 | type => 'unique', |
526 | fields => $item[3], |
527 | } |
528 | } |
529 | | |
b3384294 |
530 | /check/i '(' /[^)]+/ ')' |
0efb6e1b |
531 | { |
532 | $return = { |
533 | type => 'check', |
534 | expression => $item[3], |
535 | } |
536 | } |
537 | | |
3022f45b |
538 | /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_name parens_word_list(?) match_type(?) key_action(s?) |
0efb6e1b |
539 | { |
3022f45b |
540 | my ( $on_delete, $on_update ); |
541 | for my $action ( @{ $item[9] || [] } ) { |
542 | $on_delete = $action->{'action'} if $action->{'type'} eq 'delete'; |
543 | $on_update = $action->{'action'} if $action->{'type'} eq 'update'; |
544 | } |
545 | |
0efb6e1b |
546 | $return = { |
b3384294 |
547 | supertype => 'constraint', |
0efb6e1b |
548 | type => 'foreign_key', |
549 | fields => $item[3], |
550 | reference_table => $item[6], |
551 | reference_fields => $item[7][0], |
552 | match_type => $item[8][0], |
3022f45b |
553 | on_delete_do => $on_delete || '', |
554 | on_update_do => $on_update || '', |
0efb6e1b |
555 | } |
556 | } |
557 | |
558 | deferrable : /not/i /deferrable/i |
559 | { |
560 | $return = ( $item[1] =~ /not/i ) ? 0 : 1; |
561 | } |
4422e22a |
562 | |
0efb6e1b |
563 | deferred : /initially/i /(deferred|immediate)/i { $item[2] } |
4422e22a |
564 | |
0efb6e1b |
565 | match_type : /match full/i { 'match_full' } |
566 | | |
567 | /match partial/i { 'match_partial' } |
568 | |
3022f45b |
569 | key_action : key_delete |
570 | | |
571 | key_update |
0efb6e1b |
572 | |
3022f45b |
573 | key_delete : /on delete/i key_mutation |
574 | { |
575 | $return => { |
576 | type => 'delete', |
577 | action => $item[2], |
578 | }; |
579 | } |
580 | |
581 | key_update : /on update/i key_mutation |
582 | { |
583 | $return => { |
584 | type => 'update', |
585 | action => $item[2], |
586 | }; |
587 | } |
588 | |
589 | key_mutation : /no action/i { $return = 'no_action' } |
590 | | |
591 | /restrict/i { $return = 'restrict' } |
592 | | |
593 | /cascade/i { $return = 'cascade' } |
594 | | |
38a6a4f9 |
595 | /set null/i { $return = 'set null' } |
3022f45b |
596 | | |
38a6a4f9 |
597 | /set default/i { $return = 'set default' } |
0efb6e1b |
598 | |
0012a163 |
599 | alter : alter_table table_name /add/i table_constraint ';' |
600 | { |
601 | my $table_name = $item[2]; |
602 | my $constraint = $item[4]; |
0012a163 |
603 | push @{ $tables{ $table_name }{'constraints'} }, $constraint; |
604 | } |
605 | |
606 | alter_table : /alter/i /table/i only(?) |
607 | |
608 | only : /only/i |
609 | |
0d51cd9e |
610 | create_table : /create/i TABLE |
0efb6e1b |
611 | |
612 | create_index : /create/i /index/i |
4422e22a |
613 | |
d7fcc1d6 |
614 | default_val : /default/i /(\d+|'[^']*'|\w+\(.*?\))|\w+/ |
4422e22a |
615 | { |
f04713db |
616 | my $val = defined $item[2] ? $item[2] : ''; |
617 | $val =~ s/^'|'$//g; |
0efb6e1b |
618 | $return = { |
82968eb9 |
619 | supertype => 'constraint', |
620 | type => 'default', |
0efb6e1b |
621 | value => $val, |
622 | } |
4422e22a |
623 | } |
f27085c9 |
624 | | /null/i |
625 | { |
626 | $return = { |
627 | supertype => 'constraint', |
628 | type => 'default', |
629 | value => 'NULL', |
630 | } |
631 | } |
4422e22a |
632 | |
4422e22a |
633 | name_with_opt_paren : NAME parens_value_list(s?) |
0efb6e1b |
634 | { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] } |
4422e22a |
635 | |
636 | unique : /unique/i { 1 } |
637 | |
638 | key : /key/i | /index/i |
639 | |
0efb6e1b |
640 | table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')' |
4422e22a |
641 | { |
0efb6e1b |
642 | $return = { type => 'inherits', table_name => $item[3] } |
643 | } |
644 | | |
645 | /with(out)? oids/i |
646 | { |
647 | $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' } |
4422e22a |
648 | } |
649 | |
0d51cd9e |
650 | TABLE : /table/i |
651 | |
0efb6e1b |
652 | SEMICOLON : /\s*;\n?/ |
653 | |
4422e22a |
654 | WORD : /\w+/ |
655 | |
656 | DIGITS : /\d+/ |
657 | |
658 | COMMA : ',' |
659 | |
660 | NAME : "`" /\w+/ "`" |
661 | { $item[2] } |
662 | | /\w+/ |
663 | { $item[1] } |
0012a163 |
664 | | /[\$\w]+/ |
665 | { $item[1] } |
4422e22a |
666 | |
667 | VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ |
668 | { $item[1] } |
669 | | /'.*?'/ # XXX doesn't handle embedded quotes |
670 | { $item[1] } |
f04713db |
671 | | /null/i |
4422e22a |
672 | { 'NULL' } |
673 | |
674 | !; |
675 | |
676 | # ------------------------------------------------------------------- |
677 | sub parse { |
678 | my ( $translator, $data ) = @_; |
679 | $parser ||= Parse::RecDescent->new($GRAMMAR); |
680 | |
681 | $::RD_TRACE = $translator->trace ? 1 : undef; |
682 | $DEBUG = $translator->debug; |
683 | |
684 | unless (defined $parser) { |
685 | return $translator->error("Error instantiating Parse::RecDescent ". |
686 | "instance: Bad grammer"); |
687 | } |
688 | |
689 | my $result = $parser->startrule($data); |
690 | die "Parse failed.\n" unless defined $result; |
691 | warn Dumper($result) if $DEBUG; |
82968eb9 |
692 | |
693 | my $schema = $translator->schema; |
694 | my @tables = sort { |
695 | $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'} |
696 | } keys %{ $result }; |
697 | |
698 | for my $table_name ( @tables ) { |
699 | my $tdata = $result->{ $table_name }; |
700 | my $table = $schema->add_table( |
701 | name => $tdata->{'table_name'}, |
d7fcc1d6 |
702 | ) or die "Couldn't create table '$table_name': " . $schema->error; |
82968eb9 |
703 | |
704 | my @fields = sort { |
b3384294 |
705 | $tdata->{'fields'}->{ $a }->{'order'} |
82968eb9 |
706 | <=> |
b3384294 |
707 | $tdata->{'fields'}->{ $b }->{'order'} |
82968eb9 |
708 | } keys %{ $tdata->{'fields'} }; |
709 | |
710 | for my $fname ( @fields ) { |
711 | my $fdata = $tdata->{'fields'}{ $fname }; |
712 | my $field = $table->add_field( |
713 | name => $fdata->{'name'}, |
714 | data_type => $fdata->{'data_type'}, |
715 | size => $fdata->{'size'}, |
716 | default_value => $fdata->{'default'}, |
7eac5e12 |
717 | is_auto_increment => $fdata->{'is_auto_increment'}, |
82968eb9 |
718 | is_nullable => $fdata->{'null'}, |
719 | ) or die $table->error; |
720 | |
721 | $table->primary_key( $field->name ) if $fdata->{'is_primary_key'}; |
722 | |
723 | for my $cdata ( @{ $fdata->{'constraints'} } ) { |
724 | next unless $cdata->{'type'} eq 'foreign_key'; |
725 | $cdata->{'fields'} ||= [ $field->name ]; |
726 | push @{ $tdata->{'constraints'} }, $cdata; |
727 | } |
728 | } |
729 | |
730 | for my $idata ( @{ $tdata->{'indices'} || [] } ) { |
731 | my $index = $table->add_index( |
732 | name => $idata->{'name'}, |
733 | type => uc $idata->{'type'}, |
734 | fields => $idata->{'fields'}, |
735 | ) or die $table->error; |
736 | } |
737 | |
738 | for my $cdata ( @{ $tdata->{'constraints'} || [] } ) { |
739 | my $constraint = $table->add_constraint( |
740 | name => $cdata->{'name'}, |
741 | type => $cdata->{'type'}, |
742 | fields => $cdata->{'fields'}, |
743 | reference_table => $cdata->{'reference_table'}, |
744 | reference_fields => $cdata->{'reference_fields'}, |
745 | match_type => $cdata->{'match_type'} || '', |
746 | on_delete => $cdata->{'on_delete_do'}, |
747 | on_update => $cdata->{'on_update_do'}, |
b3384294 |
748 | expression => $cdata->{'expression'}, |
749 | ) or die "Can't add constraint of type '" . |
750 | $cdata->{'type'} . "' to table '" . $table->name . |
751 | "': " . $table->error; |
82968eb9 |
752 | } |
753 | } |
754 | |
f62bd16c |
755 | return 1; |
4422e22a |
756 | } |
757 | |
758 | 1; |
759 | |
82968eb9 |
760 | # ------------------------------------------------------------------- |
761 | # Rescue the drowning and tie your shoestrings. |
762 | # Henry David Thoreau |
763 | # ------------------------------------------------------------------- |
4422e22a |
764 | |
765 | =pod |
766 | |
0efb6e1b |
767 | =head1 AUTHORS |
4422e22a |
768 | |
769 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>, |
b8ea6076 |
770 | Allen Day E<lt>allenday@ucla.eduE<gt>. |
4422e22a |
771 | |
772 | =head1 SEE ALSO |
773 | |
774 | perl(1), Parse::RecDescent. |
775 | |
776 | =cut |