perf(datasource): Use try-with-resources when it's possible

This commit is contained in:
Alexandre Vanhecke 2017-05-21 15:13:40 +02:00
parent 24189cf5d4
commit a167429fbc
3 changed files with 382 additions and 485 deletions

View File

@ -50,9 +50,7 @@ public class FlatFile implements DataSource {
@Override @Override
public synchronized boolean isAuthAvailable(String user) { public synchronized boolean isAuthAvailable(String user) {
BufferedReader br = null; try (BufferedReader br = new BufferedReader(new FileReader(source))) {
try {
br = new BufferedReader(new FileReader(source));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
@ -63,8 +61,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) { } catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
return false; return false;
} finally {
silentClose(br);
} }
return false; return false;
} }
@ -83,15 +79,11 @@ public class FlatFile implements DataSource {
if (isAuthAvailable(auth.getNickname())) { if (isAuthAvailable(auth.getNickname())) {
return false; return false;
} }
BufferedWriter bw = null; try (BufferedWriter bw = new BufferedWriter(new FileWriter(source, true))) {
try {
bw = new BufferedWriter(new FileWriter(source, true));
bw.write(auth.getNickname() + ":" + auth.getPassword().getHash() + ":" + auth.getIp() + ":" + auth.getLastLogin() + ":" + auth.getQuitLocX() + ":" + auth.getQuitLocY() + ":" + auth.getQuitLocZ() + ":" + auth.getWorld() + ":" + auth.getEmail() + "\n"); bw.write(auth.getNickname() + ":" + auth.getPassword().getHash() + ":" + auth.getIp() + ":" + auth.getLastLogin() + ":" + auth.getQuitLocX() + ":" + auth.getQuitLocY() + ":" + auth.getQuitLocZ() + ":" + auth.getWorld() + ":" + auth.getEmail() + "\n");
} catch (IOException ex) { } catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
return false; return false;
} finally {
silentClose(bw);
} }
return true; return true;
} }
@ -109,9 +101,7 @@ public class FlatFile implements DataSource {
return false; return false;
} }
PlayerAuth newAuth = null; PlayerAuth newAuth = null;
BufferedReader br = null; try (BufferedReader br = new BufferedReader(new FileReader(source))) {
try {
br = new BufferedReader(new FileReader(source));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
@ -126,8 +116,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) { } catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
return false; return false;
} finally {
silentClose(br);
} }
if (newAuth != null) { if (newAuth != null) {
removeAuth(user); removeAuth(user);
@ -142,9 +130,7 @@ public class FlatFile implements DataSource {
return false; return false;
} }
PlayerAuth newAuth = null; PlayerAuth newAuth = null;
BufferedReader br = null; try (BufferedReader br = new BufferedReader(new FileReader(source))) {
try {
br = new BufferedReader(new FileReader(source));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
@ -160,8 +146,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) { } catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
return false; return false;
} finally {
silentClose(br);
} }
if (newAuth != null) { if (newAuth != null) {
removeAuth(auth.getNickname()); removeAuth(auth.getNickname());
@ -176,9 +160,7 @@ public class FlatFile implements DataSource {
return false; return false;
} }
PlayerAuth newAuth = null; PlayerAuth newAuth = null;
BufferedReader br = null; try (BufferedReader br = new BufferedReader(new FileReader(source))) {
try {
br = new BufferedReader(new FileReader(source));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
@ -197,8 +179,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) { } catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
return false; return false;
} finally {
silentClose(br);
} }
if (newAuth != null) { if (newAuth != null) {
removeAuth(auth.getNickname()); removeAuth(auth.getNickname());
@ -222,11 +202,9 @@ public class FlatFile implements DataSource {
if (!isAuthAvailable(user)) { if (!isAuthAvailable(user)) {
return false; return false;
} }
BufferedReader br = null;
BufferedWriter bw = null;
ArrayList<String> lines = new ArrayList<>(); ArrayList<String> lines = new ArrayList<>();
try { try (BufferedReader br = new BufferedReader(new FileReader(source));) {
br = new BufferedReader(new FileReader(source));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
@ -234,25 +212,21 @@ public class FlatFile implements DataSource {
lines.add(line); lines.add(line);
} }
} }
bw = new BufferedWriter(new FileWriter(source)); try (BufferedWriter bw = new BufferedWriter(new FileWriter(source))) {
for (String l : lines) { for (String l : lines) {
bw.write(l + "\n"); bw.write(l + "\n");
} }
}
} catch (IOException ex) { } catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
return false; return false;
} finally {
silentClose(br);
silentClose(bw);
} }
return true; return true;
} }
@Override @Override
public synchronized PlayerAuth getAuth(String user) { public synchronized PlayerAuth getAuth(String user) {
BufferedReader br = null; try (BufferedReader br = new BufferedReader(new FileReader(source))) {
try {
br = new BufferedReader(new FileReader(source));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
@ -263,8 +237,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) { } catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
return null; return null;
} finally {
silentClose(br);
} }
return null; return null;
} }
@ -279,9 +251,7 @@ public class FlatFile implements DataSource {
return false; return false;
} }
PlayerAuth newAuth = null; PlayerAuth newAuth = null;
BufferedReader br = null; try (BufferedReader br = new BufferedReader(new FileReader(source))) {
try {
br = new BufferedReader(new FileReader(source));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
@ -296,8 +266,6 @@ public class FlatFile implements DataSource {
} catch (IOException ex) { } catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
return false; return false;
} finally {
silentClose(br);
} }
if (newAuth != null) { if (newAuth != null) {
removeAuth(auth.getNickname()); removeAuth(auth.getNickname());
@ -308,10 +276,8 @@ public class FlatFile implements DataSource {
@Override @Override
public List<String> getAllAuthsByIp(String ip) { public List<String> getAllAuthsByIp(String ip) {
BufferedReader br = null;
List<String> countIp = new ArrayList<>(); List<String> countIp = new ArrayList<>();
try { try (BufferedReader br = new BufferedReader(new FileReader(source))) {
br = new BufferedReader(new FileReader(source));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
@ -323,17 +289,13 @@ public class FlatFile implements DataSource {
} catch (IOException ex) { } catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
return new ArrayList<>(); return new ArrayList<>();
} finally {
silentClose(br);
} }
} }
@Override @Override
public int countAuthsByEmail(String email) { public int countAuthsByEmail(String email) {
BufferedReader br = null;
int countEmail = 0; int countEmail = 0;
try { try (BufferedReader br = new BufferedReader(new FileReader(source))) {
br = new BufferedReader(new FileReader(source));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
@ -344,8 +306,6 @@ public class FlatFile implements DataSource {
return countEmail; return countEmail;
} catch (IOException ex) { } catch (IOException ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
} finally {
silentClose(br);
} }
return 0; return 0;
} }
@ -374,18 +334,14 @@ public class FlatFile implements DataSource {
@Override @Override
public int getAccountsRegistered() { public int getAccountsRegistered() {
BufferedReader br = null;
int result = 0; int result = 0;
try { try (BufferedReader br = new BufferedReader(new FileReader(source))) {
br = new BufferedReader(new FileReader(source));
while ((br.readLine()) != null) { while ((br.readLine()) != null) {
result++; result++;
} }
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
return result; return result;
} finally {
silentClose(br);
} }
return result; return result;
} }
@ -402,10 +358,8 @@ public class FlatFile implements DataSource {
@Override @Override
public List<PlayerAuth> getAllAuths() { public List<PlayerAuth> getAllAuths() {
BufferedReader br = null;
List<PlayerAuth> auths = new ArrayList<>(); List<PlayerAuth> auths = new ArrayList<>();
try { try (BufferedReader br = new BufferedReader(new FileReader(source))) {
br = new BufferedReader(new FileReader(source));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
@ -416,8 +370,6 @@ public class FlatFile implements DataSource {
} }
} catch (IOException ex) { } catch (IOException ex) {
ConsoleLogger.logException("Error while getting auths from flatfile:", ex); ConsoleLogger.logException("Error while getting auths from flatfile:", ex);
} finally {
silentClose(br);
} }
return auths; return auths;
} }
@ -446,14 +398,4 @@ public class FlatFile implements DataSource {
} }
return null; return null;
} }
private static void silentClose(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignored) {
// silent close
}
}
}
} }

View File

@ -15,6 +15,7 @@ import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
import javax.xml.transform.Result;
import java.sql.Blob; import java.sql.Blob;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
@ -260,15 +261,13 @@ public class MySQL implements DataSource {
@Override @Override
public boolean isAuthAvailable(String user) { public boolean isAuthAvailable(String user) {
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.NAME + "=?;"; String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
ResultSet rs = null;
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) { try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user.toLowerCase()); pst.setString(1, user.toLowerCase());
rs = pst.executeQuery(); try (ResultSet rs = pst.executeQuery()) {
return rs.next(); return rs.next();
}
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(rs);
} }
return false; return false;
} }
@ -330,9 +329,6 @@ public class MySQL implements DataSource {
@Override @Override
public boolean saveAuth(PlayerAuth auth) { public boolean saveAuth(PlayerAuth auth) {
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst;
PreparedStatement pst2;
ResultSet rs;
String sql; String sql;
boolean useSalt = !col.SALT.isEmpty() || !StringUtils.isEmpty(auth.getPassword().getSalt()); boolean useSalt = !col.SALT.isEmpty() || !StringUtils.isEmpty(auth.getPassword().getSalt());
@ -341,7 +337,7 @@ public class MySQL implements DataSource {
+ col.LAST_LOGIN + "," + col.REAL_NAME + "," + col.EMAIL + col.LAST_LOGIN + "," + col.REAL_NAME + "," + col.EMAIL
+ (useSalt ? "," + col.SALT : "") + (useSalt ? "," + col.SALT : "")
+ ") VALUES (?,?,?,?,?,?" + (useSalt ? ",?" : "") + ");"; + ") VALUES (?,?,?,?,?,?" + (useSalt ? ",?" : "") + ");";
pst = con.prepareStatement(sql); try ( PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, auth.getNickname()); pst.setString(1, auth.getNickname());
pst.setString(2, auth.getPassword().getHash()); pst.setString(2, auth.getPassword().getHash());
pst.setString(3, auth.getIp()); pst.setString(3, auth.getIp());
@ -352,118 +348,119 @@ public class MySQL implements DataSource {
pst.setString(7, auth.getPassword().getSalt()); pst.setString(7, auth.getPassword().getSalt());
} }
pst.executeUpdate(); pst.executeUpdate();
pst.close(); }
if (!columnOthers.isEmpty()) { if (!columnOthers.isEmpty()) {
for (String column : columnOthers) { for (String column : columnOthers) {
pst = con.prepareStatement("UPDATE " + tableName + " SET " + column + "=? WHERE " + col.NAME + "=?;"); try (PreparedStatement pst = con.prepareStatement("UPDATE " + tableName + " SET " + column + "=? WHERE " + col.NAME + "=?;")) {
pst.setString(1, auth.getRealName()); pst.setString(1, auth.getRealName());
pst.setString(2, auth.getNickname()); pst.setString(2, auth.getNickname());
pst.executeUpdate(); pst.executeUpdate();
pst.close(); }
} }
} }
if (hashAlgorithm == HashAlgorithm.IPB4){ if (hashAlgorithm == HashAlgorithm.IPB4){
sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;"; sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
pst = con.prepareStatement(sql); try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, auth.getNickname()); pst.setString(1, auth.getNickname());
rs = pst.executeQuery(); try (ResultSet rs = pst.executeQuery()) {
if (rs.next()){ if (rs.next()){
// Update player group in core_members // Update player group in core_members
sql = "UPDATE " + ipbPrefix + tableName + " SET "+ tableName + ".member_group_id=? WHERE " + col.NAME + "=?;"; sql = "UPDATE " + ipbPrefix + tableName + " SET "+ tableName + ".member_group_id=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql))
{
pst2.setInt(1, ipbGroup); pst2.setInt(1, ipbGroup);
pst2.setString(2, auth.getNickname()); pst2.setString(2, auth.getNickname());
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); }
// Get current time without ms // Get current time without ms
long time = System.currentTimeMillis() / 1000; long time = System.currentTimeMillis() / 1000;
// update joined date // update joined date
sql = "UPDATE " + ipbPrefix + tableName + " SET "+ tableName + ".joined=? WHERE " + col.NAME + "=?;"; sql = "UPDATE " + ipbPrefix + tableName + " SET "+ tableName + ".joined=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setLong(1, time); pst2.setLong(1, time);
pst2.setString(2, auth.getNickname()); pst2.setString(2, auth.getNickname());
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); }
// Update last_visit // Update last_visit
sql = "UPDATE " + ipbPrefix + tableName + " SET " + tableName + ".last_visit=? WHERE " + col.NAME + "=?;"; sql = "UPDATE " + ipbPrefix + tableName + " SET " + tableName + ".last_visit=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setLong(1, time); pst2.setLong(1, time);
pst2.setString(2, auth.getNickname()); pst2.setString(2, auth.getNickname());
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close();
} }
rs.close(); }
pst.close(); }
}
} else if (hashAlgorithm == HashAlgorithm.PHPBB) { } else if (hashAlgorithm == HashAlgorithm.PHPBB) {
sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;"; sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
pst = con.prepareStatement(sql); try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, auth.getNickname()); pst.setString(1, auth.getNickname());
rs = pst.executeQuery(); try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) { if (rs.next()) {
int id = rs.getInt(col.ID); int id = rs.getInt(col.ID);
// Insert player in phpbb_user_group // Insert player in phpbb_user_group
sql = "INSERT INTO " + phpBbPrefix sql = "INSERT INTO " + phpBbPrefix
+ "user_group (group_id, user_id, group_leader, user_pending) VALUES (?,?,?,?);"; + "user_group (group_id, user_id, group_leader, user_pending) VALUES (?,?,?,?);";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, phpBbGroup); pst2.setInt(1, phpBbGroup);
pst2.setInt(2, id); pst2.setInt(2, id);
pst2.setInt(3, 0); pst2.setInt(3, 0);
pst2.setInt(4, 0); pst2.setInt(4, 0);
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); }
// Update username_clean in phpbb_users // Update username_clean in phpbb_users
sql = "UPDATE " + tableName + " SET " + tableName sql = "UPDATE " + tableName + " SET " + tableName
+ ".username_clean=? WHERE " + col.NAME + "=?;"; + ".username_clean=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setString(1, auth.getNickname()); pst2.setString(1, auth.getNickname());
pst2.setString(2, auth.getNickname()); pst2.setString(2, auth.getNickname());
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); }
// Update player group in phpbb_users // Update player group in phpbb_users
sql = "UPDATE " + tableName + " SET " + tableName sql = "UPDATE " + tableName + " SET " + tableName
+ ".group_id=? WHERE " + col.NAME + "=?;"; + ".group_id=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, phpBbGroup); pst2.setInt(1, phpBbGroup);
pst2.setString(2, auth.getNickname()); pst2.setString(2, auth.getNickname());
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); }
// Get current time without ms // Get current time without ms
long time = System.currentTimeMillis() / 1000; long time = System.currentTimeMillis() / 1000;
// Update user_regdate // Update user_regdate
sql = "UPDATE " + tableName + " SET " + tableName sql = "UPDATE " + tableName + " SET " + tableName
+ ".user_regdate=? WHERE " + col.NAME + "=?;"; + ".user_regdate=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setLong(1, time); pst2.setLong(1, time);
pst2.setString(2, auth.getNickname()); pst2.setString(2, auth.getNickname());
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); }
// Update user_lastvisit // Update user_lastvisit
sql = "UPDATE " + tableName + " SET " + tableName sql = "UPDATE " + tableName + " SET " + tableName
+ ".user_lastvisit=? WHERE " + col.NAME + "=?;"; + ".user_lastvisit=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setLong(1, time); pst2.setLong(1, time);
pst2.setString(2, auth.getNickname()); pst2.setString(2, auth.getNickname());
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); }
// Increment num_users // Increment num_users
sql = "UPDATE " + phpBbPrefix sql = "UPDATE " + phpBbPrefix
+ "config SET config_value = config_value + 1 WHERE config_name = 'num_users';"; + "config SET config_value = config_value + 1 WHERE config_name = 'num_users';";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close();
} }
rs.close(); }
pst.close(); }
}
} else if (hashAlgorithm == HashAlgorithm.WORDPRESS) { } else if (hashAlgorithm == HashAlgorithm.WORDPRESS) {
// NOTE: Eclipse says pst should be closed HERE, but it's a bug, we already close it above. -sgdc3 // NOTE: Eclipse says pst should be closed HERE, but it's a bug, we already close it above. -sgdc3
pst = con.prepareStatement("SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;"); try (PreparedStatement pst = con.prepareStatement("SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;")) {
pst.setString(1, auth.getNickname()); pst.setString(1, auth.getNickname());
rs = pst.executeQuery(); try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) { if (rs.next()) {
int id = rs.getInt(col.ID); int id = rs.getInt(col.ID);
sql = "INSERT INTO " + wordpressPrefix + "usermeta (user_id, meta_key, meta_value) VALUES (?,?,?)"; sql = "INSERT INTO " + wordpressPrefix + "usermeta (user_id, meta_key, meta_value) VALUES (?,?,?)";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
// First Name // First Name
pst2.setInt(1, id); pst2.setInt(1, id);
pst2.setString(2, "first_name"); pst2.setString(2, "first_name");
@ -528,20 +525,20 @@ public class MySQL implements DataSource {
// Execute queries // Execute queries
pst2.executeBatch(); pst2.executeBatch();
pst2.clearBatch(); pst2.clearBatch();
pst2.close();
} }
rs.close(); }
pst.close(); }
}
} else if (hashAlgorithm == HashAlgorithm.XFBCRYPT) { } else if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
// NOTE: Eclipse says pst should be closed HERE, but it's a bug, we already close it above. -sgdc3 // NOTE: Eclipse says pst should be closed HERE, but it's a bug, we already close it above. -sgdc3
pst = con.prepareStatement("SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;"); try (PreparedStatement pst = con.prepareStatement("SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;")) {
pst.setString(1, auth.getNickname()); pst.setString(1, auth.getNickname());
rs = pst.executeQuery(); try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) { if (rs.next()) {
int id = rs.getInt(col.ID); int id = rs.getInt(col.ID);
// Insert player password, salt in xf_user_authenticate // Insert player password, salt in xf_user_authenticate
sql = "INSERT INTO xf_user_authenticate (user_id, scheme_class, data) VALUES (?,?,?)"; sql = "INSERT INTO xf_user_authenticate (user_id, scheme_class, data) VALUES (?,?,?)";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, id); pst2.setInt(1, id);
pst2.setString(2, XfBCrypt.SCHEME_CLASS); pst2.setString(2, XfBCrypt.SCHEME_CLASS);
String serializedHash = XfBCrypt.serializeHash(auth.getPassword().getHash()); String serializedHash = XfBCrypt.serializeHash(auth.getPassword().getHash());
@ -550,24 +547,24 @@ public class MySQL implements DataSource {
blob.setBytes(1, bytes); blob.setBytes(1, bytes);
pst2.setBlob(3, blob); pst2.setBlob(3, blob);
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); }
// Update player group in xf_users // Update player group in xf_users
sql = "UPDATE " + tableName + " SET "+ tableName + ".user_group_id=? WHERE " + col.NAME + "=?;"; sql = "UPDATE " + tableName + " SET "+ tableName + ".user_group_id=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, xfGroup); pst2.setInt(1, xfGroup);
pst2.setString(2, auth.getNickname()); pst2.setString(2, auth.getNickname());
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); }
// Update player permission combination in xf_users // Update player permission combination in xf_users
sql = "UPDATE " + tableName + " SET "+ tableName + ".permission_combination_id=? WHERE " + col.NAME + "=?;"; sql = "UPDATE " + tableName + " SET "+ tableName + ".permission_combination_id=? WHERE " + col.NAME + "=?;";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, xfGroup); pst2.setInt(1, xfGroup);
pst2.setString(2, auth.getNickname()); pst2.setString(2, auth.getNickname());
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); }
// Insert player privacy combination in xf_user_privacy // Insert player privacy combination in xf_user_privacy
sql = "INSERT INTO xf_user_privacy (user_id, allow_view_profile, allow_post_profile, allow_send_personal_conversation, allow_view_identities, allow_receive_news_feed) VALUES (?,?,?,?,?,?)"; sql = "INSERT INTO xf_user_privacy (user_id, allow_view_profile, allow_post_profile, allow_send_personal_conversation, allow_view_identities, allow_receive_news_feed) VALUES (?,?,?,?,?,?)";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, id); pst2.setInt(1, id);
pst2.setString(2, "everyone"); pst2.setString(2, "everyone");
pst2.setString(3, "members"); pst2.setString(3, "members");
@ -575,18 +572,18 @@ public class MySQL implements DataSource {
pst2.setString(5, "everyone"); pst2.setString(5, "everyone");
pst2.setString(6, "everyone"); pst2.setString(6, "everyone");
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); }
// Insert player group relation in xf_user_group_relation // Insert player group relation in xf_user_group_relation
sql = "INSERT INTO xf_user_group_relation (user_id, user_group_id, is_primary) VALUES (?,?,?)"; sql = "INSERT INTO xf_user_group_relation (user_id, user_group_id, is_primary) VALUES (?,?,?)";
pst2 = con.prepareStatement(sql); try (PreparedStatement pst2 = con.prepareStatement(sql)) {
pst2.setInt(1, id); pst2.setInt(1, id);
pst2.setInt(2, xfGroup); pst2.setInt(2, xfGroup);
pst2.setString(3, "1"); pst2.setString(3, "1");
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close();
} }
rs.close(); }
pst.close(); }
}
} }
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
@ -605,28 +602,29 @@ public class MySQL implements DataSource {
user = user.toLowerCase(); user = user.toLowerCase();
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
boolean useSalt = !col.SALT.isEmpty(); boolean useSalt = !col.SALT.isEmpty();
PreparedStatement pst;
if (useSalt) { if (useSalt) {
String sql = String.format("UPDATE %s SET %s = ?, %s = ? WHERE %s = ?;", String sql = String.format("UPDATE %s SET %s = ?, %s = ? WHERE %s = ?;",
tableName, col.PASSWORD, col.SALT, col.NAME); tableName, col.PASSWORD, col.SALT, col.NAME);
pst = con.prepareStatement(sql); try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, password.getHash()); pst.setString(1, password.getHash());
pst.setString(2, password.getSalt()); pst.setString(2, password.getSalt());
pst.setString(3, user); pst.setString(3, user);
pst.executeUpdate();
}
} else { } else {
String sql = String.format("UPDATE %s SET %s = ? WHERE %s = ?;", String sql = String.format("UPDATE %s SET %s = ? WHERE %s = ?;",
tableName, col.PASSWORD, col.NAME); tableName, col.PASSWORD, col.NAME);
pst = con.prepareStatement(sql); try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, password.getHash()); pst.setString(1, password.getHash());
pst.setString(2, user); pst.setString(2, user);
}
pst.executeUpdate(); pst.executeUpdate();
pst.close(); }
}
if (hashAlgorithm == HashAlgorithm.XFBCRYPT) { if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
String sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;"; String sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
pst = con.prepareStatement(sql); try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user); pst.setString(1, user);
ResultSet rs = pst.executeQuery(); try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) { if (rs.next()) {
int id = rs.getInt(col.ID); int id = rs.getInt(col.ID);
// Insert password in the correct table // Insert password in the correct table
@ -648,8 +646,8 @@ public class MySQL implements DataSource {
pst2.executeUpdate(); pst2.executeUpdate();
pst2.close(); pst2.close();
} }
rs.close(); }
pst.close(); }
} }
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
@ -678,7 +676,6 @@ public class MySQL implements DataSource {
@Override @Override
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) { public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
Set<String> list = new HashSet<>(); Set<String> list = new HashSet<>();
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + " < ?"; String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + " < ?";
if (!includeEntriesWithLastLoginZero) { if (!includeEntriesWithLastLoginZero) {
select += " AND " + col.LAST_LOGIN + " <> 0"; select += " AND " + col.LAST_LOGIN + " <> 0";
@ -702,31 +699,28 @@ public class MySQL implements DataSource {
public boolean removeAuth(String user) { public boolean removeAuth(String user) {
user = user.toLowerCase(); user = user.toLowerCase();
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;"; String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
PreparedStatement xfSelect = null;
PreparedStatement xfDelete = null;
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) { try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
if (hashAlgorithm == HashAlgorithm.XFBCRYPT) { if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;"; sql = "SELECT " + col.ID + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
xfSelect = con.prepareStatement(sql); try (PreparedStatement xfSelect = con.prepareStatement(sql)) {
xfSelect.setString(1, user); xfSelect.setString(1, user);
try (ResultSet rs = xfSelect.executeQuery()) { try (ResultSet rs = xfSelect.executeQuery()) {
if (rs.next()) { if (rs.next()) {
int id = rs.getInt(col.ID); int id = rs.getInt(col.ID);
sql = "DELETE FROM xf_user_authenticate WHERE " + col.ID + "=?;"; sql = "DELETE FROM xf_user_authenticate WHERE " + col.ID + "=?;";
xfDelete = con.prepareStatement(sql); try (PreparedStatement xfDelete = con.prepareStatement(sql)) {
xfDelete.setInt(1, id); xfDelete.setInt(1, id);
xfDelete.executeUpdate(); xfDelete.executeUpdate();
} }
} }
} }
}
}
pst.setString(1, user.toLowerCase()); pst.setString(1, user.toLowerCase());
pst.executeUpdate(); pst.executeUpdate();
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(xfSelect);
close(xfDelete);
} }
return false; return false;
} }
@ -925,8 +919,8 @@ public class MySQL implements DataSource {
public List<PlayerAuth> getAllAuths() { public List<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<>(); List<PlayerAuth> auths = new ArrayList<>();
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
Statement st = con.createStatement(); try (Statement st = con.createStatement()) {
ResultSet rs = st.executeQuery("SELECT * FROM " + tableName); try (ResultSet rs = st.executeQuery("SELECT * FROM " + tableName)) {
while (rs.next()) { while (rs.next()) {
PlayerAuth pAuth = buildAuthFromResultSet(rs); PlayerAuth pAuth = buildAuthFromResultSet(rs);
if (hashAlgorithm == HashAlgorithm.XFBCRYPT) { if (hashAlgorithm == HashAlgorithm.XFBCRYPT) {
@ -944,8 +938,8 @@ public class MySQL implements DataSource {
} }
auths.add(pAuth); auths.add(pAuth);
} }
rs.close(); }
st.close(); }
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} }
@ -1055,8 +1049,9 @@ public class MySQL implements DataSource {
// Rename lastlogin to lastlogin_old // Rename lastlogin to lastlogin_old
String sql = String.format("ALTER TABLE %s CHANGE COLUMN %s %s BIGINT", String sql = String.format("ALTER TABLE %s CHANGE COLUMN %s %s BIGINT",
tableName, col.LAST_LOGIN, lastLoginOld); tableName, col.LAST_LOGIN, lastLoginOld);
PreparedStatement pst = con.prepareStatement(sql); try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.execute(); pst.execute();
}
// Create lastlogin column // Create lastlogin column
sql = String.format("ALTER TABLE %s ADD COLUMN %s " sql = String.format("ALTER TABLE %s ADD COLUMN %s "

View File

@ -159,19 +159,15 @@ public class SQLite implements DataSource {
@Override @Override
public boolean isAuthAvailable(String user) { public boolean isAuthAvailable(String user) {
PreparedStatement pst = null; String sql = "SELECT 1 FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=LOWER(?);";
ResultSet rs = null; try (PreparedStatement pst = con.prepareStatement(sql)) {
try {
pst = con.prepareStatement("SELECT 1 FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=LOWER(?);");
pst.setString(1, user); pst.setString(1, user);
rs = pst.executeQuery(); try (ResultSet rs = pst.executeQuery()) {
return rs.next(); return rs.next();
}
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.warning(ex.getMessage()); ConsoleLogger.warning(ex.getMessage());
return false; return false;
} finally {
close(rs);
close(pst);
} }
} }
@ -197,27 +193,22 @@ public class SQLite implements DataSource {
@Override @Override
public PlayerAuth getAuth(String user) { public PlayerAuth getAuth(String user) {
PreparedStatement pst = null; String sql = "SELECT * FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=LOWER(?);";
ResultSet rs = null; try (PreparedStatement pst = con.prepareStatement(sql)) {
try {
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=LOWER(?);");
pst.setString(1, user); pst.setString(1, user);
rs = pst.executeQuery(); try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) { if (rs.next()) {
return buildAuthFromResultSet(rs); return buildAuthFromResultSet(rs);
} }
}
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(rs);
close(pst);
} }
return null; return null;
} }
@Override @Override
public boolean saveAuth(PlayerAuth auth) { public boolean saveAuth(PlayerAuth auth) {
PreparedStatement pst = null;
try { try {
HashedPassword password = auth.getPassword(); HashedPassword password = auth.getPassword();
if (col.SALT.isEmpty()) { if (col.SALT.isEmpty()) {
@ -225,9 +216,9 @@ public class SQLite implements DataSource {
ConsoleLogger.warning("Warning! Detected hashed password with separate salt but the salt column " ConsoleLogger.warning("Warning! Detected hashed password with separate salt but the salt column "
+ "is not set in the config!"); + "is not set in the config!");
} }
pst = con.prepareStatement("INSERT INTO " + tableName + "(" + col.NAME + "," + col.PASSWORD + try (PreparedStatement pst = con.prepareStatement("INSERT INTO " + tableName + "(" + col.NAME + "," + col.PASSWORD +
"," + col.IP + "," + col.LAST_LOGIN + "," + col.REAL_NAME + "," + col.EMAIL + "," + col.IP + "," + col.LAST_LOGIN + "," + col.REAL_NAME + "," + col.EMAIL +
") VALUES (?,?,?,?,?,?);"); ") VALUES (?,?,?,?,?,?);")) {
pst.setString(1, auth.getNickname()); pst.setString(1, auth.getNickname());
pst.setString(2, password.getHash()); pst.setString(2, password.getHash());
pst.setString(3, auth.getIp()); pst.setString(3, auth.getIp());
@ -235,10 +226,11 @@ public class SQLite implements DataSource {
pst.setString(5, auth.getRealName()); pst.setString(5, auth.getRealName());
pst.setString(6, auth.getEmail()); pst.setString(6, auth.getEmail());
pst.executeUpdate(); pst.executeUpdate();
}
} else { } else {
pst = con.prepareStatement("INSERT INTO " + tableName + "(" + col.NAME + "," + col.PASSWORD + "," try (PreparedStatement pst = con.prepareStatement("INSERT INTO " + tableName + "(" + col.NAME + "," + col.PASSWORD + ","
+ col.IP + "," + col.LAST_LOGIN + "," + col.REAL_NAME + "," + col.EMAIL + "," + col.SALT + col.IP + "," + col.LAST_LOGIN + "," + col.REAL_NAME + "," + col.EMAIL + "," + col.SALT
+ ") VALUES (?,?,?,?,?,?,?);"); + ") VALUES (?,?,?,?,?,?,?);")) {
pst.setString(1, auth.getNickname()); pst.setString(1, auth.getNickname());
pst.setString(2, password.getHash()); pst.setString(2, password.getHash());
pst.setString(3, auth.getIp()); pst.setString(3, auth.getIp());
@ -248,10 +240,9 @@ public class SQLite implements DataSource {
pst.setString(7, password.getSalt()); pst.setString(7, password.getSalt());
pst.executeUpdate(); pst.executeUpdate();
} }
}
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(pst);
} }
return true; return true;
} }
@ -264,13 +255,11 @@ public class SQLite implements DataSource {
@Override @Override
public boolean updatePassword(String user, HashedPassword password) { public boolean updatePassword(String user, HashedPassword password) {
user = user.toLowerCase(); user = user.toLowerCase();
PreparedStatement pst = null;
try {
boolean useSalt = !col.SALT.isEmpty(); boolean useSalt = !col.SALT.isEmpty();
String sql = "UPDATE " + tableName + " SET " + col.PASSWORD + " = ?" String sql = "UPDATE " + tableName + " SET " + col.PASSWORD + " = ?"
+ (useSalt ? ", " + col.SALT + " = ?" : "") + (useSalt ? ", " + col.SALT + " = ?" : "")
+ " WHERE " + col.NAME + " = ?"; + " WHERE " + col.NAME + " = ?";
pst = con.prepareStatement(sql); try (PreparedStatement pst = con.prepareStatement(sql)){
pst.setString(1, password.getHash()); pst.setString(1, password.getHash());
if (useSalt) { if (useSalt) {
pst.setString(2, password.getSalt()); pst.setString(2, password.getSalt());
@ -282,17 +271,14 @@ public class SQLite implements DataSource {
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(pst);
} }
return false; return false;
} }
@Override @Override
public boolean updateSession(PlayerAuth auth) { public boolean updateSession(PlayerAuth auth) {
PreparedStatement pst = null; String sql = "UPDATE " + tableName + " SET " + col.IP + "=?, " + col.LAST_LOGIN + "=?, " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
try { try (PreparedStatement pst = con.prepareStatement(sql)){
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IP + "=?, " + col.LAST_LOGIN + "=?, " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;");
pst.setString(1, auth.getIp()); pst.setString(1, auth.getIp());
pst.setLong(2, auth.getLastLogin()); pst.setLong(2, auth.getLastLogin());
pst.setString(3, auth.getRealName()); pst.setString(3, auth.getRealName());
@ -301,8 +287,6 @@ public class SQLite implements DataSource {
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(pst);
} }
return false; return false;
} }
@ -310,7 +294,6 @@ public class SQLite implements DataSource {
@Override @Override
public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) { public Set<String> getRecordsToPurge(long until, boolean includeEntriesWithLastLoginZero) {
Set<String> list = new HashSet<>(); Set<String> list = new HashSet<>();
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + " < ?"; String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + " < ?";
if (!includeEntriesWithLastLoginZero) { if (!includeEntriesWithLastLoginZero) {
select += " AND " + col.LAST_LOGIN + " <> 0"; select += " AND " + col.LAST_LOGIN + " <> 0";
@ -344,28 +327,24 @@ public class SQLite implements DataSource {
@Override @Override
public boolean removeAuth(String user) { public boolean removeAuth(String user) {
PreparedStatement pst = null; String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
try { try (PreparedStatement pst = con.prepareStatement(sql)) {
pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;");
pst.setString(1, user.toLowerCase()); pst.setString(1, user.toLowerCase());
pst.executeUpdate(); pst.executeUpdate();
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(pst);
} }
return false; return false;
} }
@Override @Override
public boolean updateQuitLoc(PlayerAuth auth) { public boolean updateQuitLoc(PlayerAuth auth) {
PreparedStatement pst = null; String sql = "UPDATE " + tableName + " SET "
try {
pst = con.prepareStatement("UPDATE " + tableName + " SET "
+ col.LASTLOC_X + "=?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, " + col.LASTLOC_X + "=?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, "
+ col.LASTLOC_WORLD + "=?, " + col.LASTLOC_YAW + "=?, " + col.LASTLOC_PITCH + "=? " + col.LASTLOC_WORLD + "=?, " + col.LASTLOC_YAW + "=?, " + col.LASTLOC_PITCH + "=? "
+ "WHERE " + col.NAME + "=?;"); + "WHERE " + col.NAME + "=?;";
try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setDouble(1, auth.getQuitLocX()); pst.setDouble(1, auth.getQuitLocX());
pst.setDouble(2, auth.getQuitLocY()); pst.setDouble(2, auth.getQuitLocY());
pst.setDouble(3, auth.getQuitLocZ()); pst.setDouble(3, auth.getQuitLocZ());
@ -377,8 +356,6 @@ public class SQLite implements DataSource {
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(pst);
} }
return false; return false;
} }
@ -410,22 +387,18 @@ public class SQLite implements DataSource {
@Override @Override
public List<String> getAllAuthsByIp(String ip) { public List<String> getAllAuthsByIp(String ip) {
PreparedStatement pst = null;
ResultSet rs = null;
List<String> countIp = new ArrayList<>(); List<String> countIp = new ArrayList<>();
try { String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.IP + "=?;";
pst = con.prepareStatement("SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.IP + "=?;"); try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, ip); pst.setString(1, ip);
rs = pst.executeQuery(); try (ResultSet rs = pst.executeQuery()) {
while (rs.next()) { while (rs.next()) {
countIp.add(rs.getString(col.NAME)); countIp.add(rs.getString(col.NAME));
} }
return countIp; return countIp;
}
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(rs);
close(pst);
} }
return new ArrayList<>(); return new ArrayList<>();
} }
@ -453,66 +426,53 @@ public class SQLite implements DataSource {
@Override @Override
public boolean isLogged(String user) { public boolean isLogged(String user) {
PreparedStatement pst = null; String sql = "SELECT * FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=?;";
ResultSet rs = null; try (PreparedStatement pst = con.prepareStatement(sql)) {
try {
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=?;");
pst.setString(1, user); pst.setString(1, user);
rs = pst.executeQuery(); try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) if (rs.next())
return rs.getInt(col.IS_LOGGED) == 1; return rs.getInt(col.IS_LOGGED) == 1;
}
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(rs);
close(pst);
} }
return false; return false;
} }
@Override @Override
public void setLogged(String user) { public void setLogged(String user) {
PreparedStatement pst = null; String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE LOWER(" + col.NAME + ")=?;";
try { try (PreparedStatement pst = con.prepareStatement(sql)) {
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE LOWER(" + col.NAME + ")=?;");
pst.setInt(1, 1); pst.setInt(1, 1);
pst.setString(2, user); pst.setString(2, user);
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(pst);
} }
} }
@Override @Override
public void setUnlogged(String user) { public void setUnlogged(String user) {
PreparedStatement pst = null; String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE LOWER(" + col.NAME + ")=?;";
if (user != null) if (user != null)
try { try (PreparedStatement pst = con.prepareStatement(sql)) {
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE LOWER(" + col.NAME + ")=?;");
pst.setInt(1, 0); pst.setInt(1, 0);
pst.setString(2, user); pst.setString(2, user);
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(pst);
} }
} }
@Override @Override
public void purgeLogged() { public void purgeLogged() {
PreparedStatement pst = null; String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.IS_LOGGED + "=?;";
try { try (PreparedStatement pst = con.prepareStatement(sql)) {
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.IS_LOGGED + "=?;");
pst.setInt(1, 0); pst.setInt(1, 0);
pst.setInt(2, 1); pst.setInt(2, 1);
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
logSqlException(ex); logSqlException(ex);
} finally {
close(pst);
} }
} }