use strict;
use warnings;
-our ( $DEBUG, $GRAMMAR, @EXPORT_OK );
+
our $VERSION = '1.59';
+
+our $DEBUG;
$DEBUG = 0 unless defined $DEBUG;
use Data::Dumper;
-use Parse::RecDescent;
-use Exporter;
-use base qw(Exporter);
-
-@EXPORT_OK = qw(parse);
+use SQL::Translator::Utils qw/ddl_parser_instance/;
-# Enable warnings within the Parse::RecDescent module.
-$::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
-$::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c.
-$::RD_HINT = 1; # Give out hints to help fix problems.
+use base qw(Exporter);
+our @EXPORT_OK = qw(parse);
-$GRAMMAR = q!
+our $GRAMMAR = <<'END_OF_GRAMMAR';
{ my ( %tables, @views, @triggers, $table_order, $field_order, @table_comments) }
eofile : /^\Z/
-
statement : create
| comment_on_table
| comment_on_column
commit : /commit/i ';'
-connect : /^\s*\\\connect.*\n/
+connect : /^\s*\\connect.*\n/
set : /set/i /[^;]*/ ';'
drop : /drop/i /[^;]*/ ';'
string :
- /'(\\.|''|[^\\\'])*'/
+ /'(\.|''|[^\\'])*'/
nonstring : /[^;\'"]+/
};
}
|
- /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar)/i
+ /(bit|box|cidr|circle|date|inet|line|lseg|macaddr|money|numeric|decimal|path|point|polygon|timetz|time|varchar|json|hstore)/i
{
$return = { type => $item[1] };
}
storage_type : /(plain|external|extended|main)/i
-temporary : /temp(orary)?\\b/i
+temporary : /temp(orary)?\b/i
{
1;
}
create_index : CREATE /index/i
-default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+/ ( '::' data_type )(?)
+default_val : DEFAULT /(\d+|'[^']*'|\w+\(.*\))|\w+|\(\d+\)/ ( '::' data_type )(?)
{
my $val = defined $item[2] ? $item[2] : '';
$val =~ s/^'|'$//g;
+ $val =~ s/^\((\d+)\)\z/$1/; # for example (0)::smallint
$return = {
supertype => 'constraint',
type => 'default',
| /null/i
{ 'NULL' }
-!;
+END_OF_GRAMMAR
sub parse {
my ( $translator, $data ) = @_;
- my $parser = Parse::RecDescent->new($GRAMMAR);
- $::RD_TRACE = $translator->trace ? 1 : undef;
- $DEBUG = $translator->debug;
+ # Enable warnings within the Parse::RecDescent module.
+ local $::RD_ERRORS = 1 unless defined $::RD_ERRORS; # Make sure the parser dies when it encounters an error
+ local $::RD_WARN = 1 unless defined $::RD_WARN; # Enable warnings. This will warn on unused rules &c.
+ local $::RD_HINT = 1 unless defined $::RD_HINT; # Give out hints to help fix problems.
- unless (defined $parser) {
- return $translator->error("Error instantiating Parse::RecDescent ".
- "instance: Bad grammer");
- }
+ local $::RD_TRACE = $translator->trace ? 1 : undef;
+ local $DEBUG = $translator->debug;
+
+ my $parser = ddl_parser_instance('PostgreSQL');
my $result = $parser->startrule($data);
die "Parse failed.\n" unless defined $result;