SQLT::Parser::PostgreSQL parses table def with default values
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / SQLite.pm
index 0f6b520..593b4ba 100644 (file)
@@ -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
@@ -149,8 +131,9 @@ like-op::=
 =cut
 
 use strict;
-use vars qw[ $DEBUG $VERSION $GRAMMAR @EXPORT_OK ];
-$VERSION = '1.59';
+use warnings;
+our ( $DEBUG, $GRAMMAR, @EXPORT_OK );
+our $VERSION = '1.59';
 $DEBUG   = 0 unless defined $DEBUG;
 
 use Data::Dumper;
@@ -169,6 +152,16 @@ $GRAMMAR = q!
 
 {
     my ( %tables, $table_order, @table_comments, @views, @triggers );
+
+    sub _err {
+      my $max_lines = 5;
+      my @up_to_N_lines = split (/\n/, $_[1], $max_lines + 1);
+      die sprintf ("Unable to parse line %d:\n%s\n",
+        $_[0],
+        join "\n", (map { "'$_'" } @up_to_N_lines[0..$max_lines - 1 ]), @up_to_N_lines > $max_lines ? '...' : ()
+      );
+    }
+
 }
 
 #
@@ -192,7 +185,7 @@ statement : begin_transaction
     | drop
     | comment
     | create
-    | <error>
+    | /^\Z/ | { _err ($thisline, $text) }
 
 begin_transaction : /begin/i TRANSACTION(?) SEMICOLON
 
@@ -445,8 +438,8 @@ table_constraint : PRIMARY_KEY parens_field_list conflict_clause(?)
       }
     }
 
-ref_def : /(\w+)\s*\((\w+)\)/
-    { $return = { reference_table => $1, reference_fields => $2 } }
+ref_def : table_name parens_field_list
+    { $return = { reference_table => $item[1]{name}, reference_fields => $item[2] } }
 
 table_name : qualified_name
 
@@ -632,7 +625,6 @@ VALUE : /[-+]?\.?\d+(?:[eE]\d+)?/
 
 !;
 
-# -------------------------------------------------------------------
 sub parse {
     my ( $translator, $data ) = @_;
     my $parser = Parse::RecDescent->new($GRAMMAR);