From b0a73374d44ca1a270839390b059bfc743ba1975 Mon Sep 17 00:00:00 2001 From: Aurora Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Sun, 5 Jun 2022 11:32:37 +0300 Subject: [PATCH] Refactored PlayerCountQueries to reduce boilerplate code - Created utility methods in Database that allow faster extraction of results without implementing so many extra QueryStatements. --- .../plan/storage/database/Database.java | 69 +++- .../database/queries/MapRowExtractor.java | 28 ++ .../queries/QueryParameterSetter.java | 56 +++ .../database/queries/RowExtractor.java | 27 ++ .../queries/analysis/PlayerCountQueries.java | 364 +++++------------- 5 files changed, 278 insertions(+), 266 deletions(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/MapRowExtractor.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/QueryParameterSetter.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/RowExtractor.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/Database.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/Database.java index ae2086ee3..2c93b987c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/Database.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/Database.java @@ -17,11 +17,16 @@ package com.djrapitops.plan.storage.database; import com.djrapitops.plan.exceptions.database.DBInitException; -import com.djrapitops.plan.storage.database.queries.Query; +import com.djrapitops.plan.storage.database.queries.*; import com.djrapitops.plan.storage.database.sql.building.Sql; import com.djrapitops.plan.storage.database.transactions.Transaction; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.*; import java.util.concurrent.CompletableFuture; +import java.util.function.Supplier; /** * Interface for interacting with a Plan SQL database. @@ -52,6 +57,68 @@ public interface Database { */ T query(Query query); + default Optional queryOptional(String sql, RowExtractor rowExtractor, Object... parameters) { + return query(new QueryStatement>(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + QueryParameterSetter.setParameters(statement, parameters); + } + + @Override + public Optional processResults(ResultSet set) throws SQLException { + return set.next() ? Optional.of(rowExtractor.extract(set)) : Optional.empty(); + } + }); + } + + default List queryList(String sql, RowExtractor rowExtractor, Object... parameters) { + return queryCollection(sql, rowExtractor, ArrayList::new, parameters); + } + + default Set querySet(String sql, RowExtractor rowExtractor, Object... parameters) { + return queryCollection(sql, rowExtractor, HashSet::new, parameters); + } + + default , T> C queryCollection(String sql, RowExtractor rowExtractor, Supplier collectionConstructor, Object... parameters) { + return query(new QueryStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + QueryParameterSetter.setParameters(statement, parameters); + } + + @Override + public C processResults(ResultSet set) throws SQLException { + C collection = collectionConstructor.get(); + while (set.next()) { + collection.add(rowExtractor.extract(set)); + } + return collection; + } + }); + } + + default Map queryMap(String sql, MapRowExtractor rowExtractor, Object... parameters) { + return queryMap(sql, rowExtractor, HashMap::new, parameters); + } + + default , K, V> M queryMap(String sql, MapRowExtractor rowExtractor, Supplier mapConstructor, Object... parameters) { + return query(new QueryStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + QueryParameterSetter.setParameters(statement, parameters); + } + + @Override + public M processResults(ResultSet set) throws SQLException { + M map = mapConstructor.get(); + while (set.next()) { + rowExtractor.extract(set, map); + } + return map; + } + }); + } + /** * Execute an SQL Transaction. * diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/MapRowExtractor.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/MapRowExtractor.java new file mode 100644 index 000000000..521c648a1 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/MapRowExtractor.java @@ -0,0 +1,28 @@ +/* + * 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 . + */ +package com.djrapitops.plan.storage.database.queries; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Map; + +@FunctionalInterface +public interface MapRowExtractor { + + void extract(ResultSet set, Map to) throws SQLException; + +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/QueryParameterSetter.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/QueryParameterSetter.java new file mode 100644 index 000000000..c91de3f6d --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/QueryParameterSetter.java @@ -0,0 +1,56 @@ +/* + * 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 . + */ +package com.djrapitops.plan.storage.database.queries; + +import com.djrapitops.plan.identification.ServerUUID; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Types; +import java.util.UUID; + +public class QueryParameterSetter { + + private QueryParameterSetter() {} + + public static void setParameters(PreparedStatement statement, Object... parameters) throws SQLException { + int index = 1; + for (Object parameter : parameters) { + setParameter(statement, index, parameter); + index++; + } + } + + private static void setParameter(PreparedStatement statement, int index, Object parameter) throws SQLException { + if (parameter == null) { + statement.setNull(index, Types.VARCHAR); + } else if (parameter instanceof Integer) { + statement.setInt(index, (Integer) parameter); + } else if (parameter instanceof Long) { + statement.setLong(index, (Long) parameter); + } else if (parameter instanceof Double) { + statement.setDouble(index, (Double) parameter); + } else if (parameter instanceof Float) { + statement.setFloat(index, (Float) parameter); + } else if (parameter instanceof String) { + statement.setString(index, (String) parameter); + } else if (parameter instanceof UUID || parameter instanceof ServerUUID) { + statement.setString(index, parameter.toString()); + } + } + +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/RowExtractor.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/RowExtractor.java new file mode 100644 index 000000000..2bd3b6b89 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/RowExtractor.java @@ -0,0 +1,27 @@ +/* + * 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 . + */ +package com.djrapitops.plan.storage.database.queries; + +import java.sql.ResultSet; +import java.sql.SQLException; + +@FunctionalInterface +public interface RowExtractor { + + T extract(ResultSet set) throws SQLException; + +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/PlayerCountQueries.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/PlayerCountQueries.java index 04c6529f4..fc7285d5d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/PlayerCountQueries.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/analysis/PlayerCountQueries.java @@ -28,7 +28,10 @@ import com.djrapitops.plan.storage.database.sql.tables.UsersTable; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.*; +import java.util.Map; +import java.util.NavigableMap; +import java.util.TreeMap; +import java.util.UUID; import static com.djrapitops.plan.storage.database.sql.building.Sql.*; @@ -39,49 +42,21 @@ import static com.djrapitops.plan.storage.database.sql.building.Sql.*; */ public class PlayerCountQueries { + private static final String PLAYER_COUNT = "player_count"; + private PlayerCountQueries() { // Static method class } - private static QueryStatement queryPlayerCount(String sql, long after, long before, ServerUUID serverUUID) { - return new QueryStatement(sql) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, before); - statement.setLong(2, after); - statement.setString(3, serverUUID.toString()); - } - - @Override - public Integer processResults(ResultSet set) throws SQLException { - return set.next() ? set.getInt("player_count") : 0; - } - }; - } - - private static QueryStatement queryPlayerCount(String sql, long after, long before) { - return new QueryStatement(sql) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, before); - statement.setLong(2, after); - } - - @Override - public Integer processResults(ResultSet set) throws SQLException { - return set.next() ? set.getInt("player_count") : 0; - } - }; - } - public static Query uniquePlayerCount(long after, long before, ServerUUID serverUUID) { - String sql = SELECT + "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as player_count" + - FROM + SessionsTable.TABLE_NAME + - WHERE + SessionsTable.SESSION_END + "<=?" + - AND + SessionsTable.SESSION_START + ">=?" + - AND + SessionsTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID; - - return queryPlayerCount(sql, after, before, serverUUID); + return database -> database.queryOptional(SELECT + "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as " + PLAYER_COUNT + + FROM + SessionsTable.TABLE_NAME + + WHERE + SessionsTable.SESSION_END + "<=?" + + AND + SessionsTable.SESSION_START + ">=?" + + AND + SessionsTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID, + set -> set.getInt(PLAYER_COUNT), + before, after, serverUUID) + .orElse(0); } /** @@ -92,38 +67,29 @@ public class PlayerCountQueries { * @return Unique player count (players who played within time frame) */ public static Query uniquePlayerCount(long after, long before) { - String sql = SELECT + "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as player_count" + - FROM + SessionsTable.TABLE_NAME + - WHERE + SessionsTable.SESSION_END + "<=?" + - AND + SessionsTable.SESSION_START + ">=?"; - - return queryPlayerCount(sql, after, before); + return database -> database.queryOptional(SELECT + "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as " + PLAYER_COUNT + + FROM + SessionsTable.TABLE_NAME + + WHERE + SessionsTable.SESSION_END + "<=?" + + AND + SessionsTable.SESSION_START + ">=?", + set -> set.getInt(PLAYER_COUNT), + before, after) + .orElse(0); } public static Query> uniquePlayerCounts(long after, long before) { - String sql = SELECT + ServerTable.SERVER_UUID + ",COUNT(DISTINCT " + SessionsTable.USER_ID + ") as player_count" + + String sql = SELECT + ServerTable.SERVER_UUID + ",COUNT(DISTINCT " + SessionsTable.USER_ID + ") as " + PLAYER_COUNT + FROM + SessionsTable.TABLE_NAME + INNER_JOIN + ServerTable.TABLE_NAME + " se on se." + ServerTable.ID + "=" + SessionsTable.TABLE_NAME + '.' + SessionsTable.SERVER_ID + WHERE + SessionsTable.SESSION_END + "<=?" + AND + SessionsTable.SESSION_START + ">=?" + GROUP_BY + SessionsTable.SERVER_ID; - return new QueryStatement>(sql) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, before); - statement.setLong(2, after); - } - - @Override - public Map processResults(ResultSet set) throws SQLException { - Map byServer = new HashMap<>(); - while (set.next()) { - byServer.put(ServerUUID.fromString(set.getString(ServerTable.SERVER_UUID)), set.getInt("player_count")); - } - return byServer; - } - }; + return database -> database.queryMap(sql, + (set, byServer) -> byServer.put( + ServerUUID.fromString(set.getString(ServerTable.SERVER_UUID)), + set.getInt(PLAYER_COUNT) + ), + before, after); } /** @@ -141,31 +107,17 @@ public class PlayerCountQueries { String selectUniquePlayersPerDay = SELECT + sql.dateToEpochSecond(sql.dateToDayStamp(sql.epochSecondToDate('(' + SessionsTable.SESSION_START + "+?)/1000"))) + "*1000 as date," + - "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as player_count" + + "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as " + PLAYER_COUNT + FROM + SessionsTable.TABLE_NAME + WHERE + SessionsTable.SESSION_END + "<=?" + AND + SessionsTable.SESSION_START + ">=?" + AND + SessionsTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + GROUP_BY + "date"; - return database.query(new QueryStatement>(selectUniquePlayersPerDay, 100) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, timeZoneOffset); - statement.setLong(2, before); - statement.setLong(3, after); - statement.setString(4, serverUUID.toString()); - } - - @Override - public NavigableMap processResults(ResultSet set) throws SQLException { - NavigableMap uniquePerDay = new TreeMap<>(); - while (set.next()) { - uniquePerDay.put(set.getLong("date"), set.getInt("player_count")); - } - return uniquePerDay; - } - }); + return database.queryMap(selectUniquePlayersPerDay, + (set, perDay) -> perDay.put(set.getLong("date"), set.getInt(PLAYER_COUNT)), + TreeMap::new, + timeZoneOffset, before, after, serverUUID); }; } @@ -184,31 +136,17 @@ public class PlayerCountQueries { String selectUniquePlayersPerDay = SELECT + sql.dateToEpochSecond(sql.dateToHourStamp(sql.epochSecondToDate('(' + SessionsTable.SESSION_START + "+?)/1000"))) + "*1000 as date," + - "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as player_count" + + "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as " + PLAYER_COUNT + FROM + SessionsTable.TABLE_NAME + WHERE + SessionsTable.SESSION_END + "<=?" + AND + SessionsTable.SESSION_START + ">=?" + AND + SessionsTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + GROUP_BY + "date"; - return database.query(new QueryStatement>(selectUniquePlayersPerDay, 100) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, timeZoneOffset); - statement.setLong(2, before); - statement.setLong(3, after); - statement.setString(4, serverUUID.toString()); - } - - @Override - public NavigableMap processResults(ResultSet set) throws SQLException { - NavigableMap uniquePerDay = new TreeMap<>(); - while (set.next()) { - uniquePerDay.put(set.getLong("date"), set.getInt("player_count")); - } - return uniquePerDay; - } - }); + return database.queryMap(selectUniquePlayersPerDay, + (set, perDay) -> perDay.put(set.getLong("date"), set.getInt(PLAYER_COUNT)), + TreeMap::new, + timeZoneOffset, before, after, serverUUID); }; } @@ -226,29 +164,16 @@ public class PlayerCountQueries { String selectUniquePlayersPerDay = SELECT + sql.dateToEpochSecond(sql.dateToDayStamp(sql.epochSecondToDate('(' + SessionsTable.SESSION_START + "+?)/1000"))) + "*1000 as date," + - "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as player_count" + + "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as " + PLAYER_COUNT + FROM + SessionsTable.TABLE_NAME + WHERE + SessionsTable.SESSION_END + "<=?" + AND + SessionsTable.SESSION_START + ">=?" + GROUP_BY + "date"; - return database.query(new QueryStatement>(selectUniquePlayersPerDay, 100) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, timeZoneOffset); - statement.setLong(2, before); - statement.setLong(3, after); - } - - @Override - public NavigableMap processResults(ResultSet set) throws SQLException { - NavigableMap uniquePerDay = new TreeMap<>(); - while (set.next()) { - uniquePerDay.put(set.getLong("date"), set.getInt("player_count")); - } - return uniquePerDay; - } - }); + return database.queryMap(selectUniquePlayersPerDay, + (set, perDay) -> perDay.put(set.getLong("date"), set.getInt(PLAYER_COUNT)), + TreeMap::new, + timeZoneOffset, before, after); }; } @@ -266,29 +191,16 @@ public class PlayerCountQueries { String selectUniquePlayersPerDay = SELECT + sql.dateToEpochSecond(sql.dateToHourStamp(sql.epochSecondToDate('(' + SessionsTable.SESSION_START + "+?)/1000"))) + "*1000 as date," + - "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as player_count" + + "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as " + PLAYER_COUNT + FROM + SessionsTable.TABLE_NAME + WHERE + SessionsTable.SESSION_END + "<=?" + AND + SessionsTable.SESSION_START + ">=?" + GROUP_BY + "date"; - return database.query(new QueryStatement>(selectUniquePlayersPerDay, 100) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, timeZoneOffset); - statement.setLong(2, before); - statement.setLong(3, after); - } - - @Override - public NavigableMap processResults(ResultSet set) throws SQLException { - NavigableMap uniquePerDay = new TreeMap<>(); - while (set.next()) { - uniquePerDay.put(set.getLong("date"), set.getInt("player_count")); - } - return uniquePerDay; - } - }); + return database.queryMap(selectUniquePlayersPerDay, + (set, perDay) -> perDay.put(set.getLong("date"), set.getInt(PLAYER_COUNT)), + TreeMap::new, + timeZoneOffset, before, after); }; } @@ -298,74 +210,60 @@ public class PlayerCountQueries { String selectUniquePlayersPerDay = SELECT + sql.dateToEpochSecond(sql.dateToDayStamp(sql.epochSecondToDate('(' + SessionsTable.SESSION_START + "+?)/1000"))) + "*1000 as date," + - "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as player_count" + + "COUNT(DISTINCT " + SessionsTable.USER_ID + ") as " + PLAYER_COUNT + FROM + SessionsTable.TABLE_NAME + WHERE + SessionsTable.SESSION_END + "<=?" + AND + SessionsTable.SESSION_START + ">=?" + AND + SessionsTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + GROUP_BY + "date"; - String selectAverage = SELECT + "AVG(player_count) as average" + FROM + '(' + selectUniquePlayersPerDay + ") q1"; + String selectAverage = SELECT + "AVG(" + PLAYER_COUNT + ") as average" + FROM + '(' + selectUniquePlayersPerDay + ") q1"; - return database.query(new QueryStatement(selectAverage, 100) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, timeZoneOffset); - statement.setLong(2, before); - statement.setLong(3, after); - statement.setString(4, serverUUID.toString()); - } - - @Override - public Integer processResults(ResultSet set) throws SQLException { - return set.next() ? (int) set.getDouble("average") : 0; - } - }); + return database.queryOptional(selectAverage, + set -> (int) set.getDouble("average"), + timeZoneOffset, before, after, serverUUID) + .orElse(0); }; } public static Query newPlayerCount(long after, long before, ServerUUID serverUUID) { - String sql = SELECT + "COUNT(1) as player_count" + + String sql = SELECT + "COUNT(1) as " + PLAYER_COUNT + FROM + UserInfoTable.TABLE_NAME + WHERE + UserInfoTable.REGISTERED + "<=?" + AND + UserInfoTable.REGISTERED + ">=?" + AND + UserInfoTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID; - return queryPlayerCount(sql, after, before, serverUUID); + return database -> database.queryOptional(sql, + set -> set.getInt(PLAYER_COUNT), + before, after, serverUUID) + .orElse(0); } public static Query newPlayerCount(long after, long before) { - String sql = SELECT + "COUNT(1) as player_count" + + String sql = SELECT + "COUNT(1) as " + PLAYER_COUNT + FROM + UsersTable.TABLE_NAME + WHERE + UsersTable.REGISTERED + "<=?" + AND + UsersTable.REGISTERED + ">=?"; - return queryPlayerCount(sql, after, before); + return database -> database.queryOptional(sql, + set -> set.getInt(PLAYER_COUNT), + before, after) + .orElse(0); } public static Query> newPlayerCounts(long after, long before) { - String sql = SELECT + "s." + ServerTable.SERVER_UUID + ",COUNT(1) as player_count" + + String sql = SELECT + "s." + ServerTable.SERVER_UUID + ",COUNT(1) as " + PLAYER_COUNT + FROM + UserInfoTable.TABLE_NAME + INNER_JOIN + ServerTable.TABLE_NAME + " s on s." + ServerTable.ID + '=' + UserInfoTable.TABLE_NAME + '.' + UserInfoTable.SERVER_ID + WHERE + UserInfoTable.REGISTERED + "<=?" + AND + UserInfoTable.REGISTERED + ">=?" + GROUP_BY + UserInfoTable.SERVER_ID; - return new QueryStatement>(sql) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, before); - statement.setLong(2, after); - } - - @Override - public Map processResults(ResultSet set) throws SQLException { - Map byServer = new HashMap<>(); - while (set.next()) { - byServer.put(ServerUUID.fromString(set.getString(ServerTable.SERVER_UUID)), set.getInt("player_count")); - } - return byServer; - } - }; + return database -> database.queryMap(sql, + (set, byServer) -> byServer.put( + ServerUUID.fromString(set.getString(ServerTable.SERVER_UUID)), + set.getInt(PLAYER_COUNT) + ), + before, after); } /** @@ -383,31 +281,17 @@ public class PlayerCountQueries { String selectNewPlayersQuery = SELECT + sql.dateToEpochSecond(sql.dateToDayStamp(sql.epochSecondToDate('(' + UserInfoTable.REGISTERED + "+?)/1000"))) + "*1000 as date," + - "COUNT(1) as player_count" + + "COUNT(1) as " + PLAYER_COUNT + FROM + UserInfoTable.TABLE_NAME + WHERE + UserInfoTable.REGISTERED + "<=?" + AND + UserInfoTable.REGISTERED + ">=?" + AND + UserInfoTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + GROUP_BY + "date"; - return database.query(new QueryStatement>(selectNewPlayersQuery, 100) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, timeZoneOffset); - statement.setLong(2, before); - statement.setLong(3, after); - statement.setString(4, serverUUID.toString()); - } - - @Override - public NavigableMap processResults(ResultSet set) throws SQLException { - NavigableMap newPerDay = new TreeMap<>(); - while (set.next()) { - newPerDay.put(set.getLong("date"), set.getInt("player_count")); - } - return newPerDay; - } - }); + return database.queryMap(selectNewPlayersQuery, + (set, perDay) -> perDay.put(set.getLong("date"), set.getInt(PLAYER_COUNT)), + TreeMap::new, + timeZoneOffset, before, after, serverUUID); }; } @@ -426,31 +310,17 @@ public class PlayerCountQueries { String selectNewPlayersQuery = SELECT + sql.dateToEpochSecond(sql.dateToHourStamp(sql.epochSecondToDate('(' + UserInfoTable.REGISTERED + "+?)/1000"))) + "*1000 as date," + - "COUNT(1) as player_count" + + "COUNT(1) as " + PLAYER_COUNT + FROM + UserInfoTable.TABLE_NAME + WHERE + UserInfoTable.REGISTERED + "<=?" + AND + UserInfoTable.REGISTERED + ">=?" + AND + UserInfoTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + GROUP_BY + "date"; - return database.query(new QueryStatement>(selectNewPlayersQuery, 100) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, timeZoneOffset); - statement.setLong(2, before); - statement.setLong(3, after); - statement.setString(4, serverUUID.toString()); - } - - @Override - public NavigableMap processResults(ResultSet set) throws SQLException { - NavigableMap newPerDay = new TreeMap<>(); - while (set.next()) { - newPerDay.put(set.getLong("date"), set.getInt("player_count")); - } - return newPerDay; - } - }); + return database.queryMap(selectNewPlayersQuery, + (set, perDay) -> perDay.put(set.getLong("date"), set.getInt(PLAYER_COUNT)), + TreeMap::new, + timeZoneOffset, before, after, serverUUID); }; } @@ -468,29 +338,16 @@ public class PlayerCountQueries { String selectNewPlayersQuery = SELECT + sql.dateToEpochSecond(sql.dateToDayStamp(sql.epochSecondToDate('(' + UserInfoTable.REGISTERED + "+?)/1000"))) + "*1000 as date," + - "COUNT(1) as player_count" + + "COUNT(1) as " + PLAYER_COUNT + FROM + UsersTable.TABLE_NAME + WHERE + UsersTable.REGISTERED + "<=?" + AND + UsersTable.REGISTERED + ">=?" + GROUP_BY + "date"; - return database.query(new QueryStatement>(selectNewPlayersQuery, 100) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, timeZoneOffset); - statement.setLong(2, before); - statement.setLong(3, after); - } - - @Override - public NavigableMap processResults(ResultSet set) throws SQLException { - NavigableMap newPerDay = new TreeMap<>(); - while (set.next()) { - newPerDay.put(set.getLong("date"), set.getInt("player_count")); - } - return newPerDay; - } - }); + return database.queryMap(selectNewPlayersQuery, + (set, perDay) -> perDay.put(set.getLong("date"), set.getInt(PLAYER_COUNT)), + TreeMap::new, + timeZoneOffset, before, after); }; } @@ -508,29 +365,16 @@ public class PlayerCountQueries { String selectNewPlayersQuery = SELECT + sql.dateToEpochSecond(sql.dateToHourStamp(sql.epochSecondToDate('(' + UserInfoTable.REGISTERED + "+?)/1000"))) + "*1000 as date," + - "COUNT(1) as player_count" + + "COUNT(1) as " + PLAYER_COUNT + FROM + UsersTable.TABLE_NAME + WHERE + UsersTable.REGISTERED + "<=?" + AND + UsersTable.REGISTERED + ">=?" + GROUP_BY + "date"; - return database.query(new QueryStatement>(selectNewPlayersQuery, 100) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, timeZoneOffset); - statement.setLong(2, before); - statement.setLong(3, after); - } - - @Override - public NavigableMap processResults(ResultSet set) throws SQLException { - NavigableMap newPerDay = new TreeMap<>(); - while (set.next()) { - newPerDay.put(set.getLong("date"), set.getInt("player_count")); - } - return newPerDay; - } - }); + return database.queryMap(selectNewPlayersQuery, + (set, perDay) -> perDay.put(set.getLong("date"), set.getInt(PLAYER_COUNT)), + TreeMap::new, + timeZoneOffset, before, after); }; } @@ -540,28 +384,18 @@ public class PlayerCountQueries { String selectNewPlayersQuery = SELECT + sql.dateToEpochSecond(sql.dateToDayStamp(sql.epochSecondToDate('(' + UserInfoTable.REGISTERED + "+?)/1000"))) + "*1000 as date," + - "COUNT(1) as player_count" + + "COUNT(1) as " + PLAYER_COUNT + FROM + UserInfoTable.TABLE_NAME + WHERE + UserInfoTable.REGISTERED + "<=?" + AND + UserInfoTable.REGISTERED + ">=?" + AND + UserInfoTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + GROUP_BY + "date"; - String selectAverage = SELECT + "AVG(player_count) as average" + FROM + '(' + selectNewPlayersQuery + ") q1"; + String selectAverage = SELECT + "AVG(" + PLAYER_COUNT + ") as average" + FROM + '(' + selectNewPlayersQuery + ") q1"; - return database.query(new QueryStatement(selectAverage, 100) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setLong(1, timeZoneOffset); - statement.setLong(2, before); - statement.setLong(3, after); - statement.setString(4, serverUUID.toString()); - } - - @Override - public Integer processResults(ResultSet set) throws SQLException { - return set.next() ? (int) set.getDouble("average") : 0; - } - }); + return database.queryOptional(selectAverage, + set -> (int) set.getDouble("average"), + timeZoneOffset, before, after, serverUUID) + .orElse(0); }; } @@ -602,7 +436,7 @@ public class PlayerCountQueries { } public static Query operators(ServerUUID serverUUID) { - String sql = SELECT + "COUNT(1) as player_count" + FROM + UserInfoTable.TABLE_NAME + + String sql = SELECT + "COUNT(1) as " + PLAYER_COUNT + FROM + UserInfoTable.TABLE_NAME + WHERE + UserInfoTable.SERVER_ID + "=" + ServerTable.SELECT_SERVER_ID + AND + UserInfoTable.OP + "=?"; return new QueryStatement(sql) { @@ -614,7 +448,7 @@ public class PlayerCountQueries { @Override public Integer processResults(ResultSet set) throws SQLException { - return set.next() ? set.getInt("player_count") : 0; + return set.next() ? set.getInt(PLAYER_COUNT) : 0; } }; }