Refactored PingTable#getPing to a query

This commit is contained in:
Rsl1122 2019-02-01 18:22:50 +02:00
parent 2d05c83b42
commit b23235b50b
3 changed files with 33 additions and 35 deletions

View File

@ -17,11 +17,13 @@
package com.djrapitops.plan.db.access.queries;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.data.container.Ping;
import com.djrapitops.plan.data.container.UserInfo;
import com.djrapitops.plan.db.access.HasMoreThanZeroQueryStatement;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.tables.GeoInfoTable;
import com.djrapitops.plan.db.sql.tables.PingTable;
import com.djrapitops.plan.db.sql.tables.UserInfoTable;
import com.djrapitops.plan.db.sql.tables.UsersTable;
@ -167,4 +169,34 @@ public class PlayerFetchQueries {
}
};
}
public static Query<List<Ping>> playerPingData(UUID playerUUID) {
String sql = "SELECT * FROM " + PingTable.TABLE_NAME +
" WHERE " + PingTable.USER_UUID + "=?";
return new QueryStatement<List<Ping>>(sql, 10000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, playerUUID.toString());
}
@Override
public List<Ping> processResults(ResultSet set) throws SQLException {
List<Ping> pings = new ArrayList<>();
while (set.next()) {
pings.add(new Ping(
set.getLong(PingTable.DATE),
UUID.fromString(set.getString(PingTable.SERVER_UUID)),
set.getInt(PingTable.MIN_PING),
set.getInt(PingTable.MAX_PING),
set.getDouble(PingTable.AVG_PING)
)
);
}
return pings;
}
};
}
}

View File

@ -54,7 +54,7 @@ public class PlayerContainerQuery implements Query<PlayerContainer> {
container.putAll(db.getUsersTable().getUserInformation(uuid));
container.putCachingSupplier(PlayerKeys.GEO_INFO, () -> db.query(PlayerFetchQueries.playerGeoInfo(uuid)));
container.putCachingSupplier(PlayerKeys.PING, () -> db.getPingTable().getPing(uuid));
container.putCachingSupplier(PlayerKeys.PING, () -> db.query(PlayerFetchQueries.playerPingData(uuid)));
container.putCachingSupplier(PlayerKeys.NICKNAMES, () -> db.getNicknamesTable().getNicknameInformation(uuid));
container.putCachingSupplier(PlayerKeys.PER_SERVER, () -> db.query(new PerServerContainerQuery(uuid)));

View File

@ -20,16 +20,12 @@ import com.djrapitops.plan.data.container.Ping;
import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.patches.PingOptimizationPatch;
import com.djrapitops.plan.db.sql.parsing.CreateTableParser;
import com.djrapitops.plan.db.sql.parsing.Sql;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
@ -90,34 +86,4 @@ public class PingTable extends Table {
}
});
}
public List<Ping> getPing(UUID uuid) {
String sql = "SELECT * FROM " + tableName +
" WHERE " + USER_UUID + "=?";
return query(new QueryStatement<List<Ping>>(sql, 10000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
@Override
public List<Ping> processResults(ResultSet set) throws SQLException {
List<Ping> pings = new ArrayList<>();
while (set.next()) {
pings.add(new Ping(
set.getLong(DATE),
UUID.fromString(set.getString(SERVER_UUID)),
set.getInt(MIN_PING),
set.getInt(MAX_PING),
set.getDouble(AVG_PING)
)
);
}
return pings;
}
});
}
}