From 5433116cdb70f71cad33071d3b5f9640c8d507c4 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Fri, 6 Oct 2017 17:33:57 +0300 Subject: [PATCH] Refactored WorldTable --- .../plan/database/tables/WorldTable.java | 55 +++++++++---------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTable.java b/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTable.java index 117ac07ed..c65f8e68d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/database/tables/WorldTable.java @@ -3,10 +3,11 @@ package main.java.com.djrapitops.plan.database.tables; import com.djrapitops.plugin.utilities.Verify; 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.QueryAllStatement; 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; @@ -25,7 +26,6 @@ public class WorldTable extends Table { public final String statementSelectID; private final String columnWorldId = "id"; private final String columnWorldName = "world_name"; - private final String columnServerID = "server_id"; /** * Constructor. @@ -55,20 +55,19 @@ public class WorldTable extends Table { * @throws SQLException Database error occurs. */ public List getWorlds() throws SQLException { - PreparedStatement statement = null; - ResultSet set = null; - try (Connection connection = getConnection()) { - statement = connection.prepareStatement("SELECT * FROM " + tableName); - set = statement.executeQuery(); - List worldNames = new ArrayList<>(); - while (set.next()) { - String worldName = set.getString(columnWorldName); - worldNames.add(worldName); + String sql = "SELECT * FROM " + tableName; + + return query(new QueryAllStatement>(sql) { + @Override + public List processResults(ResultSet set) throws SQLException { + List worldNames = new ArrayList<>(); + while (set.next()) { + String worldName = set.getString(columnWorldName); + worldNames.add(worldName); + } + return worldNames; } - return worldNames; - } finally { - close(set, statement); - } + }); } /** @@ -89,21 +88,19 @@ public class WorldTable extends Table { return; } - PreparedStatement statement = null; - try (Connection connection = getConnection()) { - statement = connection.prepareStatement("INSERT INTO " + tableName + " (" - + columnWorldName - + ") VALUES (?)"); - for (String world : worldsToSave) { - statement.setString(1, world); - statement.addBatch(); - } + String sql = "INSERT INTO " + tableName + " (" + + columnWorldName + + ") VALUES (?)"; - statement.executeBatch(); - commit(connection); - } finally { - close(statement); - } + executeBatch(new ExecStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + for (String world : worldsToSave) { + statement.setString(1, world); + statement.addBatch(); + } + } + }); } public String getColumnID() {