Commit | Line | Data |
84012a55 |
1 | package SQL::Translator::Parser::PostgreSQL; |
4422e22a |
2 | |
3 | # ------------------------------------------------------------------- |
90075866 |
4 | # $Id: PostgreSQL.pm,v 1.37 2004-02-09 22:23:40 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 ]; |
90075866 |
111 | $VERSION = sprintf "%d.%02d", q$Revision: 1.37 $ =~ /(\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 | |
ac397a49 |
258 | table_comment : comment |
259 | { |
260 | my $comment = $item[1]; |
261 | $return = $comment; |
262 | push @table_comments, $comment; |
263 | } |
264 | |
4422e22a |
265 | comment : /^\s*(?:#|-{2}).*\n/ |
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 | |
f2f71b8e |
290 | field : comment(s?) field_name data_type field_meta(s?) 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 | |
0efb6e1b |
326 | field_meta : default_val |
82968eb9 |
327 | | column_constraint |
4422e22a |
328 | |
0efb6e1b |
329 | column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?) |
330 | { |
331 | my $desc = $item{'column_constraint_type'}; |
332 | my $type = $desc->{'type'}; |
333 | my $fields = $desc->{'fields'} || []; |
334 | my $expression = $desc->{'expression'} || ''; |
335 | |
336 | $return = { |
82968eb9 |
337 | supertype => 'constraint', |
0efb6e1b |
338 | name => $item{'constraint_name'}[0] || '', |
339 | type => $type, |
340 | expression => $type eq 'check' ? $expression : '', |
f04713db |
341 | deferrable => $item{'deferrable'}, |
0efb6e1b |
342 | deferred => $item{'deferred'}, |
343 | reference_table => $desc->{'reference_table'}, |
344 | reference_fields => $desc->{'reference_fields'}, |
345 | match_type => $desc->{'match_type'}, |
346 | on_delete_do => $desc->{'on_delete_do'}, |
347 | on_update_do => $desc->{'on_update_do'}, |
4422e22a |
348 | } |
349 | } |
350 | |
0efb6e1b |
351 | constraint_name : /constraint/i name_with_opt_quotes { $item[2] } |
352 | |
353 | column_constraint_type : /not null/i { $return = { type => 'not_null' } } |
354 | | |
f04713db |
355 | /null/i |
0efb6e1b |
356 | { $return = { type => 'null' } } |
357 | | |
f04713db |
358 | /unique/i |
0efb6e1b |
359 | { $return = { type => 'unique' } } |
360 | | |
361 | /primary key/i |
362 | { $return = { type => 'primary_key' } } |
363 | | |
364 | /check/i '(' /[^)]+/ ')' |
b3384294 |
365 | { $return = { type => 'check', expression => $item[3] } } |
0efb6e1b |
366 | | |
3022f45b |
367 | /references/i table_name parens_word_list(?) match_type(?) key_action(s?) |
0efb6e1b |
368 | { |
3022f45b |
369 | my ( $on_delete, $on_update ); |
370 | for my $action ( @{ $item[5] || [] } ) { |
371 | $on_delete = $action->{'action'} if $action->{'type'} eq 'delete'; |
372 | $on_update = $action->{'action'} if $action->{'type'} eq 'update'; |
373 | } |
374 | |
0efb6e1b |
375 | $return = { |
376 | type => 'foreign_key', |
377 | reference_table => $item[2], |
0138f7bb |
378 | reference_fields => $item[3][0], |
0efb6e1b |
379 | match_type => $item[4][0], |
3022f45b |
380 | on_delete_do => $on_delete, |
381 | on_update_do => $on_update, |
ba1a1626 |
382 | } |
4422e22a |
383 | } |
384 | |
0efb6e1b |
385 | table_name : name_with_opt_quotes |
4422e22a |
386 | |
0efb6e1b |
387 | field_name : name_with_opt_quotes |
4422e22a |
388 | |
0012a163 |
389 | name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] } |
4422e22a |
390 | |
0efb6e1b |
391 | double_quote: /"/ |
4422e22a |
392 | |
0efb6e1b |
393 | index_name : WORD |
4422e22a |
394 | |
0efb6e1b |
395 | data_type : pg_data_type parens_value_list(?) |
4422e22a |
396 | { |
3022f45b |
397 | my $data_type = $item[1]; |
ba1a1626 |
398 | |
0efb6e1b |
399 | # |
400 | # We can deduce some sizes from the data type's name. |
401 | # |
82968eb9 |
402 | $data_type->{'size'} ||= $item[2][0]; |
4422e22a |
403 | |
3022f45b |
404 | $return = $data_type; |
4422e22a |
405 | } |
406 | |
0efb6e1b |
407 | pg_data_type : |
50840472 |
408 | /(bigint|int8)/i |
3022f45b |
409 | { |
410 | $return = { |
50840472 |
411 | type => 'integer', |
b8ea6076 |
412 | size => 20, |
3022f45b |
413 | }; |
414 | } |
0efb6e1b |
415 | | |
1bbd4a2b |
416 | /(smallint|int2)/i |
3022f45b |
417 | { |
418 | $return = { |
419 | type => 'integer', |
b8ea6076 |
420 | size => 5, |
3022f45b |
421 | }; |
422 | } |
0efb6e1b |
423 | | |
50840472 |
424 | /(integer|int4?)/i # interval must come before this |
3022f45b |
425 | { |
426 | $return = { |
427 | type => 'integer', |
b8ea6076 |
428 | size => 10, |
3022f45b |
429 | }; |
430 | } |
50840472 |
431 | | |
432 | /(real|float4)/i |
433 | { |
434 | $return = { |
435 | type => 'real', |
b8ea6076 |
436 | size => 10, |
50840472 |
437 | }; |
438 | } |
0efb6e1b |
439 | | |
1bbd4a2b |
440 | /(double precision|float8?)/i |
3022f45b |
441 | { |
442 | $return = { |
443 | type => 'float', |
b8ea6076 |
444 | size => 20, |
3022f45b |
445 | }; |
446 | } |
0efb6e1b |
447 | | |
50840472 |
448 | /(bigserial|serial8)/i |
3022f45b |
449 | { |
50840472 |
450 | $return = { |
7eac5e12 |
451 | type => 'integer', |
b8ea6076 |
452 | size => 20, |
7eac5e12 |
453 | is_auto_increment => 1, |
3022f45b |
454 | }; |
455 | } |
0efb6e1b |
456 | | |
1bbd4a2b |
457 | /serial4?/i |
3022f45b |
458 | { |
459 | $return = { |
7eac5e12 |
460 | type => 'integer', |
b8ea6076 |
461 | size => 11, |
7eac5e12 |
462 | is_auto_increment => 1, |
3022f45b |
463 | }; |
464 | } |
0efb6e1b |
465 | | |
1bbd4a2b |
466 | /(bit varying|varbit)/i |
3022f45b |
467 | { |
468 | $return = { type => 'varbit' }; |
469 | } |
0efb6e1b |
470 | | |
1bbd4a2b |
471 | /character varying/i |
3022f45b |
472 | { |
473 | $return = { type => 'varchar' }; |
474 | } |
0efb6e1b |
475 | | |
1bbd4a2b |
476 | /char(acter)?/i |
3022f45b |
477 | { |
478 | $return = { type => 'char' }; |
479 | } |
0efb6e1b |
480 | | |
1bbd4a2b |
481 | /bool(ean)?/i |
3022f45b |
482 | { |
483 | $return = { type => 'boolean' }; |
484 | } |
0efb6e1b |
485 | | |
1bbd4a2b |
486 | /bytea/i |
3022f45b |
487 | { |
82968eb9 |
488 | $return = { type => 'bytea' }; |
3022f45b |
489 | } |
0efb6e1b |
490 | | |
50840472 |
491 | /(timestamptz|timestamp)/i |
3022f45b |
492 | { |
493 | $return = { type => 'timestamp' }; |
494 | } |
0efb6e1b |
495 | | |
38a6a4f9 |
496 | /text/i |
497 | { |
498 | $return = { |
499 | type => 'text', |
500 | size => 64_000, |
501 | }; |
502 | } |
503 | | |
504 | /(bit|box|cidr|circle|date|inet|interval|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i |
3022f45b |
505 | { |
506 | $return = { type => $item[1] }; |
507 | } |
0efb6e1b |
508 | |
4422e22a |
509 | parens_value_list : '(' VALUE(s /,/) ')' |
510 | { $item[2] } |
511 | |
0efb6e1b |
512 | parens_word_list : '(' WORD(s /,/) ')' |
513 | { $item[2] } |
4422e22a |
514 | |
0efb6e1b |
515 | field_size : '(' num_range ')' { $item{'num_range'} } |
4422e22a |
516 | |
0efb6e1b |
517 | num_range : DIGITS ',' DIGITS |
4422e22a |
518 | { $return = $item[1].','.$item[3] } |
519 | | DIGITS |
520 | { $return = $item[1] } |
521 | |
f2f71b8e |
522 | table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?) |
0efb6e1b |
523 | { |
524 | my $desc = $item{'table_constraint_type'}; |
525 | my $type = $desc->{'type'}; |
526 | my $fields = $desc->{'fields'}; |
527 | my $expression = $desc->{'expression'}; |
f2f71b8e |
528 | my @comments = ( @{ $item[1] }, @{ $item[-1] } ); |
0efb6e1b |
529 | |
530 | $return = { |
531 | name => $item{'constraint_name'}[0] || '', |
b3384294 |
532 | supertype => 'constraint', |
533 | type => $type, |
0efb6e1b |
534 | fields => $type ne 'check' ? $fields : [], |
535 | expression => $type eq 'check' ? $expression : '', |
f04713db |
536 | deferrable => $item{'deferrable'}, |
0efb6e1b |
537 | deferred => $item{'deferred'}, |
538 | reference_table => $desc->{'reference_table'}, |
539 | reference_fields => $desc->{'reference_fields'}, |
540 | match_type => $desc->{'match_type'}[0], |
0012a163 |
541 | on_delete_do => $desc->{'on_delete_do'}, |
542 | on_update_do => $desc->{'on_update_do'}, |
f2f71b8e |
543 | comments => [ @comments ], |
0efb6e1b |
544 | } |
545 | } |
4422e22a |
546 | |
0efb6e1b |
547 | table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')' |
548 | { |
549 | $return = { |
550 | type => 'primary_key', |
551 | fields => $item[3], |
552 | } |
553 | } |
554 | | |
555 | /unique/i '(' name_with_opt_quotes(s /,/) ')' |
556 | { |
557 | $return = { |
558 | type => 'unique', |
559 | fields => $item[3], |
560 | } |
561 | } |
562 | | |
b3384294 |
563 | /check/i '(' /[^)]+/ ')' |
0efb6e1b |
564 | { |
565 | $return = { |
566 | type => 'check', |
567 | expression => $item[3], |
568 | } |
569 | } |
570 | | |
3022f45b |
571 | /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_name parens_word_list(?) match_type(?) key_action(s?) |
0efb6e1b |
572 | { |
3022f45b |
573 | my ( $on_delete, $on_update ); |
574 | for my $action ( @{ $item[9] || [] } ) { |
575 | $on_delete = $action->{'action'} if $action->{'type'} eq 'delete'; |
576 | $on_update = $action->{'action'} if $action->{'type'} eq 'update'; |
577 | } |
578 | |
0efb6e1b |
579 | $return = { |
b3384294 |
580 | supertype => 'constraint', |
0efb6e1b |
581 | type => 'foreign_key', |
582 | fields => $item[3], |
583 | reference_table => $item[6], |
584 | reference_fields => $item[7][0], |
585 | match_type => $item[8][0], |
3022f45b |
586 | on_delete_do => $on_delete || '', |
587 | on_update_do => $on_update || '', |
0efb6e1b |
588 | } |
589 | } |
590 | |
591 | deferrable : /not/i /deferrable/i |
592 | { |
593 | $return = ( $item[1] =~ /not/i ) ? 0 : 1; |
594 | } |
4422e22a |
595 | |
0efb6e1b |
596 | deferred : /initially/i /(deferred|immediate)/i { $item[2] } |
4422e22a |
597 | |
0efb6e1b |
598 | match_type : /match full/i { 'match_full' } |
599 | | |
600 | /match partial/i { 'match_partial' } |
601 | |
3022f45b |
602 | key_action : key_delete |
603 | | |
604 | key_update |
0efb6e1b |
605 | |
3022f45b |
606 | key_delete : /on delete/i key_mutation |
607 | { |
8c12c406 |
608 | $return = { |
3022f45b |
609 | type => 'delete', |
610 | action => $item[2], |
611 | }; |
612 | } |
613 | |
614 | key_update : /on update/i key_mutation |
615 | { |
8c12c406 |
616 | $return = { |
3022f45b |
617 | type => 'update', |
618 | action => $item[2], |
619 | }; |
620 | } |
621 | |
622 | key_mutation : /no action/i { $return = 'no_action' } |
623 | | |
624 | /restrict/i { $return = 'restrict' } |
625 | | |
626 | /cascade/i { $return = 'cascade' } |
627 | | |
38a6a4f9 |
628 | /set null/i { $return = 'set null' } |
3022f45b |
629 | | |
38a6a4f9 |
630 | /set default/i { $return = 'set default' } |
0efb6e1b |
631 | |
00ef67ea |
632 | alter : alter_table table_name add_column field ';' |
633 | { |
634 | my $field_def = $item[4]; |
635 | $tables{ $item[2] }{'fields'}{ $field_def->{'name'} } = { |
636 | %$field_def, order => $field_order++ |
637 | }; |
638 | 1; |
639 | } |
640 | |
641 | alter : alter_table table_name ADD table_constraint ';' |
0012a163 |
642 | { |
643 | my $table_name = $item[2]; |
644 | my $constraint = $item[4]; |
0012a163 |
645 | push @{ $tables{ $table_name }{'constraints'} }, $constraint; |
00ef67ea |
646 | 1; |
0012a163 |
647 | } |
648 | |
00ef67ea |
649 | alter : alter_table table_name drop_column NAME restrict_or_cascade(?) ';' |
650 | { |
651 | $tables{ $item[2] }{'fields'}{ $item[4] }{'drop'} = 1; |
652 | 1; |
653 | } |
0012a163 |
654 | |
00ef67ea |
655 | alter : alter_table table_name alter_column NAME alter_default_val ';' |
656 | { |
657 | $tables{ $item[2] }{'fields'}{ $item[4] }{'default'} = |
658 | $item[5]->{'value'}; |
659 | 1; |
660 | } |
661 | |
662 | # |
663 | # These will just parse for now but won't affect the structure. - ky |
664 | # |
665 | alter : alter_table table_name /rename/i /to/i NAME ';' |
666 | { 1 } |
667 | |
668 | alter : alter_table table_name alter_column NAME SET /statistics/i INTEGER ';' |
669 | { 1 } |
670 | |
671 | alter : alter_table table_name alter_column NAME SET /storage/i storage_type ';' |
672 | { 1 } |
673 | |
674 | alter : alter_table table_name rename_column NAME /to/i NAME ';' |
675 | { 1 } |
676 | |
677 | alter : alter_table table_name DROP /constraint/i NAME restrict_or_cascade ';' |
678 | { 1 } |
679 | |
680 | alter : alter_table table_name /owner/i /to/i NAME ';' |
681 | { 1 } |
682 | |
683 | storage_type : /(plain|external|extended|main)/i |
684 | |
685 | alter_default_val : SET default_val |
686 | { |
687 | $return = { value => $item[2]->{'value'} } |
688 | } |
689 | | DROP DEFAULT |
690 | { |
691 | $return = { value => undef } |
692 | } |
693 | |
694 | # |
695 | # This is a little tricky to get right, at least WRT to making the |
696 | # tests pass. The problem is that the constraints are stored just as |
697 | # a list (no name access), and the tests expect the constraints in a |
698 | # particular order. I'm going to leave the rule but disable the code |
699 | # for now. - ky |
700 | # |
701 | alter : alter_table table_name alter_column NAME alter_nullable ';' |
702 | { |
703 | # my $table_name = $item[2]; |
704 | # my $field_name = $item[4]; |
705 | # my $is_nullable = $item[5]->{'is_nullable'}; |
706 | # |
707 | # $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} = |
708 | # $is_nullable; |
709 | # |
710 | # if ( $is_nullable ) { |
711 | # 1; |
712 | # push @{ $tables{ $table_name }{'constraints'} }, { |
713 | # type => 'not_null', |
714 | # fields => [ $field_name ], |
715 | # }; |
716 | # } |
717 | # else { |
718 | # for my $i ( |
719 | # 0 .. $#{ $tables{ $table_name }{'constraints'} || [] } |
720 | # ) { |
721 | # my $c = $tables{ $table_name }{'constraints'}[ $i ] or next; |
722 | # my $fields = join( '', @{ $c->{'fields'} || [] } ) or next; |
723 | # if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) { |
724 | # delete $tables{ $table_name }{'constraints'}[ $i ]; |
725 | # last; |
726 | # } |
727 | # } |
728 | # } |
729 | |
730 | 1; |
731 | } |
732 | |
733 | alter_nullable : SET not_null |
734 | { |
735 | $return = { is_nullable => 0 } |
736 | } |
737 | | DROP not_null |
738 | { |
739 | $return = { is_nullable => 1 } |
740 | } |
741 | |
742 | not_null : /not/i /null/i |
743 | |
744 | add_column : ADD COLUMN(?) |
745 | |
746 | alter_table : ALTER TABLE ONLY(?) |
747 | |
748 | drop_column : DROP COLUMN(?) |
749 | |
750 | alter_column : ALTER COLUMN(?) |
751 | |
752 | rename_column : /rename/i COLUMN(?) |
753 | |
754 | restrict_or_cascade : /restrict/i | |
755 | /cascade/i |
756 | |
757 | # |
758 | # End basically useless stuff. - ky |
759 | # |
0012a163 |
760 | |
211e2e90 |
761 | create_table : CREATE TABLE |
0efb6e1b |
762 | |
211e2e90 |
763 | create_index : CREATE /index/i |
4422e22a |
764 | |
00ef67ea |
765 | default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*?\))|\w+/ |
4422e22a |
766 | { |
f04713db |
767 | my $val = defined $item[2] ? $item[2] : ''; |
768 | $val =~ s/^'|'$//g; |
0efb6e1b |
769 | $return = { |
82968eb9 |
770 | supertype => 'constraint', |
771 | type => 'default', |
0efb6e1b |
772 | value => $val, |
773 | } |
4422e22a |
774 | } |
f27085c9 |
775 | | /null/i |
776 | { |
777 | $return = { |
778 | supertype => 'constraint', |
779 | type => 'default', |
780 | value => 'NULL', |
781 | } |
782 | } |
4422e22a |
783 | |
4422e22a |
784 | name_with_opt_paren : NAME parens_value_list(s?) |
0efb6e1b |
785 | { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] } |
4422e22a |
786 | |
787 | unique : /unique/i { 1 } |
788 | |
789 | key : /key/i | /index/i |
790 | |
0efb6e1b |
791 | table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')' |
4422e22a |
792 | { |
0efb6e1b |
793 | $return = { type => 'inherits', table_name => $item[3] } |
794 | } |
795 | | |
796 | /with(out)? oids/i |
797 | { |
798 | $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' } |
4422e22a |
799 | } |
800 | |
00ef67ea |
801 | ADD : /add/i |
802 | |
803 | ALTER : /alter/i |
804 | |
211e2e90 |
805 | CREATE : /create/i |
806 | |
00ef67ea |
807 | ONLY : /only/i |
808 | |
809 | DEFAULT : /default/i |
810 | |
811 | DROP : /drop/i |
812 | |
813 | COLUMN : /column/i |
814 | |
0d51cd9e |
815 | TABLE : /table/i |
816 | |
0efb6e1b |
817 | SEMICOLON : /\s*;\n?/ |
818 | |
00ef67ea |
819 | INTEGER : /\d+/ |
820 | |
4422e22a |
821 | WORD : /\w+/ |
822 | |
823 | DIGITS : /\d+/ |
824 | |
825 | COMMA : ',' |
826 | |
00ef67ea |
827 | SET : /set/i |
828 | |
4422e22a |
829 | NAME : "`" /\w+/ "`" |
830 | { $item[2] } |
831 | | /\w+/ |
832 | { $item[1] } |
0012a163 |
833 | | /[\$\w]+/ |
834 | { $item[1] } |
4422e22a |
835 | |
836 | VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ |
837 | { $item[1] } |
838 | | /'.*?'/ # XXX doesn't handle embedded quotes |
839 | { $item[1] } |
f04713db |
840 | | /null/i |
4422e22a |
841 | { 'NULL' } |
842 | |
843 | !; |
844 | |
845 | # ------------------------------------------------------------------- |
846 | sub parse { |
847 | my ( $translator, $data ) = @_; |
848 | $parser ||= Parse::RecDescent->new($GRAMMAR); |
849 | |
850 | $::RD_TRACE = $translator->trace ? 1 : undef; |
851 | $DEBUG = $translator->debug; |
852 | |
853 | unless (defined $parser) { |
854 | return $translator->error("Error instantiating Parse::RecDescent ". |
855 | "instance: Bad grammer"); |
856 | } |
857 | |
858 | my $result = $parser->startrule($data); |
859 | die "Parse failed.\n" unless defined $result; |
860 | warn Dumper($result) if $DEBUG; |
82968eb9 |
861 | |
862 | my $schema = $translator->schema; |
863 | my @tables = sort { |
864 | $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'} |
865 | } keys %{ $result }; |
866 | |
867 | for my $table_name ( @tables ) { |
868 | my $tdata = $result->{ $table_name }; |
869 | my $table = $schema->add_table( |
870 | name => $tdata->{'table_name'}, |
d7fcc1d6 |
871 | ) or die "Couldn't create table '$table_name': " . $schema->error; |
82968eb9 |
872 | |
873 | my @fields = sort { |
b3384294 |
874 | $tdata->{'fields'}->{ $a }->{'order'} |
82968eb9 |
875 | <=> |
b3384294 |
876 | $tdata->{'fields'}->{ $b }->{'order'} |
82968eb9 |
877 | } keys %{ $tdata->{'fields'} }; |
878 | |
879 | for my $fname ( @fields ) { |
880 | my $fdata = $tdata->{'fields'}{ $fname }; |
00ef67ea |
881 | next if $fdata->{'drop'}; |
82968eb9 |
882 | my $field = $table->add_field( |
883 | name => $fdata->{'name'}, |
884 | data_type => $fdata->{'data_type'}, |
885 | size => $fdata->{'size'}, |
886 | default_value => $fdata->{'default'}, |
7eac5e12 |
887 | is_auto_increment => $fdata->{'is_auto_increment'}, |
00ef67ea |
888 | is_nullable => $fdata->{'is_nullable'}, |
82968eb9 |
889 | ) or die $table->error; |
890 | |
891 | $table->primary_key( $field->name ) if $fdata->{'is_primary_key'}; |
892 | |
893 | for my $cdata ( @{ $fdata->{'constraints'} } ) { |
894 | next unless $cdata->{'type'} eq 'foreign_key'; |
895 | $cdata->{'fields'} ||= [ $field->name ]; |
896 | push @{ $tdata->{'constraints'} }, $cdata; |
897 | } |
898 | } |
899 | |
900 | for my $idata ( @{ $tdata->{'indices'} || [] } ) { |
901 | my $index = $table->add_index( |
902 | name => $idata->{'name'}, |
903 | type => uc $idata->{'type'}, |
904 | fields => $idata->{'fields'}, |
905 | ) or die $table->error; |
906 | } |
907 | |
908 | for my $cdata ( @{ $tdata->{'constraints'} || [] } ) { |
909 | my $constraint = $table->add_constraint( |
910 | name => $cdata->{'name'}, |
911 | type => $cdata->{'type'}, |
912 | fields => $cdata->{'fields'}, |
913 | reference_table => $cdata->{'reference_table'}, |
914 | reference_fields => $cdata->{'reference_fields'}, |
915 | match_type => $cdata->{'match_type'} || '', |
916 | on_delete => $cdata->{'on_delete_do'}, |
917 | on_update => $cdata->{'on_update_do'}, |
b3384294 |
918 | expression => $cdata->{'expression'}, |
919 | ) or die "Can't add constraint of type '" . |
920 | $cdata->{'type'} . "' to table '" . $table->name . |
921 | "': " . $table->error; |
82968eb9 |
922 | } |
923 | } |
924 | |
f62bd16c |
925 | return 1; |
4422e22a |
926 | } |
927 | |
928 | 1; |
929 | |
82968eb9 |
930 | # ------------------------------------------------------------------- |
931 | # Rescue the drowning and tie your shoestrings. |
932 | # Henry David Thoreau |
933 | # ------------------------------------------------------------------- |
4422e22a |
934 | |
935 | =pod |
936 | |
0efb6e1b |
937 | =head1 AUTHORS |
4422e22a |
938 | |
939 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>, |
b8ea6076 |
940 | Allen Day E<lt>allenday@ucla.eduE<gt>. |
4422e22a |
941 | |
942 | =head1 SEE ALSO |
943 | |
944 | perl(1), Parse::RecDescent. |
945 | |
946 | =cut |