Executable and ExecBatchStatement:

- New interface Executable allows moving execution logic to
  ExecStatement, as well as forces execution inside Transactions.
- ExecBatchStatement splits batch functionality away from ExecStatement
  since the semantics of batch execution are similar to that of single
  executions
- Deprecated: Table#execute, Table#executeBatch, Table#query
- Removed Transaction#executeBatch
This commit is contained in:
Rsl1122 2019-01-24 18:55:32 +02:00
parent 6f0ccae59b
commit 98607edfb6
10 changed files with 100 additions and 30 deletions

View File

@ -0,0 +1,37 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.db.access;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* SQL executing batch statement that closes appropriate elements.
*
* @author Rsl1122
*/
public abstract class ExecBatchStatement extends ExecStatement {
public ExecBatchStatement(String sql) {
super(sql);
}
@Override
protected boolean callExecute(PreparedStatement statement) throws SQLException {
return statement.executeBatch().length > 0;
}
}

View File

@ -16,6 +16,9 @@
*/
package com.djrapitops.plan.db.access;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@ -24,7 +27,7 @@ import java.sql.SQLException;
*
* @author Rsl1122
*/
public abstract class ExecStatement {
public abstract class ExecStatement implements Executable {
private final String sql;
@ -32,6 +35,17 @@ public abstract class ExecStatement {
this.sql = sql;
}
@Override
public boolean execute(Connection connection) {
try {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
return execute(preparedStatement);
}
} catch (SQLException e) {
throw DBOpException.forCause(sql, e);
}
}
public boolean execute(PreparedStatement statement) throws SQLException {
try {
prepare(statement);
@ -41,7 +55,7 @@ public abstract class ExecStatement {
}
}
private boolean callExecute(PreparedStatement statement) throws SQLException {
protected boolean callExecute(PreparedStatement statement) throws SQLException {
if (sql.startsWith("UPDATE") || sql.startsWith("INSERT") || sql.startsWith("DELETE") || sql.startsWith("REPLACE")) {
return statement.executeUpdate() > 0;
} else {
@ -50,6 +64,7 @@ public abstract class ExecStatement {
}
}
@Deprecated
public void executeBatch(PreparedStatement statement) throws SQLException {
try {
prepare(statement);

View File

@ -0,0 +1,30 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.db.access;
import java.sql.Connection;
/**
* Interface for everything that updates rows in the database.
*
* @author Rsl1122
*/
public interface Executable {
boolean execute(Connection connection);
}

View File

@ -20,6 +20,7 @@ import com.djrapitops.plan.api.exceptions.database.DBOpException;
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.Executable;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plugin.utilities.Verify;
@ -120,14 +121,8 @@ public abstract class Transaction {
return db.query(query);
}
protected boolean execute(ExecStatement statement) {
try {
try (PreparedStatement preparedStatement = connection.prepareStatement(statement.getSql())) {
return statement.execute(preparedStatement);
}
} catch (SQLException e) {
throw DBOpException.forCause(statement.getSql(), e);
}
protected boolean execute(Executable executable) {
return executable.execute(connection);
}
protected boolean execute(String sql) {
@ -150,16 +145,6 @@ public abstract class Transaction {
}
}
protected void executeBatch(ExecStatement statement) {
try {
try (PreparedStatement preparedStatement = connection.prepareStatement(statement.getSql())) {
statement.executeBatch(preparedStatement);
}
} catch (SQLException e) {
throw DBOpException.forCause(statement.getSql(), e);
}
}
protected DBType getDBType() {
return db.getType();
}

View File

@ -18,7 +18,7 @@ package com.djrapitops.plan.db.patches;
import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
import com.djrapitops.plan.db.sql.tables.GeoInfoTable;
@ -79,7 +79,7 @@ public class IPAnonPatch extends Patch {
GeoInfoTable.IP_HASH + "=? " +
"WHERE " + GeoInfoTable.IP + "=?";
executeBatch(new ExecStatement(sql) {
execute(new ExecBatchStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (List<GeoInfo> geoInfos : allGeoInfo.values()) {

View File

@ -17,7 +17,7 @@
package com.djrapitops.plan.db.patches;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.tables.KillsTable;
@ -69,7 +69,7 @@ public class KillsServerIDPatch extends Patch {
String sql = "UPDATE " + KillsTable.TABLE_NAME + " SET server_id=? WHERE " + KillsTable.SESSION_ID + "=?";
executeBatch(new ExecStatement(sql) {
execute(new ExecBatchStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Integer> entry : sessionIDServerIDRelation.entrySet()) {

View File

@ -18,7 +18,7 @@ package com.djrapitops.plan.db.patches;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.sql.parsing.Select;
import com.djrapitops.plan.db.sql.tables.NicknamesTable;
@ -117,7 +117,7 @@ public class NicknameLastSeenPatch extends Patch {
" AND user_id=?" +
" AND server_id=?";
executeBatch(new ExecStatement(updateSQL) {
execute(new ExecBatchStatement(updateSQL) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Set<Nickname>> entry : nicknames.entrySet()) {

View File

@ -17,7 +17,7 @@
package com.djrapitops.plan.db.patches;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.tables.WorldTimesTable;
@ -66,7 +66,7 @@ public class WorldTimesSeverIDPatch extends Patch {
"server_id=?" +
" WHERE " + WorldTimesTable.SESSION_ID + "=?";
executeBatch(new ExecStatement(sql) {
execute(new ExecBatchStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<Integer, Integer> entry : sessionIDServerIDRelation.entrySet()) {

View File

@ -17,7 +17,7 @@
package com.djrapitops.plan.db.patches;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.QueryAllStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.sql.tables.SessionsTable;
@ -136,7 +136,7 @@ public class WorldsServerIDPatch extends Patch {
WorldTimesTable.WORLD_ID + "=?" +
" WHERE " + WorldTimesTable.WORLD_ID + "=?" +
" AND " + "server_id=?";
executeBatch(new ExecStatement(sql) {
execute(new ExecBatchStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
for (Map.Entry<WorldObj, List<WorldObj>> entry : oldToNewMap.entrySet()) {

View File

@ -129,14 +129,17 @@ public abstract class Table {
return Objects.hashCode(tableName, db, supportsMySQLQueries);
}
@Deprecated
protected boolean execute(ExecStatement statement) {
return db.execute(statement);
}
@Deprecated
protected void executeBatch(ExecStatement statement) {
db.executeBatch(statement);
}
@Deprecated
protected <T> T query(QueryStatement<T> statement) {
return db.query(statement);
}