X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FParser%2FSQLite.pm;h=da7feb439c674a88a711ccf30b082d4bdeb5c2e7;hb=f27f9229eaf8c5ce637bea1f99eb8f9bdb42799c;hp=c1db5c44d26e25aa104d33a47cfe3b2cb12b6bfc;hpb=08e01b5c174510eb5c21f9a981273f130c5cd02c;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Parser/SQLite.pm b/lib/SQL/Translator/Parser/SQLite.pm index c1db5c4..da7feb4 100644 --- a/lib/SQL/Translator/Parser/SQLite.pm +++ b/lib/SQL/Translator/Parser/SQLite.pm @@ -1,23 +1,5 @@ package SQL::Translator::Parser::SQLite; -# ------------------------------------------------------------------- -# Copyright (C) 2002-2009 SQLFairy Authors -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; version 2. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA -# ------------------------------------------------------------------- - =head1 NAME SQL::Translator::Parser::SQLite - parser for SQLite @@ -32,7 +14,7 @@ SQL::Translator::Parser::SQLite - parser for SQLite =head1 DESCRIPTION -This is a grammar for parsing CREATE statements for SQLite as +This is a grammar for parsing CREATE statements for SQLite as described here: http://www.sqlite.org/lang.html @@ -40,7 +22,7 @@ described here: CREATE INDEX sql-statement ::= - CREATE [TEMP | TEMPORARY] [UNIQUE] INDEX index-name + CREATE [TEMP | TEMPORARY] [UNIQUE] INDEX index-name ON [database-name .] table-name ( column-name [, column-name]* ) [ ON CONFLICT conflict-algorithm ] @@ -94,19 +76,19 @@ sql-statement ::= trigger-action database-event ::= - DELETE | - INSERT | - UPDATE | + DELETE | + INSERT | + UPDATE | UPDATE OF column-list trigger-action ::= - [ FOR EACH ROW | FOR EACH STATEMENT ] [ WHEN expression ] - BEGIN + [ FOR EACH ROW | FOR EACH STATEMENT ] [ WHEN expression ] + BEGIN trigger-step ; [ trigger-step ; ]* END trigger-step ::= - update-statement | insert-statement | + update-statement | insert-statement | delete-statement | select-statement CREATE VIEW @@ -149,6 +131,7 @@ like-op::= =cut use strict; +use warnings; use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ]; $VERSION = '1.59'; $DEBUG = 0 unless defined $DEBUG; @@ -167,19 +150,19 @@ $::RD_HINT = 1; # Give out hints to help fix problems. $GRAMMAR = q! -{ +{ my ( %tables, $table_order, @table_comments, @views, @triggers ); } # # The "eofile" rule makes the parser fail if any "statement" rule -# fails. Otherwise, the first successful match by a "statement" +# fails. Otherwise, the first successful match by a "statement" # won't cause the failure needed to know that the parse, as a whole, # failed. -ky # -startrule : statement(s) eofile { +startrule : statement(s) eofile { $return = { - tables => \%tables, + tables => \%tables, views => \@views, triggers => \@triggers, } @@ -214,7 +197,7 @@ comment : /^\s*(?:#|-{2}).*\n/ $return = $comment; } -comment : /\/\*/ /[^\*]+/ /\*\// +comment : /\/\*/ /[^\*]+/ /\*\// { my $comment = $item[2]; $comment =~ s/^\s*|\s*$//g; @@ -224,12 +207,12 @@ comment : /\/\*/ /[^\*]+/ /\*\// # # Create Index # -create : CREATE TEMPORARY(?) UNIQUE(?) INDEX WORD ON table_name parens_field_list conflict_clause(?) SEMICOLON +create : CREATE TEMPORARY(?) UNIQUE(?) INDEX NAME ON table_name parens_field_list conflict_clause(?) SEMICOLON { my $db_name = $item[7]->{'db_name'} || ''; my $table_name = $item[7]->{'name'}; - my $index = { + my $index = { name => $item[5], fields => $item[8], on_conflict => $item[9][0], @@ -269,9 +252,9 @@ create : CREATE TEMPORARY(?) TABLE table_name '(' definition(s /,/) ')' SEMICOLO } } -definition : constraint_def | column_def +definition : constraint_def | column_def -column_def: comment(s?) NAME type(?) column_constraint(s?) +column_def: comment(s?) NAME type(?) column_constraint_def(s?) { my $column = { supertype => 'column', @@ -304,6 +287,9 @@ column_def: comment(s?) NAME type(?) column_constraint(s?) elsif ( $c->{'type'} eq 'default' ) { $column->{'default'} = $c->{'value'}; } + elsif ( $c->{'type'} eq 'autoincrement' ) { + $column->{'is_auto_inc'} = 1; + } } $column; @@ -317,6 +303,16 @@ type : WORD parens_value_list(?) } } +column_constraint_def : CONSTRAINT constraint_name column_constraint + { + $return = { + name => $item[2], + %{ $item[3] }, + } + } + | + column_constraint + column_constraint : NOT_NULL conflict_clause(?) { $return = { @@ -329,7 +325,7 @@ column_constraint : NOT_NULL conflict_clause(?) $return = { type => 'primary_key', sort_order => $item[2][0], - on_conflict => $item[2][0], + on_conflict => $item[2][0], } } | @@ -337,7 +333,7 @@ column_constraint : NOT_NULL conflict_clause(?) { $return = { type => 'unique', - on_conflict => $item[2][0], + on_conflict => $item[2][0], } } | @@ -346,7 +342,7 @@ column_constraint : NOT_NULL conflict_clause(?) $return = { type => 'check', expression => $item[3], - on_conflict => $item[5][0], + on_conflict => $item[5][0], } } | @@ -366,8 +362,32 @@ column_constraint : NOT_NULL conflict_clause(?) reference_fields => $item[2]{'reference_fields'}, } } + | + AUTOINCREMENT + { + $return = { + type => 'autoincrement', + } + } + +constraint_def : comment(s?) CONSTRAINT constraint_name table_constraint + { + $return = { + comments => $item[1], + name => $item[3], + %{ $item[4] }, + } + } + | + comment(s?) table_constraint + { + $return = { + comments => $item[1], + %{ $item[2] }, + } + } -constraint_def : PRIMARY_KEY parens_field_list conflict_clause(?) +table_constraint : PRIMARY_KEY parens_field_list conflict_clause(?) { $return = { supertype => 'constraint', @@ -396,20 +416,33 @@ constraint_def : PRIMARY_KEY parens_field_list conflict_clause(?) on_conflict => $item[5][0], } } + | + FOREIGN_KEY parens_field_list REFERENCES ref_def + { + $return = { + supertype => 'constraint', + type => 'foreign_key', + fields => $item[2], + reference_table => $item[4]{'reference_table'}, + reference_fields => $item[4]{'reference_fields'}, + } + } ref_def : /(\w+)\s*\((\w+)\)/ { $return = { reference_table => $1, reference_fields => $2 } } table_name : qualified_name - -qualified_name : NAME + +qualified_name : NAME { $return = { name => $item[1] } } -qualified_name : /(\w+)\.(\w+)/ +qualified_name : /(\w+)\.(\w+)/ { $return = { db_name => $1, name => $2 } } field_name : NAME +constraint_name : NAME + conflict_clause : /on conflict/i conflict_algorigthm conflict_algorigthm : /(rollback|abort|fail|ignore|replace)/i @@ -475,7 +508,7 @@ for_each : /FOR EACH ROW/i when : WHEN expr { $item[2] } string : - /'(\\.|''|[^\\\'])*'/ + /'(\\.|''|[^\\\'])*'/ nonstring : /[^;\'"]+/ @@ -484,7 +517,7 @@ statement_body : string | nonstring trigger_step : /(select|delete|insert|update)/i statement_body(s?) SEMICOLON { $return = join( ' ', $item[1], join ' ', @{ $item[2] || [] } ) - } + } before_or_after : /(before|after)/i { $return = lc $1 } @@ -499,11 +532,11 @@ trigger_name : qualified_name # # Create View # -create : CREATE TEMPORARY(?) VIEW view_name AS select_statement +create : CREATE TEMPORARY(?) VIEW view_name AS select_statement { push @views, { name => $item[4]->{'name'}, - sql => $item[6], + sql => $item[6], is_temporary => $item[2][0] ? 1 : 0, } } @@ -534,6 +567,8 @@ NOT_NULL : /not null/i PRIMARY_KEY : /primary key/i +FOREIGN_KEY : /foreign key/i + CHECK_C : /check/i DEFAULT : /default/i @@ -554,17 +589,21 @@ WHEN : /when/i REFERENCES : /references/i +CONSTRAINT : /constraint/i + +AUTOINCREMENT : /autoincrement/i + UNIQUE : /unique/i { 1 } SEMICOLON : ';' -NAME : /'?(\w+)'?/ { $return = $1 } +NAME : /["']?(\w+)["']?/ { $return = $1 } VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ { $item[1] } - | /'.*?'/ - { - # remove leading/trailing quotes + | /'.*?'/ + { + # remove leading/trailing quotes my $val = $item[1]; $val =~ s/^['"]|['"]$//g; $return = $val; @@ -576,7 +615,6 @@ VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ !; -# ------------------------------------------------------------------- sub parse { my ( $translator, $data ) = @_; my $parser = Parse::RecDescent->new($GRAMMAR); @@ -594,15 +632,15 @@ sub parse { warn Dumper( $result ) if $DEBUG; my $schema = $translator->schema; - my @tables = + my @tables = map { $_->[1] } - sort { $a->[0] <=> $b->[0] } + sort { $a->[0] <=> $b->[0] } map { [ $result->{'tables'}{ $_ }->{'order'}, $_ ] } keys %{ $result->{'tables'} }; for my $table_name ( @tables ) { my $tdata = $result->{'tables'}{ $table_name }; - my $table = $schema->add_table( + my $table = $schema->add_table( name => $tdata->{'name'}, ) or die $schema->error; @@ -631,7 +669,7 @@ sub parse { for my $idata ( @{ $tdata->{'indices'} || [] } ) { my $index = $table->add_index( name => $idata->{'name'}, - type => uc $idata->{'type'}, + type => uc ($idata->{'type'}||''), fields => $idata->{'fields'}, ) or die $table->error; } @@ -644,9 +682,9 @@ sub parse { reference_table => $cdata->{'reference_table'}, reference_fields => $cdata->{'reference_fields'}, match_type => $cdata->{'match_type'} || '', - on_delete => $cdata->{'on_delete'} + on_delete => $cdata->{'on_delete'} || $cdata->{'on_delete_do'}, - on_update => $cdata->{'on_update'} + on_update => $cdata->{'on_update'} || $cdata->{'on_update_do'}, ) or die $table->error; }