package SQL::Translator::Parser::PostgreSQL;
-# -------------------------------------------------------------------
-# 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::PostgreSQL - parser for PostgreSQL
=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;
$GRAMMAR = q!
-{ my ( %tables, @views, $table_order, $field_order, @table_comments) }
+{ my ( %tables, @views, @triggers, $table_order, $field_order, @table_comments) }
#
# The "eofile" rule makes the parser fail if any "statement" rule
# won't cause the failure needed to know that the parse, as a whole,
# failed. -ky
#
-startrule : statement(s) eofile { { tables => \%tables, views => \@views } }
+startrule : statement(s) eofile {
+ {
+ tables => \%tables,
+ views => \@views,
+ triggers => \@triggers,
+ }
+}
eofile : /^\Z/
}
}
+trigger_name : name_with_opt_quotes
+
+trigger_scope : /FOR/i /EACH/i /(ROW|STATEMENT)/i { $return = lc $1 }
+
+before_or_after : /(before|after)/i { $return = lc $1 }
+
+trigger_action : /.+/
+
+database_event : /insert|update|delete/i
+database_events : database_event(s /OR/)
+
+create : CREATE /TRIGGER/i trigger_name before_or_after database_events /ON/i table_id trigger_scope(?) trigger_action
+ {
+ # Hack to pass roundtrip tests which have trigger statements terminated by double semicolon
+ # and expect the returned data to have the same
+ my $action = $item{trigger_action};
+ $action =~ s/;$//;
+
+ push @triggers, {
+ name => $item{trigger_name},
+ perform_action_when => $item{before_or_after},
+ database_events => $item{database_events},
+ on_table => $item{table_id}{table_name},
+ scope => $item{'trigger_scope(?)'}[0],
+ action => $action,
+ }
+ }
+
#
# Create anything else (e.g., domain, etc.)
#
index_name : name_with_opt_quotes
+
data_type : pg_data_type parens_value_list(?)
{
my $data_type = $item[1];
$view->extra ( temporary => 1 ) if $vinfo->{is_temporary};
}
+ for my $trigger (@{ $result->{triggers} }) {
+ $schema->add_trigger( %$trigger );
+ }
+
return 1;
}