Added getCount()

This commit is contained in:
Robin Kupper
2011-11-16 18:44:31 +01:00
parent 95c48db4c5
commit 95cb3738fa
2 changed files with 59 additions and 31 deletions

View File

@@ -279,12 +279,11 @@ public class LogBlock extends JavaPlugin
public List<BlockChange> getBlockChanges(QueryParams params) throws SQLException { public List<BlockChange> getBlockChanges(QueryParams params) throws SQLException {
final Connection conn = getConnection(); final Connection conn = getConnection();
Statement state = null; Statement state = null;
ResultSet rs = null;
if (conn == null) if (conn == null)
throw new SQLException("No connection"); throw new SQLException("No connection");
try { try {
state = conn.createStatement(); state = conn.createStatement();
rs = state.executeQuery(params.getQuery()); final ResultSet rs = state.executeQuery(params.getQuery());
final List<BlockChange> blockchanges = new ArrayList<BlockChange>(); final List<BlockChange> blockchanges = new ArrayList<BlockChange>();
while (rs.next()) while (rs.next())
blockchanges.add(new BlockChange(rs, params)); blockchanges.add(new BlockChange(rs, params));
@@ -295,4 +294,22 @@ public class LogBlock extends JavaPlugin
conn.close(); 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();
}
}
} }

View File

@@ -37,7 +37,7 @@ public class QueryParams implements Cloneable
public List<Integer> types = new ArrayList<Integer>(); public List<Integer> types = new ArrayList<Integer>();
public World world = null; public World world = null;
public String match = 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; private final LogBlock logblock;
public QueryParams(LogBlock logblock) { public QueryParams(LogBlock logblock) {
@@ -60,45 +60,56 @@ public class QueryParams implements Cloneable
public String getQuery() { public String getQuery() {
if (bct == BlockChangeType.CHAT) { if (bct == BlockChangeType.CHAT) {
String select = "SELECT "; 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` "; String from = "FROM `lb-chat` ";
if (needId)
select += "id, ";
if (needDate)
select += "date, ";
if (needPlayer)
select += "playername, ";
if (needPlayer || players.size() > 0) if (needPlayer || players.size() > 0)
from += "INNER JOIN `lb-players` USING (playerid) "; from += "INNER JOIN `lb-players` USING (playerid) ";
if (needMessage) return select + " " + from + getWhere() + "ORDER BY date " + order + ", id " + order + " " + getLimit();
select += "message, ";
return select.substring(0, select.length() - 2) + " " + from + getWhere() + "ORDER BY date " + order + ", id " + order + " " + getLimit();
} }
if (sum == SummarizationMode.NONE) { if (sum == SummarizationMode.NONE) {
String select = "SELECT "; 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() + "` "; 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) if (needPlayer || players.size() > 0)
from += "INNER JOIN `lb-players` USING (playerid) "; from += "INNER JOIN `lb-players` USING (playerid) ";
if (needCoords) if (needSignText)
select += "x, y, z, ";
if (needSignText) {
select += "signtext, ";
from += "LEFT JOIN `" + getTable() + "-sign` USING (id) "; from += "LEFT JOIN `" + getTable() + "-sign` USING (id) ";
} if (needChestAccess)
if (needChestAccess) {
select += "itemtype, itemamount, itemdata, ";
from += "LEFT JOIN `" + getTable() + "-chest` USING (id) "; from += "LEFT JOIN `" + getTable() + "-chest` USING (id) ";
} return select + " " + from + getWhere() + "ORDER BY date " + order + ", id " + order + " " + getLimit();
return select.substring(0, select.length() - 2) + " " + from + getWhere() + "ORDER BY date " + order + ", id " + order + " " + getLimit();
} else if (sum == SummarizationMode.TYPES) } 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(); 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 else