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