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 afac60b2e..e4dd6940e 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 @@ -38,6 +38,6 @@ public class ExtensionProviderTable { public static final String PROVIDED_CONDITION = "provided_condition"; // Can be null public static final String FORMAT_TYPE = "format_type"; // Can be null - public static final String PLAYER_NAME = "player_name"; // default false + public static final String IS_PLAYER_NAME = "player_name"; // default false } \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreStringProviderTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreStringProviderTransaction.java index f1c01ac50..8ee23c900 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreStringProviderTransaction.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/providers/StoreStringProviderTransaction.java @@ -16,12 +16,23 @@ */ package com.djrapitops.plan.extension.implementation.storage.transactions.providers; +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.ExtensionIconTable; +import com.djrapitops.plan.db.sql.tables.ExtensionPluginTable; +import com.djrapitops.plan.db.sql.tables.ExtensionTabTable; import com.djrapitops.plan.extension.implementation.ProviderInformation; import com.djrapitops.plan.extension.implementation.providers.DataProvider; +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.ExtensionProviderTable.*; + /** * Transaction to store information about a {@link com.djrapitops.plan.extension.implementation.providers.StringDataProvider}. * @@ -29,20 +40,87 @@ import java.util.UUID; */ public class StoreStringProviderTransaction extends Transaction { - private final DataProvider provider; private final boolean playerName; private final UUID serverUUID; + private ProviderInformation providerInformation; public StoreStringProviderTransaction(DataProvider provider, boolean playerName, UUID serverUUID) { - this.provider = provider; this.playerName = playerName; this.serverUUID = serverUUID; + providerInformation = provider.getProviderInformation(); } @Override protected void performOperations() { - ProviderInformation providerInformation = provider.getProviderInformation(); + execute(storeProvider()); + } - // TODO Store provider in a table + private Executable storeProvider() { + return connection -> { + if (!updateProvider().execute(connection)) { + return insertProvider().execute(connection); + } + return false; + }; + } + + private Executable updateProvider() { + String sql = "UPDATE " + TABLE_NAME + + " SET (" + + TEXT + "=?," + + DESCRIPTION + "=?," + + PRIORITY + "=?," + + CONDITION + "=?," + + IS_PLAYER_NAME + "=?," + + TAB_ID + "=" + ExtensionTabTable.STATEMENT_SELECT_TAB_ID + "," + + ICON_ID + "=" + ExtensionIconTable.STATEMENT_SELECT_ICON_ID + + ")" + WHERE + PLUGIN_ID + "=" + ExtensionPluginTable.STATEMENT_SELECT_PLUGIN_ID + + AND + PROVIDER_NAME + "=?"; + + return new ExecStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, providerInformation.getText()); + statement.setString(2, providerInformation.getDescription()); + statement.setInt(3, providerInformation.getPriority()); + statement.setString(4, providerInformation.getCondition().orElse(null)); + statement.setBoolean(5, playerName); + ExtensionTabTable.set3TabValuesToStatement(statement, 6, providerInformation.getTab().orElse(null), providerInformation.getPluginName(), serverUUID); + ExtensionIconTable.set3IconValuesToStatement(statement, 9, providerInformation.getIcon()); + ExtensionPluginTable.set2PluginValuesToStatement(statement, 11, providerInformation.getPluginName(), serverUUID); + statement.setString(13, providerInformation.getName()); + } + }; + } + + private Executable insertProvider() { + String sql = "INSERT INTO " + TABLE_NAME + "(" + + PROVIDER_NAME + "," + + TEXT + "," + + DESCRIPTION + "," + + PRIORITY + "," + + CONDITION + "," + + IS_PLAYER_NAME + "," + + TAB_ID + "," + + ICON_ID + "," + + PLUGIN_ID + + ") VALUES (?,?,?,?,?,?," + + ExtensionTabTable.STATEMENT_SELECT_TAB_ID + "," + + ExtensionIconTable.STATEMENT_SELECT_ICON_ID + "," + + ExtensionPluginTable.STATEMENT_SELECT_PLUGIN_ID + ")"; + return new ExecStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, providerInformation.getName()); + statement.setString(2, providerInformation.getText()); + statement.setString(3, providerInformation.getDescription()); + statement.setInt(4, providerInformation.getPriority()); + statement.setString(5, providerInformation.getCondition().orElse(null)); + statement.setBoolean(6, playerName); + ExtensionTabTable.set3TabValuesToStatement(statement, 7, providerInformation.getTab().orElse(null), providerInformation.getPluginName(), serverUUID); + ExtensionIconTable.set3IconValuesToStatement(statement, 10, providerInformation.getIcon()); + ExtensionPluginTable.set2PluginValuesToStatement(statement, 12, providerInformation.getPluginName(), serverUUID); + } + }; } } \ No newline at end of file