Commit | Line | Data |
84012a55 |
1 | package SQL::Translator::Parser::PostgreSQL; |
4422e22a |
2 | |
4422e22a |
3 | =head1 NAME |
4 | |
84012a55 |
5 | SQL::Translator::Parser::PostgreSQL - parser for PostgreSQL |
4422e22a |
6 | |
7 | =head1 SYNOPSIS |
8 | |
9 | use SQL::Translator; |
84012a55 |
10 | use SQL::Translator::Parser::PostgreSQL; |
4422e22a |
11 | |
12 | my $translator = SQL::Translator->new; |
84012a55 |
13 | $translator->parser("SQL::Translator::Parser::PostgreSQL"); |
4422e22a |
14 | |
15 | =head1 DESCRIPTION |
16 | |
ea93df61 |
17 | The grammar was started from the MySQL parsers. Here is the description |
0efb6e1b |
18 | from PostgreSQL: |
19 | |
20 | Table: |
629b76f9 |
21 | (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createtable.html) |
22 | |
23 | CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name ( |
ea93df61 |
24 | { column_name data_type [ DEFAULT default_expr ] |
629b76f9 |
25 | [ column_constraint [, ... ] ] |
26 | | table_constraint } [, ... ] |
27 | ) |
28 | [ INHERITS ( parent_table [, ... ] ) ] |
29 | [ WITH OIDS | WITHOUT OIDS ] |
ea93df61 |
30 | |
629b76f9 |
31 | where column_constraint is: |
ea93df61 |
32 | |
629b76f9 |
33 | [ CONSTRAINT constraint_name ] |
34 | { NOT NULL | NULL | UNIQUE | PRIMARY KEY | |
35 | CHECK (expression) | |
36 | REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ] |
37 | [ ON DELETE action ] [ ON UPDATE action ] } |
ea93df61 |
38 | [ DEFERRABLE | NOT DEFERRABLE ] |
629b76f9 |
39 | [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] |
ea93df61 |
40 | |
629b76f9 |
41 | and table_constraint is: |
ea93df61 |
42 | |
629b76f9 |
43 | [ CONSTRAINT constraint_name ] |
44 | { UNIQUE ( column_name [, ... ] ) | |
45 | PRIMARY KEY ( column_name [, ... ] ) | |
46 | CHECK ( expression ) | |
ea93df61 |
47 | FOREIGN KEY ( column_name [, ... ] ) |
629b76f9 |
48 | REFERENCES reftable [ ( refcolumn [, ... ] ) ] |
ea93df61 |
49 | [ MATCH FULL | MATCH PARTIAL ] |
629b76f9 |
50 | [ ON DELETE action ] [ ON UPDATE action ] } |
ea93df61 |
51 | [ DEFERRABLE | NOT DEFERRABLE ] |
629b76f9 |
52 | [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] |
0efb6e1b |
53 | |
54 | Index: |
629b76f9 |
55 | (http://www.postgresql.org/docs/view.php?version=7.3&idoc=1&file=sql-createindex.html) |
0efb6e1b |
56 | |
629b76f9 |
57 | CREATE [ UNIQUE ] INDEX index_name ON table |
58 | [ USING acc_method ] ( column [ ops_name ] [, ...] ) |
59 | [ WHERE predicate ] |
60 | CREATE [ UNIQUE ] INDEX index_name ON table |
61 | [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] ) |
62 | [ WHERE predicate ] |
4422e22a |
63 | |
0012a163 |
64 | Alter table: |
65 | |
66 | ALTER TABLE [ ONLY ] table [ * ] |
67 | ADD [ COLUMN ] column type [ column_constraint [ ... ] ] |
68 | ALTER TABLE [ ONLY ] table [ * ] |
69 | ALTER [ COLUMN ] column { SET DEFAULT value | DROP DEFAULT } |
70 | ALTER TABLE [ ONLY ] table [ * ] |
71 | ALTER [ COLUMN ] column SET STATISTICS integer |
72 | ALTER TABLE [ ONLY ] table [ * ] |
73 | RENAME [ COLUMN ] column TO newcolumn |
74 | ALTER TABLE table |
75 | RENAME TO new_table |
76 | ALTER TABLE table |
77 | ADD table_constraint_definition |
ea93df61 |
78 | ALTER TABLE [ ONLY ] table |
0012a163 |
79 | DROP CONSTRAINT constraint { RESTRICT | CASCADE } |
80 | ALTER TABLE table |
ea93df61 |
81 | OWNER TO new_owner |
0012a163 |
82 | |
3022f45b |
83 | View table: |
84 | |
85 | CREATE [ OR REPLACE ] VIEW view [ ( column name list ) ] AS SELECT query |
86 | |
4422e22a |
87 | =cut |
88 | |
89 | use strict; |
f27f9229 |
90 | use warnings; |
da06ac74 |
91 | use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ]; |
11ad2df9 |
92 | $VERSION = '1.59'; |
4422e22a |
93 | $DEBUG = 0 unless defined $DEBUG; |
94 | |
95 | use Data::Dumper; |
96 | use Parse::RecDescent; |
97 | use Exporter; |
98 | use base qw(Exporter); |
99 | |
100 | @EXPORT_OK = qw(parse); |
101 | |
102 | # Enable warnings within the Parse::RecDescent module. |
103 | $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error |
104 | $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c. |
105 | $::RD_HINT = 1; # Give out hints to help fix problems. |
106 | |
4422e22a |
107 | $GRAMMAR = q! |
108 | |
330e4686 |
109 | { my ( %tables, @views, $table_order, $field_order, @table_comments) } |
4422e22a |
110 | |
629b76f9 |
111 | # |
112 | # The "eofile" rule makes the parser fail if any "statement" rule |
ea93df61 |
113 | # fails. Otherwise, the first successful match by a "statement" |
629b76f9 |
114 | # won't cause the failure needed to know that the parse, as a whole, |
115 | # failed. -ky |
116 | # |
330e4686 |
117 | startrule : statement(s) eofile { { tables => \%tables, views => \@views } } |
4422e22a |
118 | |
0efb6e1b |
119 | eofile : /^\Z/ |
ea93df61 |
120 | |
0efb6e1b |
121 | |
122 | statement : create |
ac397a49 |
123 | | comment_on_table |
124 | | comment_on_column |
e4a9818d |
125 | | comment_on_other |
0efb6e1b |
126 | | comment |
0012a163 |
127 | | alter |
0efb6e1b |
128 | | grant |
129 | | revoke |
4422e22a |
130 | | drop |
4d2da1f7 |
131 | | insert |
0efb6e1b |
132 | | connect |
4d2da1f7 |
133 | | update |
0012a163 |
134 | | set |
34338cb2 |
135 | | select |
136 | | copy |
137 | | readin_symbol |
1f5b2625 |
138 | | commit |
4422e22a |
139 | | <error> |
140 | |
1f5b2625 |
141 | commit : /commit/i ';' |
142 | |
0efb6e1b |
143 | connect : /^\s*\\\connect.*\n/ |
144 | |
f04713db |
145 | set : /set/i /[^;]*/ ';' |
0012a163 |
146 | |
330e4686 |
147 | revoke : /revoke/i WORD(s /,/) /on/i TABLE(?) table_id /from/i name_with_opt_quotes(s /,/) ';' |
0efb6e1b |
148 | { |
330e4686 |
149 | my $table_info = $item{'table_id'}; |
a20abbda |
150 | my $schema_name = $table_info->{'schema_name'}; |
151 | my $table_name = $table_info->{'table_name'}; |
0efb6e1b |
152 | push @{ $tables{ $table_name }{'permissions'} }, { |
153 | type => 'revoke', |
154 | actions => $item[2], |
91c75eab |
155 | users => $item[7], |
0efb6e1b |
156 | } |
157 | } |
158 | |
34338cb2 |
159 | revoke : /revoke/i WORD(s /,/) /on/i SCHEMA(?) schema_name /from/i name_with_opt_quotes(s /,/) ';' |
160 | { 1 } |
161 | |
330e4686 |
162 | grant : /grant/i WORD(s /,/) /on/i TABLE(?) table_id /to/i name_with_opt_quotes(s /,/) ';' |
0efb6e1b |
163 | { |
330e4686 |
164 | my $table_info = $item{'table_id'}; |
a20abbda |
165 | my $schema_name = $table_info->{'schema_name'}; |
166 | my $table_name = $table_info->{'table_name'}; |
0efb6e1b |
167 | push @{ $tables{ $table_name }{'permissions'} }, { |
168 | type => 'grant', |
169 | actions => $item[2], |
91c75eab |
170 | users => $item[7], |
0efb6e1b |
171 | } |
172 | } |
173 | |
34338cb2 |
174 | grant : /grant/i WORD(s /,/) /on/i SCHEMA(?) schema_name /to/i name_with_opt_quotes(s /,/) ';' |
175 | { 1 } |
176 | |
0efb6e1b |
177 | drop : /drop/i /[^;]*/ ';' |
4422e22a |
178 | |
9bf756df |
179 | string : |
ea93df61 |
180 | /'(\\.|''|[^\\\'])*'/ |
4d2da1f7 |
181 | |
9bf756df |
182 | nonstring : /[^;\'"]+/ |
183 | |
9644fab3 |
184 | statement_body : string | nonstring |
9bf756df |
185 | |
9644fab3 |
186 | insert : /insert/i statement_body(s?) ';' |
9bf756df |
187 | |
9644fab3 |
188 | update : /update/i statement_body(s?) ';' |
4d2da1f7 |
189 | |
0012a163 |
190 | # |
191 | # Create table. |
192 | # |
330e4686 |
193 | create : CREATE temporary(?) TABLE table_id '(' create_definition(s? /,/) ')' table_option(s?) ';' |
0efb6e1b |
194 | { |
330e4686 |
195 | my $table_info = $item{'table_id'}; |
a20abbda |
196 | my $schema_name = $table_info->{'schema_name'}; |
197 | my $table_name = $table_info->{'table_name'}; |
198 | $tables{ $table_name }{'order'} = ++$table_order; |
199 | $tables{ $table_name }{'schema_name'} = $schema_name; |
200 | $tables{ $table_name }{'table_name'} = $table_name; |
0efb6e1b |
201 | |
330e4686 |
202 | $tables{ $table_name }{'temporary'} = $item[2][0]; |
3e98f7d9 |
203 | |
ac397a49 |
204 | if ( @table_comments ) { |
205 | $tables{ $table_name }{'comments'} = [ @table_comments ]; |
206 | @table_comments = (); |
207 | } |
208 | |
0efb6e1b |
209 | my @constraints; |
3e98f7d9 |
210 | for my $definition ( @{ $item[6] } ) { |
b3384294 |
211 | if ( $definition->{'supertype'} eq 'field' ) { |
0efb6e1b |
212 | my $field_name = $definition->{'name'}; |
ea93df61 |
213 | $tables{ $table_name }{'fields'}{ $field_name } = |
00ef67ea |
214 | { %$definition, order => $field_order++ }; |
ea93df61 |
215 | |
0138f7bb |
216 | for my $constraint ( @{ $definition->{'constraints'} || [] } ) { |
217 | $constraint->{'fields'} = [ $field_name ]; |
7d5bcab8 |
218 | push @{ $tables{ $table_name }{'constraints'} }, |
0138f7bb |
219 | $constraint; |
0efb6e1b |
220 | } |
221 | } |
b3384294 |
222 | elsif ( $definition->{'supertype'} eq 'constraint' ) { |
223 | push @{ $tables{ $table_name }{'constraints'} }, $definition; |
0efb6e1b |
224 | } |
b3384294 |
225 | elsif ( $definition->{'supertype'} eq 'index' ) { |
0efb6e1b |
226 | push @{ $tables{ $table_name }{'indices'} }, $definition; |
227 | } |
228 | } |
229 | |
3e98f7d9 |
230 | for my $option ( @{ $item[8] } ) { |
ea93df61 |
231 | $tables{ $table_name }{'table_options(s?)'}{ $option->{'type'} } = |
0efb6e1b |
232 | $option; |
233 | } |
234 | |
235 | 1; |
236 | } |
237 | |
330e4686 |
238 | create : CREATE unique(?) /(index|key)/i index_name /on/i table_id using_method(?) '(' field_name(s /,/) ')' where_predicate(?) ';' |
4422e22a |
239 | { |
330e4686 |
240 | my $table_info = $item{'table_id'}; |
a20abbda |
241 | my $schema_name = $table_info->{'schema_name'}; |
242 | my $table_name = $table_info->{'table_name'}; |
243 | push @{ $tables{ $table_name }{'indices'} }, |
4422e22a |
244 | { |
b3384294 |
245 | name => $item{'index_name'}, |
246 | supertype => $item{'unique'}[0] ? 'constraint' : 'index', |
247 | type => $item{'unique'}[0] ? 'unique' : 'normal', |
248 | fields => $item[9], |
249 | method => $item{'using_method'}[0], |
4422e22a |
250 | } |
251 | ; |
252 | } |
253 | |
330e4686 |
254 | create : CREATE or_replace(?) temporary(?) VIEW view_id view_fields(?) /AS/i view_target ';' |
255 | { |
256 | push @views, { |
257 | schema_name => $item{view_id}{schema_name}, |
258 | view_name => $item{view_id}{view_name}, |
259 | sql => $item{view_target}, |
260 | fields => $item[6], |
261 | is_temporary => $item[3][0], |
262 | } |
263 | } |
264 | |
0012a163 |
265 | # |
211e2e90 |
266 | # Create anything else (e.g., domain, etc.) |
0012a163 |
267 | # |
211e2e90 |
268 | create : CREATE WORD /[^;]+/ ';' |
ac397a49 |
269 | { @table_comments = (); } |
0012a163 |
270 | |
0efb6e1b |
271 | using_method : /using/i WORD { $item[2] } |
272 | |
273 | where_predicate : /where/i /[^;]+/ |
274 | |
275 | create_definition : field |
0efb6e1b |
276 | | table_constraint |
4422e22a |
277 | | <error> |
278 | |
ea93df61 |
279 | comment : /^\s*(?:#|-{2})(.*)\n/ |
280 | { |
a82fa2cb |
281 | my $comment = $item[1]; |
282 | $comment =~ s/^\s*(#|-*)\s*//; |
283 | $comment =~ s/\s*$//; |
ac397a49 |
284 | $return = $comment; |
285 | push @table_comments, $comment; |
286 | } |
287 | |
330e4686 |
288 | comment_on_table : /comment/i /on/i /table/i table_id /is/i comment_phrase ';' |
ac397a49 |
289 | { |
330e4686 |
290 | my $table_info = $item{'table_id'}; |
a20abbda |
291 | my $schema_name = $table_info->{'schema_name'}; |
292 | my $table_name = $table_info->{'table_name'}; |
293 | push @{ $tables{ $table_name }{'comments'} }, $item{'comment_phrase'}; |
ac397a49 |
294 | } |
295 | |
296 | comment_on_column : /comment/i /on/i /column/i column_name /is/i comment_phrase ';' |
297 | { |
298 | my $table_name = $item[4]->{'table'}; |
299 | my $field_name = $item[4]->{'field'}; |
e1fa2c52 |
300 | if ($tables{ $table_name }{'fields'}{ $field_name } ) { |
ea93df61 |
301 | push @{ $tables{ $table_name }{'fields'}{ $field_name }{'comments'} }, |
e1fa2c52 |
302 | $item{'comment_phrase'}; |
303 | } |
304 | else { |
305 | die "No such column as $table_name.$field_name"; |
306 | } |
ac397a49 |
307 | } |
308 | |
e4a9818d |
309 | comment_on_other : /comment/i /on/i /\w+/ /\w+/ /is/i comment_phrase ';' |
310 | { |
311 | push(@table_comments, $item{'comment_phrase'}); |
312 | } |
313 | |
314 | # [added by cjm 20041019] |
315 | # [TODO: other comment-on types] |
316 | # for now we just have a general mechanism for handling other |
317 | # kinds of comments than table/column; I'm not sure of the best |
318 | # way to incorporate these into the datamodel |
319 | # |
320 | # this is the exhaustive list of types of comment: |
321 | #COMMENT ON DATABASE my_database IS 'Development Database'; |
322 | #COMMENT ON INDEX my_index IS 'Enforces uniqueness on employee id'; |
323 | #COMMENT ON RULE my_rule IS 'Logs UPDATES of employee records'; |
324 | #COMMENT ON SEQUENCE my_sequence IS 'Used to generate primary keys'; |
325 | #COMMENT ON TABLE my_table IS 'Employee Information'; |
326 | #COMMENT ON TYPE my_type IS 'Complex Number support'; |
327 | #COMMENT ON VIEW my_view IS 'View of departmental costs'; |
328 | #COMMENT ON COLUMN my_table.my_field IS 'Employee ID number'; |
329 | #COMMENT ON TRIGGER my_trigger ON my_table IS 'Used for R.I.'; |
330 | # |
331 | # this is tested by test 08 |
332 | |
ac397a49 |
333 | column_name : NAME '.' NAME |
334 | { $return = { table => $item[1], field => $item[3] } } |
335 | |
e4a9818d |
336 | comment_phrase : /null/i |
337 | { $return = 'NULL' } |
338 | |
339 | comment_phrase : /'/ comment_phrase_unquoted(s) /'/ |
340 | { my $phrase = join(' ', @{ $item[2] }); |
341 | $return = $phrase} |
342 | |
343 | # [cjm TODO: double-single quotes in a comment_phrase] |
344 | comment_phrase_unquoted : /[^\']*/ |
345 | { $return = $item[1] } |
346 | |
347 | |
ea93df61 |
348 | xxxcomment_phrase : /'.*?'|NULL/ |
349 | { |
8e9e79dc |
350 | my $val = $item[1] || ''; |
ac397a49 |
351 | $val =~ s/^'|'$//g; |
352 | $return = $val; |
353 | } |
354 | |
a82fa2cb |
355 | field : field_comment(s?) field_name data_type field_meta(s?) field_comment(s?) |
4422e22a |
356 | { |
3022f45b |
357 | my ( $default, @constraints, $is_pk ); |
00ef67ea |
358 | my $is_nullable = 1; |
41fc9cb3 |
359 | for my $meta ( @{ $item[4] } ) { |
82968eb9 |
360 | if ( $meta->{'type'} eq 'default' ) { |
361 | $default = $meta; |
362 | next; |
363 | } |
364 | elsif ( $meta->{'type'} eq 'not_null' ) { |
00ef67ea |
365 | $is_nullable = 0; |
82968eb9 |
366 | } |
367 | elsif ( $meta->{'type'} eq 'primary_key' ) { |
368 | $is_pk = 1; |
369 | } |
4422e22a |
370 | |
82968eb9 |
371 | push @constraints, $meta if $meta->{'supertype'} eq 'constraint'; |
372 | } |
0efb6e1b |
373 | |
f2f71b8e |
374 | my @comments = ( @{ $item[1] }, @{ $item[5] } ); |
375 | |
0a7fc605 |
376 | $return = { |
b3384294 |
377 | supertype => 'field', |
ea93df61 |
378 | name => $item{'field_name'}, |
7eac5e12 |
379 | data_type => $item{'data_type'}{'type'}, |
380 | size => $item{'data_type'}{'size'}, |
00ef67ea |
381 | is_nullable => $is_nullable, |
7eac5e12 |
382 | default => $default->{'value'}, |
383 | constraints => [ @constraints ], |
384 | comments => [ @comments ], |
385 | is_primary_key => $is_pk || 0, |
386 | is_auto_increment => $item{'data_type'}{'is_auto_increment'}, |
ea93df61 |
387 | } |
4422e22a |
388 | } |
389 | | <error> |
390 | |
ea93df61 |
391 | field_comment : /^\s*(?:#|-{2})(.*)\n/ |
392 | { |
a82fa2cb |
393 | my $comment = $item[1]; |
394 | $comment =~ s/^\s*(#|-*)\s*//; |
395 | $comment =~ s/\s*$//; |
396 | $return = $comment; |
397 | } |
398 | |
0efb6e1b |
399 | field_meta : default_val |
82968eb9 |
400 | | column_constraint |
4422e22a |
401 | |
330e4686 |
402 | view_fields : '(' field_name(s /,/) ')' |
403 | { $return = join (',', @{$item[2]} ) } |
404 | |
0efb6e1b |
405 | column_constraint : constraint_name(?) column_constraint_type deferrable(?) deferred(?) |
406 | { |
407 | my $desc = $item{'column_constraint_type'}; |
408 | my $type = $desc->{'type'}; |
409 | my $fields = $desc->{'fields'} || []; |
410 | my $expression = $desc->{'expression'} || ''; |
411 | |
412 | $return = { |
82968eb9 |
413 | supertype => 'constraint', |
0efb6e1b |
414 | name => $item{'constraint_name'}[0] || '', |
415 | type => $type, |
416 | expression => $type eq 'check' ? $expression : '', |
f04713db |
417 | deferrable => $item{'deferrable'}, |
0efb6e1b |
418 | deferred => $item{'deferred'}, |
419 | reference_table => $desc->{'reference_table'}, |
420 | reference_fields => $desc->{'reference_fields'}, |
421 | match_type => $desc->{'match_type'}, |
100684f3 |
422 | on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'}, |
423 | on_update => $desc->{'on_update'} || $desc->{'on_update_do'}, |
ea93df61 |
424 | } |
4422e22a |
425 | } |
426 | |
0efb6e1b |
427 | constraint_name : /constraint/i name_with_opt_quotes { $item[2] } |
428 | |
429 | column_constraint_type : /not null/i { $return = { type => 'not_null' } } |
430 | | |
f04713db |
431 | /null/i |
0efb6e1b |
432 | { $return = { type => 'null' } } |
433 | | |
f04713db |
434 | /unique/i |
0efb6e1b |
435 | { $return = { type => 'unique' } } |
436 | | |
ea93df61 |
437 | /primary key/i |
0efb6e1b |
438 | { $return = { type => 'primary_key' } } |
439 | | |
ea93df61 |
440 | /check/i '(' /[^)]+/ ')' |
b3384294 |
441 | { $return = { type => 'check', expression => $item[3] } } |
0efb6e1b |
442 | | |
330e4686 |
443 | /references/i table_id parens_word_list(?) match_type(?) key_action(s?) |
0efb6e1b |
444 | { |
330e4686 |
445 | my $table_info = $item{'table_id'}; |
a20abbda |
446 | my $schema_name = $table_info->{'schema_name'}; |
447 | my $table_name = $table_info->{'table_name'}; |
3022f45b |
448 | my ( $on_delete, $on_update ); |
449 | for my $action ( @{ $item[5] || [] } ) { |
450 | $on_delete = $action->{'action'} if $action->{'type'} eq 'delete'; |
451 | $on_update = $action->{'action'} if $action->{'type'} eq 'update'; |
452 | } |
453 | |
0efb6e1b |
454 | $return = { |
455 | type => 'foreign_key', |
a20abbda |
456 | reference_table => $table_name, |
0138f7bb |
457 | reference_fields => $item[3][0], |
0efb6e1b |
458 | match_type => $item[4][0], |
100684f3 |
459 | on_delete => $on_delete, |
460 | on_update => $on_update, |
ba1a1626 |
461 | } |
4422e22a |
462 | } |
463 | |
330e4686 |
464 | table_id : schema_qualification(?) name_with_opt_quotes { |
465 | $return = { schema_name => $item[1][0], table_name => $item[2] } |
a20abbda |
466 | } |
467 | |
330e4686 |
468 | view_id : schema_qualification(?) name_with_opt_quotes { |
469 | $return = { schema_name => $item[1][0], view_name => $item[2] } |
470 | } |
471 | |
472 | view_target : /select|with/i /[^;]+/ { |
473 | $return = "$item[1] $item[2]"; |
474 | } |
475 | |
476 | # SELECT views _may_ support outer parens, and we used to produce |
477 | # such sql, although non-standard. Use ugly lookeahead to parse |
478 | view_target : '(' /select/i / [^;]+ (?= \) ) /x ')' { |
479 | $return = "$item[2] $item[3]" |
480 | } |
481 | |
ea93df61 |
482 | view_target_spec : |
330e4686 |
483 | |
484 | schema_qualification : name_with_opt_quotes '.' |
4422e22a |
485 | |
34338cb2 |
486 | schema_name : name_with_opt_quotes |
487 | |
0efb6e1b |
488 | field_name : name_with_opt_quotes |
4422e22a |
489 | |
0012a163 |
490 | name_with_opt_quotes : double_quote(?) NAME double_quote(?) { $item[2] } |
4422e22a |
491 | |
0efb6e1b |
492 | double_quote: /"/ |
4422e22a |
493 | |
d4056aa7 |
494 | index_name : name_with_opt_quotes |
4422e22a |
495 | |
0efb6e1b |
496 | data_type : pg_data_type parens_value_list(?) |
ea93df61 |
497 | { |
3022f45b |
498 | my $data_type = $item[1]; |
ba1a1626 |
499 | |
0efb6e1b |
500 | # |
501 | # We can deduce some sizes from the data type's name. |
502 | # |
44ffdb73 |
503 | if ( my $size = $item[2][0] ) { |
504 | $data_type->{'size'} = $size; |
505 | } |
4422e22a |
506 | |
3022f45b |
507 | $return = $data_type; |
4422e22a |
508 | } |
509 | |
0efb6e1b |
510 | pg_data_type : |
50840472 |
511 | /(bigint|int8)/i |
ea93df61 |
512 | { |
513 | $return = { |
50840472 |
514 | type => 'integer', |
b8ea6076 |
515 | size => 20, |
3022f45b |
516 | }; |
517 | } |
0efb6e1b |
518 | | |
1bbd4a2b |
519 | /(smallint|int2)/i |
ea93df61 |
520 | { |
3022f45b |
521 | $return = { |
ea93df61 |
522 | type => 'integer', |
b8ea6076 |
523 | size => 5, |
3022f45b |
524 | }; |
525 | } |
0efb6e1b |
526 | | |
c4c363bb |
527 | /interval/i |
528 | { |
529 | $return = { type => 'interval' }; |
530 | } |
531 | | |
50840472 |
532 | /(integer|int4?)/i # interval must come before this |
ea93df61 |
533 | { |
3022f45b |
534 | $return = { |
ea93df61 |
535 | type => 'integer', |
b8ea6076 |
536 | size => 10, |
3022f45b |
537 | }; |
538 | } |
ea93df61 |
539 | | |
50840472 |
540 | /(real|float4)/i |
ea93df61 |
541 | { |
50840472 |
542 | $return = { |
ea93df61 |
543 | type => 'real', |
b8ea6076 |
544 | size => 10, |
50840472 |
545 | }; |
546 | } |
0efb6e1b |
547 | | |
1bbd4a2b |
548 | /(double precision|float8?)/i |
ea93df61 |
549 | { |
3022f45b |
550 | $return = { |
ea93df61 |
551 | type => 'float', |
b8ea6076 |
552 | size => 20, |
ea93df61 |
553 | }; |
3022f45b |
554 | } |
0efb6e1b |
555 | | |
50840472 |
556 | /(bigserial|serial8)/i |
ea93df61 |
557 | { |
558 | $return = { |
559 | type => 'integer', |
560 | size => 20, |
7eac5e12 |
561 | is_auto_increment => 1, |
3022f45b |
562 | }; |
563 | } |
0efb6e1b |
564 | | |
1bbd4a2b |
565 | /serial4?/i |
ea93df61 |
566 | { |
567 | $return = { |
7eac5e12 |
568 | type => 'integer', |
ea93df61 |
569 | size => 11, |
7eac5e12 |
570 | is_auto_increment => 1, |
3022f45b |
571 | }; |
572 | } |
0efb6e1b |
573 | | |
1bbd4a2b |
574 | /(bit varying|varbit)/i |
ea93df61 |
575 | { |
3022f45b |
576 | $return = { type => 'varbit' }; |
577 | } |
0efb6e1b |
578 | | |
1bbd4a2b |
579 | /character varying/i |
ea93df61 |
580 | { |
3022f45b |
581 | $return = { type => 'varchar' }; |
582 | } |
0efb6e1b |
583 | | |
1bbd4a2b |
584 | /char(acter)?/i |
ea93df61 |
585 | { |
3022f45b |
586 | $return = { type => 'char' }; |
587 | } |
0efb6e1b |
588 | | |
1bbd4a2b |
589 | /bool(ean)?/i |
ea93df61 |
590 | { |
3022f45b |
591 | $return = { type => 'boolean' }; |
592 | } |
0efb6e1b |
593 | | |
1bbd4a2b |
594 | /bytea/i |
ea93df61 |
595 | { |
82968eb9 |
596 | $return = { type => 'bytea' }; |
3022f45b |
597 | } |
0efb6e1b |
598 | | |
b5a782a0 |
599 | /(timestamptz|timestamp)(?:\(\d\))?( with(?:out)? time zone)?/i |
ea93df61 |
600 | { |
b5a782a0 |
601 | $return = { type => 'timestamp' . ($2||'') }; |
3022f45b |
602 | } |
0efb6e1b |
603 | | |
38a6a4f9 |
604 | /text/i |
ea93df61 |
605 | { |
606 | $return = { |
38a6a4f9 |
607 | type => 'text', |
608 | size => 64_000, |
609 | }; |
610 | } |
611 | | |
c4c363bb |
612 | /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i |
ea93df61 |
613 | { |
3022f45b |
614 | $return = { type => $item[1] }; |
615 | } |
0efb6e1b |
616 | |
4422e22a |
617 | parens_value_list : '(' VALUE(s /,/) ')' |
618 | { $item[2] } |
619 | |
efd0c9ec |
620 | |
621 | parens_word_list : '(' name_with_opt_quotes(s /,/) ')' |
0efb6e1b |
622 | { $item[2] } |
4422e22a |
623 | |
0efb6e1b |
624 | field_size : '(' num_range ')' { $item{'num_range'} } |
4422e22a |
625 | |
0efb6e1b |
626 | num_range : DIGITS ',' DIGITS |
4422e22a |
627 | { $return = $item[1].','.$item[3] } |
628 | | DIGITS |
629 | { $return = $item[1] } |
630 | |
f2f71b8e |
631 | table_constraint : comment(s?) constraint_name(?) table_constraint_type deferrable(?) deferred(?) comment(s?) |
0efb6e1b |
632 | { |
633 | my $desc = $item{'table_constraint_type'}; |
634 | my $type = $desc->{'type'}; |
635 | my $fields = $desc->{'fields'}; |
636 | my $expression = $desc->{'expression'}; |
f2f71b8e |
637 | my @comments = ( @{ $item[1] }, @{ $item[-1] } ); |
0efb6e1b |
638 | |
639 | $return = { |
d4056aa7 |
640 | name => $item[2][0] || '', |
b3384294 |
641 | supertype => 'constraint', |
642 | type => $type, |
0efb6e1b |
643 | fields => $type ne 'check' ? $fields : [], |
644 | expression => $type eq 'check' ? $expression : '', |
f04713db |
645 | deferrable => $item{'deferrable'}, |
0efb6e1b |
646 | deferred => $item{'deferred'}, |
647 | reference_table => $desc->{'reference_table'}, |
648 | reference_fields => $desc->{'reference_fields'}, |
840447a5 |
649 | match_type => $desc->{'match_type'}, |
100684f3 |
650 | on_delete => $desc->{'on_delete'} || $desc->{'on_delete_do'}, |
651 | on_update => $desc->{'on_update'} || $desc->{'on_update_do'}, |
f2f71b8e |
652 | comments => [ @comments ], |
ea93df61 |
653 | } |
0efb6e1b |
654 | } |
4422e22a |
655 | |
ea93df61 |
656 | table_constraint_type : /primary key/i '(' name_with_opt_quotes(s /,/) ')' |
657 | { |
0efb6e1b |
658 | $return = { |
659 | type => 'primary_key', |
660 | fields => $item[3], |
661 | } |
662 | } |
663 | | |
ea93df61 |
664 | /unique/i '(' name_with_opt_quotes(s /,/) ')' |
665 | { |
0efb6e1b |
666 | $return = { |
667 | type => 'unique', |
668 | fields => $item[3], |
669 | } |
670 | } |
671 | | |
ea93df61 |
672 | /check/i '(' /[^)]+/ ')' |
0efb6e1b |
673 | { |
674 | $return = { |
675 | type => 'check', |
676 | expression => $item[3], |
677 | } |
678 | } |
679 | | |
330e4686 |
680 | /foreign key/i '(' name_with_opt_quotes(s /,/) ')' /references/i table_id parens_word_list(?) match_type(?) key_action(s?) |
0efb6e1b |
681 | { |
3022f45b |
682 | my ( $on_delete, $on_update ); |
683 | for my $action ( @{ $item[9] || [] } ) { |
684 | $on_delete = $action->{'action'} if $action->{'type'} eq 'delete'; |
685 | $on_update = $action->{'action'} if $action->{'type'} eq 'update'; |
686 | } |
ea93df61 |
687 | |
0efb6e1b |
688 | $return = { |
b3384294 |
689 | supertype => 'constraint', |
0efb6e1b |
690 | type => 'foreign_key', |
691 | fields => $item[3], |
a20abbda |
692 | reference_table => $item[6]->{'table_name'}, |
0efb6e1b |
693 | reference_fields => $item[7][0], |
694 | match_type => $item[8][0], |
100684f3 |
695 | on_delete => $on_delete || '', |
696 | on_update => $on_update || '', |
0efb6e1b |
697 | } |
698 | } |
699 | |
ea93df61 |
700 | deferrable : not(?) /deferrable/i |
701 | { |
0efb6e1b |
702 | $return = ( $item[1] =~ /not/i ) ? 0 : 1; |
703 | } |
4422e22a |
704 | |
0efb6e1b |
705 | deferred : /initially/i /(deferred|immediate)/i { $item[2] } |
4422e22a |
706 | |
840447a5 |
707 | match_type : /match/i /partial|full|simple/i { $item[2] } |
0efb6e1b |
708 | |
ea93df61 |
709 | key_action : key_delete |
3022f45b |
710 | | |
711 | key_update |
0efb6e1b |
712 | |
3022f45b |
713 | key_delete : /on delete/i key_mutation |
ea93df61 |
714 | { |
715 | $return = { |
3022f45b |
716 | type => 'delete', |
717 | action => $item[2], |
718 | }; |
719 | } |
720 | |
721 | key_update : /on update/i key_mutation |
ea93df61 |
722 | { |
723 | $return = { |
3022f45b |
724 | type => 'update', |
725 | action => $item[2], |
726 | }; |
727 | } |
728 | |
729 | key_mutation : /no action/i { $return = 'no_action' } |
730 | | |
731 | /restrict/i { $return = 'restrict' } |
732 | | |
733 | /cascade/i { $return = 'cascade' } |
734 | | |
38a6a4f9 |
735 | /set null/i { $return = 'set null' } |
3022f45b |
736 | | |
38a6a4f9 |
737 | /set default/i { $return = 'set default' } |
0efb6e1b |
738 | |
ea93df61 |
739 | alter : alter_table table_id add_column field ';' |
740 | { |
00ef67ea |
741 | my $field_def = $item[4]; |
a20abbda |
742 | $tables{ $item[2]->{'table_name'} }{'fields'}{ $field_def->{'name'} } = { |
00ef67ea |
743 | %$field_def, order => $field_order++ |
744 | }; |
745 | 1; |
746 | } |
747 | |
ea93df61 |
748 | alter : alter_table table_id ADD table_constraint ';' |
749 | { |
a20abbda |
750 | my $table_name = $item[2]->{'table_name'}; |
0012a163 |
751 | my $constraint = $item[4]; |
0012a163 |
752 | push @{ $tables{ $table_name }{'constraints'} }, $constraint; |
00ef67ea |
753 | 1; |
0012a163 |
754 | } |
755 | |
ea93df61 |
756 | alter : alter_table table_id drop_column NAME restrict_or_cascade(?) ';' |
00ef67ea |
757 | { |
a20abbda |
758 | $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'drop'} = 1; |
00ef67ea |
759 | 1; |
760 | } |
0012a163 |
761 | |
ea93df61 |
762 | alter : alter_table table_id alter_column NAME alter_default_val ';' |
00ef67ea |
763 | { |
ea93df61 |
764 | $tables{ $item[2]->{'table_name'} }{'fields'}{ $item[4] }{'default'} = |
00ef67ea |
765 | $item[5]->{'value'}; |
766 | 1; |
767 | } |
768 | |
769 | # |
770 | # These will just parse for now but won't affect the structure. - ky |
771 | # |
330e4686 |
772 | alter : alter_table table_id /rename/i /to/i NAME ';' |
00ef67ea |
773 | { 1 } |
774 | |
ea93df61 |
775 | alter : alter_table table_id alter_column NAME SET /statistics/i INTEGER ';' |
00ef67ea |
776 | { 1 } |
777 | |
330e4686 |
778 | alter : alter_table table_id alter_column NAME SET /storage/i storage_type ';' |
00ef67ea |
779 | { 1 } |
780 | |
330e4686 |
781 | alter : alter_table table_id rename_column NAME /to/i NAME ';' |
00ef67ea |
782 | { 1 } |
783 | |
330e4686 |
784 | alter : alter_table table_id DROP /constraint/i NAME restrict_or_cascade ';' |
00ef67ea |
785 | { 1 } |
786 | |
330e4686 |
787 | alter : alter_table table_id /owner/i /to/i NAME ';' |
00ef67ea |
788 | { 1 } |
789 | |
6f28e7ac |
790 | alter : alter_sequence NAME /owned/i /by/i column_name ';' |
791 | { 1 } |
792 | |
00ef67ea |
793 | storage_type : /(plain|external|extended|main)/i |
794 | |
330e4686 |
795 | temporary : /temp(orary)?\\b/i |
796 | { |
797 | 1; |
798 | } |
3e98f7d9 |
799 | |
330e4686 |
800 | or_replace : /or replace/i |
3e98f7d9 |
801 | |
ea93df61 |
802 | alter_default_val : SET default_val |
803 | { |
804 | $return = { value => $item[2]->{'value'} } |
805 | } |
806 | | DROP DEFAULT |
807 | { |
808 | $return = { value => undef } |
00ef67ea |
809 | } |
00ef67ea |
810 | |
811 | # |
ea93df61 |
812 | # This is a little tricky to get right, at least WRT to making the |
00ef67ea |
813 | # tests pass. The problem is that the constraints are stored just as |
814 | # a list (no name access), and the tests expect the constraints in a |
ea93df61 |
815 | # particular order. I'm going to leave the rule but disable the code |
00ef67ea |
816 | # for now. - ky |
817 | # |
330e4686 |
818 | alter : alter_table table_id alter_column NAME alter_nullable ';' |
00ef67ea |
819 | { |
a20abbda |
820 | # my $table_name = $item[2]->{'table_name'}; |
00ef67ea |
821 | # my $field_name = $item[4]; |
822 | # my $is_nullable = $item[5]->{'is_nullable'}; |
823 | # |
ea93df61 |
824 | # $tables{ $table_name }{'fields'}{ $field_name }{'is_nullable'} = |
00ef67ea |
825 | # $is_nullable; |
826 | # |
827 | # if ( $is_nullable ) { |
828 | # 1; |
829 | # push @{ $tables{ $table_name }{'constraints'} }, { |
830 | # type => 'not_null', |
831 | # fields => [ $field_name ], |
832 | # }; |
833 | # } |
834 | # else { |
ea93df61 |
835 | # for my $i ( |
836 | # 0 .. $#{ $tables{ $table_name }{'constraints'} || [] } |
00ef67ea |
837 | # ) { |
838 | # my $c = $tables{ $table_name }{'constraints'}[ $i ] or next; |
839 | # my $fields = join( '', @{ $c->{'fields'} || [] } ) or next; |
840 | # if ( $c->{'type'} eq 'not_null' && $fields eq $field_name ) { |
841 | # delete $tables{ $table_name }{'constraints'}[ $i ]; |
842 | # last; |
843 | # } |
844 | # } |
845 | # } |
846 | |
847 | 1; |
848 | } |
849 | |
ea93df61 |
850 | alter_nullable : SET not_null |
851 | { |
852 | $return = { is_nullable => 0 } |
00ef67ea |
853 | } |
854 | | DROP not_null |
ea93df61 |
855 | { |
856 | $return = { is_nullable => 1 } |
00ef67ea |
857 | } |
858 | |
859 | not_null : /not/i /null/i |
860 | |
99f3fab6 |
861 | not : /not/i |
862 | |
00ef67ea |
863 | add_column : ADD COLUMN(?) |
864 | |
865 | alter_table : ALTER TABLE ONLY(?) |
866 | |
ea93df61 |
867 | alter_sequence : ALTER SEQUENCE |
6f28e7ac |
868 | |
00ef67ea |
869 | drop_column : DROP COLUMN(?) |
870 | |
871 | alter_column : ALTER COLUMN(?) |
872 | |
873 | rename_column : /rename/i COLUMN(?) |
874 | |
ea93df61 |
875 | restrict_or_cascade : /restrict/i | |
00ef67ea |
876 | /cascade/i |
877 | |
34338cb2 |
878 | # Handle functions that can be called |
ea93df61 |
879 | select : SELECT select_function ';' |
34338cb2 |
880 | { 1 } |
881 | |
882 | # Read the setval function but don't do anything with it because this parser |
883 | # isn't handling sequences |
ea93df61 |
884 | select_function : schema_qualification(?) /setval/i '(' VALUE /,/ VALUE /,/ /(true|false)/i ')' |
34338cb2 |
885 | { 1 } |
886 | |
887 | # Skipping all COPY commands |
888 | copy : COPY WORD /[^;]+/ ';' { 1 } |
889 | { 1 } |
890 | |
891 | # The "\." allows reading in from STDIN but this isn't needed for schema |
892 | # creation, so it is skipped. |
893 | readin_symbol : '\.' |
894 | {1} |
895 | |
00ef67ea |
896 | # |
897 | # End basically useless stuff. - ky |
898 | # |
0012a163 |
899 | |
211e2e90 |
900 | create_table : CREATE TABLE |
0efb6e1b |
901 | |
211e2e90 |
902 | create_index : CREATE /index/i |
4422e22a |
903 | |
37e74f25 |
904 | default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/ |
ea93df61 |
905 | { |
f04713db |
906 | my $val = defined $item[2] ? $item[2] : ''; |
ea93df61 |
907 | $val =~ s/^'|'$//g; |
0efb6e1b |
908 | $return = { |
82968eb9 |
909 | supertype => 'constraint', |
910 | type => 'default', |
0efb6e1b |
911 | value => $val, |
912 | } |
4422e22a |
913 | } |
f27085c9 |
914 | | /null/i |
ea93df61 |
915 | { |
f27085c9 |
916 | $return = { |
917 | supertype => 'constraint', |
918 | type => 'default', |
919 | value => 'NULL', |
920 | } |
921 | } |
4422e22a |
922 | |
4422e22a |
923 | name_with_opt_paren : NAME parens_value_list(s?) |
0efb6e1b |
924 | { $item[2][0] ? "$item[1]($item[2][0][0])" : $item[1] } |
4422e22a |
925 | |
926 | unique : /unique/i { 1 } |
927 | |
928 | key : /key/i | /index/i |
929 | |
0efb6e1b |
930 | table_option : /inherits/i '(' name_with_opt_quotes(s /,/) ')' |
ea93df61 |
931 | { |
0efb6e1b |
932 | $return = { type => 'inherits', table_name => $item[3] } |
933 | } |
934 | | |
935 | /with(out)? oids/i |
936 | { |
937 | $return = { type => $item[1] =~ /out/i ? 'without_oids' : 'with_oids' } |
4422e22a |
938 | } |
939 | |
00ef67ea |
940 | ADD : /add/i |
941 | |
942 | ALTER : /alter/i |
943 | |
211e2e90 |
944 | CREATE : /create/i |
945 | |
00ef67ea |
946 | ONLY : /only/i |
947 | |
948 | DEFAULT : /default/i |
949 | |
950 | DROP : /drop/i |
951 | |
952 | COLUMN : /column/i |
953 | |
0d51cd9e |
954 | TABLE : /table/i |
955 | |
330e4686 |
956 | VIEW : /view/i |
957 | |
34338cb2 |
958 | SCHEMA : /schema/i |
959 | |
0efb6e1b |
960 | SEMICOLON : /\s*;\n?/ |
961 | |
6f28e7ac |
962 | SEQUENCE : /sequence/i |
963 | |
34338cb2 |
964 | SELECT : /select/i |
965 | |
966 | COPY : /copy/i |
967 | |
00ef67ea |
968 | INTEGER : /\d+/ |
969 | |
4422e22a |
970 | WORD : /\w+/ |
971 | |
972 | DIGITS : /\d+/ |
973 | |
974 | COMMA : ',' |
975 | |
00ef67ea |
976 | SET : /set/i |
977 | |
4422e22a |
978 | NAME : "`" /\w+/ "`" |
979 | { $item[2] } |
980 | | /\w+/ |
981 | { $item[1] } |
0012a163 |
982 | | /[\$\w]+/ |
983 | { $item[1] } |
4422e22a |
984 | |
985 | VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ |
986 | { $item[1] } |
987 | | /'.*?'/ # XXX doesn't handle embedded quotes |
988 | { $item[1] } |
f04713db |
989 | | /null/i |
4422e22a |
990 | { 'NULL' } |
991 | |
992 | !; |
993 | |
4422e22a |
994 | sub parse { |
995 | my ( $translator, $data ) = @_; |
9fa2a755 |
996 | my $parser = Parse::RecDescent->new($GRAMMAR); |
4422e22a |
997 | |
998 | $::RD_TRACE = $translator->trace ? 1 : undef; |
999 | $DEBUG = $translator->debug; |
1000 | |
1001 | unless (defined $parser) { |
1002 | return $translator->error("Error instantiating Parse::RecDescent ". |
1003 | "instance: Bad grammer"); |
1004 | } |
1005 | |
1006 | my $result = $parser->startrule($data); |
1007 | die "Parse failed.\n" unless defined $result; |
1008 | warn Dumper($result) if $DEBUG; |
82968eb9 |
1009 | |
1010 | my $schema = $translator->schema; |
ea93df61 |
1011 | my @tables = sort { |
330e4686 |
1012 | ( $result->{tables}{ $a }{'order'} || 0 ) <=> ( $result->{tables}{ $b }{'order'} || 0 ) |
1013 | } keys %{ $result->{tables} }; |
82968eb9 |
1014 | |
1015 | for my $table_name ( @tables ) { |
330e4686 |
1016 | my $tdata = $result->{tables}{ $table_name }; |
ea93df61 |
1017 | my $table = $schema->add_table( |
a20abbda |
1018 | #schema => $tdata->{'schema_name'}, |
1019 | name => $tdata->{'table_name'}, |
d7fcc1d6 |
1020 | ) or die "Couldn't create table '$table_name': " . $schema->error; |
82968eb9 |
1021 | |
3e98f7d9 |
1022 | $table->extra(temporary => 1) if $tdata->{'temporary'}; |
1023 | |
a82fa2cb |
1024 | $table->comments( $tdata->{'comments'} ); |
1025 | |
ea93df61 |
1026 | my @fields = sort { |
1027 | $tdata->{'fields'}{ $a }{'order'} |
82968eb9 |
1028 | <=> |
429f639c |
1029 | $tdata->{'fields'}{ $b }{'order'} |
82968eb9 |
1030 | } keys %{ $tdata->{'fields'} }; |
1031 | |
1032 | for my $fname ( @fields ) { |
1033 | my $fdata = $tdata->{'fields'}{ $fname }; |
00ef67ea |
1034 | next if $fdata->{'drop'}; |
82968eb9 |
1035 | my $field = $table->add_field( |
1036 | name => $fdata->{'name'}, |
1037 | data_type => $fdata->{'data_type'}, |
1038 | size => $fdata->{'size'}, |
1039 | default_value => $fdata->{'default'}, |
7eac5e12 |
1040 | is_auto_increment => $fdata->{'is_auto_increment'}, |
00ef67ea |
1041 | is_nullable => $fdata->{'is_nullable'}, |
a82fa2cb |
1042 | comments => $fdata->{'comments'}, |
82968eb9 |
1043 | ) or die $table->error; |
1044 | |
1045 | $table->primary_key( $field->name ) if $fdata->{'is_primary_key'}; |
1046 | |
1047 | for my $cdata ( @{ $fdata->{'constraints'} } ) { |
1048 | next unless $cdata->{'type'} eq 'foreign_key'; |
1049 | $cdata->{'fields'} ||= [ $field->name ]; |
1050 | push @{ $tdata->{'constraints'} }, $cdata; |
1051 | } |
1052 | } |
1053 | |
1054 | for my $idata ( @{ $tdata->{'indices'} || [] } ) { |
1055 | my $index = $table->add_index( |
1056 | name => $idata->{'name'}, |
1057 | type => uc $idata->{'type'}, |
1058 | fields => $idata->{'fields'}, |
3406fd5b |
1059 | ) or die $table->error . ' ' . $table->name; |
82968eb9 |
1060 | } |
1061 | |
1062 | for my $cdata ( @{ $tdata->{'constraints'} || [] } ) { |
1063 | my $constraint = $table->add_constraint( |
1064 | name => $cdata->{'name'}, |
1065 | type => $cdata->{'type'}, |
1066 | fields => $cdata->{'fields'}, |
1067 | reference_table => $cdata->{'reference_table'}, |
1068 | reference_fields => $cdata->{'reference_fields'}, |
1069 | match_type => $cdata->{'match_type'} || '', |
100684f3 |
1070 | on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'}, |
1071 | on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'}, |
b3384294 |
1072 | expression => $cdata->{'expression'}, |
1073 | ) or die "Can't add constraint of type '" . |
ea93df61 |
1074 | $cdata->{'type'} . "' to table '" . $table->name . |
b3384294 |
1075 | "': " . $table->error; |
82968eb9 |
1076 | } |
1077 | } |
1078 | |
330e4686 |
1079 | for my $vinfo (@{$result->{views}}) { |
1080 | my $sql = $vinfo->{sql}; |
1081 | $sql =~ s/\A\s+|\s+\z//g; |
1082 | my $view = $schema->add_view ( |
1083 | name => $vinfo->{view_name}, |
1084 | sql => $sql, |
1085 | fields => $vinfo->{fields}, |
1086 | ); |
1087 | |
1088 | $view->extra ( temporary => 1 ) if $vinfo->{is_temporary}; |
1089 | } |
1090 | |
f62bd16c |
1091 | return 1; |
4422e22a |
1092 | } |
1093 | |
1094 | 1; |
1095 | |
82968eb9 |
1096 | # ------------------------------------------------------------------- |
1097 | # Rescue the drowning and tie your shoestrings. |
ea93df61 |
1098 | # Henry David Thoreau |
82968eb9 |
1099 | # ------------------------------------------------------------------- |
4422e22a |
1100 | |
1101 | =pod |
1102 | |
0efb6e1b |
1103 | =head1 AUTHORS |
4422e22a |
1104 | |
11ad2df9 |
1105 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>, |
b8ea6076 |
1106 | Allen Day E<lt>allenday@ucla.eduE<gt>. |
4422e22a |
1107 | |
1108 | =head1 SEE ALSO |
1109 | |
1110 | perl(1), Parse::RecDescent. |
1111 | |
1112 | =cut |