Refactored WorldTable

This commit is contained in:
Rsl1122 2017-10-06 17:33:57 +03:00
parent 94ba4fa0e9
commit 5433116cdb

View File

@ -3,10 +3,11 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify; import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException; 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.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.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser; import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -25,7 +26,6 @@ public class WorldTable extends Table {
public final String statementSelectID; public final String statementSelectID;
private final String columnWorldId = "id"; private final String columnWorldId = "id";
private final String columnWorldName = "world_name"; private final String columnWorldName = "world_name";
private final String columnServerID = "server_id";
/** /**
* Constructor. * Constructor.
@ -55,20 +55,19 @@ public class WorldTable extends Table {
* @throws SQLException Database error occurs. * @throws SQLException Database error occurs.
*/ */
public List<String> getWorlds() throws SQLException { public List<String> getWorlds() throws SQLException {
PreparedStatement statement = null; String sql = "SELECT * FROM " + tableName;
ResultSet set = null;
try (Connection connection = getConnection()) { return query(new QueryAllStatement<List<String>>(sql) {
statement = connection.prepareStatement("SELECT * FROM " + tableName); @Override
set = statement.executeQuery(); public List<String> processResults(ResultSet set) throws SQLException {
List<String> worldNames = new ArrayList<>(); List<String> worldNames = new ArrayList<>();
while (set.next()) { while (set.next()) {
String worldName = set.getString(columnWorldName); String worldName = set.getString(columnWorldName);
worldNames.add(worldName); worldNames.add(worldName);
}
return worldNames;
} }
return worldNames; });
} finally {
close(set, statement);
}
} }
/** /**
@ -89,21 +88,19 @@ public class WorldTable extends Table {
return; return;
} }
PreparedStatement statement = null; String sql = "INSERT INTO " + tableName + " ("
try (Connection connection = getConnection()) { + columnWorldName
statement = connection.prepareStatement("INSERT INTO " + tableName + " (" + ") VALUES (?)";
+ columnWorldName
+ ") VALUES (?)");
for (String world : worldsToSave) {
statement.setString(1, world);
statement.addBatch();
}
statement.executeBatch(); executeBatch(new ExecStatement(sql) {
commit(connection); @Override
} finally { public void prepare(PreparedStatement statement) throws SQLException {
close(statement); for (String world : worldsToSave) {
} statement.setString(1, world);
statement.addBatch();
}
}
});
} }
public String getColumnID() { public String getColumnID() {