Boolean result store transaction

This commit is contained in:
Rsl1122 2019-03-18 14:24:07 +02:00
parent 47cd1bab6e
commit 770a896864
2 changed files with 70 additions and 4 deletions

View File

@ -16,6 +16,12 @@
*/ */
package com.djrapitops.plan.db.sql.tables; 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'. * 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 FORMAT_TYPE = "format_type"; // Can be null
public static final String IS_PLAYER_NAME = "player_name"; // default false 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);
}
} }

View File

@ -16,10 +16,19 @@
*/ */
package com.djrapitops.plan.extension.implementation.storage.transactions.results; 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.access.transactions.Transaction;
import com.djrapitops.plan.db.sql.tables.ExtensionProviderTable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID; 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}. * 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 String pluginName;
private final UUID serverUUID; private final UUID serverUUID;
private final String methodName; private final String providerName;
private final UUID playerUUID; private final UUID playerUUID;
private final boolean value; 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.pluginName = pluginName;
this.serverUUID = serverUUID; this.serverUUID = serverUUID;
this.methodName = methodName; this.providerName = providerName;
this.playerUUID = playerUUID; this.playerUUID = playerUUID;
this.value = value; this.value = value;
} }
@Override @Override
protected void performOperations() { 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);
}
};
} }
} }