Actually there was an empty test for it as well :)
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / SQLite.pm
CommitLineData
72aa2647 1package SQL::Translator::Parser::SQLite;
2
3# -------------------------------------------------------------------
d4f84dd1 4# $Id: SQLite.pm 1445 2009-02-07 17:50:03Z ashberlin $
72aa2647 5# -------------------------------------------------------------------
478f608d 6# Copyright (C) 2002-2009 SQLFairy Authors
72aa2647 7#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License as
10# published by the Free Software Foundation; version 2.
11#
12# This program is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15# General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=head1 NAME
24
25SQL::Translator::Parser::SQLite - parser for SQLite
26
27=head1 SYNOPSIS
28
29 use SQL::Translator;
30 use SQL::Translator::Parser::SQLite;
31
32 my $translator = SQL::Translator->new;
33 $translator->parser("SQL::Translator::Parser::SQLite");
34
35=head1 DESCRIPTION
36
37This is a grammar for parsing CREATE statements for SQLite as
38described here:
39
40 http://www.sqlite.org/lang.html
41
42CREATE INDEX
43
44sql-statement ::=
45 CREATE [TEMP | TEMPORARY] [UNIQUE] INDEX index-name
46 ON [database-name .] table-name ( column-name [, column-name]* )
47 [ ON CONFLICT conflict-algorithm ]
48
49column-name ::=
50 name [ ASC | DESC ]
51
52CREATE TABLE
53
54sql-command ::=
55 CREATE [TEMP | TEMPORARY] TABLE table-name (
56 column-def [, column-def]*
57 [, constraint]*
58 )
59
60sql-command ::=
61 CREATE [TEMP | TEMPORARY] TABLE table-name AS select-statement
62
63column-def ::=
64 name [type] [[CONSTRAINT name] column-constraint]*
65
66type ::=
67 typename |
68 typename ( number ) |
69 typename ( number , number )
70
71column-constraint ::=
72 NOT NULL [ conflict-clause ] |
73 PRIMARY KEY [sort-order] [ conflict-clause ] |
74 UNIQUE [ conflict-clause ] |
75 CHECK ( expr ) [ conflict-clause ] |
76 DEFAULT value
77
78constraint ::=
79 PRIMARY KEY ( name [, name]* ) [ conflict-clause ]|
80 UNIQUE ( name [, name]* ) [ conflict-clause ] |
81 CHECK ( expr ) [ conflict-clause ]
82
83conflict-clause ::=
84 ON CONFLICT conflict-algorithm
85
86CREATE TRIGGER
87
88sql-statement ::=
89 CREATE [TEMP | TEMPORARY] TRIGGER trigger-name [ BEFORE | AFTER ]
90 database-event ON [database-name .] table-name
91 trigger-action
92
93sql-statement ::=
94 CREATE [TEMP | TEMPORARY] TRIGGER trigger-name INSTEAD OF
95 database-event ON [database-name .] view-name
96 trigger-action
97
98database-event ::=
99 DELETE |
100 INSERT |
101 UPDATE |
102 UPDATE OF column-list
103
104trigger-action ::=
105 [ FOR EACH ROW | FOR EACH STATEMENT ] [ WHEN expression ]
106 BEGIN
107 trigger-step ; [ trigger-step ; ]*
108 END
109
110trigger-step ::=
111 update-statement | insert-statement |
51af147e 112 delete-statement | select-statement
72aa2647 113
114CREATE VIEW
115
116sql-command ::=
117 CREATE [TEMP | TEMPORARY] VIEW view-name AS select-statement
118
119ON CONFLICT clause
120
121 conflict-clause ::=
122 ON CONFLICT conflict-algorithm
123
124 conflict-algorithm ::=
125 ROLLBACK | ABORT | FAIL | IGNORE | REPLACE
126
127expression
128
129expr ::=
130 expr binary-op expr |
131 expr like-op expr |
132 unary-op expr |
133 ( expr ) |
134 column-name |
135 table-name . column-name |
136 database-name . table-name . column-name |
137 literal-value |
138 function-name ( expr-list | * ) |
139 expr (+) |
140 expr ISNULL |
141 expr NOTNULL |
142 expr [NOT] BETWEEN expr AND expr |
143 expr [NOT] IN ( value-list ) |
144 expr [NOT] IN ( select-statement ) |
145 ( select-statement ) |
146 CASE [expr] ( WHEN expr THEN expr )+ [ELSE expr] END
147
148like-op::=
149 LIKE | GLOB | NOT LIKE | NOT GLOB
150
151=cut
152
153use strict;
478f608d 154use vars qw[ $DEBUG $GRAMMAR @EXPORT_OK ];
72aa2647 155$DEBUG = 0 unless defined $DEBUG;
156
157use Data::Dumper;
158use Parse::RecDescent;
159use Exporter;
160use base qw(Exporter);
161
162@EXPORT_OK = qw(parse);
163
164# Enable warnings within the Parse::RecDescent module.
165$::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
166$::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
167$::RD_HINT = 1; # Give out hints to help fix problems.
168
169$GRAMMAR = q!
170
171{
70698e1c 172 my ( %tables, $table_order, @table_comments, @views, @triggers );
72aa2647 173}
174
175#
176# The "eofile" rule makes the parser fail if any "statement" rule
177# fails. Otherwise, the first successful match by a "statement"
178# won't cause the failure needed to know that the parse, as a whole,
179# failed. -ky
180#
70698e1c 181startrule : statement(s) eofile {
8c12c406 182 $return = {
70698e1c 183 tables => \%tables,
184 views => \@views,
185 triggers => \@triggers,
186 }
187}
72aa2647 188
189eofile : /^\Z/
190
191statement : begin_transaction
192 | commit
ac9c4e2e 193 | drop
72aa2647 194 | comment
195 | create
196 | <error>
197
f9c96971 198begin_transaction : /begin/i TRANSACTION(?) SEMICOLON
72aa2647 199
200commit : /commit/i SEMICOLON
201
ac9c4e2e 202drop : /drop/i TABLE <commit> table_name SEMICOLON
203
72aa2647 204comment : /^\s*(?:#|-{2}).*\n/
205 {
206 my $comment = $item[1];
207 $comment =~ s/^\s*(#|-{2})\s*//;
208 $comment =~ s/\s*$//;
209 $return = $comment;
210 }
211
212comment : /\/\*/ /[^\*]+/ /\*\//
213 {
214 my $comment = $item[2];
215 $comment =~ s/^\s*|\s*$//g;
216 $return = $comment;
217 }
218
219#
220# Create Index
221#
222create : CREATE TEMPORARY(?) UNIQUE(?) INDEX WORD ON table_name parens_field_list conflict_clause(?) SEMICOLON
223 {
224 my $db_name = $item[7]->{'db_name'} || '';
225 my $table_name = $item[7]->{'name'};
226
227 my $index = {
228 name => $item[5],
229 fields => $item[8],
230 on_conflict => $item[9][0],
231 is_temporary => $item[2][0] ? 1 : 0,
232 };
233
234 my $is_unique = $item[3][0];
235
236 if ( $is_unique ) {
237 $index->{'type'} = 'unique';
238 push @{ $tables{ $table_name }{'constraints'} }, $index;
239 }
240 else {
241 push @{ $tables{ $table_name }{'indices'} }, $index;
242 }
243 }
244
245#
246# Create Table
247#
248create : CREATE TEMPORARY(?) TABLE table_name '(' definition(s /,/) ')' SEMICOLON
249 {
250 my $db_name = $item[4]->{'db_name'} || '';
251 my $table_name = $item[4]->{'name'};
252
253 $tables{ $table_name }{'name'} = $table_name;
254 $tables{ $table_name }{'is_temporary'} = $item[2][0] ? 1 : 0;
255 $tables{ $table_name }{'order'} = ++$table_order;
256
257 for my $def ( @{ $item[6] } ) {
258 if ( $def->{'supertype'} eq 'column' ) {
259 push @{ $tables{ $table_name }{'fields'} }, $def;
260 }
261 elsif ( $def->{'supertype'} eq 'constraint' ) {
262 push @{ $tables{ $table_name }{'constraints'} }, $def;
263 }
264 }
265 }
266
267definition : constraint_def | column_def
268
269column_def: NAME type(?) column_constraint(s?)
270 {
271 my $column = {
272 supertype => 'column',
273 name => $item[1],
274 data_type => $item[2][0]->{'type'},
275 size => $item[2][0]->{'size'},
276 is_nullable => 1,
277 is_primary_key => 0,
278 is_unique => 0,
279 check => '',
280 default => undef,
281 constraints => $item[3],
282 };
283
284 for my $c ( @{ $item[3] } ) {
285 if ( $c->{'type'} eq 'not_null' ) {
286 $column->{'is_nullable'} = 0;
287 }
288 elsif ( $c->{'type'} eq 'primary_key' ) {
289 $column->{'is_primary_key'} = 1;
290 }
291 elsif ( $c->{'type'} eq 'unique' ) {
292 $column->{'is_unique'} = 1;
293 }
294 elsif ( $c->{'type'} eq 'check' ) {
295 $column->{'check'} = $c->{'expression'};
296 }
297 elsif ( $c->{'type'} eq 'default' ) {
298 $column->{'default'} = $c->{'value'};
299 }
300 }
301
302 $column;
303 }
304
305type : WORD parens_value_list(?)
306 {
307 $return = {
308 type => $item[1],
309 size => $item[2][0],
310 }
311 }
312
313column_constraint : NOT_NULL conflict_clause(?)
314 {
315 $return = {
316 type => 'not_null',
317 }
318 }
319 |
320 PRIMARY_KEY sort_order(?) conflict_clause(?)
321 {
322 $return = {
323 type => 'primary_key',
324 sort_order => $item[2][0],
325 on_conflict => $item[2][0],
326 }
327 }
328 |
329 UNIQUE conflict_clause(?)
330 {
331 $return = {
332 type => 'unique',
333 on_conflict => $item[2][0],
334 }
335 }
336 |
337 CHECK_C '(' expr ')' conflict_clause(?)
338 {
339 $return = {
340 type => 'check',
341 expression => $item[3],
342 on_conflict => $item[5][0],
343 }
344 }
345 |
346 DEFAULT VALUE
347 {
348 $return = {
349 type => 'default',
350 value => $item[2],
351 }
352 }
353
354constraint_def : PRIMARY_KEY parens_field_list conflict_clause(?)
355 {
356 $return = {
357 supertype => 'constraint',
358 type => 'primary_key',
359 fields => $item[2],
360 on_conflict => $item[3][0],
361 }
362 }
363 |
364 UNIQUE parens_field_list conflict_clause(?)
365 {
366 $return = {
367 supertype => 'constraint',
368 type => 'unique',
369 fields => $item[2],
370 on_conflict => $item[3][0],
371 }
372 }
373 |
374 CHECK_C '(' expr ')' conflict_clause(?)
375 {
376 $return = {
377 supertype => 'constraint',
378 type => 'check',
379 expression => $item[3],
380 on_conflict => $item[5][0],
381 }
382 }
383
384table_name : qualified_name
385
386qualified_name : NAME
387 { $return = { name => $item[1] } }
388
389qualified_name : /(\w+)\.(\w+)/
390 { $return = { db_name => $1, name => $2 } }
391
392field_name : NAME
393
394conflict_clause : /on conflict/i conflict_algorigthm
395
396conflict_algorigthm : /(rollback|abort|fail|ignore|replace)/i
397
398parens_field_list : '(' column_list ')'
399 { $item[2] }
400
401column_list : field_name(s /,/)
402
403parens_value_list : '(' VALUE(s /,/) ')'
404 { $item[2] }
405
406expr : /[^)]+/
407
408sort_order : /(ASC|DESC)/i
409
410#
411# Create Trigger
412
2661d702 413create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON table_name trigger_action SEMICOLON
72aa2647 414 {
415 my $table_name = $item[8]->{'name'};
70698e1c 416 push @triggers, {
72aa2647 417 name => $item[4],
418 is_temporary => $item[2][0] ? 1 : 0,
419 when => $item[5][0],
420 instead_of => 0,
d0fcb05d 421 db_events => [ $item[6] ],
72aa2647 422 action => $item[9],
da2f5992 423 on_table => $table_name,
72aa2647 424 }
425 }
426
427create : CREATE TEMPORARY(?) TRIGGER NAME instead_of database_event ON view_name trigger_action
428 {
429 my $table_name = $item[8]->{'name'};
70698e1c 430 push @triggers, {
72aa2647 431 name => $item[4],
432 is_temporary => $item[2][0] ? 1 : 0,
433 when => undef,
434 instead_of => 1,
d0fcb05d 435 db_events => [ $item[6] ],
72aa2647 436 action => $item[9],
da2f5992 437 on_table => $table_name,
72aa2647 438 }
439 }
440
441database_event : /(delete|insert|update)/i
442
443database_event : /update of/i column_list
444
445trigger_action : for_each(?) when(?) BEGIN_C trigger_step(s) END_C
446 {
447 $return = {
448 for_each => $item[1][0],
449 when => $item[2][0],
450 steps => $item[4],
451 }
452 }
453
d0fcb05d 454for_each : /FOR EACH ROW/i
72aa2647 455
456when : WHEN expr { $item[2] }
457
9bf756df 458string :
459 /'(\\.|''|[^\\\'])*'/
460
461nonstring : /[^;\'"]+/
462
463statement_body : (string | nonstring)(s?)
464
465trigger_step : /(select|delete|insert|update)/i statement_body SEMICOLON
72aa2647 466 {
67a1819d 467 $return = join( ' ', $item[1], join ' ', @{ $item[2] || [] } )
72aa2647 468 }
469
470before_or_after : /(before|after)/i { $return = lc $1 }
471
472instead_of : /instead of/i
473
474view_name : qualified_name
475
476#
70698e1c 477# Create View
478#
479create : CREATE TEMPORARY(?) VIEW view_name AS select_statement
480 {
481 push @views, {
482 name => $item[4]->{'name'},
483 sql => $item[6],
484 is_temporary => $item[2][0] ? 1 : 0,
485 }
486 }
487
488select_statement : SELECT /[^;]+/ SEMICOLON
489 {
490 $return = join( ' ', $item[1], $item[2] );
491 }
492
493#
72aa2647 494# Tokens
495#
496BEGIN_C : /begin/i
497
498END_C : /end/i
499
f9c96971 500TRANSACTION: /transaction/i
501
72aa2647 502CREATE : /create/i
503
504TEMPORARY : /temp(orary)?/i { 1 }
505
506TABLE : /table/i
507
508INDEX : /index/i
509
510NOT_NULL : /not null/i
511
512PRIMARY_KEY : /primary key/i
513
514CHECK_C : /check/i
515
516DEFAULT : /default/i
517
518TRIGGER : /trigger/i
519
70698e1c 520VIEW : /view/i
521
522SELECT : /select/i
523
72aa2647 524ON : /on/i
525
70698e1c 526AS : /as/i
527
72aa2647 528WORD : /\w+/
529
530WHEN : /when/i
531
532UNIQUE : /unique/i { 1 }
533
534SEMICOLON : ';'
535
70698e1c 536NAME : /'?(\w+)'?/ { $return = $1 }
72aa2647 537
70698e1c 538VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
72aa2647 539 { $item[1] }
540 | /'.*?'/
541 {
542 # remove leading/trailing quotes
543 my $val = $item[1];
544 $val =~ s/^['"]|['"]$//g;
545 $return = $val;
546 }
547 | /NULL/
548 { 'NULL' }
ac9c4e2e 549 | /CURRENT_TIMESTAMP/i
550 { 'CURRENT_TIMESTAMP' }
72aa2647 551
552!;
553
554# -------------------------------------------------------------------
555sub parse {
556 my ( $translator, $data ) = @_;
557 my $parser = Parse::RecDescent->new($GRAMMAR);
558
559 local $::RD_TRACE = $translator->trace ? 1 : undef;
560 local $DEBUG = $translator->debug;
561
562 unless (defined $parser) {
563 return $translator->error("Error instantiating Parse::RecDescent ".
564 "instance: Bad grammer");
565 }
566
567 my $result = $parser->startrule($data);
568 return $translator->error( "Parse failed." ) unless defined $result;
569 warn Dumper( $result ) if $DEBUG;
570
571 my $schema = $translator->schema;
b727fc08 572 my @tables =
573 map { $_->[1] }
574 sort { $a->[0] <=> $b->[0] }
575 map { [ $result->{'tables'}{ $_ }->{'order'}, $_ ] }
576 keys %{ $result->{'tables'} };
72aa2647 577
578 for my $table_name ( @tables ) {
70698e1c 579 my $tdata = $result->{'tables'}{ $table_name };
72aa2647 580 my $table = $schema->add_table(
581 name => $tdata->{'name'},
582 ) or die $schema->error;
583
584 $table->comments( $tdata->{'comments'} );
585
586 for my $fdata ( @{ $tdata->{'fields'} } ) {
587 my $field = $table->add_field(
588 name => $fdata->{'name'},
589 data_type => $fdata->{'data_type'},
590 size => $fdata->{'size'},
591 default_value => $fdata->{'default'},
592 is_auto_increment => $fdata->{'is_auto_inc'},
593 is_nullable => $fdata->{'is_nullable'},
594 comments => $fdata->{'comments'},
595 ) or die $table->error;
596
597 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
598
599 for my $cdata ( @{ $fdata->{'constraints'} } ) {
600 next unless $cdata->{'type'} eq 'foreign_key';
601 $cdata->{'fields'} ||= [ $field->name ];
602 push @{ $tdata->{'constraints'} }, $cdata;
603 }
604 }
605
606 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
607 my $index = $table->add_index(
608 name => $idata->{'name'},
609 type => uc $idata->{'type'},
610 fields => $idata->{'fields'},
611 ) or die $table->error;
612 }
613
614 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
615 my $constraint = $table->add_constraint(
616 name => $cdata->{'name'},
617 type => $cdata->{'type'},
618 fields => $cdata->{'fields'},
619 reference_table => $cdata->{'reference_table'},
620 reference_fields => $cdata->{'reference_fields'},
621 match_type => $cdata->{'match_type'} || '',
100684f3 622 on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'},
623 on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'},
72aa2647 624 ) or die $table->error;
625 }
626 }
627
70698e1c 628 for my $def ( @{ $result->{'views'} || [] } ) {
629 my $view = $schema->add_view(
630 name => $def->{'name'},
631 sql => $def->{'sql'},
632 );
633 }
634
635 for my $def ( @{ $result->{'triggers'} || [] } ) {
636 my $view = $schema->add_trigger(
637 name => $def->{'name'},
638 perform_action_when => $def->{'when'},
d0fcb05d 639 database_events => $def->{'db_events'},
70698e1c 640 action => $def->{'action'},
da2f5992 641 on_table => $def->{'on_table'},
70698e1c 642 );
643 }
644
72aa2647 645 return 1;
646}
647
6481;
649
650# -------------------------------------------------------------------
651# All wholsome food is caught without a net or a trap.
652# William Blake
653# -------------------------------------------------------------------
654
655=pod
656
657=head1 AUTHOR
658
659Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
660
661=head1 SEE ALSO
662
663perl(1), Parse::RecDescent, SQL::Translator::Schema.
664
665=cut