coerce from Column from Str
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Grammar / PostgreSQL.pm
1 use MooseX::Declare;
2 role SQL::Translator::Grammar::PostgreSQL {
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 # 02111-1307  USA
19 # -------------------------------------------------------------------
20
21     method _build_grammar {
22         return q!
23         { my ( %tables, @views, $table_order, $field_order, @table_comments) }
24     
25         #
26         # The "eofile" rule makes the parser fail if any "statement" rule
27         # fails.  Otherwise, the first successful match by a "statement" 
28         # won't cause the failure needed to know that the parse, as a whole,
29         # failed. -ky
30         #
31         startrule : statement(s) eofile { { tables => \%tables, views => \@views } }
32     
33         eofile : /^\Z/
34            
35     
36         statement : create
37           | comment_on_table
38           | comment_on_column
39           | comment_on_other
40           | comment
41           | alter
42           | grant
43           | revoke
44           | drop
45           | insert
46           | connect
47           | update
48           | set
49           | select
50           | copy
51           | readin_symbol
52           | <error>
53     
54         connect : /^\s*\\\connect.*\n/
55     
56         set : /set/i /[^;]*/ ';'
57     
58         revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_id /from/i name_with_opt_quotes(s /,/) ';'
59             {
60             my $table_info  = $item{'table_id'};
61             my $schema_name = $table_info->{'schema_name'};
62             my $table_name  = $table_info->{'table_name'};
63             push @{ $tables{ $table_name }{'permissions'} }, {
64                 type       => 'revoke',
65                 actions    => $item[2],
66                 users      => $item[7],
67             }
68             }
69     
70         revoke : /revoke/i WORD(s /,/) /on/i SCHEMA(?) schema_name /from/i name_with_opt_quotes(s /,/) ';'
71             { 1 }
72     
73         grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_id /to/i name_with_opt_quotes(s /,/) ';'
74             {
75             my $table_info  = $item{'table_id'};
76             my $schema_name = $table_info->{'schema_name'};
77             my $table_name  = $table_info->{'table_name'};
78             push @{ $tables{ $table_name }{'permissions'} }, {
79                 type       => 'grant',
80                 actions    => $item[2],
81                 users      => $item[7],
82             }
83             }
84     
85         grant : /grant/i WORD(s /,/) /on/i SCHEMA(?) schema_name /to/i name_with_opt_quotes(s /,/) ';'
86             { 1 }
87     
88         drop : /drop/i /[^;]*/ ';'
89     
90         string :
91            /'(\\.|''|[^\\\'])*'/ 
92     
93         nonstring : /[^;\'"]+/
94     
95         statement_body : string | nonstring
96     
97         insert : /insert/i statement_body(s?) ';'
98     
99         update : /update/i statement_body(s?) ';'
100     
101         #
102         # Create table.
103         #
104         create : CREATE temporary(?) TABLE table_id '(' create_definition(s? /,/) ')' table_option(s?) ';'
105             {
106             my $table_info  = $item{'table_id'};
107             my $schema_name = $table_info->{'schema_name'};
108             my $table_name  = $table_info->{'table_name'};
109             $tables{ $table_name }{'order'}       = ++$table_order;
110             $tables{ $table_name }{'schema_name'} = $schema_name;
111             $tables{ $table_name }{'table_name'}  = $table_name;
112     
113             $tables{ $table_name }{'temporary'} = $item[2][0];
114     
115             if ( @table_comments ) {
116                 $tables{ $table_name }{'comments'} = [ @table_comments ];
117                 @table_comments = ();
118             }
119     
120             my @constraints;
121             for my $definition ( @{ $item[6] } ) {
122                 if ( $definition->{'supertype'} eq 'field' ) {
123                 my $field_name = $definition->{'name'};
124                 $tables{ $table_name }{'fields'}{ $field_name } = 
125                     { %$definition, order => $field_order++ };
126                         
127                 for my $constraint ( @{ $definition->{'constraints'} || [] } ) {
128                     $constraint->{'fields'} = [ $field_name ];
129                     push @{ $tables{ $table_name }{'constraints'} },
130                     $constraint;
131                 }
132                 }
133                 elsif ( $definition->{'supertype'} eq 'constraint' ) {
134                 push @{ $tables{ $table_name }{'constraints'} }, $definition;
135                 }
136                 elsif ( $definition->{'supertype'} eq 'index' ) {
137                 push @{ $tables{ $table_name }{'indices'} }, $definition;
138                 }
139             }
140     
141             for my $option ( @{ $item[8] } ) {
142                 $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } = 
143                 $option;
144             }
145     
146             1;
147             }
148     
149         create : CREATE unique(?) /(index|key)/i index_name /on/i table_id using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';'
150             {
151             my $table_info  = $item{'table_id'};
152             my $schema_name = $table_info->{'schema_name'};
153             my $table_name  = $table_info->{'table_name'};
154             push @{ $tables{ $table_name }{'indices'} },
155                 {
156                 name      => $item{'index_name'},
157                 supertype => $item{'unique'}[0] ? 'constraint' : 'index',
158                 type      => $item{'unique'}[0] ? 'unique'     : 'normal',
159                 fields    => $item[9],
160                 method    => $item{'using_method'}[0],
161                 }
162             ;
163             }
164     
165         create : CREATE or_replace(?) temporary(?) VIEW view_id view_fields(?) /AS/i view_target ';'
166             {
167             push @views, {
168                 schema_name  => $item{view_id}{schema_name},
169                 view_name    => $item{view_id}{view_name},
170                 sql          => $item{view_target},
171                 fields       => $item[6],
172                 is_temporary => $item[3][0],
173             }
174             }
175     
176         #
177         # Create anything else (e.g., domain, etc.)
178         #
179         create : CREATE WORD /[^;]+/ ';'
180             { @table_comments = (); }
181     
182         using_method : /using/i WORD { $item[2] }
183     
184         where_predicate : /where/i /[^;]+/
185     
186         create_definition : field
187             | table_constraint
188             | <error>
189     
190         comment : /^\s*(?:#|-{2})(.*)\n/ 
191             { 
192             my $comment =  $item[1];
193             $comment    =~ s/^\s*(#|-*)\s*//;
194             $comment    =~ s/\s*$//;
195             $return     = $comment;
196             push @table_comments, $comment;
197             }
198     
199         comment_on_table : /comment/i /on/i /table/i table_id /is/i comment_phrase ';'
200             {
201             my $table_info  = $item{'table_id'};
202             my $schema_name = $table_info->{'schema_name'};
203             my $table_name  = $table_info->{'table_name'};
204             push @{ $tables{ $table_name }{'comments'} }, $item{'comment_phrase'};
205             }
206     
207         comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';'
208             {
209             my $table_name = $item[4]->{'table'};
210             my $field_name = $item[4]->{'field'};
211             if ($tables{ $table_name }{'fields'}{ $field_name } ) {
212               push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} }, 
213                   $item{'comment_phrase'};
214             }
215             else {
216                die "No such column as $table_name.$field_name";
217             }
218             }
219     
220         comment_on_other : /comment/i /on/i /\w+/ /\w+/ /is/i comment_phrase ';'
221             {
222             push(@table_comments, $item{'comment_phrase'});
223             }
224     
225         # [added by cjm 20041019]
226         # [TODO: other comment-on types]
227         # for now we just have a general mechanism for handling other
228         # kinds of comments than table/column; I'm not sure of the best
229         # way to incorporate these into the datamodel
230         #
231         # this is the exhaustive list of types of comment:
232         #COMMENT ON DATABASE my_database IS 'Development Database';
233         #COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee id';
234         #COMMENT ON RULE my_rule IS 'Logs UPDATES of employee records';
235         #COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys';
236         #COMMENT ON TABLE my_table IS 'Employee Information';
237         #COMMENT ON TYPE my_type IS 'Complex Number support';
238         #COMMENT ON VIEW my_view IS 'View of departmental costs';
239         #COMMENT ON COLUMN my_table.my_field IS 'Employee ID number';
240         #COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.';
241         #
242         # this is tested by test 08
243     
244         column_name : NAME '.' NAME
245             { $return = { table => $item[1], field => $item[3] } }
246     
247         comment_phrase : /null/i
248             { $return = 'NULL' }
249     
250         comment_phrase : /'/ comment_phrase_unquoted(s) /'/
251             { my $phrase = join(' ', @{ $item[2] });
252               $return = $phrase}
253     
254         # [cjm TODO: double-single quotes in a comment_phrase]
255         comment_phrase_unquoted : /[^\']*/
256             { $return = $item[1] }
257     
258     
259         xxxcomment_phrase : /'.*?'|NULL/ 
260             { 
261             my $val = $item[1] || '';
262             $val =~ s/^'|'$//g;
263             $return = $val;
264             }
265     
266         field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?)
267             {
268             my ( $default, @constraints, $is_pk );
269             my $is_nullable = 1;
270             for my $meta ( @{ $item[4] } ) {
271                 if ( $meta->{'type'} eq 'default' ) {
272                 $default = $meta;
273                 next;
274                 }
275                 elsif ( $meta->{'type'} eq 'not_null' ) {
276                 $is_nullable = 0;
277                 }
278                 elsif ( $meta->{'type'} eq 'primary_key' ) {
279                 $is_pk = 1;
280                 }
281     
282                 push @constraints, $meta if $meta->{'supertype'} eq 'constraint';
283             }
284     
285             my @comments = ( @{ $item[1] }, @{ $item[5] } );
286     
287             $return = {
288                 supertype         => 'field',
289                 name              => $item{'field_name'}, 
290                 data_type         => $item{'data_type'}{'type'},
291                 size              => $item{'data_type'}{'size'},
292                 is_nullable       => $is_nullable,
293                 default           => $default->{'value'},
294                 constraints       => [ @constraints ],
295                 comments          => [ @comments ],
296                 is_primary_key    => $is_pk || 0,
297                 is_auto_increment => $item{'data_type'}{'is_auto_increment'},
298             } 
299             }
300             | <error>
301     
302         field_comment : /^\s*(?:#|-{2})(.*)\n/ 
303             { 
304             my $comment =  $item[1];
305             $comment    =~ s/^\s*(#|-*)\s*//;
306             $comment    =~ s/\s*$//;
307             $return     = $comment;
308             }
309     
310         field_meta : default_val
311             | column_constraint
312     
313         view_fields : '(' field_name(s /,/) ')'
314             { $return = join (',', @{$item[2]} ) }
315     
316         column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?)
317             {
318             my $desc       = $item{'column_constraint_type'};
319             my $type       = $desc->{'type'};
320             my $fields     = $desc->{'fields'}     || [];
321             my $expression = $desc->{'expression'} || '';
322     
323             $return              =  {
324                 supertype        => 'constraint',
325                 name             => $item{'constraint_name'}[0] || '',
326                 type             => $type,
327                 expression       => $type eq 'check' ? $expression : '',
328                 deferrable       => $item{'deferrable'},
329                 deferred         => $item{'deferred'},
330                 reference_table  => $desc->{'reference_table'},
331                 reference_fields => $desc->{'reference_fields'},
332                 match_type       => $desc->{'match_type'},
333                 on_delete        => $desc->{'on_delete'} || $desc->{'on_delete_do'},
334                 on_update        => $desc->{'on_update'} || $desc->{'on_update_do'},
335             } 
336             }
337     
338         constraint_name : /constraint/i name_with_opt_quotes { $item[2] }
339     
340         column_constraint_type : /not null/i { $return = { type => 'not_null' } }
341             |
342             /null/i
343             { $return = { type => 'null' } }
344             |
345             /unique/i
346             { $return = { type => 'unique' } }
347             |
348             /primary key/i 
349             { $return = { type => 'primary_key' } }
350             |
351             /check/i '(' /[^)]+/ ')' 
352             { $return = { type => 'check', expression => $item[3] } }
353             |
354             /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
355             {
356             my $table_info  = $item{'table_id'};
357             my $schema_name = $table_info->{'schema_name'};
358             my $table_name  = $table_info->{'table_name'};
359             my ( $on_delete, $on_update );
360             for my $action ( @{ $item[5] || [] } ) {
361                 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
362                 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
363             }
364     
365             $return              =  {
366                 type             => 'foreign_key',
367                 reference_table  => $table_name,
368                 reference_fields => $item[3][0],
369                 match_type       => $item[4][0],
370                 on_delete        => $on_delete,
371                 on_update        => $on_update,
372             }
373             }
374     
375         table_id : schema_qualification(?) name_with_opt_quotes {
376             $return = { schema_name => $item[1][0], table_name => $item[2] }
377         }
378     
379         view_id : schema_qualification(?) name_with_opt_quotes {
380             $return = { schema_name => $item[1][0], view_name => $item[2] }
381         }
382     
383         view_target : /select|with/i /[^;]+/ {
384             $return = "$item[1] $item[2]";
385         }
386     
387         # SELECT views _may_ support outer parens, and we used to produce
388         # such sql, although non-standard. Use ugly lookeahead to parse
389         view_target : '('   /select/i    / [^;]+ (?= \) ) /x    ')'    {
390             $return = "$item[2] $item[3]"
391         }
392     
393         view_target_spec :  
394     
395         schema_qualification : name_with_opt_quotes '.'
396     
397         schema_name : name_with_opt_quotes
398     
399         field_name : name_with_opt_quotes
400     
401         name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] }
402     
403         double_quote: /"/
404     
405         index_name : name_with_opt_quotes
406     
407         data_type : pg_data_type parens_value_list(?)
408             { 
409             my $data_type = $item[1];
410     
411             #
412             # We can deduce some sizes from the data type's name.
413             #
414             if ( my $size = $item[2][0] ) {
415                 $data_type->{'size'} = $size;
416             }
417     
418             $return  = $data_type;
419             }
420     
421         pg_data_type :
422             /(bigint|int8)/i
423             { 
424                 $return = { 
425                 type => 'integer',
426                 size => 20,
427                 };
428             }
429             |
430             /(smallint|int2)/i
431             { 
432                 $return = {
433                 type => 'integer', 
434                 size => 5,
435                 };
436             }
437             |
438             /interval/i
439             {
440                 $return = { type => 'interval' };
441             }
442             |
443             /(integer|int4?)/i # interval must come before this
444             { 
445                 $return = {
446                 type => 'integer', 
447                 size => 10,
448                 };
449             }
450             |    
451             /(real|float4)/i
452             { 
453                 $return = {
454                 type => 'real', 
455                 size => 10,
456                 };
457             }
458             |
459             /(double precision|float8?)/i
460             { 
461                 $return = {
462                 type => 'float', 
463                 size => 20,
464                 }; 
465             }
466             |
467             /(bigserial|serial8)/i
468             { 
469                 $return = { 
470                 type              => 'integer', 
471                 size              => 20, 
472                 is_auto_increment => 1,
473                 };
474             }
475             |
476             /serial4?/i
477             { 
478                 $return = { 
479                 type              => 'integer',
480                 size              => 11, 
481                 is_auto_increment => 1,
482                 };
483             }
484             |
485             /(bit varying|varbit)/i
486             { 
487                 $return = { type => 'varbit' };
488             }
489             |
490             /character varying/i
491             { 
492                 $return = { type => 'varchar' };
493             }
494             |
495             /char(acter)?/i
496             { 
497                 $return = { type => 'char' };
498             }
499             |
500             /bool(ean)?/i
501             { 
502                 $return = { type => 'boolean' };
503             }
504             |
505             /bytea/i
506             { 
507                 $return = { type => 'bytea' };
508             }
509             |
510             /(timestamptz|timestamp)(?:\(\d\))?( with(?:out)? time zone)?/i
511             { 
512                 $return = { type => 'timestamp' . ($2||'') };
513             }
514             |
515             /text/i
516             { 
517                 $return = { 
518                 type => 'text',
519                 size => 64_000,
520                 };
521             }
522             |
523             /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i
524             { 
525                 $return = { type => $item[1] };
526             }
527     
528         parens_value_list : '(' VALUE(s /,/) ')'
529             { $item[2] }
530     
531     
532         parens_word_list : '(' name_with_opt_quotes(s /,/) ')'
533             { $item[2] }
534     
535         field_size : '(' num_range ')' { $item{'num_range'} }
536     
537         num_range : DIGITS ',' DIGITS
538             { $return = $item[1].','.$item[3] }
539             | DIGITS
540             { $return = $item[1] }
541     
542         table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?)
543             {
544             my $desc       = $item{'table_constraint_type'};
545             my $type       = $desc->{'type'};
546             my $fields     = $desc->{'fields'};
547             my $expression = $desc->{'expression'};
548             my @comments   = ( @{ $item[1] }, @{ $item[-1] } );
549     
550             $return              =  {
551                 name             => $item[2][0] || '',
552                 supertype        => 'constraint',
553                 type             => $type,
554                 fields           => $type ne 'check' ? $fields : [],
555                 expression       => $type eq 'check' ? $expression : '',
556                 deferrable       => $item{'deferrable'},
557                 deferred         => $item{'deferred'},
558                 reference_table  => $desc->{'reference_table'},
559                 reference_fields => $desc->{'reference_fields'},
560                 match_type       => $desc->{'match_type'},
561                 on_delete        => $desc->{'on_delete'} || $desc->{'on_delete_do'},
562                 on_update        => $desc->{'on_update'} || $desc->{'on_update_do'},
563                 comments         => [ @comments ],
564             } 
565             }
566     
567         table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')' 
568             { 
569             $return = {
570                 type   => 'primary_key',
571                 fields => $item[3],
572             }
573             }
574             |
575             /unique/i '(' name_with_opt_quotes(s /,/) ')' 
576             { 
577             $return    =  {
578                 type   => 'unique',
579                 fields => $item[3],
580             }
581             }
582             |
583             /check/i '(' /[^)]+/ ')' 
584             {
585             $return        =  {
586                 type       => 'check',
587                 expression => $item[3],
588             }
589             }
590             |
591             /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_id parens_word_list(?) match_type(?) key_action(s?)
592             {
593             my ( $on_delete, $on_update );
594             for my $action ( @{ $item[9] || [] } ) {
595                 $on_delete = $action->{'action'} if $action->{'type'} eq 'delete';
596                 $on_update = $action->{'action'} if $action->{'type'} eq 'update';
597             }
598             
599             $return              =  {
600                 supertype        => 'constraint',
601                 type             => 'foreign_key',
602                 fields           => $item[3],
603                 reference_table  => $item[6]->{'table_name'},
604                 reference_fields => $item[7][0],
605                 match_type       => $item[8][0],
606                 on_delete     => $on_delete || '',
607                 on_update     => $on_update || '',
608             }
609             }
610     
611         deferrable : not(?) /deferrable/i 
612             { 
613             $return = ( $item[1] =~ /not/i ) ? 0 : 1;
614             }
615     
616         deferred : /initially/i /(deferred|immediate)/i { $item[2] }
617     
618         match_type : /match/i /partial|full|simple/i { $item[2] }
619
620         key_action : key_delete 
621             |
622             key_update
623     
624         key_delete : /on delete/i key_mutation
625             { 
626             $return = { 
627                 type   => 'delete',
628                 action => $item[2],
629             };
630             }
631     
632         key_update : /on update/i key_mutation
633             { 
634             $return = { 
635                 type   => 'update',
636                 action => $item[2],
637             };
638             }
639     
640         key_mutation : /no action/i { $return = 'no_action' }
641             |
642             /restrict/i { $return = 'restrict' }
643             |
644             /cascade/i { $return = 'cascade' }
645             |
646             /set null/i { $return = 'set null' }
647             |
648             /set default/i { $return = 'set default' }
649     
650         alter : alter_table table_id add_column field ';' 
651             { 
652             my $field_def = $item[4];
653             $tables{ $item[2]->{'table_name'} }{'fields'}{ $field_def->{'name'} } = {
654                 %$field_def, order => $field_order++
655             };
656             1;
657             }
658     
659         alter : alter_table table_id ADD table_constraint ';' 
660             { 
661             my $table_name = $item[2]->{'table_name'};
662             my $constraint = $item[4];
663             push @{ $tables{ $table_name }{'constraints'} }, $constraint;
664             1;
665             }
666     
667         alter : alter_table table_id drop_column NAME restrict_or_cascade(?) ';' 
668             {
669             $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'drop'} = 1;
670             1;
671             }
672     
673         alter : alter_table table_id alter_column NAME alter_default_val ';' 
674             {
675             $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'default'} = 
676                 $item[5]->{'value'};
677             1;
678             }
679     
680         #
681         # These will just parse for now but won't affect the structure. - ky
682         #
683         alter : alter_table table_id /rename/i /to/i NAME ';'
684             { 1 }
685     
686         alter : alter_table table_id alter_column NAME SET /statistics/i INTEGER ';' 
687             { 1 }
688     
689         alter : alter_table table_id alter_column NAME SET /storage/i storage_type ';'
690             { 1 }
691     
692         alter : alter_table table_id rename_column NAME /to/i NAME ';'
693             { 1 }
694     
695         alter : alter_table table_id DROP /constraint/i NAME restrict_or_cascade ';'
696             { 1 }
697     
698         alter : alter_table table_id /owner/i /to/i NAME ';'
699             { 1 }
700     
701         alter : alter_sequence NAME /owned/i /by/i column_name ';'
702             { 1 }
703     
704         storage_type : /(plain|external|extended|main)/i
705     
706         temporary : /temp(orary)?\\b/i
707           {
708             1;
709           }
710     
711         or_replace : /or replace/i
712     
713         alter_default_val : SET default_val 
714             { 
715             $return = { value => $item[2]->{'value'} } 
716             }
717             | DROP DEFAULT 
718             { 
719             $return = { value => undef } 
720             } 
721     
722         #
723         # This is a little tricky to get right, at least WRT to making the 
724         # tests pass.  The problem is that the constraints are stored just as
725         # a list (no name access), and the tests expect the constraints in a
726         # particular order.  I'm going to leave the rule but disable the code 
727         # for now. - ky
728         #
729         alter : alter_table table_id alter_column NAME alter_nullable ';'
730             {
731         #        my $table_name  = $item[2]->{'table_name'};
732         #        my $field_name  = $item[4];
733         #        my $is_nullable = $item[5]->{'is_nullable'};
734         #
735         #        $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} = 
736         #            $is_nullable;
737         #
738         #        if ( $is_nullable ) {
739         #            1;
740         #            push @{ $tables{ $table_name }{'constraints'} }, {
741         #                type   => 'not_null',
742         #                fields => [ $field_name ],
743         #            };
744         #        }
745         #        else {
746         #            for my $i ( 
747         #                0 .. $#{ $tables{ $table_name }{'constraints'} || [] } 
748         #            ) {
749         #                my $c = $tables{ $table_name }{'constraints'}[ $i ] or next;
750         #                my $fields = join( '', @{ $c->{'fields'} || [] } ) or next;
751         #                if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) {
752         #                    delete $tables{ $table_name }{'constraints'}[ $i ];
753         #                    last;
754         #                }
755         #            }
756         #        }
757     
758             1;
759             }
760     
761         alter_nullable : SET not_null 
762             { 
763             $return = { is_nullable => 0 } 
764             }
765             | DROP not_null
766             { 
767             $return = { is_nullable => 1 } 
768             }
769     
770         not_null : /not/i /null/i
771     
772         not : /not/i
773     
774         add_column : ADD COLUMN(?)
775     
776         alter_table : ALTER TABLE ONLY(?)
777     
778         alter_sequence : ALTER SEQUENCE 
779     
780         drop_column : DROP COLUMN(?)
781     
782         alter_column : ALTER COLUMN(?)
783     
784         rename_column : /rename/i COLUMN(?)
785     
786         restrict_or_cascade : /restrict/i | 
787             /cascade/i
788     
789         # Handle functions that can be called
790         select : SELECT select_function ';' 
791             { 1 }
792     
793         # Read the setval function but don't do anything with it because this parser
794         # isn't handling sequences
795         select_function : schema_qualification(?) /setval/i '(' VALUE /,/ VALUE /,/ /(true|false)/i ')' 
796             { 1 }
797     
798         # Skipping all COPY commands
799         copy : COPY WORD /[^;]+/ ';' { 1 }
800             { 1 }
801     
802         # The "\." allows reading in from STDIN but this isn't needed for schema
803         # creation, so it is skipped.
804         readin_symbol : '\.'
805             {1}
806     
807         #
808         # End basically useless stuff. - ky
809         #
810     
811         create_table : CREATE TABLE
812     
813         create_index : CREATE /index/i
814     
815         default_val  : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/
816             { 
817             my $val =  defined $item[2] ? $item[2] : '';
818             $val    =~ s/^'|'$//g; 
819             $return =  {
820                 supertype => 'constraint',
821                 type      => 'default',
822                 value     => $val,
823             }
824             }
825             | /null/i
826             { 
827             $return =  {
828                 supertype => 'constraint',
829                 type      => 'default',
830                 value     => 'NULL',
831             }
832             }
833     
834         name_with_opt_paren : NAME parens_value_list(s?)
835             { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
836     
837         unique : /unique/i { 1 }
838     
839         key : /key/i | /index/i
840     
841         table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')'
842             { 
843             $return = { type => 'inherits', table_name => $item[3] }
844             }
845             |
846             /with(out)? oids/i
847             {
848             $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' }
849             }
850     
851         ADD : /add/i
852     
853         ALTER : /alter/i
854     
855         CREATE : /create/i
856     
857         ONLY : /only/i
858     
859         DEFAULT : /default/i
860     
861         DROP : /drop/i
862     
863         COLUMN : /column/i
864     
865         TABLE : /table/i
866     
867         VIEW : /view/i
868     
869         SCHEMA : /schema/i
870     
871         SEMICOLON : /\s*;\n?/
872     
873         SEQUENCE : /sequence/i
874     
875         SELECT : /select/i
876     
877         COPY : /copy/i
878     
879         INTEGER : /\d+/
880     
881         WORD : /\w+/
882     
883         DIGITS : /\d+/
884     
885         COMMA : ','
886     
887         SET : /set/i
888     
889         NAME    : "`" /\w+/ "`"
890             { $item[2] }
891             | /\w+/
892             { $item[1] }
893             | /[\$\w]+/
894             { $item[1] }
895     
896         VALUE   : /[-+]?\.?\d+(?:[eE]\d+)?/
897             { $item[1] }
898             | /'.*?'/   # XXX doesn't handle embedded quotes
899             { $item[1] }
900             | /null/i
901             { 'NULL' }
902             !;
903     }
904 }