From 95cb3738fa89fb4d69e75f95540064327317319e Mon Sep 17 00:00:00 2001 From: Robin Kupper Date: Wed, 16 Nov 2011 18:44:31 +0100 Subject: [PATCH] Added getCount() --- src/de/diddiz/LogBlock/LogBlock.java | 21 +++++++- src/de/diddiz/LogBlock/QueryParams.java | 69 ++++++++++++++----------- 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/src/de/diddiz/LogBlock/LogBlock.java b/src/de/diddiz/LogBlock/LogBlock.java index 2b458ec..7ae3587 100644 --- a/src/de/diddiz/LogBlock/LogBlock.java +++ b/src/de/diddiz/LogBlock/LogBlock.java @@ -279,12 +279,11 @@ public class LogBlock extends JavaPlugin public List getBlockChanges(QueryParams params) throws SQLException { final Connection conn = getConnection(); Statement state = null; - ResultSet rs = null; if (conn == null) throw new SQLException("No connection"); try { state = conn.createStatement(); - rs = state.executeQuery(params.getQuery()); + final ResultSet rs = state.executeQuery(params.getQuery()); final List blockchanges = new ArrayList(); while (rs.next()) blockchanges.add(new BlockChange(rs, params)); @@ -295,4 +294,22 @@ public class LogBlock extends JavaPlugin conn.close(); } } + + public int getCount(QueryParams params) throws SQLException { + final Connection conn = getConnection(); + Statement state = null; + if (conn == null) + throw new SQLException("No connection"); + try { + state = conn.createStatement(); + params.needCount = true; + final ResultSet rs = state.executeQuery(params.getQuery()); + rs.next(); + return rs.getInt(1); + } finally { + if (state != null) + state.close(); + conn.close(); + } + } } diff --git a/src/de/diddiz/LogBlock/QueryParams.java b/src/de/diddiz/LogBlock/QueryParams.java index e9ef117..bee00d7 100644 --- a/src/de/diddiz/LogBlock/QueryParams.java +++ b/src/de/diddiz/LogBlock/QueryParams.java @@ -37,7 +37,7 @@ public class QueryParams implements Cloneable public List types = new ArrayList(); public World world = null; public String match = null; - public boolean needId = false, needDate = false, needType = false, needData = false, needPlayer = false, needCoords = false, needSignText = false, needChestAccess = false, needMessage = false; + public boolean needCount = false, needId = false, needDate = false, needType = false, needData = false, needPlayer = false, needCoords = false, needSignText = false, needChestAccess = false, needMessage = false; private final LogBlock logblock; public QueryParams(LogBlock logblock) { @@ -60,45 +60,56 @@ public class QueryParams implements Cloneable public String getQuery() { if (bct == BlockChangeType.CHAT) { String select = "SELECT "; + if (needCount) + select += "COUNT(*) AS count"; + else { + if (needId) + select += "id, "; + if (needDate) + select += "date, "; + if (needPlayer) + select += "playername, "; + if (needMessage) + select += "message, "; + select = select.substring(0, select.length() - 2); + } String from = "FROM `lb-chat` "; - if (needId) - select += "id, "; - if (needDate) - select += "date, "; - if (needPlayer) - select += "playername, "; + if (needPlayer || players.size() > 0) from += "INNER JOIN `lb-players` USING (playerid) "; - if (needMessage) - select += "message, "; - return select.substring(0, select.length() - 2) + " " + from + getWhere() + "ORDER BY date " + order + ", id " + order + " " + getLimit(); + return select + " " + from + getWhere() + "ORDER BY date " + order + ", id " + order + " " + getLimit(); } if (sum == SummarizationMode.NONE) { String select = "SELECT "; + if (needCount) + select += "COUNT(*) AS count"; + else { + if (needId) + select += "`" + getTable() + "`.id, "; + if (needDate) + select += "date, "; + if (needType) + select += "replaced, type, "; + if (needData) + select += "data, "; + if (needPlayer) + select += "playername, "; + if (needCoords) + select += "x, y, z, "; + if (needSignText) + select += "signtext, "; + if (needChestAccess) + select += "itemtype, itemamount, itemdata, "; + select = select.substring(0, select.length() - 2); + } String from = "FROM `" + getTable() + "` "; - if (needId) - select += "`" + getTable() + "`.id, "; - if (needDate) - select += "date, "; - if (needType) - select += "replaced, type, "; - if (needData) - select += "data, "; - if (needPlayer) - select += "playername, "; if (needPlayer || players.size() > 0) from += "INNER JOIN `lb-players` USING (playerid) "; - if (needCoords) - select += "x, y, z, "; - if (needSignText) { - select += "signtext, "; + if (needSignText) from += "LEFT JOIN `" + getTable() + "-sign` USING (id) "; - } - if (needChestAccess) { - select += "itemtype, itemamount, itemdata, "; + if (needChestAccess) from += "LEFT JOIN `" + getTable() + "-chest` USING (id) "; - } - return select.substring(0, select.length() - 2) + " " + from + getWhere() + "ORDER BY date " + order + ", id " + order + " " + getLimit(); + return select + " " + from + getWhere() + "ORDER BY date " + order + ", id " + order + " " + getLimit(); } else if (sum == SummarizationMode.TYPES) return "SELECT type, SUM(created) AS created, SUM(destroyed) AS destroyed FROM ((SELECT type, count(type) AS created, 0 AS destroyed FROM `" + getTable() + "` INNER JOIN `lb-players` USING (playerid) " + getWhere(BlockChangeType.CREATED) + "GROUP BY type) UNION (SELECT replaced AS type, 0 AS created, count(replaced) AS destroyed FROM `" + getTable() + "` INNER JOIN `lb-players` USING (playerid) " + getWhere(BlockChangeType.DESTROYED) + "GROUP BY replaced)) AS t GROUP BY type ORDER BY SUM(created) + SUM(destroyed) " + order + " " + getLimit(); else