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