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,8 +31,9 @@ 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
*/ */
public SQLite(NewSetting settings) throws ClassNotFoundException, SQLException { public SQLite(NewSetting settings) throws ClassNotFoundException, SQLException {
this.database = settings.getProperty(DatabaseSettings.MYSQL_DATABASE); this.database = settings.getProperty(DatabaseSettings.MYSQL_DATABASE);
@ -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)) {
while (rs.next()) { selectPst.setLong(1, until);
list.add(rs.getString(col.NAME)); try (ResultSet rs = selectPst.executeQuery()) {
while (rs.next()) {
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
@ -521,7 +525,7 @@ public class SQLite implements DataSource {
@Override @Override
public boolean updateRealName(String user, String realName) { public boolean updateRealName(String user, String realName) {
String sql = "UPDATE " + tableName + " SET " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;"; String sql = "UPDATE " + tableName + " SET " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
try(PreparedStatement pst = con.prepareStatement(sql)) { try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, realName); pst.setString(1, realName);
pst.setString(2, user); pst.setString(2, user);
pst.executeUpdate(); pst.executeUpdate();
@ -535,7 +539,7 @@ public class SQLite implements DataSource {
@Override @Override
public boolean updateIp(String user, String ip) { public boolean updateIp(String user, String ip) {
String sql = "UPDATE " + tableName + " SET " + col.IP + "=? WHERE " + col.NAME + "=?;"; String sql = "UPDATE " + tableName + " SET " + col.IP + "=? WHERE " + col.NAME + "=?;";
try(PreparedStatement pst = con.prepareStatement(sql)) { try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, ip); pst.setString(1, ip);
pst.setString(2, user); pst.setString(2, user);
pst.executeUpdate(); pst.executeUpdate();
@ -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;