mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-02-04 06:21:43 +01:00
Refactored SettingsTable#fetchNewerConfig to a query
This commit is contained in:
parent
1cf8daf6c2
commit
59040df981
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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.db.access.queries.objects;
|
||||
|
||||
import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.system.settings.config.Config;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigReader;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
import java.util.Scanner;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.djrapitops.plan.db.sql.tables.SettingsTable.*;
|
||||
|
||||
/**
|
||||
* Query to fetch a newer config from the database.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class NewerConfigQuery extends QueryStatement<Optional<Config>> {
|
||||
|
||||
private static final String SELECT_STATEMENT = "SELECT " + CONFIG_CONTENT + " FROM " + TABLE_NAME +
|
||||
" WHERE " + UPDATED + ">? AND " +
|
||||
SERVER_UUID + "=? LIMIT 1";
|
||||
|
||||
private final UUID serverUUID;
|
||||
private final long updatedAfter;
|
||||
|
||||
/**
|
||||
* Create a new Query.
|
||||
*
|
||||
* @param serverUUID UUID of the server
|
||||
* @param updatedAfter Epoch ms.
|
||||
*/
|
||||
public NewerConfigQuery(UUID serverUUID, long updatedAfter) {
|
||||
super(SELECT_STATEMENT);
|
||||
this.serverUUID = serverUUID;
|
||||
this.updatedAfter = updatedAfter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setLong(1, updatedAfter);
|
||||
statement.setString(2, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Config> processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
try (ConfigReader reader = new ConfigReader(new Scanner(set.getString(CONFIG_CONTENT)))) {
|
||||
return Optional.of(reader.read());
|
||||
}
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
@ -18,18 +18,8 @@ package com.djrapitops.plan.db.sql.tables;
|
||||
|
||||
import com.djrapitops.plan.db.DBType;
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
|
||||
import com.djrapitops.plan.db.sql.parsing.Sql;
|
||||
import com.djrapitops.plan.system.settings.config.Config;
|
||||
import com.djrapitops.plan.system.settings.config.ConfigReader;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
import java.util.Scanner;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Table that represents plan_settings.
|
||||
@ -67,36 +57,4 @@ public class SettingsTable extends Table {
|
||||
.column(CONFIG_CONTENT, "TEXT").notNull()
|
||||
.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a config that was placed into the database after a certain epoch ms.
|
||||
*
|
||||
* @param updatedAfter Epoch ms.
|
||||
* @param serverUUID UUID of the server
|
||||
* @return Optional Config if a new config is found, empty if not.
|
||||
*/
|
||||
public Optional<Config> fetchNewerConfig(long updatedAfter, UUID serverUUID) {
|
||||
String sql = "SELECT " + CONFIG_CONTENT + " FROM " + tableName +
|
||||
" WHERE " + UPDATED + ">? AND " +
|
||||
SERVER_UUID + "=? LIMIT 1";
|
||||
|
||||
return Optional.ofNullable(query(new QueryStatement<Config>(sql, 10) {
|
||||
@Override
|
||||
public void prepare(PreparedStatement statement) throws SQLException {
|
||||
statement.setLong(1, updatedAfter);
|
||||
statement.setString(2, serverUUID.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Config processResults(ResultSet set) throws SQLException {
|
||||
if (set.next()) {
|
||||
try (ConfigReader reader = new ConfigReader(new Scanner(set.getString(CONFIG_CONTENT)))) {
|
||||
return reader.read();
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
@ -185,6 +185,6 @@ public class SQLFetchOps extends SQLOps implements FetchOperations {
|
||||
|
||||
@Override
|
||||
public Optional<Config> getNewConfig(long updatedAfter, UUID serverUUID) {
|
||||
return settingsTable.fetchNewerConfig(updatedAfter, serverUUID);
|
||||
return db.query(new NewerConfigQuery(serverUUID, updatedAfter));
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ package com.djrapitops.plan.system.settings.network;
|
||||
|
||||
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||
import com.djrapitops.plan.db.Database;
|
||||
import com.djrapitops.plan.db.access.queries.objects.NewerConfigQuery;
|
||||
import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
|
||||
import com.djrapitops.plan.db.access.transactions.StoreConfigTransaction;
|
||||
import com.djrapitops.plan.system.SubSystem;
|
||||
@ -181,7 +182,7 @@ public class NetworkSettingManager implements SubSystem {
|
||||
File configFile = getServerConfigFile(serverUUID);
|
||||
long lastModified = configFile.exists() ? configFile.lastModified() : -1;
|
||||
|
||||
Optional<Config> foundConfig = database.fetch().getNewConfig(lastModified, serverUUID);
|
||||
Optional<Config> foundConfig = database.query(new NewerConfigQuery(serverUUID, lastModified));
|
||||
if (foundConfig.isPresent()) {
|
||||
try {
|
||||
Config writing = foundConfig.get();
|
||||
|
@ -17,6 +17,7 @@
|
||||
package com.djrapitops.plan.system.settings.network;
|
||||
|
||||
import com.djrapitops.plan.db.Database;
|
||||
import com.djrapitops.plan.db.access.queries.objects.NewerConfigQuery;
|
||||
import com.djrapitops.plan.db.access.transactions.StoreConfigTransaction;
|
||||
import com.djrapitops.plan.system.SubSystem;
|
||||
import com.djrapitops.plan.system.database.DBSystem;
|
||||
@ -131,7 +132,7 @@ public class ServerSettingsManager implements SubSystem {
|
||||
File configFile = files.getConfigFile();
|
||||
long lastModified = configFile.exists() ? configFile.lastModified() : -1;
|
||||
|
||||
Optional<Config> foundConfig = database.fetch().getNewConfig(lastModified, serverInfo.getServerUUID());
|
||||
Optional<Config> foundConfig = database.query(new NewerConfigQuery(serverInfo.getServerUUID(), lastModified));
|
||||
if (foundConfig.isPresent()) {
|
||||
try {
|
||||
new ConfigWriter(configFile.toPath()).write(foundConfig.get());
|
||||
|
@ -956,7 +956,7 @@ public abstract class CommonDBTest {
|
||||
|
||||
db.executeTransaction(new StoreConfigTransaction(serverUUID, config, System.currentTimeMillis()));
|
||||
|
||||
Optional<Config> foundConfig = db.getSettingsTable().fetchNewerConfig(0, serverUUID);
|
||||
Optional<Config> foundConfig = db.query(new NewerConfigQuery(serverUUID, 0));
|
||||
assertTrue(foundConfig.isPresent());
|
||||
assertEquals(config, foundConfig.get());
|
||||
}
|
||||
@ -970,7 +970,7 @@ public abstract class CommonDBTest {
|
||||
|
||||
db.executeTransaction(new StoreConfigTransaction(serverUUID, config, System.currentTimeMillis()));
|
||||
|
||||
assertFalse(db.getSettingsTable().fetchNewerConfig(savedMs, serverUUID).isPresent());
|
||||
assertFalse(db.query(new NewerConfigQuery(serverUUID, savedMs)).isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user