X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FParser%2FSQLite.pm;h=01880283b590cc92e67c41be5f036b2c699ca78b;hb=56b9e6a566b09e57d63d8b854f80f84002ae0f14;hp=e2c70b7339355a5074dbedae8c3f66f2216d33a3;hpb=b727fc08127611b5f886ad72f53abd7e31dc78e0;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Parser/SQLite.pm b/lib/SQL/Translator/Parser/SQLite.pm index e2c70b7..0188028 100644 --- a/lib/SQL/Translator/Parser/SQLite.pm +++ b/lib/SQL/Translator/Parser/SQLite.pm @@ -1,11 +1,7 @@ package SQL::Translator::Parser::SQLite; # ------------------------------------------------------------------- -# $Id: SQLite.pm,v 1.3 2003-10-08 22:37:59 kycl4rk Exp $ -# ------------------------------------------------------------------- -# Copyright (C) 2003 Ken Y. Clark , -# darren chamberlain , -# Chris Mungall +# 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 @@ -111,7 +107,7 @@ trigger-action ::= trigger-step ::= update-statement | insert-statement | - delete-statement | select-statemen + delete-statement | select-statement CREATE VIEW @@ -154,7 +150,7 @@ like-op::= use strict; use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/; +$VERSION = '1.59'; $DEBUG = 0 unless defined $DEBUG; use Data::Dumper; @@ -182,7 +178,7 @@ $GRAMMAR = q! # failed. -ky # startrule : statement(s) eofile { - $return => { + $return = { tables => \%tables, views => \@views, triggers => \@triggers, @@ -193,14 +189,23 @@ eofile : /^\Z/ statement : begin_transaction | commit + | drop | comment | create | -begin_transaction : /begin transaction/i SEMICOLON +begin_transaction : /begin/i TRANSACTION(?) SEMICOLON commit : /commit/i SEMICOLON +drop : /drop/i (tbl_drop | view_drop | trg_drop) SEMICOLON + +tbl_drop: TABLE table_name + +view_drop: VIEW if_exists(?) view_name + +trg_drop: TRIGGER if_exists(?) trigger_name + comment : /^\s*(?:#|-{2}).*\n/ { my $comment = $item[1]; @@ -219,7 +224,7 @@ 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'}; @@ -266,22 +271,24 @@ create : CREATE TEMPORARY(?) TABLE table_name '(' definition(s /,/) ')' SEMICOLO definition : constraint_def | column_def -column_def: NAME type(?) column_constraint(s?) +column_def: comment(s?) NAME type(?) column_constraint_def(s?) { my $column = { supertype => 'column', - name => $item[1], - data_type => $item[2][0]->{'type'}, - size => $item[2][0]->{'size'}, + name => $item[2], + data_type => $item[3][0]->{'type'}, + size => $item[3][0]->{'size'}, is_nullable => 1, is_primary_key => 0, is_unique => 0, check => '', default => undef, - constraints => $item[3], + constraints => $item[4], + comments => $item[1], }; - for my $c ( @{ $item[3] } ) { + + for my $c ( @{ $item[4] } ) { if ( $c->{'type'} eq 'not_null' ) { $column->{'is_nullable'} = 0; } @@ -297,6 +304,9 @@ column_def: 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; @@ -310,6 +320,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 = { @@ -350,8 +370,41 @@ column_constraint : NOT_NULL conflict_clause(?) value => $item[2], } } + | + REFERENCES ref_def + { + $return = { + type => 'foreign_key', + reference_table => $item[2]{'reference_table'}, + 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', @@ -381,6 +434,9 @@ constraint_def : PRIMARY_KEY parens_field_list conflict_clause(?) } } +ref_def : /(\w+)\s*\((\w+)\)/ + { $return = { reference_table => $1, reference_fields => $2 } } + table_name : qualified_name qualified_name : NAME @@ -391,6 +447,8 @@ qualified_name : /(\w+)\.(\w+)/ field_name : NAME +constraint_name : NAME + conflict_clause : /on conflict/i conflict_algorigthm conflict_algorigthm : /(rollback|abort|fail|ignore|replace)/i @@ -410,7 +468,7 @@ sort_order : /(ASC|DESC)/i # # Create Trigger -create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON table_name trigger_action +create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON table_name trigger_action SEMICOLON { my $table_name = $item[8]->{'name'}; push @triggers, { @@ -418,8 +476,9 @@ create : CREATE TEMPORARY(?) TRIGGER NAME before_or_after(?) database_event ON t is_temporary => $item[2][0] ? 1 : 0, when => $item[5][0], instead_of => 0, - db_event => $item[6], + db_events => [ $item[6] ], action => $item[9], + on_table => $table_name, } } @@ -431,8 +490,9 @@ create : CREATE TEMPORARY(?) TRIGGER NAME instead_of database_event ON view_name is_temporary => $item[2][0] ? 1 : 0, when => undef, instead_of => 1, - db_event => $item[6], + db_events => [ $item[6] ], action => $item[9], + on_table => $table_name, } } @@ -449,21 +509,32 @@ trigger_action : for_each(?) when(?) BEGIN_C trigger_step(s) END_C } } -for_each : /FOR EACH ROW/i | /FOR EACH STATEMENT/i +for_each : /FOR EACH ROW/i when : WHEN expr { $item[2] } -trigger_step : /(select|delete|insert|update)/i /[^;]+/ SEMICOLON +string : + /'(\\.|''|[^\\\'])*'/ + +nonstring : /[^;\'"]+/ + +statement_body : string | nonstring + +trigger_step : /(select|delete|insert|update)/i statement_body(s?) SEMICOLON { - $return = join( ' ', $item[1], $item[2] ) + $return = join( ' ', $item[1], join ' ', @{ $item[2] || [] } ) } before_or_after : /(before|after)/i { $return = lc $1 } instead_of : /instead of/i +if_exists : /if exists/i + view_name : qualified_name +trigger_name : qualified_name + # # Create View # @@ -488,6 +559,8 @@ BEGIN_C : /begin/i END_C : /end/i +TRANSACTION: /transaction/i + CREATE : /create/i TEMPORARY : /temp(orary)?/i { 1 } @@ -518,11 +591,17 @@ WORD : /\w+/ 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] } @@ -535,6 +614,8 @@ VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/ } | /NULL/ { 'NULL' } + | /CURRENT_TIMESTAMP/i + { 'CURRENT_TIMESTAMP' } !; @@ -593,7 +674,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; } @@ -606,8 +687,10 @@ sub parse { reference_table => $cdata->{'reference_table'}, reference_fields => $cdata->{'reference_fields'}, match_type => $cdata->{'match_type'} || '', - on_delete => $cdata->{'on_delete_do'}, - on_update => $cdata->{'on_update_do'}, + on_delete => $cdata->{'on_delete'} + || $cdata->{'on_delete_do'}, + on_update => $cdata->{'on_update'} + || $cdata->{'on_update_do'}, ) or die $table->error; } } @@ -623,8 +706,9 @@ sub parse { my $view = $schema->add_trigger( name => $def->{'name'}, perform_action_when => $def->{'when'}, - database_event => $def->{'db_event'}, + database_events => $def->{'db_events'}, action => $def->{'action'}, + on_table => $def->{'on_table'}, ); } @@ -642,7 +726,7 @@ sub parse { =head1 AUTHOR -Ken Y. Clark Ekclark@cpan.orgE. +Ken Youens-Clark Ekclark@cpan.orgE. =head1 SEE ALSO