Variable table and column names? wtf?!
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / SQLite.pm
index 0ffc472..88302f2 100644 (file)
@@ -1,9 +1,7 @@
 package SQL::Translator::Parser::SQLite;
 
 # -------------------------------------------------------------------
-# $Id: SQLite.pm,v 1.7 2005-06-28 16:39:41 mwz444 Exp $
-# -------------------------------------------------------------------
-# Copyright (C) 2002-4 SQLFairy Authors
+# 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
@@ -152,7 +150,7 @@ like-op::=
 
 use strict;
 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
+$VERSION = '1.59';
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -191,14 +189,17 @@ eofile : /^\Z/
 
 statement : begin_transaction
     | commit
+    | drop
     | comment
     | create
     | <error>
 
-begin_transaction : /begin transaction/i SEMICOLON
+begin_transaction : /begin/i TRANSACTION(?) SEMICOLON
 
 commit : /commit/i SEMICOLON
 
+drop : /drop/i TABLE <commit> table_name SEMICOLON
+
 comment : /^\s*(?:#|-{2}).*\n/
     {
         my $comment =  $item[1];
@@ -408,7 +409,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, {
@@ -416,8 +417,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,
         }
     }
 
@@ -429,8 +431,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,
         }
     }
 
@@ -447,13 +450,20 @@ 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 }
@@ -486,6 +496,8 @@ BEGIN_C : /begin/i
 
 END_C : /end/i
 
+TRANSACTION: /transaction/i
+
 CREATE : /create/i
 
 TEMPORARY : /temp(orary)?/i { 1 }
@@ -533,6 +545,8 @@ VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
     }
     | /NULL/
     { 'NULL' }
+    | /CURRENT_TIMESTAMP/i
+    { 'CURRENT_TIMESTAMP' }
 
 !;
 
@@ -621,8 +635,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'},
         );
     }