Return-Path: X-Original-To: apmail-db-derby-commits-archive@www.apache.org Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 664DA10647 for ; Thu, 19 Sep 2013 07:52:33 +0000 (UTC) Received: (qmail 26756 invoked by uid 500); 19 Sep 2013 07:52:32 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 26700 invoked by uid 500); 19 Sep 2013 07:52:18 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 26573 invoked by uid 99); 19 Sep 2013 07:52:07 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 19 Sep 2013 07:52:07 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 19 Sep 2013 07:52:06 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id D401B2388900; Thu, 19 Sep 2013 07:51:45 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1524645 - in /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute: GenericTriggerExecutor.java RowTriggerExecutor.java StatementTriggerExecutor.java Date: Thu, 19 Sep 2013 07:51:45 -0000 To: derby-commits@db.apache.org From: kahatlen@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130919075145.D401B2388900@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kahatlen Date: Thu Sep 19 07:51:45 2013 New Revision: 1524645 URL: http://svn.apache.org/r1524645 Log: DERBY-534: Support use of the WHEN clause in CREATE TRIGGER statements Move common logic for executing WHEN clause and trigger action to the base class GenericTriggerExecutor. In addition to reducing code duplication, the change makes row triggers reuse the prepared statement for the WHEN clause (same as it already does for the trigger action), and it makes statement triggers not leave the before and after result sets open if the WHEN clause evaluates to false. Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericTriggerExecutor.java db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/RowTriggerExecutor.java db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/StatementTriggerExecutor.java Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericTriggerExecutor.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericTriggerExecutor.java?rev=1524645&r1=1524644&r2=1524645&view=diff ============================================================================== --- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericTriggerExecutor.java (original) +++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/GenericTriggerExecutor.java Thu Sep 19 07:51:45 2013 @@ -112,7 +112,7 @@ abstract class GenericTriggerExecutor return whenClause; } - protected SPSDescriptor getAction() throws StandardException + private SPSDescriptor getAction() throws StandardException { if (!actionRetrieved) { @@ -134,7 +134,7 @@ abstract class GenericTriggerExecutor * to {@code TRUE}, {@code false} otherwise * @exception StandardException on error */ - final boolean executeSPS(SPSDescriptor sps, boolean isWhen) + private boolean executeSPS(SPSDescriptor sps, boolean isWhen) throws StandardException { boolean recompile = false; @@ -301,7 +301,7 @@ abstract class GenericTriggerExecutor } /** - * Cleanup after executing the SPS for the trigger action. + * Cleanup after executing the SPS for the WHEN clause and trigger action. * * @exception StandardException on error */ @@ -312,34 +312,32 @@ abstract class GenericTriggerExecutor } actionPS = null; spsActionActivation = null; + + if (spsWhenActivation != null) { + spsWhenActivation.close(); + } + whenPS = null; + spsWhenActivation = null; } /** - * Evaluate the WHEN clause, if there is one, and return whether the - * trigger action should be executed. + *

+ * Execute the WHEN clause SPS and the trigger action SPS. + *

+ * + *

+ * If there is no WHEN clause, the trigger action should always be + * executed. If there is a WHEN clause, the trigger action should only + * be executed if the WHEN clause returns TRUE. + *

* - * @return {@code true} if the trigger action should be executed (that is, - * if there is no WHEN clause or if the WHEN clause evaluates to TRUE), - * {@code false} otherwise - * @throws StandardException if an error happens when executing the - * WHEN clause + * @throws StandardException if trigger execution fails */ - final boolean executeWhenClause() throws StandardException { + final void executeWhenClauseAndAction() throws StandardException { SPSDescriptor whenClauseDescriptor = getWhenClause(); - - if (whenClauseDescriptor == null) { - // Always execute the trigger action if there is no WHEN clause. - return true; - } - - try { - return executeSPS(whenClauseDescriptor, true); - } finally { - if (spsWhenActivation != null) { - spsWhenActivation.close(); - } - whenPS = null; - spsWhenActivation = null; + if (whenClauseDescriptor == null || + executeSPS(whenClauseDescriptor, true)) { + executeSPS(getAction(), false); } } } Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/RowTriggerExecutor.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/RowTriggerExecutor.java?rev=1524645&r1=1524644&r2=1524645&view=diff ============================================================================== --- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/RowTriggerExecutor.java (original) +++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/RowTriggerExecutor.java Thu Sep 19 07:51:45 2013 @@ -110,11 +110,7 @@ class RowTriggerExecutor extends Generic if (event.isAfter()) tec.updateAICounters(); - // Execute the trigger action only if the WHEN clause returns - // TRUE or there is no WHEN clause. - if (executeWhenClause()) { - executeSPS(getAction(), false); - } + executeWhenClauseAndAction(); /* For BEFORE ROW triggers, update the ai values after the SPS Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/StatementTriggerExecutor.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/StatementTriggerExecutor.java?rev=1524645&r1=1524644&r2=1524645&view=diff ============================================================================== --- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/StatementTriggerExecutor.java (original) +++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/StatementTriggerExecutor.java Thu Sep 19 07:51:45 2013 @@ -78,15 +78,11 @@ class StatementTriggerExecutor extends G tec.setBeforeResultSet(brs); tec.setAfterResultSet(ars); - // Execute the trigger action only if the WHEN clause returns - // TRUE or there is no WHEN clause. - if (executeWhenClause()) { - try { - executeSPS(getAction(), false); - } finally { - clearSPS(); - tec.clearTrigger(); - } + try { + executeWhenClauseAndAction(); + } finally { + clearSPS(); + tec.clearTrigger(); } } }