From d1085eed2597e9449abdd8711f8dcc99dadbe13e Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 22 Jan 2019 20:26:37 +0200 Subject: [PATCH] Added conditional execution to Transaction --- .../db/access/transactions/Transaction.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/Transaction.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/Transaction.java index 4bb801b5b..fc8bb10df 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/Transaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/Transaction.java @@ -50,6 +50,10 @@ public abstract class Transaction { Verify.nullCheck(db, () -> new IllegalArgumentException("Given database was null")); Verify.isFalse(success, () -> new IllegalStateException("Transaction has already been executed")); + if (!shouldBeExecuted()) { + return; + } + try { initializeTransaction(db); execute(); @@ -59,6 +63,21 @@ public abstract class Transaction { } } + /** + * Override this method for conditional execution. + *

+ * Please note that the transaction has not been initialized and class variables are not available for + * queries. The condition should depend on other variables (Like the data that is to be stored) given to the transaction. + * + * @return false if the transaction should not execute. + */ + protected boolean shouldBeExecuted() { + return true; + } + + /** + * Implement this method for transaction execution. + */ protected abstract void execute(); private void initializeTransaction(SQLDB db) {