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