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 # -------------------------------------------------------------------
22 method _build_grammar {
25 my ( %tables, $table_order, @table_comments, @views, @triggers );
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,
34 startrule : statement(s) eofile {
38 triggers => \@triggers,
44 statement : begin_transaction
51 begin_transaction : /begin/i TRANSACTION(?) SEMICOLON
53 commit : /commit/i SEMICOLON
55 drop : /drop/i (tbl_drop | view_drop | trg_drop) SEMICOLON
57 tbl_drop: TABLE <commit> table_name
59 view_drop: VIEW if_exists(?) view_name
61 trg_drop: TRIGGER if_exists(?) trigger_name
63 comment : /^\s*(?:#|-{2}).*\n/
65 my $comment = $item[1];
66 $comment =~ s/^\s*(#|-{2})\s*//;
71 comment : /\/\*/ /[^\*]+/ /\*\//
73 my $comment = $item[2];
74 $comment =~ s/^\s*|\s*$//g;
81 create : CREATE TEMPORARY(?) UNIQUE(?) INDEX WORD ON table_name parens_field_list conflict_clause(?) SEMICOLON
83 my $db_name = $item[7]->{'db_name'} || '';
84 my $table_name = $item[7]->{'name'};
89 on_conflict => $item[9][0],
90 is_temporary => $item[2][0] ? 1 : 0,
93 my $is_unique = $item[3][0];
96 $index->{'type'} = 'unique';
97 push @{ $tables{ $table_name }{'constraints'} }, $index;
100 push @{ $tables{ $table_name }{'indices'} }, $index;
107 create : CREATE TEMPORARY(?) TABLE table_name '(' definition(s /,/) ')' SEMICOLON
109 my $db_name = $item[4]->{'db_name'} || '';
110 my $table_name = $item[4]->{'name'};
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;
116 for my $def ( @{ $item[6] } ) {
117 if ( $def->{'supertype'} eq 'column' ) {
118 push @{ $tables{ $table_name }{'columns'} }, $def;
120 elsif ( $def->{'supertype'} eq 'constraint' ) {
121 push @{ $tables{ $table_name }{'constraints'} }, $def;
126 definition : constraint_def | column_def
128 column_def: comment(s?) NAME type(?) column_constraint(s?)
131 supertype => 'column',
133 data_type => $item[3][0]->{'type'},
134 size => $item[3][0]->{'size'},
140 constraints => $item[4],
141 comments => $item[1],
145 for my $c ( @{ $item[4] } ) {
146 if ( $c->{'type'} eq 'not_null' ) {
147 $column->{'is_nullable'} = 0;
149 elsif ( $c->{'type'} eq 'primary_key' ) {
150 $column->{'is_primary_key'} = 1;
152 elsif ( $c->{'type'} eq 'unique' ) {
153 $column->{'is_unique'} = 1;
155 elsif ( $c->{'type'} eq 'check' ) {
156 $column->{'check'} = $c->{'expression'};
158 elsif ( $c->{'type'} eq 'default' ) {
159 $column->{'default'} = $c->{'value'};
166 type : WORD parens_value_list(?)
174 column_constraint : NOT_NULL conflict_clause(?)
181 PRIMARY_KEY sort_order(?) conflict_clause(?)
184 type => 'primary_key',
185 sort_order => $item[2][0],
186 on_conflict => $item[2][0],
190 UNIQUE conflict_clause(?)
194 on_conflict => $item[2][0],
198 CHECK_C '(' expr ')' conflict_clause(?)
202 expression => $item[3],
203 on_conflict => $item[5][0],
215 constraint_def : PRIMARY_KEY parens_field_list conflict_clause(?)
218 supertype => 'constraint',
219 type => 'primary_key',
221 on_conflict => $item[3][0],
225 UNIQUE parens_field_list conflict_clause(?)
228 supertype => 'constraint',
231 on_conflict => $item[3][0],
235 CHECK_C '(' expr ')' conflict_clause(?)
238 supertype => 'constraint',
240 expression => $item[3],
241 on_conflict => $item[5][0],
245 table_name : qualified_name
247 qualified_name : NAME
248 { $return = { name => $item[1] } }
250 qualified_name : /(\w+)\.(\w+)/
251 { $return = { db_name => $1, name => $2 } }
255 conflict_clause : /on conflict/i conflict_algorigthm
257 conflict_algorigthm : /(rollback|abort|fail|ignore|replace)/i
259 parens_field_list : '(' column_list ')'
262 column_list : field_name(s /,/)
264 parens_value_list : '(' VALUE(s /,/) ')'
269 sort_order : /(ASC|DESC)/i
274 create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON table_name trigger_action SEMICOLON
276 my $table_name = $item[8]->{'name'};
279 is_temporary => $item[2][0] ? 1 : 0,
282 db_events => [ $item[6] ],
284 on_table => $table_name,
288 create : CREATE TEMPORARY(?) TRIGGER NAME instead_of database_event ON view_name trigger_action
290 my $table_name = $item[8]->{'name'};
293 is_temporary => $item[2][0] ? 1 : 0,
296 db_events => [ $item[6] ],
298 on_table => $table_name,
302 database_event : /(delete|insert|update)/i
304 database_event : /update of/i column_list
306 trigger_action : for_each(?) when(?) BEGIN_C trigger_step(s) END_C
309 for_each => $item[1][0],
315 for_each : /FOR EACH ROW/i
317 when : WHEN expr { $item[2] }
320 /'(\\.|''|[^\\\'])*'/
322 nonstring : /[^;\'"]+/
324 statement_body : string | nonstring
326 trigger_step : /(select|delete|insert|update)/i statement_body(s?) SEMICOLON
328 $return = join( ' ', $item[1], join ' ', @{ $item[2] || [] } )
331 before_or_after : /(before|after)/i { $return = lc $1 }
333 instead_of : /instead of/i
335 if_exists : /if exists/i
337 view_name : qualified_name
339 trigger_name : qualified_name
344 create : CREATE TEMPORARY(?) VIEW view_name AS select_statement
347 name => $item[4]->{'name'},
349 is_temporary => $item[2][0] ? 1 : 0,
353 select_statement : SELECT /[^;]+/ SEMICOLON
355 $return = join( ' ', $item[1], $item[2] );
365 TRANSACTION: /transaction/i
369 TEMPORARY : /temp(orary)?/i { 1 }
375 NOT_NULL : /not null/i
377 PRIMARY_KEY : /primary key/i
397 UNIQUE : /unique/i { 1 }
401 NAME : /'?(\w+)'?/ { $return = $1 }
403 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
407 # remove leading/trailing quotes
409 $val =~ s/^['"]|['"]$//g;
414 | /CURRENT_TIMESTAMP/i
415 { 'CURRENT_TIMESTAMP' }