Handles order given as attrib.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / PostgreSQL.pm
CommitLineData
84012a55 1package SQL::Translator::Parser::PostgreSQL;
4422e22a 2
3# -------------------------------------------------------------------
ac397a49 4# $Id: PostgreSQL.pm,v 1.32 2003-11-17 19:09:33 allenday 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 28SQL::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 40The grammar was started from the MySQL parsers. Here is the description
41from PostgreSQL:
42
43Table:
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
77Index:
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 87Alter 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 106View table:
107
108 CREATE [ OR REPLACE ] VIEW view [ ( column name list ) ] AS SELECT query
109
4422e22a 110=cut
111
112use strict;
113use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
ac397a49 114$VERSION = sprintf "%d.%02d", q$Revision: 1.32 $ =~ /(\d+)\.(\d+)/;
4422e22a 115$DEBUG = 0 unless defined $DEBUG;
116
117use Data::Dumper;
118use Parse::RecDescent;
119use Exporter;
120use 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
129my $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
ac397a49 134{ my ( %tables, $table_order, @table_comments) }
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 142startrule : statement(s) eofile { \%tables }
4422e22a 143
0efb6e1b 144eofile : /^\Z/
145
146statement : create
ac397a49 147 | comment_on_table
148 | comment_on_column
0efb6e1b 149 | comment
0012a163 150 | alter
0efb6e1b 151 | grant
152 | revoke
4422e22a 153 | drop
4d2da1f7 154 | insert
0efb6e1b 155 | connect
4d2da1f7 156 | update
0012a163 157 | set
4422e22a 158 | <error>
159
0efb6e1b 160connect : /^\s*\\\connect.*\n/
161
f04713db 162set : /set/i /[^;]*/ ';'
0012a163 163
0d51cd9e 164revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_name /from/i name_with_opt_quotes(s /,/) ';'
0efb6e1b 165 {
166 my $table_name = $item{'table_name'};
167 push @{ $tables{ $table_name }{'permissions'} }, {
168 type => 'revoke',
169 actions => $item[2],
91c75eab 170 users => $item[7],
0efb6e1b 171 }
172 }
173
0d51cd9e 174grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_name /to/i name_with_opt_quotes(s /,/) ';'
0efb6e1b 175 {
176 my $table_name = $item{'table_name'};
177 push @{ $tables{ $table_name }{'permissions'} }, {
178 type => 'grant',
179 actions => $item[2],
91c75eab 180 users => $item[7],
0efb6e1b 181 }
182 }
183
184drop : /drop/i /[^;]*/ ';'
4422e22a 185
4d2da1f7 186insert : /insert/i /[^;]*/ ';'
187
188update : /update/i /[^;]*/ ';'
189
0012a163 190#
191# Create table.
192#
ba1a1626 193create : create_table table_name '(' create_definition(s /,/) ')' table_option(s?) ';'
0efb6e1b 194 {
195 my $table_name = $item{'table_name'};
196 $tables{ $table_name }{'order'} = ++$table_order;
197 $tables{ $table_name }{'table_name'} = $table_name;
198
ac397a49 199 if ( @table_comments ) {
200 $tables{ $table_name }{'comments'} = [ @table_comments ];
201 @table_comments = ();
202 }
203
0efb6e1b 204 my $i = 1;
205 my @constraints;
206 for my $definition ( @{ $item[4] } ) {
b3384294 207 if ( $definition->{'supertype'} eq 'field' ) {
0efb6e1b 208 my $field_name = $definition->{'name'};
209 $tables{ $table_name }{'fields'}{ $field_name } =
210 { %$definition, order => $i };
211 $i++;
4422e22a 212
0138f7bb 213 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
214 $constraint->{'fields'} = [ $field_name ];
7d5bcab8 215 push @{ $tables{ $table_name }{'constraints'} },
0138f7bb 216 $constraint;
0efb6e1b 217 }
218 }
b3384294 219 elsif ( $definition->{'supertype'} eq 'constraint' ) {
220 push @{ $tables{ $table_name }{'constraints'} }, $definition;
0efb6e1b 221 }
b3384294 222 elsif ( $definition->{'supertype'} eq 'index' ) {
0efb6e1b 223 push @{ $tables{ $table_name }{'indices'} }, $definition;
224 }
225 }
226
227 for my $option ( @{ $item[6] } ) {
3022f45b 228 $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } =
0efb6e1b 229 $option;
230 }
231
232 1;
233 }
234
235create : /create/i unique(?) /(index|key)/i index_name /on/i table_name using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
4422e22a 236 {
237 push @{ $tables{ $item{'table_name'} }{'indices'} },
238 {
b3384294 239 name => $item{'index_name'},
240 supertype => $item{'unique'}[0] ? 'constraint' : 'index',
241 type => $item{'unique'}[0] ? 'unique' : 'normal',
242 fields => $item[9],
243 method => $item{'using_method'}[0],
4422e22a 244 }
245 ;
ac397a49 246
4422e22a 247 }
248
0012a163 249#
250# Create anything else (e.g., domain, function, etc.)
251#
252create : /create/i WORD /[^;]+/ ';'
ac397a49 253 { @table_comments = (); }
0012a163 254
0efb6e1b 255using_method : /using/i WORD { $item[2] }
256
257where_predicate : /where/i /[^;]+/
258
259create_definition : field
0efb6e1b 260 | table_constraint
4422e22a 261 | <error>
262
ac397a49 263table_comment : comment
264 {
265 my $comment = $item[1];
266 $return = $comment;
267 push @table_comments, $comment;
268 }
269
4422e22a 270comment : /^\s*(?:#|-{2}).*\n/
271
ac397a49 272comment_on_table : /comment/i /on/i /table/i table_name /is/i comment_phrase ';'
273 {
274 push @{ $tables{ $item{'table_name'} }{'comments'} }, $item{'comment_phrase'};
275 }
276
277comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
278 {
279 my $table_name = $item[4]->{'table'};
280 my $field_name = $item[4]->{'field'};
281 push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} },
282 $item{'comment_phrase'};
283 }
284
285column_name : NAME '.' NAME
286 { $return = { table => $item[1], field => $item[3] } }
287
288comment_phrase : /'.*?'|NULL/
289 {
290 my $val = $item[1];
291 $val =~ s/^'|'$//g;
292 $return = $val;
293 }
294
f2f71b8e 295field : comment(s?) field_name data_type field_meta(s?) comment(s?)
4422e22a 296 {
3022f45b 297 my ( $default, @constraints, $is_pk );
82968eb9 298 my $null = 1;
41fc9cb3 299 for my $meta ( @{ $item[4] } ) {
82968eb9 300 if ( $meta->{'type'} eq 'default' ) {
301 $default = $meta;
302 next;
303 }
304 elsif ( $meta->{'type'} eq 'not_null' ) {
305 $null = 0;
f04713db 306# next;
82968eb9 307 }
308 elsif ( $meta->{'type'} eq 'primary_key' ) {
309 $is_pk = 1;
310 }
4422e22a 311
82968eb9 312 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
313 }
0efb6e1b 314
f2f71b8e 315 my @comments = ( @{ $item[1] }, @{ $item[5] } );
316
0a7fc605 317 $return = {
b3384294 318 supertype => 'field',
7eac5e12 319 name => $item{'field_name'},
320 data_type => $item{'data_type'}{'type'},
321 size => $item{'data_type'}{'size'},
322 null => $null,
323 default => $default->{'value'},
324 constraints => [ @constraints ],
325 comments => [ @comments ],
326 is_primary_key => $is_pk || 0,
327 is_auto_increment => $item{'data_type'}{'is_auto_increment'},
4422e22a 328 }
329 }
330 | <error>
331
0efb6e1b 332field_meta : default_val
82968eb9 333 | column_constraint
4422e22a 334
0efb6e1b 335column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
336 {
337 my $desc = $item{'column_constraint_type'};
338 my $type = $desc->{'type'};
339 my $fields = $desc->{'fields'} || [];
340 my $expression = $desc->{'expression'} || '';
341
342 $return = {
82968eb9 343 supertype => 'constraint',
0efb6e1b 344 name => $item{'constraint_name'}[0] || '',
345 type => $type,
346 expression => $type eq 'check' ? $expression : '',
f04713db 347 deferrable => $item{'deferrable'},
0efb6e1b 348 deferred => $item{'deferred'},
349 reference_table => $desc->{'reference_table'},
350 reference_fields => $desc->{'reference_fields'},
351 match_type => $desc->{'match_type'},
352 on_delete_do => $desc->{'on_delete_do'},
353 on_update_do => $desc->{'on_update_do'},
4422e22a 354 }
355 }
356
0efb6e1b 357constraint_name : /constraint/i name_with_opt_quotes { $item[2] }
358
359column_constraint_type : /not null/i { $return = { type => 'not_null' } }
360 |
f04713db 361 /null/i
0efb6e1b 362 { $return = { type => 'null' } }
363 |
f04713db 364 /unique/i
0efb6e1b 365 { $return = { type => 'unique' } }
366 |
367 /primary key/i
368 { $return = { type => 'primary_key' } }
369 |
370 /check/i '(' /[^)]+/ ')'
b3384294 371 { $return = { type => 'check', expression => $item[3] } }
0efb6e1b 372 |
3022f45b 373 /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
0efb6e1b 374 {
3022f45b 375 my ( $on_delete, $on_update );
376 for my $action ( @{ $item[5] || [] } ) {
377 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
378 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
379 }
380
0efb6e1b 381 $return = {
382 type => 'foreign_key',
383 reference_table => $item[2],
0138f7bb 384 reference_fields => $item[3][0],
0efb6e1b 385 match_type => $item[4][0],
3022f45b 386 on_delete_do => $on_delete,
387 on_update_do => $on_update,
ba1a1626 388 }
4422e22a 389 }
390
0efb6e1b 391table_name : name_with_opt_quotes
4422e22a 392
0efb6e1b 393field_name : name_with_opt_quotes
4422e22a 394
0012a163 395name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] }
4422e22a 396
0efb6e1b 397double_quote: /"/
4422e22a 398
0efb6e1b 399index_name : WORD
4422e22a 400
0efb6e1b 401data_type : pg_data_type parens_value_list(?)
4422e22a 402 {
3022f45b 403 my $data_type = $item[1];
ba1a1626 404
0efb6e1b 405 #
406 # We can deduce some sizes from the data type's name.
407 #
82968eb9 408 $data_type->{'size'} ||= $item[2][0];
4422e22a 409
3022f45b 410 $return = $data_type;
4422e22a 411 }
412
0efb6e1b 413pg_data_type :
50840472 414 /(bigint|int8)/i
3022f45b 415 {
416 $return = {
50840472 417 type => 'integer',
b8ea6076 418 size => 20,
3022f45b 419 };
420 }
0efb6e1b 421 |
1bbd4a2b 422 /(smallint|int2)/i
3022f45b 423 {
424 $return = {
425 type => 'integer',
b8ea6076 426 size => 5,
3022f45b 427 };
428 }
0efb6e1b 429 |
50840472 430 /(integer|int4?)/i # interval must come before this
3022f45b 431 {
432 $return = {
433 type => 'integer',
b8ea6076 434 size => 10,
3022f45b 435 };
436 }
50840472 437 |
438 /(real|float4)/i
439 {
440 $return = {
441 type => 'real',
b8ea6076 442 size => 10,
50840472 443 };
444 }
0efb6e1b 445 |
1bbd4a2b 446 /(double precision|float8?)/i
3022f45b 447 {
448 $return = {
449 type => 'float',
b8ea6076 450 size => 20,
3022f45b 451 };
452 }
0efb6e1b 453 |
50840472 454 /(bigserial|serial8)/i
3022f45b 455 {
50840472 456 $return = {
7eac5e12 457 type => 'integer',
b8ea6076 458 size => 20,
7eac5e12 459 is_auto_increment => 1,
3022f45b 460 };
461 }
0efb6e1b 462 |
1bbd4a2b 463 /serial4?/i
3022f45b 464 {
465 $return = {
7eac5e12 466 type => 'integer',
b8ea6076 467 size => 11,
7eac5e12 468 is_auto_increment => 1,
3022f45b 469 };
470 }
0efb6e1b 471 |
1bbd4a2b 472 /(bit varying|varbit)/i
3022f45b 473 {
474 $return = { type => 'varbit' };
475 }
0efb6e1b 476 |
1bbd4a2b 477 /character varying/i
3022f45b 478 {
479 $return = { type => 'varchar' };
480 }
0efb6e1b 481 |
1bbd4a2b 482 /char(acter)?/i
3022f45b 483 {
484 $return = { type => 'char' };
485 }
0efb6e1b 486 |
1bbd4a2b 487 /bool(ean)?/i
3022f45b 488 {
489 $return = { type => 'boolean' };
490 }
0efb6e1b 491 |
1bbd4a2b 492 /bytea/i
3022f45b 493 {
82968eb9 494 $return = { type => 'bytea' };
3022f45b 495 }
0efb6e1b 496 |
50840472 497 /(timestamptz|timestamp)/i
3022f45b 498 {
499 $return = { type => 'timestamp' };
500 }
0efb6e1b 501 |
38a6a4f9 502 /text/i
503 {
504 $return = {
505 type => 'text',
506 size => 64_000,
507 };
508 }
509 |
510 /(bit|box|cidr|circle|date|inet|interval|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i
3022f45b 511 {
512 $return = { type => $item[1] };
513 }
0efb6e1b 514
4422e22a 515parens_value_list : '(' VALUE(s /,/) ')'
516 { $item[2] }
517
0efb6e1b 518parens_word_list : '(' WORD(s /,/) ')'
519 { $item[2] }
4422e22a 520
0efb6e1b 521field_size : '(' num_range ')' { $item{'num_range'} }
4422e22a 522
0efb6e1b 523num_range : DIGITS ',' DIGITS
4422e22a 524 { $return = $item[1].','.$item[3] }
525 | DIGITS
526 { $return = $item[1] }
527
f2f71b8e 528table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
0efb6e1b 529 {
530 my $desc = $item{'table_constraint_type'};
531 my $type = $desc->{'type'};
532 my $fields = $desc->{'fields'};
533 my $expression = $desc->{'expression'};
f2f71b8e 534 my @comments = ( @{ $item[1] }, @{ $item[-1] } );
0efb6e1b 535
536 $return = {
537 name => $item{'constraint_name'}[0] || '',
b3384294 538 supertype => 'constraint',
539 type => $type,
0efb6e1b 540 fields => $type ne 'check' ? $fields : [],
541 expression => $type eq 'check' ? $expression : '',
f04713db 542 deferrable => $item{'deferrable'},
0efb6e1b 543 deferred => $item{'deferred'},
544 reference_table => $desc->{'reference_table'},
545 reference_fields => $desc->{'reference_fields'},
546 match_type => $desc->{'match_type'}[0],
0012a163 547 on_delete_do => $desc->{'on_delete_do'},
548 on_update_do => $desc->{'on_update_do'},
f2f71b8e 549 comments => [ @comments ],
0efb6e1b 550 }
551 }
4422e22a 552
0efb6e1b 553table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')'
554 {
555 $return = {
556 type => 'primary_key',
557 fields => $item[3],
558 }
559 }
560 |
561 /unique/i '(' name_with_opt_quotes(s /,/) ')'
562 {
563 $return = {
564 type => 'unique',
565 fields => $item[3],
566 }
567 }
568 |
b3384294 569 /check/i '(' /[^)]+/ ')'
0efb6e1b 570 {
571 $return = {
572 type => 'check',
573 expression => $item[3],
574 }
575 }
576 |
3022f45b 577 /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_name parens_word_list(?) match_type(?) key_action(s?)
0efb6e1b 578 {
3022f45b 579 my ( $on_delete, $on_update );
580 for my $action ( @{ $item[9] || [] } ) {
581 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
582 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
583 }
584
0efb6e1b 585 $return = {
b3384294 586 supertype => 'constraint',
0efb6e1b 587 type => 'foreign_key',
588 fields => $item[3],
589 reference_table => $item[6],
590 reference_fields => $item[7][0],
591 match_type => $item[8][0],
3022f45b 592 on_delete_do => $on_delete || '',
593 on_update_do => $on_update || '',
0efb6e1b 594 }
595 }
596
597deferrable : /not/i /deferrable/i
598 {
599 $return = ( $item[1] =~ /not/i ) ? 0 : 1;
600 }
4422e22a 601
0efb6e1b 602deferred : /initially/i /(deferred|immediate)/i { $item[2] }
4422e22a 603
0efb6e1b 604match_type : /match full/i { 'match_full' }
605 |
606 /match partial/i { 'match_partial' }
607
3022f45b 608key_action : key_delete
609 |
610 key_update
0efb6e1b 611
3022f45b 612key_delete : /on delete/i key_mutation
613 {
614 $return => {
615 type => 'delete',
616 action => $item[2],
617 };
618 }
619
620key_update : /on update/i key_mutation
621 {
622 $return => {
623 type => 'update',
624 action => $item[2],
625 };
626 }
627
628key_mutation : /no action/i { $return = 'no_action' }
629 |
630 /restrict/i { $return = 'restrict' }
631 |
632 /cascade/i { $return = 'cascade' }
633 |
38a6a4f9 634 /set null/i { $return = 'set null' }
3022f45b 635 |
38a6a4f9 636 /set default/i { $return = 'set default' }
0efb6e1b 637
0012a163 638alter : alter_table table_name /add/i table_constraint ';'
639 {
640 my $table_name = $item[2];
641 my $constraint = $item[4];
0012a163 642 push @{ $tables{ $table_name }{'constraints'} }, $constraint;
643 }
644
645alter_table : /alter/i /table/i only(?)
646
647only : /only/i
648
0d51cd9e 649create_table : /create/i TABLE
0efb6e1b 650
651create_index : /create/i /index/i
4422e22a 652
d7fcc1d6 653default_val : /default/i /(\d+|'[^']*'|\w+\(.*?\))|\w+/
4422e22a 654 {
f04713db 655 my $val = defined $item[2] ? $item[2] : '';
656 $val =~ s/^'|'$//g;
0efb6e1b 657 $return = {
82968eb9 658 supertype => 'constraint',
659 type => 'default',
0efb6e1b 660 value => $val,
661 }
4422e22a 662 }
f27085c9 663 | /null/i
664 {
665 $return = {
666 supertype => 'constraint',
667 type => 'default',
668 value => 'NULL',
669 }
670 }
4422e22a 671
4422e22a 672name_with_opt_paren : NAME parens_value_list(s?)
0efb6e1b 673 { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
4422e22a 674
675unique : /unique/i { 1 }
676
677key : /key/i | /index/i
678
0efb6e1b 679table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')'
4422e22a 680 {
0efb6e1b 681 $return = { type => 'inherits', table_name => $item[3] }
682 }
683 |
684 /with(out)? oids/i
685 {
686 $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
4422e22a 687 }
688
0d51cd9e 689TABLE : /table/i
690
0efb6e1b 691SEMICOLON : /\s*;\n?/
692
4422e22a 693WORD : /\w+/
694
695DIGITS : /\d+/
696
697COMMA : ','
698
699NAME : "`" /\w+/ "`"
700 { $item[2] }
701 | /\w+/
702 { $item[1] }
0012a163 703 | /[\$\w]+/
704 { $item[1] }
4422e22a 705
706VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
707 { $item[1] }
708 | /'.*?'/ # XXX doesn't handle embedded quotes
709 { $item[1] }
f04713db 710 | /null/i
4422e22a 711 { 'NULL' }
712
713!;
714
715# -------------------------------------------------------------------
716sub parse {
717 my ( $translator, $data ) = @_;
718 $parser ||= Parse::RecDescent->new($GRAMMAR);
719
720 $::RD_TRACE = $translator->trace ? 1 : undef;
721 $DEBUG = $translator->debug;
722
723 unless (defined $parser) {
724 return $translator->error("Error instantiating Parse::RecDescent ".
725 "instance: Bad grammer");
726 }
727
728 my $result = $parser->startrule($data);
729 die "Parse failed.\n" unless defined $result;
730 warn Dumper($result) if $DEBUG;
82968eb9 731
732 my $schema = $translator->schema;
733 my @tables = sort {
734 $result->{ $a }->{'order'} <=> $result->{ $b }->{'order'}
735 } keys %{ $result };
736
737 for my $table_name ( @tables ) {
738 my $tdata = $result->{ $table_name };
739 my $table = $schema->add_table(
740 name => $tdata->{'table_name'},
d7fcc1d6 741 ) or die "Couldn't create table '$table_name': " . $schema->error;
82968eb9 742
743 my @fields = sort {
b3384294 744 $tdata->{'fields'}->{ $a }->{'order'}
82968eb9 745 <=>
b3384294 746 $tdata->{'fields'}->{ $b }->{'order'}
82968eb9 747 } keys %{ $tdata->{'fields'} };
748
749 for my $fname ( @fields ) {
750 my $fdata = $tdata->{'fields'}{ $fname };
751 my $field = $table->add_field(
752 name => $fdata->{'name'},
753 data_type => $fdata->{'data_type'},
754 size => $fdata->{'size'},
755 default_value => $fdata->{'default'},
7eac5e12 756 is_auto_increment => $fdata->{'is_auto_increment'},
82968eb9 757 is_nullable => $fdata->{'null'},
758 ) or die $table->error;
759
760 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
761
762 for my $cdata ( @{ $fdata->{'constraints'} } ) {
763 next unless $cdata->{'type'} eq 'foreign_key';
764 $cdata->{'fields'} ||= [ $field->name ];
765 push @{ $tdata->{'constraints'} }, $cdata;
766 }
767 }
768
769 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
770 my $index = $table->add_index(
771 name => $idata->{'name'},
772 type => uc $idata->{'type'},
773 fields => $idata->{'fields'},
774 ) or die $table->error;
775 }
776
777 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
778 my $constraint = $table->add_constraint(
779 name => $cdata->{'name'},
780 type => $cdata->{'type'},
781 fields => $cdata->{'fields'},
782 reference_table => $cdata->{'reference_table'},
783 reference_fields => $cdata->{'reference_fields'},
784 match_type => $cdata->{'match_type'} || '',
785 on_delete => $cdata->{'on_delete_do'},
786 on_update => $cdata->{'on_update_do'},
b3384294 787 expression => $cdata->{'expression'},
788 ) or die "Can't add constraint of type '" .
789 $cdata->{'type'} . "' to table '" . $table->name .
790 "': " . $table->error;
82968eb9 791 }
792 }
793
f62bd16c 794 return 1;
4422e22a 795}
796
7971;
798
82968eb9 799# -------------------------------------------------------------------
800# Rescue the drowning and tie your shoestrings.
801# Henry David Thoreau
802# -------------------------------------------------------------------
4422e22a 803
804=pod
805
0efb6e1b 806=head1 AUTHORS
4422e22a 807
808Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
b8ea6076 809Allen Day E<lt>allenday@ucla.eduE<gt>.
4422e22a 810
811=head1 SEE ALSO
812
813perl(1), Parse::RecDescent.
814
815=cut