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