moving files around per ky's request
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / PostgreSQL.pm
1 package SQL::Translator::Parser::PostgreSQL;
2
3 # -------------------------------------------------------------------
4 # $Id: PostgreSQL.pm,v 1.1 2003-02-21 19:35:17 allenday Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
7 #                    darren chamberlain <darren@cpan.org>,
8 #                    Chris Mungall <cjm@fruitfly.org>
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; version 2.
13 #
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # 02111-1307  USA
23 # -------------------------------------------------------------------
24
25 =head1 NAME
26
27 SQL::Translator::Parser::PostgreSQL - parser for PostgreSQL
28
29 =head1 SYNOPSIS
30
31   use SQL::Translator;
32   use SQL::Translator::Parser::PostgreSQL;
33
34   my $translator = SQL::Translator->new;
35   $translator->parser("SQL::Translator::Parser::PostgreSQL");
36
37 =head1 DESCRIPTION
38
39 The grammar is influenced heavily by Tim Bunce's "mysql2ora" grammar.
40
41 =cut
42
43 use strict;
44 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
45 $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
46 $DEBUG   = 0 unless defined $DEBUG;
47
48 use Data::Dumper;
49 use Parse::RecDescent;
50 use Exporter;
51 use base qw(Exporter);
52
53 @EXPORT_OK = qw(parse);
54
55 # Enable warnings within the Parse::RecDescent module.
56 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
57 $::RD_WARN   = 1; # Enable warnings. This will warn on unused rules &c.
58 $::RD_HINT   = 1; # Give out hints to help fix problems.
59
60 my $parser; # should we do this?  There's no programmic way to 
61             # change the grammar, so I think this is safe.
62
63 $GRAMMAR = q!
64
65 { our ( %tables, $table_order ) }
66
67 startrule : statement(s) { \%tables }
68
69 statement : comment
70   | drop
71   | create
72   | <error>
73
74 drop : /drop/i WORD(s) ';'
75
76 create : create_table table_name '(' comment(s?) create_definition(s /,/) ')' table_option(s?) ';'
77   {
78    my $table_name                       = $item{'table_name'};
79    $tables{ $table_name }{'order'}      = ++$table_order;
80    $tables{ $table_name }{'table_name'} = $table_name;
81
82    my $i = 1;
83    for my $definition ( @{ $item[4] } ) {
84          if ( $definition->{'type'} eq 'field' ) {
85            my $field_name = $definition->{'name'};
86            $tables{ $table_name }{'fields'}{ $field_name } = 
87                  { %$definition, order => $i };
88            $i++;
89                                 
90            if ( $definition->{'is_primary_key'} ) {
91                  push @{ $tables{ $table_name }{'indices'} },
92                    {
93                         type   => 'primary_key',
94                         fields => [ $field_name ],
95                    }
96                          ;
97            }
98          }
99          else {
100            push @{ $tables{ $table_name }{'indices'} },
101                  $definition;
102          }
103    }
104
105    for my $opt ( @{ $item{'table_option'} } ) {
106          if ( my ( $key, $val ) = each %$opt ) {
107            $tables{ $table_name }{'table_options'}{ $key } = $val;
108          }
109    }
110   }
111
112   create : /CREATE/i unique(?) /(INDEX|KEY)/i index_name /on/i table_name '(' field_name(s /,/) ')' ';'
113     {
114         push @{ $tables{ $item{'table_name'} }{'indices'} },
115             {
116                 name   => $item[4],
117                 type   => $item[2] ? 'unique' : 'normal',
118                 fields => $item[8],
119             }
120         ;
121     }
122
123 create_definition : index
124     | field
125     | <error>
126
127 comment : /^\s*(?:#|-{2}).*\n/
128
129 blank : /\s*/
130
131 field : field_name data_type field_qualifier(s?)
132     {
133         my %qualifiers = map { %$_ } @{ $item{'field_qualifier'} || [] };
134         my $null = defined $item{'not_null'} ? $item{'not_null'} : 1;
135         delete $qualifiers{'not_null'};
136         if ( my @type_quals = @{ $item{'data_type'}{'qualifiers'} || [] } ) {
137             $qualifiers{ $_ } = 1 for @type_quals;
138         }
139
140         $return = { 
141             type           => 'field',
142             name           => $item{'field_name'}, 
143             data_type      => $item{'data_type'}{'type'},
144             size           => $item{'data_type'}{'size'},
145             list           => $item{'data_type'}{'list'},
146             null           => $null,
147             %qualifiers,
148         } 
149     }
150     | <error>
151
152 field_qualifier : not_null
153     { 
154         $return = { 
155              null => $item{'not_null'},
156         } 
157     }
158
159 field_qualifier : default_val
160     { 
161         $return = { 
162              default => $item{'default_val'},
163         } 
164     }
165
166 field_qualifier : auto_inc
167     { 
168         $return = { 
169              is_auto_inc => $item{'auto_inc'},
170         } 
171     }
172
173 field_qualifier : ',' primary_key '(' field_name ')'
174     { 
175         $return = { 
176              is_primary_key => $item{'primary_key'},
177         } 
178     }
179
180 field_qualifier : ',' foreign_key '(' field_name ')' foreign_key_reference foreign_table_name '(' foreign_field_name ')'
181     { 
182         $return = {
183              is_foreign_key => $item{'foreign_key'},
184                          foreign_table => $item{'foreign_table_name'},
185                          foreign_field => $item{'foreign_field_name'},
186                          name => $item{'field_name'},
187         } 
188     }
189
190 field_qualifier : unsigned
191     { 
192         $return = { 
193              is_unsigned => $item{'unsigned'},
194         } 
195     }
196
197 index : primary_key_index
198     | unique_index
199     | fulltext_index
200     | normal_index
201
202 table_name   : WORD
203
204 foreign_table_name   : WORD
205
206 field_name   : WORD
207
208 foreign_field_name   : WORD
209
210 index_name   : WORD
211
212 data_type    : WORD parens_value_list(s?) type_qualifier(s?)
213     { 
214         my $type = $item[1];
215         my $size; # field size, applicable only to non-set fields
216         my $list; # set list, applicable only to sets (duh)
217
218         if ( uc($type) =~ /^(SET|ENUM)$/ ) {
219             $size = undef;
220             $list = $item[2][0];
221         }
222         else {
223             $size = $item[2][0];
224             $list = [];
225         }
226
227         $return        = { 
228             type       => $type,
229             size       => $size,
230             list       => $list,
231             qualifiers => $item[3],
232         } 
233     }
234
235 parens_value_list : '(' VALUE(s /,/) ')'
236     { $item[2] }
237
238 type_qualifier : /(BINARY|UNSIGNED|ZEROFILL)/i
239     { lc $item[1] }
240
241 field_type   : WORD
242
243 field_size   : '(' num_range ')' { $item{'num_range'} }
244
245 num_range    : DIGITS ',' DIGITS
246     { $return = $item[1].','.$item[3] }
247     | DIGITS
248     { $return = $item[1] }
249
250 create_table : /create/i /table/i
251
252 create_index : /create/i /index/i
253
254 not_null     : /not/i /null/i { $return = 0 }
255
256 unsigned     : /unsigned/i { $return = 0 }
257
258 default_val  : /default/i /(?:')?[\w\d.-]*(?:')?/ 
259     { 
260         $item[2] =~ s/'//g; 
261         $return  =  $item[2];
262     }
263
264 auto_inc : /auto_increment/i { 1 }
265
266 primary_key : /primary/i /key/i { 1 }
267
268 foreign_key : /foreign/i /key/i { 1 }
269
270 foreign_key_reference : /references/i { 1 }
271
272 primary_key_index : primary_key index_name(?) '(' field_name(s /,/) ')'
273     { 
274         $return    = { 
275             name   => $item{'index_name'}[0],
276             type   => 'primary_key',
277             fields => $item[4],
278         } 
279     }
280
281 normal_index : key index_name(?) '(' name_with_opt_paren(s /,/) ')'
282     { 
283         $return    = { 
284             name   => $item{'index_name'}[0],
285             type   => 'normal',
286             fields => $item[4],
287         } 
288     }
289
290 unique_index : unique key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
291     { 
292         $return    = { 
293             name   => $item{'index_name'}[0],
294             type   => 'unique',
295             fields => $item[5],
296         } 
297     }
298
299 fulltext_index : fulltext key(?) index_name(?) '(' name_with_opt_paren(s /,/) ')'
300     { 
301         $return    = { 
302             name   => $item{'index_name'}[0],
303             type   => 'fulltext',
304             fields => $item[5],
305         } 
306     }
307
308 name_with_opt_paren : NAME parens_value_list(s?)
309     { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] }
310
311 fulltext : /fulltext/i { 1 }
312
313 unique : /unique/i { 1 }
314
315 key : /key/i | /index/i
316
317 table_option : /[^\s;]*/ 
318     { 
319         $return = { split /=/, $item[1] }
320     }
321
322 WORD : /\w+/
323
324 DIGITS : /\d+/
325
326 COMMA : ','
327
328 NAME    : "`" /\w+/ "`"
329     { $item[2] }
330     | /\w+/
331     { $item[1] }
332
333 VALUE   : /[-+]?\.?\d+(?:[eE]\d+)?/
334     { $item[1] }
335     | /'.*?'/   # XXX doesn't handle embedded quotes
336     { $item[1] }
337     | /NULL/
338     { 'NULL' }
339
340 !;
341
342 # -------------------------------------------------------------------
343 sub parse {
344     my ( $translator, $data ) = @_;
345     $parser ||= Parse::RecDescent->new($GRAMMAR);
346
347     $::RD_TRACE  = $translator->trace ? 1 : undef;
348     $DEBUG       = $translator->debug;
349
350     unless (defined $parser) {
351         return $translator->error("Error instantiating Parse::RecDescent ".
352             "instance: Bad grammer");
353     }
354
355     my $result = $parser->startrule($data);
356     die "Parse failed.\n" unless defined $result;
357     warn Dumper($result) if $DEBUG;
358     return $result;
359 }
360
361 1;
362
363 #-----------------------------------------------------
364 # Where man is not nature is barren.
365 # William Blake
366 #-----------------------------------------------------
367
368 =pod
369
370 =head1 AUTHOR
371
372 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
373 Chris Mungall
374
375 =head1 SEE ALSO
376
377 perl(1), Parse::RecDescent.
378
379 =cut