AuctionStatistic uses uuid instead of id auto increment

Took 32 minutes
This commit is contained in:
Kiran Hart 2022-09-08 13:24:07 -04:00
parent 55dd436e9d
commit 39177a1fbd
No known key found for this signature in database
GPG Key ID: 5F36C7BC79D3EBC3
3 changed files with 78 additions and 10 deletions

View File

@ -18,19 +18,40 @@
package ca.tweetzy.auctionhouse.auction;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.api.interfaces.Storeable;
import ca.tweetzy.auctionhouse.auction.enums.AuctionStatisticType;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.UUID;
import java.util.function.Consumer;
@AllArgsConstructor
@Getter
public final class AuctionStatistic {
public final class AuctionStatistic implements Storeable<AuctionStatistic> {
private final UUID id;
private final UUID statOwner;
private final AuctionStatisticType statisticType;
private final double value;
private final long time;
public AuctionStatistic(UUID statOwner, AuctionStatisticType type, double value) {
this(UUID.randomUUID(), statOwner, type, value, System.currentTimeMillis());
}
@Override
public void store(Consumer<AuctionStatistic> stored) {
AuctionHouse.getInstance().getDataManager().insertStatistic(this, (error, statistic) -> {
if (error != null) return;
if (statistic != null) {
AuctionHouse.getInstance().getAuctionStatisticManager().addStatistic(statistic);
if (stored != null)
stored.accept(statistic);
}
});
}
}

View File

@ -3,10 +3,7 @@ package ca.tweetzy.auctionhouse.database;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.api.AuctionAPI;
import ca.tweetzy.auctionhouse.auction.*;
import ca.tweetzy.auctionhouse.auction.enums.AdminAction;
import ca.tweetzy.auctionhouse.auction.enums.AuctionItemCategory;
import ca.tweetzy.auctionhouse.auction.enums.AuctionSaleType;
import ca.tweetzy.auctionhouse.auction.enums.AuctionSortType;
import ca.tweetzy.auctionhouse.auction.enums.*;
import ca.tweetzy.auctionhouse.settings.Settings;
import ca.tweetzy.auctionhouse.transaction.Transaction;
import ca.tweetzy.core.database.DataManagerAbstract;
@ -393,6 +390,49 @@ public class DataManager extends DataManagerAbstract {
}));
}
public void insertStatistic(AuctionStatistic statistic, Callback<AuctionStatistic> callback) {
this.thread.execute(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO " + this.getTablePrefix() + "statistic (id, stat_owner, stat_type, value, time) VALUES (?, ?, ?, ?, ?)")) {
PreparedStatement fetch = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "statistic WHERE id = ?");
fetch.setString(1, statistic.getId().toString());
statement.setString(1, statistic.getId().toString());
statement.setString(2, statistic.getStatOwner().toString());
statement.setString(3, statistic.getStatisticType().name());
statement.setDouble(4, statistic.getValue());
statement.setLong(5, statistic.getTime());
if (callback != null) {
ResultSet res = fetch.executeQuery();
res.next();
callback.accept(null, extractAuctionStatistic(res));
}
} catch (Exception e) {
e.printStackTrace();
resolveCallback(callback, e);
}
}));
}
public void getStatistics(Callback<List<AuctionStatistic>> callback) {
List<AuctionStatistic> stats = new ArrayList<>();
this.thread.execute(() -> this.databaseConnector.connect(connection -> {
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM " + this.getTablePrefix() + "statistic")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
stats.add(extractAuctionStatistic(resultSet));
}
callback.accept(null, stats);
} catch (Exception e) {
resolveCallback(callback, e);
}
}));
}
@Deprecated
public void getStats(Callback<Map<UUID, AuctionStat<Integer, Integer, Integer, Double, Double>>> callback) {
Map<UUID, AuctionStat<Integer, Integer, Integer, Double, Double>> stats = new HashMap<>();
this.async(() -> this.databaseConnector.connect(connection -> {
@ -415,6 +455,7 @@ public class DataManager extends DataManagerAbstract {
}));
}
@Deprecated
public void updateStats(Map<UUID, AuctionStat<Integer, Integer, Integer, Double, Double>> stats, UpdateCallback callback) {
this.databaseConnector.connect(connection -> {
connection.setAutoCommit(false);
@ -588,6 +629,16 @@ public class DataManager extends DataManagerAbstract {
}));
}
private AuctionStatistic extractAuctionStatistic(ResultSet resultSet) throws SQLException {
return new AuctionStatistic(
UUID.fromString(resultSet.getString("uuid")),
UUID.fromString(resultSet.getString("stat_owner")),
AuctionStatisticType.valueOf(resultSet.getString("stat_type")),
resultSet.getDouble("value"),
resultSet.getLong("time")
);
}
private AuctionPlayer extractAuctionPlayer(ResultSet resultSet) throws SQLException {
return new AuctionPlayer(
UUID.fromString(resultSet.getString("uuid")),

View File

@ -1,8 +1,6 @@
package ca.tweetzy.auctionhouse.database.migrations;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.core.database.DataMigration;
import ca.tweetzy.core.database.MySQLConnector;
import java.sql.Connection;
import java.sql.SQLException;
@ -23,11 +21,9 @@ public class _16_StatisticVersionTwoMigration extends DataMigration {
@Override
public void migrate(Connection connection, String tablePrefix) throws SQLException {
String autoIncrement = AuctionHouse.getInstance().getDatabaseConnector() instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE " + tablePrefix + "statistic (" +
"id INTEGER PRIMARY KEY" + autoIncrement + ", " +
"id VARCHAR(36) PRIMARY KEY, " +
"stat_owner VARCHAR(36) NOT NULL, " +
"stat_type VARCHAR(20) NOT NULL, " +
"value DOUBLE NOT NULL, " +