From 770a8968642dc608f20783b0c210a9e23331277b Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 18 Mar 2019 14:24:07 +0200 Subject: [PATCH] Boolean result store transaction --- .../db/sql/tables/ExtensionProviderTable.java | 15 +++++ .../StorePlayerBooleanResultTransaction.java | 59 +++++++++++++++++-- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ExtensionProviderTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ExtensionProviderTable.java index e4dd6940e..06e23ca8c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ExtensionProviderTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/ExtensionProviderTable.java @@ -16,6 +16,12 @@ */ package com.djrapitops.plan.db.sql.tables; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.UUID; + +import static com.djrapitops.plan.db.sql.parsing.Sql.*; + /** * Table information about 'plan_extension_providers'. * @@ -40,4 +46,13 @@ public class ExtensionProviderTable { public static final String FORMAT_TYPE = "format_type"; // Can be null public static final String IS_PLAYER_NAME = "player_name"; // default false + public static final String STATEMENT_SELECT_PROVIDER_ID = SELECT + ID + FROM + TABLE_NAME + + WHERE + PROVIDER_NAME + "=?" + + AND + PLUGIN_ID + "=" + ExtensionPluginTable.STATEMENT_SELECT_PLUGIN_ID; + + public static void set3PluginValuesToStatement(PreparedStatement statement, int parameterIndex, String providerName, String pluginName, UUID serverUUID) throws SQLException { + statement.setString(parameterIndex, providerName); + ExtensionPluginTable.set2PluginValuesToStatement(statement, parameterIndex + 1, pluginName, serverUUID); + } + } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/results/StorePlayerBooleanResultTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/results/StorePlayerBooleanResultTransaction.java index cd264403a..89de8091a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/results/StorePlayerBooleanResultTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/results/StorePlayerBooleanResultTransaction.java @@ -16,10 +16,19 @@ */ package com.djrapitops.plan.extension.implementation.storage.transactions.results; +import com.djrapitops.plan.db.access.ExecStatement; +import com.djrapitops.plan.db.access.Executable; import com.djrapitops.plan.db.access.transactions.Transaction; +import com.djrapitops.plan.db.sql.tables.ExtensionProviderTable; +import java.sql.PreparedStatement; +import java.sql.SQLException; import java.util.UUID; +import static com.djrapitops.plan.db.sql.parsing.Sql.AND; +import static com.djrapitops.plan.db.sql.parsing.Sql.WHERE; +import static com.djrapitops.plan.db.sql.tables.ExtensionPlayerValues.*; + /** * Transaction to store method result of a {@link com.djrapitops.plan.extension.implementation.providers.BooleanDataProvider}. * @@ -29,21 +38,63 @@ public class StorePlayerBooleanResultTransaction extends Transaction { private final String pluginName; private final UUID serverUUID; - private final String methodName; + private final String providerName; private final UUID playerUUID; private final boolean value; - public StorePlayerBooleanResultTransaction(String pluginName, UUID serverUUID, String methodName, UUID playerUUID, boolean value) { + public StorePlayerBooleanResultTransaction(String pluginName, UUID serverUUID, String providerName, UUID playerUUID, boolean value) { this.pluginName = pluginName; this.serverUUID = serverUUID; - this.methodName = methodName; + this.providerName = providerName; this.playerUUID = playerUUID; this.value = value; } @Override protected void performOperations() { - // TODO Store data in a table + execute(storeValue()); + } + + private Executable storeValue() { + return connection -> { + if (!updateValue().execute(connection)) { + return insertValue().execute(connection); + } + return false; + }; + } + + private Executable updateValue() { + String sql = "UPDATE " + TABLE_NAME + + " SET (" + + BOOLEAN_VALUE + "=?" + + ")" + WHERE + USER_UUID + "=?" + + AND + PROVIDER_ID + "=" + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID; + + return new ExecStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setBoolean(1, value); + statement.setString(2, playerUUID.toString()); + ExtensionProviderTable.set3PluginValuesToStatement(statement, 3, providerName, pluginName, serverUUID); + } + }; + } + + private Executable insertValue() { + String sql = "INSERT INTO " + TABLE_NAME + "(" + + BOOLEAN_VALUE + "," + + USER_UUID + "," + + PROVIDER_ID + + ") VALUES (?,?," + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID + ")"; + return new ExecStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setBoolean(1, value); + statement.setString(2, playerUUID.toString()); + ExtensionProviderTable.set3PluginValuesToStatement(statement, 3, providerName, pluginName, serverUUID); + } + }; } } \ No newline at end of file