Removed AbstractSQLStatement, added Query

Query is a more abstract version of QueryStatement that will allow
DAO like queries to be made.
This commit is contained in:
Rsl1122 2019-01-18 18:16:08 +02:00
parent 4b0526c6d3
commit 3c66ca3331
6 changed files with 48 additions and 90 deletions

View File

@ -18,7 +18,7 @@ package com.djrapitops.plan.db;
import com.djrapitops.plan.api.exceptions.database.DBException;
import com.djrapitops.plan.api.exceptions.database.DBInitException;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.system.database.databases.operation.*;
@ -44,7 +44,7 @@ public interface Database {
* @param <T> Type of the object to be returned.
* @return Result of the query.
*/
<T> T query(QueryStatement<T> query);
<T> T query(Query<T> query);
/**
* Execute an SQL Transaction.

View File

@ -325,10 +325,6 @@ public abstract class SQLDB extends AbstractDatabase {
Connection connection = null;
try {
connection = getConnection();
// Inject Timings to the statement for benchmarking
if (config.isTrue(PluginSettings.DEV_MODE)) {
statement.setTimings(timings);
}
try (PreparedStatement preparedStatement = connection.prepareStatement(statement.getSql())) {
return statement.execute(preparedStatement);
}
@ -369,10 +365,6 @@ public abstract class SQLDB extends AbstractDatabase {
Connection connection = null;
try {
connection = getConnection();
// Inject Timings to the statement for benchmarking
if (config.isTrue(PluginSettings.DEV_MODE)) {
statement.setTimings(timings);
}
try (PreparedStatement preparedStatement = connection.prepareStatement(statement.getSql())) {
statement.executeBatch(preparedStatement);
}
@ -391,10 +383,6 @@ public abstract class SQLDB extends AbstractDatabase {
Connection connection = null;
try {
connection = getConnection();
// Inject Timings to the statement for benchmarking
if (config.isTrue(PluginSettings.DEV_MODE)) {
statement.setTimings(timings);
}
try (PreparedStatement preparedStatement = connection.prepareStatement(statement.getSql())) {
return statement.executeQuery(preparedStatement);
}

View File

@ -1,66 +0,0 @@
/*
* 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 com.djrapitops.plan.system.DebugChannels;
import com.djrapitops.plugin.benchmarking.Timings;
/**
* Abstract class that performs an SQL statement.
* <p>
* For Benchmarking purposes.
*
* @author Rsl1122
*/
public abstract class AbstractSQLStatement {
protected final String sql;
private Timings timings;
protected AbstractSQLStatement(String sql) {
this.sql = sql;
}
protected void startBenchmark() {
if (timings != null) {
timings.start(DebugChannels.SQL + ": " + sql);
}
}
protected void startBatchBenchmark() {
if (timings != null) {
timings.start(DebugChannels.SQL + ": " + sql + " (Batch)");
}
}
protected void stopBenchmark() {
if (timings != null) {
timings.end(DebugChannels.SQL, DebugChannels.SQL + ": " + sql);
}
}
protected void stopBatchBenchmark() {
if (timings != null) {
timings.end(DebugChannels.SQL, DebugChannels.SQL + ": " + sql + " (Batch)");
}
}
public void setTimings(Timings timings) {
this.timings = timings;
}
}

View File

@ -24,20 +24,20 @@ import java.sql.SQLException;
*
* @author Rsl1122
*/
public abstract class ExecStatement extends AbstractSQLStatement {
public abstract class ExecStatement {
private final String sql;
public ExecStatement(String sql) {
super(sql);
this.sql = sql;
}
public boolean execute(PreparedStatement statement) throws SQLException {
startBenchmark();
try {
prepare(statement);
return callExecute(statement);
} finally {
statement.close();
stopBenchmark();
}
}
@ -51,13 +51,11 @@ public abstract class ExecStatement extends AbstractSQLStatement {
}
public void executeBatch(PreparedStatement statement) throws SQLException {
startBatchBenchmark();
try {
prepare(statement);
statement.executeBatch();
} finally {
statement.close();
stopBatchBenchmark();
}
}

View File

@ -0,0 +1,15 @@
package com.djrapitops.plan.db.access;
import com.djrapitops.plan.db.SQLDB;
/**
* Interface for everything that returns results from the database.
*
* @param <T> Type of the result.
* @author Rsl1122
*/
public interface Query<T> {
T query(SQLDB db);
}

View File

@ -16,6 +16,10 @@
*/
package com.djrapitops.plan.db.access;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.SQLDB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -25,8 +29,9 @@ import java.sql.SQLException;
*
* @author Rsl1122
*/
public abstract class QueryStatement<T> extends AbstractSQLStatement {
public abstract class QueryStatement<T> implements Query<T> {
private final String sql;
private final int fetchSize;
public QueryStatement(String sql) {
@ -34,12 +39,26 @@ public abstract class QueryStatement<T> extends AbstractSQLStatement {
}
public QueryStatement(String sql, int fetchSize) {
super(sql);
this.sql = sql;
this.fetchSize = fetchSize;
}
@Override
public T query(SQLDB db) {
Connection connection = null;
try {
connection = db.getConnection();
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
return executeQuery(preparedStatement);
}
} catch (SQLException e) {
throw DBOpException.forCause(sql, e);
} finally {
db.returnToPool(connection);
}
}
public T executeQuery(PreparedStatement statement) throws SQLException {
startBenchmark();
try {
statement.setFetchSize(fetchSize);
prepare(statement);
@ -48,7 +67,6 @@ public abstract class QueryStatement<T> extends AbstractSQLStatement {
}
} finally {
statement.close();
stopBenchmark();
}
}
@ -59,4 +77,9 @@ public abstract class QueryStatement<T> extends AbstractSQLStatement {
public String getSql() {
return sql;
}
@Override
public String toString() {
return "Query (" + sql + ')';
}
}