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
134 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
136 $DEBUG = 0 unless defined $DEBUG;
139 use Parse::RecDescent;
141 use base qw(Exporter);
143 @EXPORT_OK = qw(parse);
145 # Enable warnings within the Parse::RecDescent module.
146 $::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
147 $::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
148 $::RD_HINT = 1; # Give out hints to help fix problems.
153 my ( %tables, $table_order, @table_comments, @views, @triggers );
157 # The "eofile" rule makes the parser fail if any "statement" rule
158 # fails. Otherwise, the first successful match by a "statement"
159 # won't cause the failure needed to know that the parse, as a whole,
162 startrule : statement(s) eofile {
166 triggers => \@triggers,
172 statement : begin_transaction
179 begin_transaction : /begin/i TRANSACTION(?) SEMICOLON
181 commit : /commit/i SEMICOLON
183 drop : /drop/i (tbl_drop | view_drop | trg_drop) SEMICOLON
185 tbl_drop: TABLE <commit> table_name
187 view_drop: VIEW if_exists(?) view_name
189 trg_drop: TRIGGER if_exists(?) trigger_name
191 comment : /^\s*(?:#|-{2}).*\n/
193 my $comment = $item[1];
194 $comment =~ s/^\s*(#|-{2})\s*//;
195 $comment =~ s/\s*$//;
199 comment : /\/\*/ /[^\*]+/ /\*\//
201 my $comment = $item[2];
202 $comment =~ s/^\s*|\s*$//g;
209 create : CREATE TEMPORARY(?) UNIQUE(?) INDEX NAME ON table_name parens_field_list conflict_clause(?) SEMICOLON
211 my $db_name = $item[7]->{'db_name'} || '';
212 my $table_name = $item[7]->{'name'};
217 on_conflict => $item[9][0],
218 is_temporary => $item[2][0] ? 1 : 0,
221 my $is_unique = $item[3][0];
224 $index->{'type'} = 'unique';
225 push @{ $tables{ $table_name }{'constraints'} }, $index;
228 push @{ $tables{ $table_name }{'indices'} }, $index;
235 create : CREATE TEMPORARY(?) TABLE table_name '(' definition(s /,/) ')' SEMICOLON
237 my $db_name = $item[4]->{'db_name'} || '';
238 my $table_name = $item[4]->{'name'};
240 $tables{ $table_name }{'name'} = $table_name;
241 $tables{ $table_name }{'is_temporary'} = $item[2][0] ? 1 : 0;
242 $tables{ $table_name }{'order'} = ++$table_order;
244 for my $def ( @{ $item[6] } ) {
245 if ( $def->{'supertype'} eq 'column' ) {
246 push @{ $tables{ $table_name }{'fields'} }, $def;
248 elsif ( $def->{'supertype'} eq 'constraint' ) {
249 push @{ $tables{ $table_name }{'constraints'} }, $def;
254 definition : constraint_def | column_def
256 column_def: comment(s?) NAME type(?) column_constraint_def(s?)
259 supertype => 'column',
261 data_type => $item[3][0]->{'type'},
262 size => $item[3][0]->{'size'},
268 constraints => $item[4],
269 comments => $item[1],
273 for my $c ( @{ $item[4] } ) {
274 if ( $c->{'type'} eq 'not_null' ) {
275 $column->{'is_nullable'} = 0;
277 elsif ( $c->{'type'} eq 'primary_key' ) {
278 $column->{'is_primary_key'} = 1;
280 elsif ( $c->{'type'} eq 'unique' ) {
281 $column->{'is_unique'} = 1;
283 elsif ( $c->{'type'} eq 'check' ) {
284 $column->{'check'} = $c->{'expression'};
286 elsif ( $c->{'type'} eq 'default' ) {
287 $column->{'default'} = $c->{'value'};
289 elsif ( $c->{'type'} eq 'autoincrement' ) {
290 $column->{'is_auto_inc'} = 1;
297 type : WORD parens_value_list(?)
305 column_constraint_def : CONSTRAINT constraint_name column_constraint
315 column_constraint : NOT_NULL conflict_clause(?)
322 PRIMARY_KEY sort_order(?) conflict_clause(?)
325 type => 'primary_key',
326 sort_order => $item[2][0],
327 on_conflict => $item[2][0],
331 UNIQUE conflict_clause(?)
335 on_conflict => $item[2][0],
339 CHECK_C '(' expr ')' conflict_clause(?)
343 expression => $item[3],
344 on_conflict => $item[5][0],
359 type => 'foreign_key',
360 reference_table => $item[2]{'reference_table'},
361 reference_fields => $item[2]{'reference_fields'},
368 type => 'autoincrement',
372 constraint_def : comment(s?) CONSTRAINT constraint_name table_constraint
375 comments => $item[1],
381 comment(s?) table_constraint
384 comments => $item[1],
389 table_constraint : PRIMARY_KEY parens_field_list conflict_clause(?)
392 supertype => 'constraint',
393 type => 'primary_key',
395 on_conflict => $item[3][0],
399 UNIQUE parens_field_list conflict_clause(?)
402 supertype => 'constraint',
405 on_conflict => $item[3][0],
409 CHECK_C '(' expr ')' conflict_clause(?)
412 supertype => 'constraint',
414 expression => $item[3],
415 on_conflict => $item[5][0],
419 FOREIGN_KEY parens_field_list REFERENCES ref_def
422 supertype => 'constraint',
423 type => 'foreign_key',
425 reference_table => $item[4]{'reference_table'},
426 reference_fields => $item[4]{'reference_fields'},
430 ref_def : /(\w+)\s*\((\w+)\)/
431 { $return = { reference_table => $1, reference_fields => $2 } }
433 table_name : qualified_name
435 qualified_name : NAME
436 { $return = { name => $item[1] } }
438 qualified_name : /(\w+)\.(\w+)/
439 { $return = { db_name => $1, name => $2 } }
443 constraint_name : NAME
445 conflict_clause : /on conflict/i conflict_algorigthm
447 conflict_algorigthm : /(rollback|abort|fail|ignore|replace)/i
449 parens_field_list : '(' column_list ')'
452 column_list : field_name(s /,/)
454 parens_value_list : '(' VALUE(s /,/) ')'
459 sort_order : /(ASC|DESC)/i
464 create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON table_name trigger_action SEMICOLON
466 my $table_name = $item[8]->{'name'};
469 is_temporary => $item[2][0] ? 1 : 0,
472 db_events => [ $item[6] ],
474 on_table => $table_name,
478 create : CREATE TEMPORARY(?) TRIGGER NAME instead_of database_event ON view_name trigger_action
480 my $table_name = $item[8]->{'name'};
483 is_temporary => $item[2][0] ? 1 : 0,
486 db_events => [ $item[6] ],
488 on_table => $table_name,
492 database_event : /(delete|insert|update)/i
494 database_event : /update of/i column_list
496 trigger_action : for_each(?) when(?) BEGIN_C trigger_step(s) END_C
499 for_each => $item[1][0],
505 for_each : /FOR EACH ROW/i
507 when : WHEN expr { $item[2] }
510 /'(\\.|''|[^\\\'])*'/
512 nonstring : /[^;\'"]+/
514 statement_body : string | nonstring
516 trigger_step : /(select|delete|insert|update)/i statement_body(s?) SEMICOLON
518 $return = join( ' ', $item[1], join ' ', @{ $item[2] || [] } )
521 before_or_after : /(before|after)/i { $return = lc $1 }
523 instead_of : /instead of/i
525 if_exists : /if exists/i
527 view_name : qualified_name
529 trigger_name : qualified_name
534 create : CREATE TEMPORARY(?) VIEW view_name AS select_statement
537 name => $item[4]->{'name'},
539 is_temporary => $item[2][0] ? 1 : 0,
543 select_statement : SELECT /[^;]+/ SEMICOLON
545 $return = join( ' ', $item[1], $item[2] );
555 TRANSACTION: /transaction/i
559 TEMPORARY : /temp(orary)?/i { 1 }
565 NOT_NULL : /not null/i
567 PRIMARY_KEY : /primary key/i
569 FOREIGN_KEY : /foreign key/i
589 REFERENCES : /references/i
591 CONSTRAINT : /constraint/i
593 AUTOINCREMENT : /autoincrement/i
595 UNIQUE : /unique/i { 1 }
599 NAME : /["']?(\w+)["']?/ { $return = $1 }
601 VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
605 # remove leading/trailing quotes
607 $val =~ s/^['"]|['"]$//g;
612 | /CURRENT_TIMESTAMP/i
613 { 'CURRENT_TIMESTAMP' }
618 my ( $translator, $data ) = @_;
619 my $parser = Parse::RecDescent->new($GRAMMAR);
621 local $::RD_TRACE = $translator->trace ? 1 : undef;
622 local $DEBUG = $translator->debug;
624 unless (defined $parser) {
625 return $translator->error("Error instantiating Parse::RecDescent ".
626 "instance: Bad grammer");
629 my $result = $parser->startrule($data);
630 return $translator->error( "Parse failed." ) unless defined $result;
631 warn Dumper( $result ) if $DEBUG;
633 my $schema = $translator->schema;
636 sort { $a->[0] <=> $b->[0] }
637 map { [ $result->{'tables'}{ $_ }->{'order'}, $_ ] }
638 keys %{ $result->{'tables'} };
640 for my $table_name ( @tables ) {
641 my $tdata = $result->{'tables'}{ $table_name };
642 my $table = $schema->add_table(
643 name => $tdata->{'name'},
644 ) or die $schema->error;
646 $table->comments( $tdata->{'comments'} );
648 for my $fdata ( @{ $tdata->{'fields'} } ) {
649 my $field = $table->add_field(
650 name => $fdata->{'name'},
651 data_type => $fdata->{'data_type'},
652 size => $fdata->{'size'},
653 default_value => $fdata->{'default'},
654 is_auto_increment => $fdata->{'is_auto_inc'},
655 is_nullable => $fdata->{'is_nullable'},
656 comments => $fdata->{'comments'},
657 ) or die $table->error;
659 $table->primary_key( $field->name ) if $fdata->{'is_primary_key'};
661 for my $cdata ( @{ $fdata->{'constraints'} } ) {
662 next unless $cdata->{'type'} eq 'foreign_key';
663 $cdata->{'fields'} ||= [ $field->name ];
664 push @{ $tdata->{'constraints'} }, $cdata;
668 for my $idata ( @{ $tdata->{'indices'} || [] } ) {
669 my $index = $table->add_index(
670 name => $idata->{'name'},
671 type => uc ($idata->{'type'}||''),
672 fields => $idata->{'fields'},
673 ) or die $table->error;
676 for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
677 my $constraint = $table->add_constraint(
678 name => $cdata->{'name'},
679 type => $cdata->{'type'},
680 fields => $cdata->{'fields'},
681 reference_table => $cdata->{'reference_table'},
682 reference_fields => $cdata->{'reference_fields'},
683 match_type => $cdata->{'match_type'} || '',
684 on_delete => $cdata->{'on_delete'}
685 || $cdata->{'on_delete_do'},
686 on_update => $cdata->{'on_update'}
687 || $cdata->{'on_update_do'},
688 ) or die $table->error;
692 for my $def ( @{ $result->{'views'} || [] } ) {
693 my $view = $schema->add_view(
694 name => $def->{'name'},
695 sql => $def->{'sql'},
699 for my $def ( @{ $result->{'triggers'} || [] } ) {
700 my $view = $schema->add_trigger(
701 name => $def->{'name'},
702 perform_action_when => $def->{'when'},
703 database_events => $def->{'db_events'},
704 action => $def->{'action'},
705 on_table => $def->{'on_table'},
714 # -------------------------------------------------------------------
715 # All wholsome food is caught without a net or a trap.
717 # -------------------------------------------------------------------
723 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
727 perl(1), Parse::RecDescent, SQL::Translator::Schema.