Fix Sqlite backend didn't remove old accounts.

This commit is contained in:
DNx5 2016-04-01 08:45:31 +07:00
parent 79eb0248f8
commit cb995a99a6

View File

@ -31,6 +31,7 @@ public class SQLite implements DataSource {
* Constructor for SQLite. * Constructor for SQLite.
* *
* @param settings The settings instance * @param settings The settings instance
*
* @throws ClassNotFoundException if no driver could be found for the datasource * @throws ClassNotFoundException if no driver could be found for the datasource
* @throws SQLException when initialization of a SQL datasource failed * @throws SQLException when initialization of a SQL datasource failed
*/ */
@ -56,6 +57,10 @@ public class SQLite implements DataSource {
this.con = connection; this.con = connection;
} }
private static void logSqlException(SQLException e) {
ConsoleLogger.logException("Error while executing SQL statement:", e);
}
private synchronized void connect() throws ClassNotFoundException, SQLException { private synchronized void connect() throws ClassNotFoundException, SQLException {
Class.forName("org.sqlite.JDBC"); Class.forName("org.sqlite.JDBC");
ConsoleLogger.info("SQLite driver loaded"); ConsoleLogger.info("SQLite driver loaded");
@ -283,24 +288,23 @@ public class SQLite implements DataSource {
@Override @Override
public List<String> autoPurgeDatabase(long until) { public List<String> autoPurgeDatabase(long until) {
PreparedStatement pst = null;
ResultSet rs = null;
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
try { String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;"); String delete = "DELETE FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
pst.setLong(1, until); try (PreparedStatement selectPst = con.prepareStatement(select);
rs = pst.executeQuery(); PreparedStatement deletePst = con.prepareStatement(delete)) {
selectPst.setLong(1, until);
try (ResultSet rs = selectPst.executeQuery()) {
while (rs.next()) { while (rs.next()) {
list.add(rs.getString(col.NAME)); list.add(rs.getString(col.NAME));
} }
return list; }
deletePst.setLong(1, until);
deletePst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(rs);
close(pst);
} }
return new ArrayList<>(); return list;
} }
@Override @Override
@ -592,10 +596,6 @@ public class SQLite implements DataSource {
return false; return false;
} }
private static void logSqlException(SQLException e) {
ConsoleLogger.logException("Error while executing SQL statement:", e);
}
private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException { private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException {
String salt = !col.SALT.isEmpty() ? row.getString(col.SALT) : null; String salt = !col.SALT.isEmpty() ? row.getString(col.SALT) : null;