1 package SQL::Translator::Parser::SQLite;
5 SQL::Translator::Parser::SQLite - parser for SQLite
10 use SQL::Translator::Parser::SQLite;
12 my $translator = SQL::Translator->new;
13 $translator->parser("SQL::Translator::Parser::SQLite");
17 This is a grammar for parsing CREATE statements for SQLite as
20 http://www.sqlite.org/lang.html
25 CREATE [TEMP | TEMPORARY] [UNIQUE] INDEX index-name
26 ON [database-name .] table-name ( column-name [, column-name]* )
27 [ ON CONFLICT conflict-algorithm ]
35 CREATE [TEMP | TEMPORARY] TABLE table-name (
36 column-def [, column-def]*
41 CREATE [TEMP | TEMPORARY] TABLE table-name AS select-statement
44 name [type] [[CONSTRAINT name] column-constraint]*
49 typename ( number , number )
52 NOT NULL [ conflict-clause ] |
53 PRIMARY KEY [sort-order] [ conflict-clause ] |
54 UNIQUE [ conflict-clause ] |
55 CHECK ( expr ) [ conflict-clause ] |
59 PRIMARY KEY ( name [, name]* ) [ conflict-clause ]|
60 UNIQUE ( name [, name]* ) [ conflict-clause ] |
61 CHECK ( expr ) [ conflict-clause ]
64 ON CONFLICT conflict-algorithm
69 CREATE [TEMP | TEMPORARY] TRIGGER trigger-name [ BEFORE | AFTER ]
70 database-event ON [database-name .] table-name
74 CREATE [TEMP | TEMPORARY] TRIGGER trigger-name INSTEAD OF
75 database-event ON [database-name .] view-name
85 [ FOR EACH ROW | FOR EACH STATEMENT ] [ WHEN expression ]
87 trigger-step ; [ trigger-step ; ]*
91 update-statement | insert-statement |
92 delete-statement | select-statement
97 CREATE [TEMP | TEMPORARY] VIEW view-name AS select-statement
102 ON CONFLICT conflict-algorithm
104 conflict-algorithm ::=
105 ROLLBACK | ABORT | FAIL | IGNORE | REPLACE
110 expr binary-op expr |
115 table-name . column-name |
116 database-name . table-name . column-name |
118 function-name ( expr-list | * ) |
122 expr [NOT] BETWEEN expr AND expr |
123 expr [NOT] IN ( value-list ) |
124 expr [NOT] IN ( select-statement ) |
125 ( select-statement ) |
126 CASE [expr] ( WHEN expr THEN expr )+ [ELSE expr] END
129 LIKE | GLOB | NOT LIKE | NOT GLOB
135 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
137 $DEBUG = 0 unless defined $DEBUG;
140 use Parse::RecDescent;
142 use base qw(Exporter);
144 @EXPORT_OK = qw(parse);
146 # Enable warnings within the Parse::RecDescent module.
147 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
148 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
149 $::RD_HINT = 1; # Give out hints to help fix problems.
154 my ( %tables, $table_order, @table_comments, @views, @triggers );
158 # The "eofile" rule makes the parser fail if any "statement" rule
159 # fails. Otherwise, the first successful match by a "statement"
160 # won't cause the failure needed to know that the parse, as a whole,
163 startrule : statement(s) eofile {
167 triggers => \@triggers,
173 statement : begin_transaction
180 begin_transaction : /begin/i TRANSACTION(?) SEMICOLON
182 commit : /commit/i SEMICOLON
184 drop : /drop/i (tbl_drop | view_drop | trg_drop) SEMICOLON
186 tbl_drop: TABLE <commit> table_name
188 view_drop: VIEW if_exists(?) view_name
190 trg_drop: TRIGGER if_exists(?) trigger_name
192 comment : /^\s*(?:#|-{2}).*\n/
194 my $comment = $item[1];
195 $comment =~ s/^\s*(#|-{2})\s*//;
196 $comment =~ s/\s*$//;
200 comment : /\/\*/ /[^\*]+/ /\*\//
202 my $comment = $item[2];
203 $comment =~ s/^\s*|\s*$//g;
210 create : CREATE TEMPORARY(?) UNIQUE(?) INDEX NAME ON table_name parens_field_list conflict_clause(?) SEMICOLON
212 my $db_name = $item[7]->{'db_name'} || '';
213 my $table_name = $item[7]->{'name'};
218 on_conflict => $item[9][0],
219 is_temporary => $item[2][0] ? 1 : 0,
222 my $is_unique = $item[3][0];
225 $index->{'type'} = 'unique';
226 push @{ $tables{ $table_name }{'constraints'} }, $index;
229 push @{ $tables{ $table_name }{'indices'} }, $index;
236 create : CREATE TEMPORARY(?) TABLE table_name '(' definition(s /,/) ')' SEMICOLON
238 my $db_name = $item[4]->{'db_name'} || '';
239 my $table_name = $item[4]->{'name'};
241 $tables{ $table_name }{'name'} = $table_name;
242 $tables{ $table_name }{'is_temporary'} = $item[2][0] ? 1 : 0;
243 $tables{ $table_name }{'order'} = ++$table_order;
245 for my $def ( @{ $item[6] } ) {
246 if ( $def->{'supertype'} eq 'column' ) {
247 push @{ $tables{ $table_name }{'fields'} }, $def;
249 elsif ( $def->{'supertype'} eq 'constraint' ) {
250 push @{ $tables{ $table_name }{'constraints'} }, $def;
255 definition : constraint_def | column_def
257 column_def: comment(s?) NAME type(?) column_constraint_def(s?)
260 supertype => 'column',
262 data_type => $item[3][0]->{'type'},
263 size => $item[3][0]->{'size'},
269 constraints => $item[4],
270 comments => $item[1],
274 for my $c ( @{ $item[4] } ) {
275 if ( $c->{'type'} eq 'not_null' ) {
276 $column->{'is_nullable'} = 0;
278 elsif ( $c->{'type'} eq 'primary_key' ) {
279 $column->{'is_primary_key'} = 1;
281 elsif ( $c->{'type'} eq 'unique' ) {
282 $column->{'is_unique'} = 1;
284 elsif ( $c->{'type'} eq 'check' ) {
285 $column->{'check'} = $c->{'expression'};
287 elsif ( $c->{'type'} eq 'default' ) {
288 $column->{'default'} = $c->{'value'};
290 elsif ( $c->{'type'} eq 'autoincrement' ) {
291 $column->{'is_auto_inc'} = 1;
298 type : WORD parens_value_list(?)
306 column_constraint_def : CONSTRAINT constraint_name column_constraint
316 column_constraint : NOT_NULL conflict_clause(?)
323 PRIMARY_KEY sort_order(?) conflict_clause(?)
326 type => 'primary_key',
327 sort_order => $item[2][0],
328 on_conflict => $item[2][0],
332 UNIQUE conflict_clause(?)
336 on_conflict => $item[2][0],
340 CHECK_C '(' expr ')' conflict_clause(?)
344 expression => $item[3],
345 on_conflict => $item[5][0],
360 type => 'foreign_key',
361 reference_table => $item[2]{'reference_table'},
362 reference_fields => $item[2]{'reference_fields'},
369 type => 'autoincrement',
373 constraint_def : comment(s?) CONSTRAINT constraint_name table_constraint
376 comments => $item[1],
382 comment(s?) table_constraint
385 comments => $item[1],
390 table_constraint : PRIMARY_KEY parens_field_list conflict_clause(?)
393 supertype => 'constraint',
394 type => 'primary_key',
396 on_conflict => $item[3][0],
400 UNIQUE parens_field_list conflict_clause(?)
403 supertype => 'constraint',
406 on_conflict => $item[3][0],
410 CHECK_C '(' expr ')' conflict_clause(?)
413 supertype => 'constraint',
415 expression => $item[3],
416 on_conflict => $item[5][0],
420 FOREIGN_KEY parens_field_list REFERENCES ref_def
423 supertype => 'constraint',
424 type => 'foreign_key',
426 reference_table => $item[4]{'reference_table'},
427 reference_fields => $item[4]{'reference_fields'},
431 ref_def : /(\w+)\s*\((\w+)\)/
432 { $return = { reference_table => $1, reference_fields => $2 } }
434 table_name : qualified_name
436 qualified_name : NAME
437 { $return = { name => $item[1] } }
439 qualified_name : /(\w+)\.(\w+)/
440 { $return = { db_name => $1, name => $2 } }
444 constraint_name : NAME
446 conflict_clause : /on conflict/i conflict_algorigthm
448 conflict_algorigthm : /(rollback|abort|fail|ignore|replace)/i
450 parens_field_list : '(' column_list ')'
453 column_list : field_name(s /,/)
455 parens_value_list : '(' VALUE(s /,/) ')'
460 sort_order : /(ASC|DESC)/i
465 create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON table_name trigger_action SEMICOLON
467 my $table_name = $item[8]->{'name'};
470 is_temporary => $item[2][0] ? 1 : 0,
473 db_events => [ $item[6] ],
475 on_table => $table_name,
479 create : CREATE TEMPORARY(?) TRIGGER NAME instead_of database_event ON view_name trigger_action
481 my $table_name = $item[8]->{'name'};
484 is_temporary => $item[2][0] ? 1 : 0,
487 db_events => [ $item[6] ],
489 on_table => $table_name,
493 database_event : /(delete|insert|update)/i
495 database_event : /update of/i column_list
497 trigger_action : for_each(?) when(?) BEGIN_C trigger_step(s) END_C
500 for_each => $item[1][0],
506 for_each : /FOR EACH ROW/i
508 when : WHEN expr { $item[2] }
511 /'(\\.|''|[^\\\'])*'/
513 nonstring : /[^;\'"]+/
515 statement_body : string | nonstring
517 trigger_step : /(select|delete|insert|update)/i statement_body(s?) SEMICOLON
519 $return = join( ' ', $item[1], join ' ', @{ $item[2] || [] } )
522 before_or_after : /(before|after)/i { $return = lc $1 }
524 instead_of : /instead of/i
526 if_exists : /if exists/i
528 view_name : qualified_name
530 trigger_name : qualified_name
535 create : CREATE TEMPORARY(?) VIEW view_name AS select_statement
538 name => $item[4]->{'name'},
540 is_temporary => $item[2][0] ? 1 : 0,
544 select_statement : SELECT /[^;]+/ SEMICOLON
546 $return = join( ' ', $item[1], $item[2] );
556 TRANSACTION: /transaction/i
560 TEMPORARY : /temp(orary)?/i { 1 }
566 NOT_NULL : /not null/i
568 PRIMARY_KEY : /primary key/i
570 FOREIGN_KEY : /foreign key/i
590 REFERENCES : /references/i
592 CONSTRAINT : /constraint/i
594 AUTOINCREMENT : /autoincrement/i
596 UNIQUE : /unique/i { 1 }
600 NAME : /["']?(\w+)["']?/ { $return = $1 }
602 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
606 # remove leading/trailing quotes
608 $val =~ s/^['"]|['"]$//g;
613 | /CURRENT_TIMESTAMP/i
614 { 'CURRENT_TIMESTAMP' }
619 my ( $translator, $data ) = @_;
620 my $parser = Parse::RecDescent->new($GRAMMAR);
622 local $::RD_TRACE = $translator->trace ? 1 : undef;
623 local $DEBUG = $translator->debug;
625 unless (defined $parser) {
626 return $translator->error("Error instantiating Parse::RecDescent ".
627 "instance: Bad grammer");
630 my $result = $parser->startrule($data);
631 return $translator->error( "Parse failed." ) unless defined $result;
632 warn Dumper( $result ) if $DEBUG;
634 my $schema = $translator->schema;
637 sort { $a->[0] <=> $b->[0] }
638 map { [ $result->{'tables'}{ $_ }->{'order'}, $_ ] }
639 keys %{ $result->{'tables'} };
641 for my $table_name ( @tables ) {
642 my $tdata = $result->{'tables'}{ $table_name };
643 my $table = $schema->add_table(
644 name => $tdata->{'name'},
645 ) or die $schema->error;
647 $table->comments( $tdata->{'comments'} );
649 for my $fdata ( @{ $tdata->{'fields'} } ) {
650 my $field = $table->add_field(
651 name => $fdata->{'name'},
652 data_type => $fdata->{'data_type'},
653 size => $fdata->{'size'},
654 default_value => $fdata->{'default'},
655 is_auto_increment => $fdata->{'is_auto_inc'},
656 is_nullable => $fdata->{'is_nullable'},
657 comments => $fdata->{'comments'},
658 ) or die $table->error;
660 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
662 for my $cdata ( @{ $fdata->{'constraints'} } ) {
663 next unless $cdata->{'type'} eq 'foreign_key';
664 $cdata->{'fields'} ||= [ $field->name ];
665 push @{ $tdata->{'constraints'} }, $cdata;
669 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
670 my $index = $table->add_index(
671 name => $idata->{'name'},
672 type => uc ($idata->{'type'}||''),
673 fields => $idata->{'fields'},
674 ) or die $table->error;
677 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
678 my $constraint = $table->add_constraint(
679 name => $cdata->{'name'},
680 type => $cdata->{'type'},
681 fields => $cdata->{'fields'},
682 reference_table => $cdata->{'reference_table'},
683 reference_fields => $cdata->{'reference_fields'},
684 match_type => $cdata->{'match_type'} || '',
685 on_delete => $cdata->{'on_delete'}
686 || $cdata->{'on_delete_do'},
687 on_update => $cdata->{'on_update'}
688 || $cdata->{'on_update_do'},
689 ) or die $table->error;
693 for my $def ( @{ $result->{'views'} || [] } ) {
694 my $view = $schema->add_view(
695 name => $def->{'name'},
696 sql => $def->{'sql'},
700 for my $def ( @{ $result->{'triggers'} || [] } ) {
701 my $view = $schema->add_trigger(
702 name => $def->{'name'},
703 perform_action_when => $def->{'when'},
704 database_events => $def->{'db_events'},
705 action => $def->{'action'},
706 on_table => $def->{'on_table'},
715 # -------------------------------------------------------------------
716 # All wholsome food is caught without a net or a trap.
718 # -------------------------------------------------------------------
724 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
728 perl(1), Parse::RecDescent, SQL::Translator::Schema.