Refactored ActionsTable

This commit is contained in:
Rsl1122 2017-10-04 12:11:03 +03:00
parent 62396a9a5c
commit b834f54815
4 changed files with 212 additions and 107 deletions

View File

@ -0,0 +1,46 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.database.processing;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public abstract class ExecStatement {
private final String sql;
public ExecStatement(String sql) {
this.sql = sql;
}
public void execute(PreparedStatement statement) throws SQLException {
try {
prepare(statement);
statement.execute();
} finally {
statement.close();
}
}
public void executeBatch(PreparedStatement statement) throws SQLException {
try {
prepare(statement);
statement.executeBatch();
} finally {
statement.close();
}
}
public abstract void prepare(PreparedStatement statement) throws SQLException;
public String getSql() {
return sql;
}
}

View File

@ -0,0 +1,49 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.database.processing;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public abstract class QueryStatement<T> {
private final String sql;
private final int fetchSize;
public QueryStatement(String sql) {
this(sql, 10);
}
public QueryStatement(String sql, int fetchSize) {
this.sql = sql;
this.fetchSize = fetchSize;
}
public T executeQuery(PreparedStatement statement) throws SQLException {
try {
statement.setFetchSize(fetchSize);
prepare(statement);
try (ResultSet set = statement.executeQuery()) {
return processQuery(set);
}
} finally {
statement.close();
}
}
public abstract void prepare(PreparedStatement statement) throws SQLException;
public abstract T processQuery(ResultSet set) throws SQLException;
public String getSql() {
return sql;
}
}

View File

@ -9,11 +9,12 @@ import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.data.Action;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.processing.ExecStatement;
import main.java.com.djrapitops.plan.database.processing.QueryStatement;
import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -73,22 +74,16 @@ public class ActionsTable extends UserIDTable {
}
public void insertAction(UUID uuid, Action action) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = getConnection();
statement = connection.prepareStatement(insertStatement);
statement.setString(1, uuid.toString());
statement.setString(2, Plan.getServerUUID().toString());
statement.setInt(3, action.getDoneAction().getId());
statement.setLong(4, action.getDate());
statement.setString(5, action.getAdditionalInfo());
statement.execute();
commit(connection);
} finally {
close(statement, connection);
}
execute(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
statement.setString(2, Plan.getServerUUID().toString());
statement.setInt(3, action.getDoneAction().getId());
statement.setLong(4, action.getDate());
statement.setString(5, action.getAdditionalInfo());
}
});
}
/**
@ -99,108 +94,101 @@ public class ActionsTable extends UserIDTable {
* @throws SQLException DB Error
*/
public List<Action> getActions(UUID uuid) throws SQLException {
List<Action> actions = new ArrayList<>();
Connection connection = null;
PreparedStatement statement = null;
ResultSet set = null;
try {
connection = getConnection();
statement = connection.prepareStatement(Select.from(tableName, "*")
.where(columnUserID + "=" + usersTable.statementSelectID)
.toString());
statement.setFetchSize(5000);
statement.setString(1, uuid.toString());
set = statement.executeQuery();
while (set.next()) {
int serverID = set.getInt(columnServerID);
long date = set.getLong(columnDate);
Actions doneAction = Actions.getById(set.getInt(columnActionID));
String additionalInfo = set.getString(columnAdditionalInfo);
actions.add(new Action(date, doneAction, additionalInfo, serverID));
String sql = Select.all(tableName)
.where(columnUserID + "=" + usersTable.statementSelectID)
.toString();
return query(new QueryStatement<List<Action>>(sql, 5000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
return actions;
} finally {
close(set, statement, connection);
}
@Override
public List<Action> processQuery(ResultSet set) throws SQLException {
List<Action> actions = new ArrayList<>();
while (set.next()) {
int serverID = set.getInt(columnServerID);
long date = set.getLong(columnDate);
Actions doneAction = Actions.getById(set.getInt(columnActionID));
String additionalInfo = set.getString(columnAdditionalInfo);
actions.add(new Action(date, doneAction, additionalInfo, serverID));
}
return actions;
}
});
}
public Map<UUID, Map<UUID, List<Action>>> getAllActions() throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet set = null;
try {
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
connection = getConnection();
statement = connection.prepareStatement("SELECT " +
columnActionID + ", " +
columnDate + ", " +
columnAdditionalInfo + ", " +
usersUUIDColumn + ", " +
serverUUIDColumn +
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
" JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID
);
statement.setFetchSize(5000);
set = statement.executeQuery();
Map<UUID, Map<UUID, List<Action>>> map = new HashMap<>();
while (set.next()) {
UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
UUID uuid = UUID.fromString(set.getString("uuid"));
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
String sql = "SELECT " +
columnActionID + ", " +
columnDate + ", " +
columnAdditionalInfo + ", " +
usersUUIDColumn + ", " +
serverUUIDColumn +
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
" JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID;
Map<UUID, List<Action>> serverMap = map.getOrDefault(serverUUID, new HashMap<>());
List<Action> actions = serverMap.getOrDefault(uuid, new ArrayList<>());
return query(new QueryStatement<Map<UUID, Map<UUID, List<Action>>>>(sql, 20000) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
long date = set.getLong(columnDate);
Actions doneAction = Actions.getById(set.getInt(columnActionID));
String additionalInfo = set.getString(columnAdditionalInfo);
actions.add(new Action(date, doneAction, additionalInfo, -1));
serverMap.put(uuid, actions);
map.put(serverUUID, serverMap);
}
return map;
} finally {
close(set, statement, connection);
}
@Override
public Map<UUID, Map<UUID, List<Action>>> processQuery(ResultSet set) throws SQLException {
Map<UUID, Map<UUID, List<Action>>> map = new HashMap<>();
while (set.next()) {
UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
UUID uuid = UUID.fromString(set.getString("uuid"));
Map<UUID, List<Action>> serverMap = map.getOrDefault(serverUUID, new HashMap<>());
List<Action> actions = serverMap.getOrDefault(uuid, new ArrayList<>());
long date = set.getLong(columnDate);
Actions doneAction = Actions.getById(set.getInt(columnActionID));
String additionalInfo = set.getString(columnAdditionalInfo);
actions.add(new Action(date, doneAction, additionalInfo, -1));
serverMap.put(uuid, actions);
map.put(serverUUID, serverMap);
}
return map;
}
});
}
public void insertActions(Map<UUID, Map<UUID, List<Action>>> allActions) throws SQLException {
if (Verify.isEmpty(allActions)) {
return;
}
Connection connection = null;
PreparedStatement statement = null;
try {
connection = getConnection();
statement = connection.prepareStatement(insertStatement);
// Every Server
for (UUID serverUUID : allActions.keySet()) {
// Every User
for (Map.Entry<UUID, List<Action>> entry : allActions.get(serverUUID).entrySet()) {
UUID uuid = entry.getKey();
// Every Action
List<Action> actions = entry.getValue();
for (Action action : actions) {
statement.setString(1, uuid.toString());
statement.setString(2, serverUUID.toString());
statement.setInt(3, action.getDoneAction().getId());
statement.setLong(4, action.getDate());
statement.setString(5, action.getAdditionalInfo());
statement.addBatch();
executeBatch(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (UUID serverUUID : allActions.keySet()) {
// Every User
for (Map.Entry<UUID, List<Action>> entry : allActions.get(serverUUID).entrySet()) {
UUID uuid = entry.getKey();
// Every Action
List<Action> actions = entry.getValue();
for (Action action : actions) {
statement.setString(1, uuid.toString());
statement.setString(2, serverUUID.toString());
statement.setInt(3, action.getDoneAction().getId());
statement.setLong(4, action.getDate());
statement.setString(5, action.getAdditionalInfo());
statement.addBatch();
}
}
}
}
statement.executeBatch();
commit(connection);
} finally {
close(statement, connection);
}
});
}
}

View File

@ -4,6 +4,8 @@ import com.djrapitops.plugin.utilities.Verify;
import com.google.common.base.Objects;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.processing.ExecStatement;
import main.java.com.djrapitops.plan.database.processing.QueryStatement;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import java.sql.Connection;
@ -76,8 +78,8 @@ public abstract class Table {
/**
* Executes an SQL Statement
*
* @param statementString Statement to execute
* @return What execute returns.
* @param statementString Statement to setUp
* @return What setUp returns.
* @throws SQLException DB error
*/
protected boolean execute(String statementString) throws SQLException {
@ -93,9 +95,9 @@ public abstract class Table {
}
/**
* Used to execute queries while possible SQLExceptions are suppressed.
* Used to setUp queries while possible SQLExceptions are suppressed.
*
* @param statements SQL statements to execute
* @param statements SQL statements to setUp
*/
protected void executeUnsafe(String... statements) {
Verify.nullCheck(statements);
@ -176,4 +178,24 @@ public abstract class Table {
public SQLDB getDb() {
return db;
}
protected void execute(ExecStatement statement) throws SQLException {
try (Connection connection = getConnection()) {
statement.execute(connection.prepareStatement(statement.getSql()));
commit(connection);
}
}
protected void executeBatch(ExecStatement statement) throws SQLException {
try (Connection connection = getConnection()) {
statement.executeBatch(connection.prepareStatement(statement.getSql()));
commit(connection);
}
}
protected <T> T query(QueryStatement<T> statement) throws SQLException {
try (Connection connection = getConnection()) {
return statement.executeQuery(connection.prepareStatement(statement.getSql()));
}
}
}