Plan/Plan/common/src/main/java/com/djrapitops/plan/extension/implementation/storage/transactions/results/StoreServerDoubleResultTran...

97 lines
3.7 KiB
Java

/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.extension.implementation.storage.transactions.results;
import com.djrapitops.plan.extension.implementation.ProviderInformation;
import com.djrapitops.plan.extension.implementation.providers.Parameters;
import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.storage.database.sql.tables.extension.ExtensionProviderTable;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Executable;
import com.djrapitops.plan.storage.database.transactions.ThrowawayTransaction;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import static com.djrapitops.plan.storage.database.sql.building.Sql.WHERE;
import static com.djrapitops.plan.storage.database.sql.tables.extension.ExtensionServerValueTable.*;
/**
* Transaction to store method result of a double.
*
* @author AuroraLS3
*/
public class StoreServerDoubleResultTransaction extends ThrowawayTransaction {
private final String pluginName;
private final ServerUUID serverUUID;
private final String providerName;
private final double value;
private final boolean percentage;
public StoreServerDoubleResultTransaction(ProviderInformation info, Parameters parameters, double value) {
this.pluginName = info.getPluginName();
this.providerName = info.getName();
this.percentage = info.isPercentage();
this.serverUUID = parameters.getServerUUID();
this.value = value;
}
@Override
protected void performOperations() {
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 " +
(percentage ? PERCENTAGE_VALUE : DOUBLE_VALUE) + "=?" +
WHERE + PROVIDER_ID + "=" + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID;
return new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setDouble(1, value);
ExtensionProviderTable.set3PluginValuesToStatement(statement, 2, providerName, pluginName, serverUUID);
}
};
}
private Executable insertValue() {
String sql = "INSERT INTO " + TABLE_NAME + "(" +
(percentage ? PERCENTAGE_VALUE : DOUBLE_VALUE) + "," +
PROVIDER_ID +
") VALUES (?," + ExtensionProviderTable.STATEMENT_SELECT_PROVIDER_ID + ")";
return new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setDouble(1, value);
ExtensionProviderTable.set3PluginValuesToStatement(statement, 2, providerName, pluginName, serverUUID);
}
};
}
}