2 role SQL::Translator::Grammar::SQLite {
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
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.
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.
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
19 # -------------------------------------------------------------------
21 method _build_grammar {
24 my ( %tables, $table_order, @table_comments, @views, @triggers );
28 # The "eofile" rule makes the parser fail if any "statement" rule
29 # fails. Otherwise, the first successful match by a "statement"
30 # won't cause the failure needed to know that the parse, as a whole,
33 startrule : statement(s) eofile {
37 triggers => \@triggers,
43 statement : begin_transaction
50 begin_transaction : /begin/i TRANSACTION(?) SEMICOLON
52 commit : /commit/i SEMICOLON
54 drop : /drop/i (tbl_drop | view_drop | trg_drop) SEMICOLON
56 tbl_drop: TABLE <commit> table_name
58 view_drop: VIEW if_exists(?) view_name
60 trg_drop: TRIGGER if_exists(?) trigger_name
62 comment : /^\s*(?:#|-{2}).*\n/
64 my $comment = $item[1];
65 $comment =~ s/^\s*(#|-{2})\s*//;
70 comment : /\/\*/ /[^\*]+/ /\*\//
72 my $comment = $item[2];
73 $comment =~ s/^\s*|\s*$//g;
80 create : CREATE TEMPORARY(?) UNIQUE(?) INDEX WORD ON table_name parens_field_list conflict_clause(?) SEMICOLON
82 my $db_name = $item[7]->{'db_name'} || '';
83 my $table_name = $item[7]->{'name'};
88 on_conflict => $item[9][0],
89 is_temporary => $item[2][0] ? 1 : 0,
92 my $is_unique = $item[3][0];
95 $index->{'type'} = 'unique';
96 push @{ $tables{ $table_name }{'constraints'} }, $index;
99 push @{ $tables{ $table_name }{'indices'} }, $index;
106 create : CREATE TEMPORARY(?) TABLE table_name '(' definition(s /,/) ')' SEMICOLON
108 my $db_name = $item[4]->{'db_name'} || '';
109 my $table_name = $item[4]->{'name'};
111 $tables{ $table_name }{'name'} = $table_name;
112 $tables{ $table_name }{'is_temporary'} = $item[2][0] ? 1 : 0;
113 $tables{ $table_name }{'order'} = ++$table_order;
115 for my $def ( @{ $item[6] } ) {
116 if ( $def->{'supertype'} eq 'column' ) {
117 push @{ $tables{ $table_name }{'columns'} }, $def;
119 elsif ( $def->{'supertype'} eq 'constraint' ) {
120 push @{ $tables{ $table_name }{'constraints'} }, $def;
125 definition : constraint_def | column_def
127 column_def: comment(s?) NAME type(?) column_constraint(s?)
130 supertype => 'column',
132 data_type => $item[3][0]->{'type'},
133 size => $item[3][0]->{'size'},
139 constraints => $item[4],
140 comments => $item[1],
144 for my $c ( @{ $item[4] } ) {
145 if ( $c->{'type'} eq 'not_null' ) {
146 $column->{'is_nullable'} = 0;
148 elsif ( $c->{'type'} eq 'primary_key' ) {
149 $column->{'is_primary_key'} = 1;
151 elsif ( $c->{'type'} eq 'unique' ) {
152 $column->{'is_unique'} = 1;
154 elsif ( $c->{'type'} eq 'check' ) {
155 $column->{'check'} = $c->{'expression'};
157 elsif ( $c->{'type'} eq 'default' ) {
158 $column->{'default'} = $c->{'value'};
165 type : WORD parens_value_list(?)
173 column_constraint : NOT_NULL conflict_clause(?)
180 PRIMARY_KEY sort_order(?) conflict_clause(?)
183 type => 'primary_key',
184 sort_order => $item[2][0],
185 on_conflict => $item[2][0],
189 UNIQUE conflict_clause(?)
193 on_conflict => $item[2][0],
197 CHECK_C '(' expr ')' conflict_clause(?)
201 expression => $item[3],
202 on_conflict => $item[5][0],
214 constraint_def : PRIMARY_KEY parens_field_list conflict_clause(?)
217 supertype => 'constraint',
218 type => 'primary_key',
220 on_conflict => $item[3][0],
224 UNIQUE parens_field_list conflict_clause(?)
227 supertype => 'constraint',
230 on_conflict => $item[3][0],
234 CHECK_C '(' expr ')' conflict_clause(?)
237 supertype => 'constraint',
239 expression => $item[3],
240 on_conflict => $item[5][0],
244 table_name : qualified_name
246 qualified_name : NAME
247 { $return = { name => $item[1] } }
249 qualified_name : /(\w+)\.(\w+)/
250 { $return = { db_name => $1, name => $2 } }
254 conflict_clause : /on conflict/i conflict_algorigthm
256 conflict_algorigthm : /(rollback|abort|fail|ignore|replace)/i
258 parens_field_list : '(' column_list ')'
261 column_list : field_name(s /,/)
263 parens_value_list : '(' VALUE(s /,/) ')'
268 sort_order : /(ASC|DESC)/i
273 create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON table_name trigger_action SEMICOLON
275 my $table_name = $item[8]->{'name'};
278 is_temporary => $item[2][0] ? 1 : 0,
281 db_events => [ $item[6] ],
283 on_table => $table_name,
287 create : CREATE TEMPORARY(?) TRIGGER NAME instead_of database_event ON view_name trigger_action
289 my $table_name = $item[8]->{'name'};
292 is_temporary => $item[2][0] ? 1 : 0,
295 db_events => [ $item[6] ],
297 on_table => $table_name,
301 database_event : /(delete|insert|update)/i
303 database_event : /update of/i column_list
305 trigger_action : for_each(?) when(?) BEGIN_C trigger_step(s) END_C
308 for_each => $item[1][0],
314 for_each : /FOR EACH ROW/i
316 when : WHEN expr { $item[2] }
319 /'(\\.|''|[^\\\'])*'/
321 nonstring : /[^;\'"]+/
323 statement_body : string | nonstring
325 trigger_step : /(select|delete|insert|update)/i statement_body(s?) SEMICOLON
327 $return = join( ' ', $item[1], join ' ', @{ $item[2] || [] } )
330 before_or_after : /(before|after)/i { $return = lc $1 }
332 instead_of : /instead of/i
334 if_exists : /if exists/i
336 view_name : qualified_name
338 trigger_name : qualified_name
343 create : CREATE TEMPORARY(?) VIEW view_name AS select_statement
346 name => $item[4]->{'name'},
348 is_temporary => $item[2][0] ? 1 : 0,
352 select_statement : SELECT /[^;]+/ SEMICOLON
354 $return = join( ' ', $item[1], $item[2] );
364 TRANSACTION: /transaction/i
368 TEMPORARY : /temp(orary)?/i { 1 }
374 NOT_NULL : /not null/i
376 PRIMARY_KEY : /primary key/i
396 UNIQUE : /unique/i { 1 }
400 NAME : /'?(\w+)'?/ { $return = $1 }
402 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
406 # remove leading/trailing quotes
408 $val =~ s/^['"]|['"]$//g;
413 | /CURRENT_TIMESTAMP/i
414 { 'CURRENT_TIMESTAMP' }