mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-06 15:44:49 +08:00
Refactored VersionTable
This commit is contained in:
parent
7af6cd238f
commit
94ba4fa0e9
@ -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;
|
|
||||||
try (Connection connection = getConnection()) {
|
|
||||||
statement = connection.prepareStatement(usingMySQL ?
|
|
||||||
"SHOW TABLES LIKE ?" :
|
"SHOW TABLES LIKE ?" :
|
||||||
"SELECT tbl_name FROM sqlite_master WHERE tbl_name=?");
|
"SELECT tbl_name FROM sqlite_master WHERE tbl_name=?";
|
||||||
|
|
||||||
|
return query(new QueryStatement<Boolean>(sql) {
|
||||||
|
@Override
|
||||||
|
public void prepare(PreparedStatement statement) throws SQLException {
|
||||||
statement.setString(1, tableName);
|
statement.setString(1, tableName);
|
||||||
|
|
||||||
set = statement.executeQuery();
|
|
||||||
|
|
||||||
return !set.next();
|
|
||||||
} finally {
|
|
||||||
close(set, statement);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean processResults(ResultSet set) throws SQLException {
|
||||||
|
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()) {
|
return query(new QueryAllStatement<Integer>(sql) {
|
||||||
|
@Override
|
||||||
|
public Integer processResults(ResultSet set) throws SQLException {
|
||||||
int version = 0;
|
int version = 0;
|
||||||
statement = connection.prepareStatement("SELECT * FROM " + tableName);
|
|
||||||
set = statement.executeQuery();
|
|
||||||
if (set.next()) {
|
if (set.next()) {
|
||||||
version = set.getInt("version");
|
version = set.getInt("version");
|
||||||
}
|
}
|
||||||
return version;
|
return version;
|
||||||
} catch (Exception exc) {
|
|
||||||
Log.toLog("VersionsTable.getVersion", exc);
|
|
||||||
} finally {
|
|
||||||
close(set, statement);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -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);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user