don't use a precompiled version
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Grammar / SQLite.pm
1 use MooseX::Declare;
2 role SQL::Translator::Grammar::SQLite {
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 #my $GRAMMAR = q!
22 method _build_grammar {
23 return q!
24
25     my ( %tables, $table_order, @table_comments, @views, @triggers );
26 }
27
28 #
29 # The "eofile" rule makes the parser fail if any "statement" rule
30 # fails.  Otherwise, the first successful match by a "statement" 
31 # won't cause the failure needed to know that the parse, as a whole,
32 # failed. -ky
33 #
34 startrule : statement(s) eofile { 
35     $return      = {
36         tables   => \%tables, 
37         views    => \@views,
38         triggers => \@triggers,
39     }
40 }
41
42 eofile : /^\Z/
43
44 statement : begin_transaction
45     | commit
46     | drop
47     | comment
48     | create
49     | <error>
50
51 begin_transaction : /begin/i TRANSACTION(?) SEMICOLON
52
53 commit : /commit/i SEMICOLON
54
55 drop : /drop/i (tbl_drop | view_drop | trg_drop) SEMICOLON
56
57 tbl_drop: TABLE <commit> table_name
58
59 view_drop: VIEW if_exists(?) view_name
60
61 trg_drop: TRIGGER if_exists(?) trigger_name
62
63 comment : /^\s*(?:#|-{2}).*\n/
64     {
65         my $comment =  $item[1];
66         $comment    =~ s/^\s*(#|-{2})\s*//;
67         $comment    =~ s/\s*$//;
68         $return     = $comment;
69     }
70
71 comment : /\/\*/ /[^\*]+/ /\*\// 
72     {
73         my $comment = $item[2];
74         $comment    =~ s/^\s*|\s*$//g;
75         $return = $comment;
76     }
77
78 #
79 # Create Index
80 #
81 create : CREATE TEMPORARY(?) UNIQUE(?) INDEX WORD ON table_name parens_field_list conflict_clause(?) SEMICOLON
82     {
83         my $db_name    = $item[7]->{'db_name'} || '';
84         my $table_name = $item[7]->{'name'};
85
86         my $index        =  { 
87             name         => $item[5],
88             columns       => $item[8],
89             on_conflict  => $item[9][0],
90             is_temporary => $item[2][0] ? 1 : 0,
91         };
92
93         my $is_unique = $item[3][0];
94
95         if ( $is_unique ) {
96             $index->{'type'} = 'unique';
97             push @{ $tables{ $table_name }{'constraints'} }, $index;
98         }
99         else {
100             push @{ $tables{ $table_name }{'indices'} }, $index;
101         }
102     }
103
104 #
105 # Create Table
106 #
107 create : CREATE TEMPORARY(?) TABLE table_name '(' definition(s /,/) ')' SEMICOLON
108     {
109         my $db_name    = $item[4]->{'db_name'} || '';
110         my $table_name = $item[4]->{'name'};
111
112         $tables{ $table_name }{'name'}         = $table_name;
113         $tables{ $table_name }{'is_temporary'} = $item[2][0] ? 1 : 0;
114         $tables{ $table_name }{'order'}        = ++$table_order;
115
116         for my $def ( @{ $item[6] } ) {
117             if ( $def->{'supertype'} eq 'column' ) {
118                 push @{ $tables{ $table_name }{'columns'} }, $def;
119             }
120             elsif ( $def->{'supertype'} eq 'constraint' ) {
121                 push @{ $tables{ $table_name }{'constraints'} }, $def;
122             }
123         }
124     }
125
126 definition : constraint_def | column_def 
127
128 column_def: comment(s?) NAME type(?) column_constraint(s?)
129     {
130         my $column = {
131             supertype      => 'column',
132             name           => $item[2],
133             data_type      => $item[3][0]->{'type'},
134             size           => $item[3][0]->{'size'},
135             is_nullable    => 1,
136             is_primary_key => 0,
137             is_unique      => 0,
138             check          => '',
139             default        => undef,
140             constraints    => $item[4],
141             comments       => $item[1],
142         };
143
144
145         for my $c ( @{ $item[4] } ) {
146             if ( $c->{'type'} eq 'not_null' ) {
147                 $column->{'is_nullable'} = 0;
148             }
149             elsif ( $c->{'type'} eq 'primary_key' ) {
150                 $column->{'is_primary_key'} = 1;
151             }
152             elsif ( $c->{'type'} eq 'unique' ) {
153                 $column->{'is_unique'} = 1;
154             }
155             elsif ( $c->{'type'} eq 'check' ) {
156                 $column->{'check'} = $c->{'expression'};
157             }
158             elsif ( $c->{'type'} eq 'default' ) {
159                 $column->{'default'} = $c->{'value'};
160             }
161         }
162
163         $column;
164     }
165
166 type : WORD parens_value_list(?)
167     {
168         $return = {
169             type => $item[1],
170             size => $item[2][0],
171         }
172     }
173
174 column_constraint : NOT_NULL conflict_clause(?)
175     {
176         $return = {
177             type => 'not_null',
178         }
179     }
180     |
181     PRIMARY_KEY sort_order(?) conflict_clause(?)
182     {
183         $return = {
184             type        => 'primary_key',
185             sort_order  => $item[2][0],
186             on_conflict => $item[2][0], 
187         }
188     }
189     |
190     UNIQUE conflict_clause(?)
191     {
192         $return = {
193             type        => 'unique',
194             on_conflict => $item[2][0], 
195         }
196     }
197     |
198     CHECK_C '(' expr ')' conflict_clause(?)
199     {
200         $return = {
201             type        => 'check',
202             expression  => $item[3],
203             on_conflict => $item[5][0], 
204         }
205     }
206     |
207     DEFAULT VALUE
208     {
209         $return   = {
210             type  => 'default',
211             value => $item[2],
212         }
213     }
214
215 constraint_def : PRIMARY_KEY parens_field_list conflict_clause(?)
216     {
217         $return         = {
218             supertype   => 'constraint',
219             type        => 'primary_key',
220             columns      => $item[2],
221             on_conflict => $item[3][0],
222         }
223     }
224     |
225     UNIQUE parens_field_list conflict_clause(?)
226     {
227         $return         = {
228             supertype   => 'constraint',
229             type        => 'unique',
230             columns      => $item[2],
231             on_conflict => $item[3][0],
232         }
233     }
234     |
235     CHECK_C '(' expr ')' conflict_clause(?)
236     {
237         $return         = {
238             supertype   => 'constraint',
239             type        => 'check',
240             expression  => $item[3],
241             on_conflict => $item[5][0],
242         }
243     }
244
245 table_name : qualified_name
246     
247 qualified_name : NAME 
248     { $return = { name => $item[1] } }
249
250 qualified_name : /(\w+)\.(\w+)/ 
251     { $return = { db_name => $1, name => $2 } }
252
253 field_name : NAME
254
255 conflict_clause : /on conflict/i conflict_algorigthm
256
257 conflict_algorigthm : /(rollback|abort|fail|ignore|replace)/i
258
259 parens_field_list : '(' column_list ')'
260     { $item[2] }
261
262 column_list : field_name(s /,/)
263
264 parens_value_list : '(' VALUE(s /,/) ')'
265     { $item[2] }
266
267 expr : /[^)]+/
268
269 sort_order : /(ASC|DESC)/i
270
271 #
272 # Create Trigger
273
274 create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON table_name trigger_action SEMICOLON
275     {
276         my $table_name = $item[8]->{'name'};
277         push @triggers, {
278             name         => $item[4],
279             is_temporary => $item[2][0] ? 1 : 0,
280             when         => $item[5][0],
281             instead_of   => 0,
282             db_events    => [ $item[6] ],
283             action       => $item[9],
284             on_table     => $table_name,
285         }
286     }
287
288 create : CREATE TEMPORARY(?) TRIGGER NAME instead_of database_event ON view_name trigger_action
289     {
290         my $table_name = $item[8]->{'name'};
291         push @triggers, {
292             name         => $item[4],
293             is_temporary => $item[2][0] ? 1 : 0,
294             when         => undef,
295             instead_of   => 1,
296             db_events    => [ $item[6] ],
297             action       => $item[9],
298             on_table     => $table_name,
299         }
300     }
301
302 database_event : /(delete|insert|update)/i
303
304 database_event : /update of/i column_list
305
306 trigger_action : for_each(?) when(?) BEGIN_C trigger_step(s) END_C
307     {
308         $return = {
309             for_each => $item[1][0],
310             when     => $item[2][0],
311             steps    => $item[4],
312         }
313     }
314
315 for_each : /FOR EACH ROW/i
316
317 when : WHEN expr { $item[2] }
318
319 string :
320    /'(\\.|''|[^\\\'])*'/ 
321
322 nonstring : /[^;\'"]+/
323
324 statement_body : string | nonstring
325
326 trigger_step : /(select|delete|insert|update)/i statement_body(s?) SEMICOLON
327     {
328         $return = join( ' ', $item[1], join ' ', @{ $item[2] || [] } )
329     }   
330
331 before_or_after : /(before|after)/i { $return = lc $1 }
332
333 instead_of : /instead of/i
334
335 if_exists : /if exists/i
336
337 view_name : qualified_name
338
339 trigger_name : qualified_name
340
341 #
342 # Create View
343 #
344 create : CREATE TEMPORARY(?) VIEW view_name AS select_statement 
345     {
346         push @views, {
347             name         => $item[4]->{'name'},
348             sql          => $item[6], 
349             is_temporary => $item[2][0] ? 1 : 0,
350         }
351     }
352
353 select_statement : SELECT /[^;]+/ SEMICOLON
354     {
355         $return = join( ' ', $item[1], $item[2] );
356     }
357
358 #
359 # Tokens
360 #
361 BEGIN_C : /begin/i
362
363 END_C : /end/i
364
365 TRANSACTION: /transaction/i
366
367 CREATE : /create/i
368
369 TEMPORARY : /temp(orary)?/i { 1 }
370
371 TABLE : /table/i
372
373 INDEX : /index/i
374
375 NOT_NULL : /not null/i
376
377 PRIMARY_KEY : /primary key/i
378
379 CHECK_C : /check/i
380
381 DEFAULT : /default/i
382
383 TRIGGER : /trigger/i
384
385 VIEW : /view/i
386
387 SELECT : /select/i
388
389 ON : /on/i
390
391 AS : /as/i
392
393 WORD : /\w+/
394
395 WHEN : /when/i
396
397 UNIQUE : /unique/i { 1 }
398
399 SEMICOLON : ';'
400
401 NAME : /'?(\w+)'?/ { $return = $1 }
402
403 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
404     { $item[1] }
405     | /'.*?'/   
406     { 
407         # remove leading/trailing quotes 
408         my $val = $item[1];
409         $val    =~ s/^['"]|['"]$//g;
410         $return = $val;
411     }
412     | /NULL/
413     { 'NULL' }
414     | /CURRENT_TIMESTAMP/i
415     { 'CURRENT_TIMESTAMP' }
416 !;
417 }
418 }