mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2025-02-02 13:01:28 +01:00
Improving logging in SQLite
This commit is contained in:
parent
2dbcd50eea
commit
dbd896b462
@ -164,16 +164,14 @@ public class SQLite implements DataSource {
|
|||||||
rs = pst.executeQuery();
|
rs = pst.executeQuery();
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
return buildAuthFromResultSet(rs);
|
return buildAuthFromResultSet(rs);
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return null;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(rs);
|
close(rs);
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -210,8 +208,7 @@ public class SQLite implements DataSource {
|
|||||||
pst.executeUpdate();
|
pst.executeUpdate();
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return false;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
@ -241,13 +238,13 @@ public class SQLite implements DataSource {
|
|||||||
pst.setString(2, user);
|
pst.setString(2, user);
|
||||||
}
|
}
|
||||||
pst.executeUpdate();
|
pst.executeUpdate();
|
||||||
|
return true;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return false;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -260,29 +257,25 @@ public class SQLite implements DataSource {
|
|||||||
pst.setString(3, auth.getRealName());
|
pst.setString(3, auth.getRealName());
|
||||||
pst.setString(4, auth.getNickname());
|
pst.setString(4, auth.getNickname());
|
||||||
pst.executeUpdate();
|
pst.executeUpdate();
|
||||||
|
return true;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return false;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int purgeDatabase(long until) {
|
public int purgeDatabase(long until) {
|
||||||
PreparedStatement pst = null;
|
String sql = "DELETE FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
|
||||||
try {
|
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
|
|
||||||
pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;");
|
|
||||||
pst.setLong(1, until);
|
pst.setLong(1, until);
|
||||||
return pst.executeUpdate();
|
return pst.executeUpdate();
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return 0;
|
|
||||||
} finally {
|
|
||||||
close(pst);
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -299,12 +292,12 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return new ArrayList<>();
|
|
||||||
} finally {
|
} finally {
|
||||||
close(rs);
|
close(rs);
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -314,13 +307,13 @@ public class SQLite implements DataSource {
|
|||||||
pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;");
|
pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;");
|
||||||
pst.setString(1, user);
|
pst.setString(1, user);
|
||||||
pst.executeUpdate();
|
pst.executeUpdate();
|
||||||
|
return true;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return false;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -334,13 +327,13 @@ public class SQLite implements DataSource {
|
|||||||
pst.setString(4, auth.getWorld());
|
pst.setString(4, auth.getWorld());
|
||||||
pst.setString(5, auth.getNickname());
|
pst.setString(5, auth.getNickname());
|
||||||
pst.executeUpdate();
|
pst.executeUpdate();
|
||||||
|
return true;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return false;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -358,29 +351,26 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
return countIp;
|
return countIp;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return 0;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(rs);
|
close(rs);
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean updateEmail(PlayerAuth auth) {
|
public boolean updateEmail(PlayerAuth auth) {
|
||||||
PreparedStatement pst = null;
|
String sql = "UPDATE " + tableName + " SET " + col.EMAIL + "=? WHERE " + col.NAME + "=?;";
|
||||||
try {
|
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.EMAIL + "=? WHERE " + col.NAME + "=?;");
|
|
||||||
pst.setString(1, auth.getEmail());
|
pst.setString(1, auth.getEmail());
|
||||||
pst.setString(2, auth.getNickname());
|
pst.setString(2, auth.getNickname());
|
||||||
pst.executeUpdate();
|
pst.executeUpdate();
|
||||||
|
return true;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return false;
|
|
||||||
} finally {
|
|
||||||
close(pst);
|
|
||||||
}
|
}
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -388,7 +378,7 @@ public class SQLite implements DataSource {
|
|||||||
try {
|
try {
|
||||||
con.close();
|
con.close();
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -401,7 +391,7 @@ public class SQLite implements DataSource {
|
|||||||
try {
|
try {
|
||||||
st.close();
|
st.close();
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -411,7 +401,7 @@ public class SQLite implements DataSource {
|
|||||||
try {
|
try {
|
||||||
rs.close();
|
rs.close();
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -420,24 +410,24 @@ public class SQLite implements DataSource {
|
|||||||
public List<String> getAllAuthsByName(PlayerAuth auth) {
|
public List<String> getAllAuthsByName(PlayerAuth auth) {
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
List<String> countIp = new ArrayList<>();
|
List<String> names = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
|
// TODO ljacqu 20160214: Use SELECT name if only the name is required
|
||||||
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + col.IP + "=?;");
|
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + col.IP + "=?;");
|
||||||
pst.setString(1, auth.getIp());
|
pst.setString(1, auth.getIp());
|
||||||
rs = pst.executeQuery();
|
rs = pst.executeQuery();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
countIp.add(rs.getString(col.NAME));
|
names.add(rs.getString(col.NAME));
|
||||||
}
|
}
|
||||||
return countIp;
|
return names;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
logSqlException(ex);
|
logSqlException(ex);
|
||||||
return new ArrayList<>();
|
|
||||||
} catch (NullPointerException npe) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
} finally {
|
} finally {
|
||||||
close(rs);
|
close(rs);
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -454,14 +444,12 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
return countIp;
|
return countIp;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return new ArrayList<>();
|
|
||||||
} catch (NullPointerException npe) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
} finally {
|
} finally {
|
||||||
close(rs);
|
close(rs);
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -478,14 +466,12 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
return countEmail;
|
return countEmail;
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return new ArrayList<>();
|
} finally {
|
||||||
} catch (NullPointerException npe) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
} finally {
|
|
||||||
close(rs);
|
close(rs);
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -520,8 +506,7 @@ public class SQLite implements DataSource {
|
|||||||
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) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return false;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(rs);
|
close(rs);
|
||||||
close(pst);
|
close(pst);
|
||||||
@ -538,7 +523,7 @@ public class SQLite implements DataSource {
|
|||||||
pst.setString(2, user);
|
pst.setString(2, user);
|
||||||
pst.executeUpdate();
|
pst.executeUpdate();
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
@ -554,7 +539,7 @@ public class SQLite implements DataSource {
|
|||||||
pst.setString(2, user);
|
pst.setString(2, user);
|
||||||
pst.executeUpdate();
|
pst.executeUpdate();
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
@ -569,7 +554,7 @@ public class SQLite implements DataSource {
|
|||||||
pst.setInt(2, 1);
|
pst.setInt(2, 1);
|
||||||
pst.executeUpdate();
|
pst.executeUpdate();
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
@ -577,22 +562,20 @@ public class SQLite implements DataSource {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getAccountsRegistered() {
|
public int getAccountsRegistered() {
|
||||||
int result = 0;
|
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
ResultSet rs;
|
ResultSet rs;
|
||||||
try {
|
try {
|
||||||
pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";");
|
pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";");
|
||||||
rs = pst.executeQuery();
|
rs = pst.executeQuery();
|
||||||
if (rs != null && rs.next()) {
|
if (rs != null && rs.next()) {
|
||||||
result = rs.getInt(1);
|
return rs.getInt(1);
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return result;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
return result;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -604,7 +587,7 @@ public class SQLite implements DataSource {
|
|||||||
pst.setString(2, oldOne);
|
pst.setString(2, oldOne);
|
||||||
pst.executeUpdate();
|
pst.executeUpdate();
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
@ -623,8 +606,7 @@ public class SQLite implements DataSource {
|
|||||||
auths.add(auth);
|
auths.add(auth);
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return auths;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
@ -644,8 +626,7 @@ public class SQLite implements DataSource {
|
|||||||
auths.add(auth);
|
auths.add(auth);
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
ConsoleLogger.showError(ex.getMessage());
|
logSqlException(ex);
|
||||||
return auths;
|
|
||||||
} finally {
|
} finally {
|
||||||
close(pst);
|
close(pst);
|
||||||
}
|
}
|
||||||
@ -669,8 +650,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void logSqlException(SQLException e) {
|
private static void logSqlException(SQLException e) {
|
||||||
ConsoleLogger.showError("Error while executing SQL statement: " + StringUtils.formatException(e));
|
ConsoleLogger.logException("Error while executing SQL statement:", e);
|
||||||
ConsoleLogger.writeStackTrace(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException {
|
private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException {
|
||||||
|
Loading…
Reference in New Issue
Block a user