Refactored VersionTable

This commit is contained in:
Rsl1122 2017-10-06 17:31:04 +03:00
parent 7af6cd238f
commit 94ba4fa0e9

View File

@ -1,12 +1,13 @@
package main.java.com.djrapitops.plan.database.tables; package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Log;
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.processing.QueryStatement;
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;
@ -29,20 +30,21 @@ public class VersionTable extends Table {
} }
public boolean isNewDatabase() throws SQLException { public boolean isNewDatabase() throws SQLException {
PreparedStatement statement = null; String sql = usingMySQL ?
ResultSet set = null; "SHOW TABLES LIKE ?" :
try (Connection connection = getConnection()) { "SELECT tbl_name FROM sqlite_master WHERE tbl_name=?";
statement = connection.prepareStatement(usingMySQL ?
"SHOW TABLES LIKE ?" :
"SELECT tbl_name FROM sqlite_master WHERE tbl_name=?");
statement.setString(1, tableName);
set = statement.executeQuery(); return query(new QueryStatement<Boolean>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, tableName);
}
return !set.next(); @Override
} finally { public Boolean processResults(ResultSet set) throws SQLException {
close(set, statement); return !set.next();
} }
});
} }
/** /**
@ -50,23 +52,18 @@ public class VersionTable extends Table {
*/ */
@Override @Override
public int getVersion() throws SQLException { public int getVersion() throws SQLException {
PreparedStatement statement = null; String sql = "SELECT * FROM " + tableName;
ResultSet set = null;
try (Connection connection = getConnection()) {
int version = 0;
statement = connection.prepareStatement("SELECT * FROM " + tableName);
set = statement.executeQuery();
if (set.next()) {
version = set.getInt("version");
}
return version;
} catch (Exception exc) {
Log.toLog("VersionsTable.getVersion", exc);
} finally {
close(set, statement);
}
return 1; return query(new QueryAllStatement<Integer>(sql) {
@Override
public Integer processResults(ResultSet set) throws SQLException {
int version = 0;
if (set.next()) {
version = set.getInt("version");
}
return version;
}
});
} }
/** /**
@ -77,14 +74,14 @@ public class VersionTable extends Table {
*/ */
public void setVersion(int version) throws SQLException { public void setVersion(int version) throws SQLException {
removeAllData(); removeAllData();
PreparedStatement statement = null;
try (Connection connection = getConnection()) {
statement = connection.prepareStatement("INSERT INTO " + tableName + " (version) VALUES (" + version + ")");
statement.executeUpdate(); String sql = "INSERT INTO " + tableName + " (version) VALUES (?)";
commit(connection);
} finally { execute(new ExecStatement(sql) {
close(statement); @Override
} public void prepare(PreparedStatement statement) throws SQLException {
statement.setInt(1, version);
}
});
} }
} }