SQLite improvements:
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / SQLite.pm
index 1533bea..0de6e12 100644 (file)
@@ -1,11 +1,7 @@
 package SQL::Translator::Parser::SQLite;
 
 # -------------------------------------------------------------------
-# $Id: SQLite.pm,v 1.5 2004-02-04 17:32:42 dlc Exp $
-# -------------------------------------------------------------------
-# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>,
-#                    darren chamberlain <darren@cpan.org>,
-#                    Chris Mungall <cjm@fruitfly.org>
+# 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
@@ -154,7 +150,7 @@ like-op::=
 
 use strict;
 use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/;
+$VERSION = '1.59';
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -193,14 +189,23 @@ 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 (tbl_drop | view_drop | trg_drop) SEMICOLON
+
+tbl_drop: TABLE <commit> table_name
+
+view_drop: VIEW if_exists(?) view_name
+
+trg_drop: TRIGGER if_exists(?) trigger_name
+
 comment : /^\s*(?:#|-{2}).*\n/
     {
         my $comment =  $item[1];
@@ -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(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;
             }
@@ -410,7 +417,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 +425,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 +439,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 +458,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 +508,8 @@ BEGIN_C : /begin/i
 
 END_C : /end/i
 
+TRANSACTION: /transaction/i
+
 CREATE : /create/i
 
 TEMPORARY : /temp(orary)?/i { 1 }
@@ -535,6 +557,8 @@ VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
     }
     | /NULL/
     { 'NULL' }
+    | /CURRENT_TIMESTAMP/i
+    { 'CURRENT_TIMESTAMP' }
 
 !;
 
@@ -606,8 +630,8 @@ 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 +647,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'},
         );
     }