From da1adb632ecfb4e891dd11f8a969ba338fd37cfd Mon Sep 17 00:00:00 2001 From: Xephi Date: Tue, 29 Dec 2015 17:07:33 +0100 Subject: [PATCH 01/10] Start a rework of some SQL Queries, add a Query builder --- .../authme/datasource/CacheDataSource.java | 7 + .../xephi/authme/datasource/DataSource.java | 4 + .../fr/xephi/authme/datasource/FlatFile.java | 7 + .../fr/xephi/authme/datasource/MySQL.java | 228 +++++--- .../fr/xephi/authme/datasource/SQLite.java | 493 +++++++++++------- .../authme/datasource/queries/Query.java | 215 ++++++++ 6 files changed, 694 insertions(+), 260 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/datasource/queries/Query.java diff --git a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java index 8b26b08c..85c0c50a 100644 --- a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java @@ -10,6 +10,8 @@ import com.google.common.cache.RemovalNotification; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; +import java.sql.Connection; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; @@ -478,4 +480,9 @@ public class CacheDataSource implements DataSource { public List getLoggedPlayers() { return new ArrayList<>(PlayerCache.getInstance().getCache().values()); } + + @Override + public Connection getConnection() throws SQLException { + return source.getConnection(); + } } diff --git a/src/main/java/fr/xephi/authme/datasource/DataSource.java b/src/main/java/fr/xephi/authme/datasource/DataSource.java index e43dffa8..1917c91f 100644 --- a/src/main/java/fr/xephi/authme/datasource/DataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/DataSource.java @@ -2,6 +2,8 @@ package fr.xephi.authme.datasource; import fr.xephi.authme.cache.auth.PlayerAuth; +import java.sql.Connection; +import java.sql.SQLException; import java.util.List; /** @@ -215,6 +217,8 @@ public interface DataSource { */ List getLoggedPlayers(); + Connection getConnection() throws SQLException; + enum DataSourceType { MYSQL, FILE, diff --git a/src/main/java/fr/xephi/authme/datasource/FlatFile.java b/src/main/java/fr/xephi/authme/datasource/FlatFile.java index b7fb5517..e97d8f39 100644 --- a/src/main/java/fr/xephi/authme/datasource/FlatFile.java +++ b/src/main/java/fr/xephi/authme/datasource/FlatFile.java @@ -7,6 +7,8 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -929,4 +931,9 @@ public class FlatFile implements DataSource { public List getLoggedPlayers() { return new ArrayList<>(); } + + @Override + public Connection getConnection() throws SQLException { + return null; + } } diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index e779afff..713901bf 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -5,6 +5,7 @@ import com.zaxxer.hikari.pool.HikariPool.PoolInitializationException; import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.datasource.queries.Query; import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.settings.Settings; @@ -134,7 +135,8 @@ public class MySQL implements DataSource { * * @return Connection * @throws SQLException */ - private synchronized Connection getConnection() throws SQLException { + @Override + public synchronized Connection getConnection() throws SQLException { return ds.getConnection(); } @@ -254,8 +256,12 @@ public class MySQL implements DataSource { @Override public synchronized boolean isAuthAvailable(String user) { try (Connection con = getConnection()) { - String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnName + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); + PreparedStatement pst = con.prepareStatement(new Query(this) + .select(columnName) + .from(tableName) + .addWhere(columnName + "=?", null) + .build() + .getQuery()); pst.setString(1, user.toLowerCase()); ResultSet rs = pst.executeQuery(); return rs.next(); @@ -277,8 +283,12 @@ public class MySQL implements DataSource { public synchronized PlayerAuth getAuth(String user) { PlayerAuth pAuth; try (Connection con = getConnection()) { - String sql = "SELECT * FROM " + tableName + " WHERE " + columnName + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); + PreparedStatement pst = con.prepareStatement(new Query(this) + .select("*") + .from(tableName) + .addWhere(columnName + "=?", null) + .build() + .getQuery()); pst.setString(1, user.toLowerCase()); ResultSet rs = pst.executeQuery(); if (!rs.next()) { @@ -304,7 +314,12 @@ public class MySQL implements DataSource { rs.close(); pst.close(); if (Settings.getPasswordHash == HashAlgorithm.XENFORO) { - pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + columnID + "=?;"); + pst = con.prepareStatement(new Query(this) + .select("data") + .from("xf_user_authenticate") + .addWhere(columnID + "=?", null) + .build() + .getQuery()); pst.setInt(1, id); rs = pst.executeQuery(); if (rs.next()) { @@ -595,10 +610,16 @@ public class MySQL implements DataSource { */ @Override public synchronized boolean updateSession(PlayerAuth auth) { - try (Connection con = getConnection()) { - String sql = "UPDATE " + tableName + " SET " - + columnIp + "=?, " + columnLastLogin + "=?, " + columnRealName + "=? WHERE " + columnName + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); + try(Connection con = getConnection()) { + PreparedStatement pst = con.prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnIp + "=?") + .addUpdateSet(columnLastLogin + "=?") + .addUpdateSet(columnRealName + "=?") + .addWhere(columnName + "=?", null) + .build() + .getQuery()); pst.setString(1, auth.getIp()); pst.setLong(2, auth.getLastLogin()); pst.setString(3, auth.getRealName()); @@ -624,9 +645,13 @@ public class MySQL implements DataSource { @Override public synchronized int purgeDatabase(long until) { int result = 0; - try (Connection con = getConnection()) { - String sql = "DELETE FROM " + tableName + " WHERE " + columnLastLogin + " autoPurgeDatabase(long until) { List list = new ArrayList<>(); - try (Connection con = getConnection()) { - String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnLastLogin + "<" + until; - Statement st = con.createStatement(); - ResultSet rs = st.executeQuery(sql); + try(Connection con = getConnection()) { + PreparedStatement st = con.prepareStatement(new Query(this) + .select(columnName) + .from(tableName) + .addWhere(columnLastLogin + "<" + until, null) + .build() + .getQuery()); + ResultSet rs = st.executeQuery(); while (rs.next()) { list.add(rs.getString(columnName)); } rs.close(); - sql = "DELETE FROM " + tableName + " WHERE " + columnLastLogin + "<" + until; - st.executeUpdate(sql); + st.close(); + st = con.prepareStatement(new Query(this) + .delete() + .from(tableName) + .addWhere(columnLastLogin + "<" + until, null) + .build() + .getQuery()); + st.executeUpdate(); st.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); @@ -718,11 +753,17 @@ public class MySQL implements DataSource { */ @Override public synchronized boolean updateQuitLoc(PlayerAuth auth) { - try (Connection con = getConnection()) { - String sql = "UPDATE " + tableName - + " SET " + lastlocX + " =?, " + lastlocY + "=?, " + lastlocZ + "=?, " + lastlocWorld + "=?" - + " WHERE " + columnName + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); + try(Connection con = getConnection()) { + PreparedStatement pst = con.prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(lastlocX + "=?") + .addUpdateSet(lastlocY + "=?") + .addUpdateSet(lastlocZ + "=?") + .addUpdateSet(lastlocWorld + "=?") + .addWhere(columnName + "=?", null) + .build() + .getQuery()); pst.setDouble(1, auth.getQuitLocX()); pst.setDouble(2, auth.getQuitLocY()); pst.setDouble(3, auth.getQuitLocZ()); @@ -751,8 +792,12 @@ public class MySQL implements DataSource { public synchronized int getIps(String ip) { int countIp = 0; try (Connection con = getConnection()) { - String sql = "SELECT COUNT(*) FROM " + tableName + " WHERE " + columnIp + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); + PreparedStatement pst = con.prepareStatement(new Query(this) + .select("COUNT(*)") + .from(tableName) + .addWhere(columnIp + "=?", null) + .build() + .getQuery()); pst.setString(1, ip); ResultSet rs = pst.executeQuery(); while (rs.next()) { @@ -779,8 +824,13 @@ public class MySQL implements DataSource { @Override public synchronized boolean updateEmail(PlayerAuth auth) { try (Connection con = getConnection()) { - String sql = "UPDATE " + tableName + " SET " + columnEmail + " =? WHERE " + columnName + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); + PreparedStatement pst = con.prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnEmail + "=?") + .addWhere(columnName + "=?", null) + .build() + .getQuery()); pst.setString(1, auth.getEmail()); pst.setString(2, auth.getNickname()); pst.executeUpdate(); @@ -808,8 +858,13 @@ public class MySQL implements DataSource { return false; } try (Connection con = getConnection()) { - String sql = "UPDATE " + tableName + " SET " + columnSalt + " =? WHERE " + columnName + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); + PreparedStatement pst = con.prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnSalt + "=?") + .addWhere(columnName + "=?", null) + .build() + .getQuery()); pst.setString(1, auth.getSalt()); pst.setString(2, auth.getNickname()); pst.executeUpdate(); @@ -864,9 +919,12 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByName(PlayerAuth auth) { List result = new ArrayList<>(); try (Connection con = getConnection()) { - String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnIp + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); - pst.setString(1, auth.getIp()); + PreparedStatement pst = con.prepareStatement(new Query(this) + .select(columnName) + .from(tableName) + .addWhere(columnIp + "='" + auth.getIp() + "'", null) + .build() + .getQuery()); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); @@ -893,9 +951,12 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByIp(String ip) { List result = new ArrayList<>(); try (Connection con = getConnection()) { - String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnIp + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); - pst.setString(1, ip); + PreparedStatement pst = con.prepareStatement(new Query(this) + .select(columnName) + .from(tableName) + .addWhere(columnIp + "='" + ip + "'", null) + .build() + .getQuery()); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); @@ -922,9 +983,12 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByEmail(String email){ List countEmail = new ArrayList<>(); try (Connection con = getConnection()) { - String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnEmail + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); - pst.setString(1, email); + PreparedStatement pst = con.prepareStatement(new Query(this) + .select(columnName) + .from(tableName) + .addWhere(columnEmail + "='" + email + "'", null) + .build() + .getQuery()); ResultSet rs = pst.executeQuery(); while (rs.next()) { countEmail.add(rs.getString(columnName)); @@ -948,7 +1012,12 @@ public class MySQL implements DataSource { @Override public synchronized void purgeBanned(List banned) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + columnName + "=?;"); + PreparedStatement pst = con.prepareStatement(new Query(this) + .delete() + .from(tableName) + .addWhere(columnName + "=?", null) + .build() + .getQuery()); for (String name : banned) { pst.setString(1, name); pst.executeUpdate(); @@ -981,9 +1050,12 @@ public class MySQL implements DataSource { public boolean isLogged(String user) { boolean isLogged = false; try (Connection con = getConnection()) { - String sql = "SELECT " + columnLogged + " FROM " + tableName + " WHERE " + columnName + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); - pst.setString(1, user); + PreparedStatement pst = con.prepareStatement(new Query(this) + .select(columnLogged) + .from(tableName) + .addWhere(columnName + "='" + user + "'", null) + .build() + .getQuery()); ResultSet rs = pst.executeQuery(); isLogged = rs.next() && (rs.getInt(columnLogged) == 1); } catch (SQLException ex) { @@ -1003,10 +1075,13 @@ public class MySQL implements DataSource { @Override public void setLogged(String user) { try (Connection con = getConnection()) { - String sql = "UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnName + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); - pst.setInt(1, 1); - pst.setString(2, user.toLowerCase()); + PreparedStatement pst = con.prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnLogged + "=" + 1) + .addWhere(columnName + "='" + user.toLowerCase() + "'", null) + .build() + .getQuery()); pst.executeUpdate(); pst.close(); } catch (SQLException ex) { @@ -1025,10 +1100,13 @@ public class MySQL implements DataSource { @Override public void setUnlogged(String user) { try (Connection con = getConnection()) { - String sql = "UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnName + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); - pst.setInt(1, 0); - pst.setString(2, user.toLowerCase()); + PreparedStatement pst = con.prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnLogged + "=" + 0) + .addWhere(columnName + "='" + user.toLowerCase() + "'", null) + .build() + .getQuery()); pst.executeUpdate(); pst.close(); } catch (SQLException ex) { @@ -1045,10 +1123,13 @@ public class MySQL implements DataSource { @Override public void purgeLogged() { try (Connection con = getConnection()) { - String sql = "UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnLogged + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); - pst.setInt(1, 0); - pst.setInt(2, 1); + PreparedStatement pst = con.prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnLogged + "=" + 0) + .addWhere(columnLogged + "=" + 1, null) + .build() + .getQuery()); pst.executeUpdate(); pst.close(); } catch (Exception ex) { @@ -1068,8 +1149,12 @@ public class MySQL implements DataSource { public int getAccountsRegistered() { int result = 0; try (Connection con = getConnection()) { - Statement st = con.createStatement(); - ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM " + tableName); + PreparedStatement st = con.prepareStatement(new Query(this) + .select("COUNT(*)") + .from(tableName) + .build() + .getQuery()); + ResultSet rs = st.executeQuery(); if (rs.next()) { result = rs.getInt(1); } @@ -1093,11 +1178,16 @@ public class MySQL implements DataSource { @Override public void updateName(String oldOne, String newOne) { try (Connection con = getConnection()) { - String sql = "UPDATE " + tableName + " SET " + columnName + "=? WHERE " + columnName + "=?;"; - PreparedStatement pst = con.prepareStatement(sql); - pst.setString(1, newOne); - pst.setString(2, oldOne); + PreparedStatement pst = + con.prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnName + "='" + newOne + "'") + .addWhere(columnName + "='" + oldOne + "'", null) + .build() + .getQuery()); pst.executeUpdate(); + pst.close(); } catch (Exception ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -1115,9 +1205,19 @@ public class MySQL implements DataSource { public List getAllAuths() { List auths = new ArrayList<>(); try (Connection con = getConnection()) { - Statement st = con.createStatement(); - ResultSet rs = st.executeQuery("SELECT * FROM " + tableName); - PreparedStatement pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + columnID + "=?;"); + PreparedStatement st = con.prepareStatement(new Query(this) + .select("*") + .from(tableName) + .build() + .getQuery()); + ResultSet rs = st + .executeQuery(); + PreparedStatement pst = con.prepareStatement(new Query(this) + .select("data") + .from("xf_user_authenticate") + .addWhere(columnID + "=?", null) + .build() + .getQuery()); while (rs.next()) { String salt = !columnSalt.isEmpty() ? rs.getString(columnSalt) : ""; int group = !salt.isEmpty() && !columnGroup.isEmpty() ? rs.getInt(columnGroup) : -1; diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java index 9c4301ae..b753ba24 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -2,6 +2,8 @@ package fr.xephi.authme.datasource; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.datasource.queries.Query; +import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.settings.Settings; import java.sql.*; @@ -74,6 +76,23 @@ public class SQLite implements DataSource { } + private synchronized void reconnect() throws ClassNotFoundException, SQLException { + Class.forName("org.sqlite.JDBC"); + this.con = DriverManager.getConnection("jdbc:sqlite:plugins/AuthMe/" + database + ".db"); + } + + @Override + public synchronized Connection getConnection() throws SQLException + { + if (this.con.isClosed()) + try { + reconnect(); + } catch (ClassNotFoundException e) { + ConsoleLogger.writeStackTrace(e); + } + return this.con; + } + /** * Method setup. * @@ -145,7 +164,12 @@ public class SQLite implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);"); + pst = getConnection().prepareStatement(new Query(this) + .select("*") + .from(tableName) + .addWhere("LOWER(" + columnName + ")=LOWER(?)", null) + .build() + .getQuery()); pst.setString(1, user); rs = pst.executeQuery(); return rs.next(); @@ -170,7 +194,12 @@ public class SQLite implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);"); + pst = getConnection().prepareStatement(new Query(this) + .select("*") + .from(tableName) + .addWhere("LOWER(" + columnName + ")=LOWER(?)", null) + .build() + .getQuery()); pst.setString(1, user); rs = pst.executeQuery(); if (rs.next()) { @@ -242,8 +271,14 @@ public class SQLite implements DataSource { */ @Override public synchronized boolean updatePassword(PlayerAuth auth) { - PreparedStatement pst = null; try { + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnPassword + "=?") + .addWhere(columnName + "=?", null) + .build() + .getQuery()); pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnPassword + "=? WHERE " + columnName + "=?;"); pst.setString(1, auth.getHash()); pst.setString(2, auth.getNickname()); @@ -251,8 +286,6 @@ public class SQLite implements DataSource { } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); return false; - } finally { - close(pst); } return true; } @@ -262,25 +295,32 @@ public class SQLite implements DataSource { * * @param auth PlayerAuth * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth) */ @Override - public boolean updateSession(PlayerAuth auth) { - PreparedStatement pst = null; + public synchronized boolean updateSession(PlayerAuth auth) { try { - pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnIp + "=?, " + columnLastLogin + "=?, " + columnRealName + "=? WHERE " + columnName + "=?;"); + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnIp + "=?") + .addUpdateSet(columnLastLogin + "=?") + .addUpdateSet(columnRealName + "=?") + .addWhere(columnName + "=?", null) + .build() + .getQuery()); pst.setString(1, auth.getIp()); pst.setLong(2, auth.getLastLogin()); pst.setString(3, auth.getRealName()); pst.setString(4, auth.getNickname()); pst.executeUpdate(); + return true; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - close(pst); } - return true; + return false; } /** @@ -288,22 +328,27 @@ public class SQLite implements DataSource { * * @param until long * - * @return int * @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long) + * @return int + * + * @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long) */ @Override - public int purgeDatabase(long until) { - PreparedStatement pst = null; + public synchronized int purgeDatabase(long until) { + int result = 0; try { - - pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + columnLastLogin + " * @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long) + * @return List + * + * @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long) */ @Override - public List autoPurgeDatabase(long until) { - PreparedStatement pst = null; - ResultSet rs = null; + public synchronized List autoPurgeDatabase(long until) { List list = new ArrayList<>(); try { - pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + "(); - } finally { - close(rs); - close(pst); + ConsoleLogger.writeStackTrace(ex); } + return list; } /** @@ -363,26 +417,35 @@ public class SQLite implements DataSource { * * @param auth PlayerAuth * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth) */ @Override - public boolean updateQuitLoc(PlayerAuth auth) { - PreparedStatement pst = null; + public synchronized boolean updateQuitLoc(PlayerAuth auth) { try { - pst = con.prepareStatement("UPDATE " + tableName + " SET " + lastlocX + "=?, " + lastlocY + "=?, " + lastlocZ + "=?, " + lastlocWorld + "=? WHERE " + columnName + "=?;"); + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(lastlocX + "=?") + .addUpdateSet(lastlocY + "=?") + .addUpdateSet(lastlocZ + "=?") + .addUpdateSet(lastlocWorld + "=?") + .addWhere(columnName + "=?", null) + .build() + .getQuery()); pst.setDouble(1, auth.getQuitLocX()); pst.setDouble(2, auth.getQuitLocY()); pst.setDouble(3, auth.getQuitLocZ()); pst.setString(4, auth.getWorld()); pst.setString(5, auth.getNickname()); pst.executeUpdate(); + return true; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - close(pst); + ConsoleLogger.writeStackTrace(ex); } - return true; + return false; } /** @@ -390,28 +453,31 @@ public class SQLite implements DataSource { * * @param ip String * - * @return int * @see fr.xephi.authme.datasource.DataSource#getIps(String) + * @return int + * + * @see fr.xephi.authme.datasource.DataSource#getIps(String) */ @Override - public int getIps(String ip) { - PreparedStatement pst = null; - ResultSet rs = null; + public synchronized int getIps(String ip) { int countIp = 0; try { - pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .select("COUNT(*)") + .from(tableName) + .addWhere(columnIp + "=?", null) + .build() + .getQuery()); pst.setString(1, ip); - rs = pst.executeQuery(); + ResultSet rs = pst.executeQuery(); while (rs.next()) { - countIp++; + countIp = rs.getInt(1); } - return countIp; + rs.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - return 0; - } finally { - close(rs); - close(pst); + ConsoleLogger.writeStackTrace(ex); } + return countIp; } /** @@ -419,23 +485,29 @@ public class SQLite implements DataSource { * * @param auth PlayerAuth * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth) */ @Override - public boolean updateEmail(PlayerAuth auth) { - PreparedStatement pst = null; + public synchronized boolean updateEmail(PlayerAuth auth) { try { - pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnEmail + "=? WHERE " + columnName + "=?;"); + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnEmail + "=?") + .addWhere(columnName + "=?", null) + .build() + .getQuery()); pst.setString(1, auth.getEmail()); pst.setString(2, auth.getNickname()); pst.executeUpdate(); + return true; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - close(pst); + ConsoleLogger.writeStackTrace(ex); } - return true; + return false; } /** @@ -443,26 +515,32 @@ public class SQLite implements DataSource { * * @param auth PlayerAuth * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth) */ @Override - public boolean updateSalt(PlayerAuth auth) { + public synchronized boolean updateSalt(PlayerAuth auth) { if (columnSalt.isEmpty()) { return false; } - PreparedStatement pst = null; try { - pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnSalt + "=? WHERE " + columnName + "=?;"); + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnSalt + "=?") + .addWhere(columnName + "=?", null) + .build() + .getQuery()); pst.setString(1, auth.getSalt()); pst.setString(2, auth.getNickname()); pst.executeUpdate(); + return true; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - close(pst); + ConsoleLogger.writeStackTrace(ex); } - return true; + return false; } /** @@ -523,30 +601,30 @@ public class SQLite implements DataSource { * * @param auth PlayerAuth * - * @return List * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth) + * @return List + * + * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth) */ @Override - public List getAllAuthsByName(PlayerAuth auth) { - PreparedStatement pst = null; - ResultSet rs = null; - List countIp = new ArrayList<>(); - try { - pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); - pst.setString(1, auth.getIp()); - rs = pst.executeQuery(); + public synchronized List getAllAuthsByName(PlayerAuth auth) { + List result = new ArrayList<>(); + try (Connection con = getConnection()) { + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .select(columnName) + .from(tableName) + .addWhere(columnIp + "='" + auth.getIp() + "'", null) + .build() + .getQuery()); + ResultSet rs = pst.executeQuery(); while (rs.next()) { - countIp.add(rs.getString(columnName)); + result.add(rs.getString(columnName)); } - return countIp; + rs.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - return new ArrayList<>(); - } catch (NullPointerException npe) { - return new ArrayList<>(); - } finally { - close(rs); - close(pst); + ConsoleLogger.writeStackTrace(ex); } + return result; } /** @@ -554,30 +632,30 @@ public class SQLite implements DataSource { * * @param ip String * - * @return List * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String) + * @return List + * + * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String) */ @Override - public List getAllAuthsByIp(String ip) { - PreparedStatement pst = null; - ResultSet rs = null; - List countIp = new ArrayList<>(); + public synchronized List getAllAuthsByIp(String ip) { + List result = new ArrayList<>(); try { - pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); - pst.setString(1, ip); - rs = pst.executeQuery(); + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .select(columnName) + .from(tableName) + .addWhere(columnIp + "='" + ip + "'", null) + .build() + .getQuery()); + ResultSet rs = pst.executeQuery(); while (rs.next()) { - countIp.add(rs.getString(columnName)); + result.add(rs.getString(columnName)); } - return countIp; + rs.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - return new ArrayList<>(); - } catch (NullPointerException npe) { - return new ArrayList<>(); - } finally { - close(rs); - close(pst); + ConsoleLogger.writeStackTrace(ex); } + return result; } /** @@ -585,30 +663,30 @@ public class SQLite implements DataSource { * * @param email String * - * @return List * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String) + * @return List + * + * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String) */ @Override - public List getAllAuthsByEmail(String email) { - PreparedStatement pst = null; - ResultSet rs = null; + public synchronized List getAllAuthsByEmail(String email){ List countEmail = new ArrayList<>(); try { - pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;"); - pst.setString(1, email); - rs = pst.executeQuery(); + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .select(columnName) + .from(tableName) + .addWhere(columnEmail + "='" + email + "'", null) + .build() + .getQuery()); + ResultSet rs = pst.executeQuery(); while (rs.next()) { countEmail.add(rs.getString(columnName)); } - return countEmail; + rs.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - return new ArrayList<>(); - } catch (NullPointerException npe) { - return new ArrayList<>(); - } finally { - close(rs); - close(pst); + ConsoleLogger.writeStackTrace(ex); } + return countEmail; } /** @@ -616,21 +694,24 @@ public class SQLite implements DataSource { * * @param banned List * - * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List) + * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List) */ @Override - public void purgeBanned(List banned) { - PreparedStatement pst = null; + public synchronized void purgeBanned(List banned) { try { + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .delete() + .from(tableName) + .addWhere(columnName + "=?", null) + .build() + .getQuery()); for (String name : banned) { - pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + columnName + "=?;"); pst.setString(1, name); pst.executeUpdate(); } } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - } finally { - close(pst); + ConsoleLogger.writeStackTrace(ex); } } @@ -653,22 +734,21 @@ public class SQLite implements DataSource { */ @Override public boolean isLogged(String user) { - PreparedStatement pst = null; - ResultSet rs = null; + boolean isLogged = false; try { - pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=?;"); - pst.setString(1, user); - rs = pst.executeQuery(); - if (rs.next()) - return (rs.getInt(columnLogged) == 1); + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .select(columnLogged) + .from(tableName) + .addWhere(columnName + "='" + user + "'", null) + .build() + .getQuery()); + ResultSet rs = pst.executeQuery(); + isLogged = rs.next() && (rs.getInt(columnLogged) == 1); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - close(rs); - close(pst); + ConsoleLogger.writeStackTrace(ex); } - return false; + return isLogged; } /** @@ -680,16 +760,18 @@ public class SQLite implements DataSource { */ @Override public void setLogged(String user) { - PreparedStatement pst = null; try { - pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE LOWER(" + columnName + ")=?;"); - pst.setInt(1, 1); - pst.setString(2, user); + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnLogged + "='1'") + .addWhere(columnName + "='" + user.toLowerCase() + "'", null) + .build() + .getQuery()); pst.executeUpdate(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - } finally { - close(pst); + ConsoleLogger.writeStackTrace(ex); } } @@ -702,18 +784,19 @@ public class SQLite implements DataSource { */ @Override public void setUnlogged(String user) { - PreparedStatement pst = null; - if (user != null) - try { - pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE LOWER(" + columnName + ")=?;"); - pst.setInt(1, 0); - pst.setString(2, user); - pst.executeUpdate(); - } catch (SQLException ex) { - ConsoleLogger.showError(ex.getMessage()); - } finally { - close(pst); - } + try { + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnLogged + "='0'") + .addWhere(columnName + "='" + user.toLowerCase() + "'", null) + .build() + .getQuery()); + pst.executeUpdate(); + } catch (SQLException ex) { + ConsoleLogger.showError(ex.getMessage()); + ConsoleLogger.writeStackTrace(ex); + } } /** @@ -723,40 +806,45 @@ public class SQLite implements DataSource { */ @Override public void purgeLogged() { - PreparedStatement pst = null; try { - pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnLogged + "=?;"); - pst.setInt(1, 0); - pst.setInt(2, 1); + PreparedStatement pst = getConnection().prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnLogged + "='0'") + .addWhere(columnLogged + "='1'", null) + .build() + .getQuery()); pst.executeUpdate(); - } catch (SQLException ex) { + } catch (Exception ex) { ConsoleLogger.showError(ex.getMessage()); - } finally { - close(pst); + ConsoleLogger.writeStackTrace(ex); } } /** * Method getAccountsRegistered. * - * @return int * @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered() + * @return int + * + * @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered() */ @Override public int getAccountsRegistered() { int result = 0; - PreparedStatement pst = null; - ResultSet rs; try { - pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";"); - rs = pst.executeQuery(); - if (rs != null && rs.next()) { + PreparedStatement st = getConnection().prepareStatement(new Query(this) + .select("COUNT(*)") + .from(tableName) + .build() + .getQuery()); + ResultSet rs = st.executeQuery(); + if (rs.next()) { result = rs.getInt(1); } - } catch (SQLException ex) { + rs.close(); + } catch (Exception ex) { ConsoleLogger.showError(ex.getMessage()); - return result; - } finally { - close(pst); + ConsoleLogger.writeStackTrace(ex); } return result; } @@ -771,50 +859,63 @@ public class SQLite implements DataSource { */ @Override public void updateName(String oldOne, String newOne) { - PreparedStatement pst = null; try { - pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnName + "=? WHERE " + columnName + "=?;"); - pst.setString(1, newOne); - pst.setString(2, oldOne); + PreparedStatement pst = + getConnection().prepareStatement(new Query(this) + .update() + .from(tableName) + .addUpdateSet(columnName + "='" + newOne + "'") + .addWhere(columnName + "='" + oldOne + "'", null) + .build() + .getQuery()); pst.executeUpdate(); - } catch (SQLException ex) { + } catch (Exception ex) { ConsoleLogger.showError(ex.getMessage()); - } finally { - close(pst); + ConsoleLogger.writeStackTrace(ex); } } /** * Method getAllAuths. * - * @return List * @see fr.xephi.authme.datasource.DataSource#getAllAuths() + * @return List + * + * @see fr.xephi.authme.datasource.DataSource#getAllAuths() */ @Override public List getAllAuths() { List auths = new ArrayList<>(); - PreparedStatement pst = null; - ResultSet rs; try { - pst = con.prepareStatement("SELECT * FROM " + tableName + ";"); - rs = pst.executeQuery(); + PreparedStatement st = getConnection().prepareStatement(new Query(this) + .select("*") + .from(tableName) + .build() + .getQuery()); + ResultSet rs = st + .executeQuery(); while (rs.next()) { - PlayerAuth pAuth; - if (rs.getString(columnIp).isEmpty()) { - pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "127.0.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); - } else { - if (!columnSalt.isEmpty()) { - pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnSalt), rs.getInt(columnGroup), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); - } else { - pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); - } - } + String salt = !columnSalt.isEmpty() ? rs.getString(columnSalt) : ""; + int group = !salt.isEmpty() && !columnGroup.isEmpty() ? rs.getInt(columnGroup) : -1; + PlayerAuth pAuth = PlayerAuth.builder() + .name(rs.getString(columnName)) + .realName(rs.getString(columnRealName)) + .hash(rs.getString(columnPassword)) + .lastLogin(rs.getLong(columnLastLogin)) + .ip(rs.getString(columnIp)) + .locWorld(rs.getString(lastlocWorld)) + .locX(rs.getDouble(lastlocX)) + .locY(rs.getDouble(lastlocY)) + .locZ(rs.getDouble(lastlocZ)) + .email(rs.getString(columnEmail)) + .salt(salt) + .groupId(group) + .build(); auths.add(pAuth); } - } catch (SQLException ex) { + rs.close(); + } catch (Exception ex) { ConsoleLogger.showError(ex.getMessage()); - return auths; - } finally { - close(pst); + ConsoleLogger.writeStackTrace(ex); } return auths; } diff --git a/src/main/java/fr/xephi/authme/datasource/queries/Query.java b/src/main/java/fr/xephi/authme/datasource/queries/Query.java new file mode 100644 index 00000000..08511e00 --- /dev/null +++ b/src/main/java/fr/xephi/authme/datasource/queries/Query.java @@ -0,0 +1,215 @@ +package fr.xephi.authme.datasource.queries; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.datasource.DataSource; + +public class Query { + + private DataSource source; + private String selector = null; + private String from = null; + private HashMap where = new HashMap(); + private List into = new ArrayList(); + private List values = new ArrayList(); + private List updateSet = new ArrayList(); + private boolean isSelect = false; + private boolean isDelete = false; + private boolean isUpdate = false; + private boolean isInsert = false; + private String buildQuery = ""; + + /** + * + * @param source + */ + public Query(DataSource source) + { + this.source = source; + } + + /** + * + * @param selector + * @return Query instance + */ + public Query select(String selector) + { + this.selector = selector; + isSelect = true; + isDelete = false; + isUpdate = false; + isInsert = false; + return this; + } + + /** + * + * @return Query instance + */ + public Query update() + { + isSelect = false; + isDelete = false; + isUpdate = true; + isInsert = false; + return this; + } + + /** + * + * @return Query instance + */ + public Query delete() + { + isSelect = false; + isDelete = true; + isUpdate = false; + isInsert = false; + return this; + } + + /** + * + * @param selector + * @return Query instance + */ + public Query insert() + { + isSelect = false; + isDelete = false; + isUpdate = false; + isInsert = true; + return this; + } + + /** + * + * @param column + * @return + */ + public Query addInsertInto(String column) + { + into.add(column); + return this; + } + + /** + * + * @param value + * @return + */ + public Query addInsertValue(String value) + { + values.add(value); + return this; + } + + /** + * + * @param set + * @return + */ + public Query addUpdateSet(String set) + { + updateSet.add(set); + return this; + } + + /** + * + * @param from + * @return Query instance + */ + public Query from(String from) + { + this.from = from; + return this; + } + + /** + * + * @param where + * @param String and/or/null + * @return Query instance + */ + public Query addWhere(String where, String logic) + { + this.where.put(where, logic); + return this; + } + + public Query build(){ + StringBuilder str = new StringBuilder(); + if (isSelect) + { + str.append("SELECT ").append(selector).append(" FROM ").append(from); + } + else if (isDelete) + { + str.append("DELETE FROM ").append(from); + } + else if (isUpdate) + { + str.append("UPDATE ").append(from).append(" SET "); + Iterator iter = updateSet.iterator(); + while (iter.hasNext()) + { + String s = iter.next(); + str.append(s); + if (iter.hasNext()) + str.append(", "); + } + } + else if (isInsert) + { + str.append("INSERT INTO ").append(from).append(" ('"); + Iterator iter = into.iterator(); + while (iter.hasNext()) + { + String s = iter.next(); + str.append(s); + if (iter.hasNext()) + str.append("', '"); + else + str.append("')"); + } + str.append(" VALUES ('"); + iter = values.iterator(); + while (iter.hasNext()) + { + String s = iter.next(); + str.append(s); + if (iter.hasNext()) + str.append("', '"); + else + str.append("')"); + } + } + if (!where.isEmpty()) + { + str.append(" WHERE"); + for (String key : where.keySet()) + { + if (where.get(key) != null) + str.append(" ").append(where.get(key)); + str.append(" ").append(key); + } + } + str.append(";"); + this.buildQuery = str.toString(); + return this; + } + + public String getQuery() { + return this.buildQuery; + } +} From f7010f1d3b8ce719157a738483aa6f1eab867624 Mon Sep 17 00:00:00 2001 From: Xephi Date: Wed, 30 Dec 2015 09:59:28 +0100 Subject: [PATCH 02/10] Change to QueryType --- .../authme/datasource/queries/Query.java | 108 ++++++++---------- .../authme/datasource/queries/QueryType.java | 9 ++ 2 files changed, 54 insertions(+), 63 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/datasource/queries/QueryType.java diff --git a/src/main/java/fr/xephi/authme/datasource/queries/Query.java b/src/main/java/fr/xephi/authme/datasource/queries/Query.java index 08511e00..36260416 100644 --- a/src/main/java/fr/xephi/authme/datasource/queries/Query.java +++ b/src/main/java/fr/xephi/authme/datasource/queries/Query.java @@ -1,15 +1,10 @@ package fr.xephi.authme.datasource.queries; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; -import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.datasource.DataSource; public class Query { @@ -21,10 +16,7 @@ public class Query { private List into = new ArrayList(); private List values = new ArrayList(); private List updateSet = new ArrayList(); - private boolean isSelect = false; - private boolean isDelete = false; - private boolean isUpdate = false; - private boolean isInsert = false; + private QueryType type; private String buildQuery = ""; /** @@ -44,10 +36,7 @@ public class Query { public Query select(String selector) { this.selector = selector; - isSelect = true; - isDelete = false; - isUpdate = false; - isInsert = false; + type = QueryType.SELECT; return this; } @@ -57,10 +46,7 @@ public class Query { */ public Query update() { - isSelect = false; - isDelete = false; - isUpdate = true; - isInsert = false; + type = QueryType.UPDATE; return this; } @@ -70,10 +56,7 @@ public class Query { */ public Query delete() { - isSelect = false; - isDelete = true; - isUpdate = false; - isInsert = false; + type = QueryType.DELETE; return this; } @@ -84,10 +67,7 @@ public class Query { */ public Query insert() { - isSelect = false; - isDelete = false; - isUpdate = false; - isInsert = true; + type = QueryType.INSERT; return this; } @@ -149,49 +129,51 @@ public class Query { public Query build(){ StringBuilder str = new StringBuilder(); - if (isSelect) - { - str.append("SELECT ").append(selector).append(" FROM ").append(from); - } - else if (isDelete) - { - str.append("DELETE FROM ").append(from); - } - else if (isUpdate) - { - str.append("UPDATE ").append(from).append(" SET "); - Iterator iter = updateSet.iterator(); - while (iter.hasNext()) + switch (type) { + case SELECT: { - String s = iter.next(); - str.append(s); - if (iter.hasNext()) - str.append(", "); + str.append("SELECT ").append(selector).append(" FROM ").append(from); } - } - else if (isInsert) - { - str.append("INSERT INTO ").append(from).append(" ('"); - Iterator iter = into.iterator(); - while (iter.hasNext()) + case DELETE: { - String s = iter.next(); - str.append(s); - if (iter.hasNext()) - str.append("', '"); - else - str.append("')"); + str.append("DELETE FROM ").append(from); } - str.append(" VALUES ('"); - iter = values.iterator(); - while (iter.hasNext()) + case UPDATE: { - String s = iter.next(); - str.append(s); - if (iter.hasNext()) - str.append("', '"); - else - str.append("')"); + str.append("UPDATE ").append(from).append(" SET "); + Iterator iter = updateSet.iterator(); + while (iter.hasNext()) + { + String s = iter.next(); + str.append(s); + if (iter.hasNext()) + str.append(", "); + } + } + case INSERT: + { + str.append("INSERT INTO ").append(from).append(" ('"); + Iterator iter = into.iterator(); + while (iter.hasNext()) + { + String s = iter.next(); + str.append(s); + if (iter.hasNext()) + str.append("', '"); + else + str.append("')"); + } + str.append(" VALUES ('"); + iter = values.iterator(); + while (iter.hasNext()) + { + String s = iter.next(); + str.append(s); + if (iter.hasNext()) + str.append("', '"); + else + str.append("')"); + } } } if (!where.isEmpty()) diff --git a/src/main/java/fr/xephi/authme/datasource/queries/QueryType.java b/src/main/java/fr/xephi/authme/datasource/queries/QueryType.java new file mode 100644 index 00000000..1f1575ed --- /dev/null +++ b/src/main/java/fr/xephi/authme/datasource/queries/QueryType.java @@ -0,0 +1,9 @@ +package fr.xephi.authme.datasource.queries; + +public enum QueryType { + + DELETE, + INSERT, + SELECT, + UPDATE; +} From 14e130eaee89382e40e3c5b88f475bf2ceba72ea Mon Sep 17 00:00:00 2001 From: Xephi Date: Wed, 30 Dec 2015 12:03:48 +0100 Subject: [PATCH 03/10] Pass args through PreparedStatement --- .../fr/xephi/authme/datasource/MySQL.java | 55 ++++++++++--------- .../fr/xephi/authme/datasource/SQLite.java | 28 ++++++---- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 713901bf..881747c2 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -919,18 +919,18 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByName(PlayerAuth auth) { List result = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnIp + "='" + auth.getIp() + "'", null) + .addWhere(columnIp + "=?", null) .build() .getQuery()); + pst.setString(1, auth.getIp()); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); } rs.close(); - pst.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -950,19 +950,19 @@ public class MySQL implements DataSource { @Override public synchronized List getAllAuthsByIp(String ip) { List result = new ArrayList<>(); - try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + try { + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnIp + "='" + ip + "'", null) + .addWhere(columnIp + "=?", null) .build() .getQuery()); + pst.setString(1, ip); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); } rs.close(); - pst.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -982,19 +982,19 @@ public class MySQL implements DataSource { @Override public synchronized List getAllAuthsByEmail(String email){ List countEmail = new ArrayList<>(); - try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + try { + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnEmail + "='" + email + "'", null) + .addWhere(columnEmail + "=?", null) .build() .getQuery()); + pst.setString(1, email); ResultSet rs = pst.executeQuery(); while (rs.next()) { countEmail.add(rs.getString(columnName)); } rs.close(); - pst.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -1049,13 +1049,14 @@ public class MySQL implements DataSource { @Override public boolean isLogged(String user) { boolean isLogged = false; - try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + try { + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnLogged) .from(tableName) - .addWhere(columnName + "='" + user + "'", null) + .addWhere(columnName + "=?", null) .build() .getQuery()); + pst.setString(1, user); ResultSet rs = pst.executeQuery(); isLogged = rs.next() && (rs.getInt(columnLogged) == 1); } catch (SQLException ex) { @@ -1074,16 +1075,16 @@ public class MySQL implements DataSource { */ @Override public void setLogged(String user) { - try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + try { + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) - .addUpdateSet(columnLogged + "=" + 1) - .addWhere(columnName + "='" + user.toLowerCase() + "'", null) + .addUpdateSet(columnLogged + "='1'") + .addWhere(columnName + "=?", null) .build() .getQuery()); + pst.setString(1, user.toLowerCase()); pst.executeUpdate(); - pst.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -1099,16 +1100,16 @@ public class MySQL implements DataSource { */ @Override public void setUnlogged(String user) { - try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + try { + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) - .addUpdateSet(columnLogged + "=" + 0) - .addWhere(columnName + "='" + user.toLowerCase() + "'", null) + .addUpdateSet(columnLogged + "='0'") + .addWhere(columnName + "=?", null) .build() .getQuery()); + pst.setString(1, user.toLowerCase()); pst.executeUpdate(); - pst.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -1182,10 +1183,12 @@ public class MySQL implements DataSource { con.prepareStatement(new Query(this) .update() .from(tableName) - .addUpdateSet(columnName + "='" + newOne + "'") - .addWhere(columnName + "='" + oldOne + "'", null) + .addUpdateSet(columnName + "=?") + .addWhere(columnName + "=?", null) .build() .getQuery()); + pst.setString(1, newOne); + pst.setString(2, oldOne); pst.executeUpdate(); pst.close(); } catch (Exception ex) { diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java index b753ba24..e541baf5 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -612,9 +612,10 @@ public class SQLite implements DataSource { PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnIp + "='" + auth.getIp() + "'", null) + .addWhere(columnIp + "=?", null) .build() .getQuery()); + pst.setString(1, auth.getIp()); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); @@ -643,9 +644,10 @@ public class SQLite implements DataSource { PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnIp + "='" + ip + "'", null) + .addWhere(columnIp + "=?", null) .build() .getQuery()); + pst.setString(1, ip); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); @@ -674,9 +676,10 @@ public class SQLite implements DataSource { PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnEmail + "='" + email + "'", null) + .addWhere(columnEmail + "=?", null) .build() .getQuery()); + pst.setString(1, email); ResultSet rs = pst.executeQuery(); while (rs.next()) { countEmail.add(rs.getString(columnName)); @@ -739,9 +742,10 @@ public class SQLite implements DataSource { PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnLogged) .from(tableName) - .addWhere(columnName + "='" + user + "'", null) + .addWhere(columnName + "=?", null) .build() .getQuery()); + pst.setString(1, user); ResultSet rs = pst.executeQuery(); isLogged = rs.next() && (rs.getInt(columnLogged) == 1); } catch (SQLException ex) { @@ -765,9 +769,10 @@ public class SQLite implements DataSource { .update() .from(tableName) .addUpdateSet(columnLogged + "='1'") - .addWhere(columnName + "='" + user.toLowerCase() + "'", null) + .addWhere(columnName + "=?", null) .build() .getQuery()); + pst.setString(1, user.toLowerCase()); pst.executeUpdate(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); @@ -789,9 +794,10 @@ public class SQLite implements DataSource { .update() .from(tableName) .addUpdateSet(columnLogged + "='0'") - .addWhere(columnName + "='" + user.toLowerCase() + "'", null) + .addWhere(columnName + "=?", null) .build() .getQuery()); + pst.setString(1, user.toLowerCase()); pst.executeUpdate(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); @@ -859,15 +865,17 @@ public class SQLite implements DataSource { */ @Override public void updateName(String oldOne, String newOne) { - try { + try (Connection con = getConnection()) { PreparedStatement pst = - getConnection().prepareStatement(new Query(this) + con.prepareStatement(new Query(this) .update() .from(tableName) - .addUpdateSet(columnName + "='" + newOne + "'") - .addWhere(columnName + "='" + oldOne + "'", null) + .addUpdateSet(columnName + "=?") + .addWhere(columnName + "=?", null) .build() .getQuery()); + pst.setString(1, newOne); + pst.setString(2, oldOne); pst.executeUpdate(); } catch (Exception ex) { ConsoleLogger.showError(ex.getMessage()); From dbacf7754dc930896af4569b6b0aa6068b0ab238 Mon Sep 17 00:00:00 2001 From: Xephi Date: Wed, 30 Dec 2015 12:05:24 +0100 Subject: [PATCH 04/10] Remove source from constructor --- .../fr/xephi/authme/datasource/MySQL.java | 46 +++++++++---------- .../fr/xephi/authme/datasource/SQLite.java | 44 +++++++++--------- .../authme/datasource/queries/Query.java | 4 +- 3 files changed, 46 insertions(+), 48 deletions(-) diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 881747c2..a2c503f8 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -256,7 +256,7 @@ public class MySQL implements DataSource { @Override public synchronized boolean isAuthAvailable(String user) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query() .select(columnName) .from(tableName) .addWhere(columnName + "=?", null) @@ -283,7 +283,7 @@ public class MySQL implements DataSource { public synchronized PlayerAuth getAuth(String user) { PlayerAuth pAuth; try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query() .select("*") .from(tableName) .addWhere(columnName + "=?", null) @@ -314,7 +314,7 @@ public class MySQL implements DataSource { rs.close(); pst.close(); if (Settings.getPasswordHash == HashAlgorithm.XENFORO) { - pst = con.prepareStatement(new Query(this) + pst = con.prepareStatement(new Query() .select("data") .from("xf_user_authenticate") .addWhere(columnID + "=?", null) @@ -611,7 +611,7 @@ public class MySQL implements DataSource { @Override public synchronized boolean updateSession(PlayerAuth auth) { try(Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnIp + "=?") @@ -646,7 +646,7 @@ public class MySQL implements DataSource { public synchronized int purgeDatabase(long until) { int result = 0; try(Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query() .delete() .from(tableName) .addWhere(columnLastLogin + " autoPurgeDatabase(long until) { List list = new ArrayList<>(); try(Connection con = getConnection()) { - PreparedStatement st = con.prepareStatement(new Query(this) + PreparedStatement st = con.prepareStatement(new Query() .select(columnName) .from(tableName) .addWhere(columnLastLogin + "<" + until, null) @@ -686,7 +686,7 @@ public class MySQL implements DataSource { } rs.close(); st.close(); - st = con.prepareStatement(new Query(this) + st = con.prepareStatement(new Query() .delete() .from(tableName) .addWhere(columnLastLogin + "<" + until, null) @@ -754,7 +754,7 @@ public class MySQL implements DataSource { @Override public synchronized boolean updateQuitLoc(PlayerAuth auth) { try(Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(lastlocX + "=?") @@ -792,7 +792,7 @@ public class MySQL implements DataSource { public synchronized int getIps(String ip) { int countIp = 0; try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query() .select("COUNT(*)") .from(tableName) .addWhere(columnIp + "=?", null) @@ -824,7 +824,7 @@ public class MySQL implements DataSource { @Override public synchronized boolean updateEmail(PlayerAuth auth) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnEmail + "=?") @@ -858,7 +858,7 @@ public class MySQL implements DataSource { return false; } try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnSalt + "=?") @@ -919,7 +919,7 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByName(PlayerAuth auth) { List result = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .select(columnName) .from(tableName) .addWhere(columnIp + "=?", null) @@ -951,7 +951,7 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByIp(String ip) { List result = new ArrayList<>(); try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .select(columnName) .from(tableName) .addWhere(columnIp + "=?", null) @@ -983,7 +983,7 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByEmail(String email){ List countEmail = new ArrayList<>(); try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .select(columnName) .from(tableName) .addWhere(columnEmail + "=?", null) @@ -1012,7 +1012,7 @@ public class MySQL implements DataSource { @Override public synchronized void purgeBanned(List banned) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query() .delete() .from(tableName) .addWhere(columnName + "=?", null) @@ -1050,7 +1050,7 @@ public class MySQL implements DataSource { public boolean isLogged(String user) { boolean isLogged = false; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .select(columnLogged) .from(tableName) .addWhere(columnName + "=?", null) @@ -1076,7 +1076,7 @@ public class MySQL implements DataSource { @Override public void setLogged(String user) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnLogged + "='1'") @@ -1101,7 +1101,7 @@ public class MySQL implements DataSource { @Override public void setUnlogged(String user) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnLogged + "='0'") @@ -1124,7 +1124,7 @@ public class MySQL implements DataSource { @Override public void purgeLogged() { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnLogged + "=" + 0) @@ -1150,7 +1150,7 @@ public class MySQL implements DataSource { public int getAccountsRegistered() { int result = 0; try (Connection con = getConnection()) { - PreparedStatement st = con.prepareStatement(new Query(this) + PreparedStatement st = con.prepareStatement(new Query() .select("COUNT(*)") .from(tableName) .build() @@ -1180,7 +1180,7 @@ public class MySQL implements DataSource { public void updateName(String oldOne, String newOne) { try (Connection con = getConnection()) { PreparedStatement pst = - con.prepareStatement(new Query(this) + con.prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnName + "=?") @@ -1208,14 +1208,14 @@ public class MySQL implements DataSource { public List getAllAuths() { List auths = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement st = con.prepareStatement(new Query(this) + PreparedStatement st = con.prepareStatement(new Query() .select("*") .from(tableName) .build() .getQuery()); ResultSet rs = st .executeQuery(); - PreparedStatement pst = con.prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query() .select("data") .from("xf_user_authenticate") .addWhere(columnID + "=?", null) diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java index e541baf5..a58a71b6 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -164,7 +164,7 @@ public class SQLite implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - pst = getConnection().prepareStatement(new Query(this) + pst = getConnection().prepareStatement(new Query() .select("*") .from(tableName) .addWhere("LOWER(" + columnName + ")=LOWER(?)", null) @@ -194,7 +194,7 @@ public class SQLite implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - pst = getConnection().prepareStatement(new Query(this) + pst = getConnection().prepareStatement(new Query() .select("*") .from(tableName) .addWhere("LOWER(" + columnName + ")=LOWER(?)", null) @@ -272,7 +272,7 @@ public class SQLite implements DataSource { @Override public synchronized boolean updatePassword(PlayerAuth auth) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnPassword + "=?") @@ -302,7 +302,7 @@ public class SQLite implements DataSource { @Override public synchronized boolean updateSession(PlayerAuth auth) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnIp + "=?") @@ -336,7 +336,7 @@ public class SQLite implements DataSource { public synchronized int purgeDatabase(long until) { int result = 0; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .delete() .from(tableName) .addWhere(columnLastLogin + " autoPurgeDatabase(long until) { List list = new ArrayList<>(); try { - PreparedStatement st = getConnection().prepareStatement(new Query(this) + PreparedStatement st = getConnection().prepareStatement(new Query() .select(columnName) .from(tableName) .addWhere(columnLastLogin + "<" + until, null) @@ -375,7 +375,7 @@ public class SQLite implements DataSource { list.add(rs.getString(columnName)); } rs.close(); - st = getConnection().prepareStatement(new Query(this) + st = getConnection().prepareStatement(new Query() .delete() .from(tableName) .addWhere(columnLastLogin + "<" + until, null) @@ -424,7 +424,7 @@ public class SQLite implements DataSource { @Override public synchronized boolean updateQuitLoc(PlayerAuth auth) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(lastlocX + "=?") @@ -461,7 +461,7 @@ public class SQLite implements DataSource { public synchronized int getIps(String ip) { int countIp = 0; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .select("COUNT(*)") .from(tableName) .addWhere(columnIp + "=?", null) @@ -492,7 +492,7 @@ public class SQLite implements DataSource { @Override public synchronized boolean updateEmail(PlayerAuth auth) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnEmail + "=?") @@ -525,7 +525,7 @@ public class SQLite implements DataSource { return false; } try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnSalt + "=?") @@ -609,7 +609,7 @@ public class SQLite implements DataSource { public synchronized List getAllAuthsByName(PlayerAuth auth) { List result = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .select(columnName) .from(tableName) .addWhere(columnIp + "=?", null) @@ -641,7 +641,7 @@ public class SQLite implements DataSource { public synchronized List getAllAuthsByIp(String ip) { List result = new ArrayList<>(); try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .select(columnName) .from(tableName) .addWhere(columnIp + "=?", null) @@ -673,7 +673,7 @@ public class SQLite implements DataSource { public synchronized List getAllAuthsByEmail(String email){ List countEmail = new ArrayList<>(); try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .select(columnName) .from(tableName) .addWhere(columnEmail + "=?", null) @@ -702,7 +702,7 @@ public class SQLite implements DataSource { @Override public synchronized void purgeBanned(List banned) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .delete() .from(tableName) .addWhere(columnName + "=?", null) @@ -739,7 +739,7 @@ public class SQLite implements DataSource { public boolean isLogged(String user) { boolean isLogged = false; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .select(columnLogged) .from(tableName) .addWhere(columnName + "=?", null) @@ -765,7 +765,7 @@ public class SQLite implements DataSource { @Override public void setLogged(String user) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnLogged + "='1'") @@ -790,7 +790,7 @@ public class SQLite implements DataSource { @Override public void setUnlogged(String user) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnLogged + "='0'") @@ -813,7 +813,7 @@ public class SQLite implements DataSource { @Override public void purgeLogged() { try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = getConnection().prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnLogged + "='0'") @@ -838,7 +838,7 @@ public class SQLite implements DataSource { public int getAccountsRegistered() { int result = 0; try { - PreparedStatement st = getConnection().prepareStatement(new Query(this) + PreparedStatement st = getConnection().prepareStatement(new Query() .select("COUNT(*)") .from(tableName) .build() @@ -867,7 +867,7 @@ public class SQLite implements DataSource { public void updateName(String oldOne, String newOne) { try (Connection con = getConnection()) { PreparedStatement pst = - con.prepareStatement(new Query(this) + con.prepareStatement(new Query() .update() .from(tableName) .addUpdateSet(columnName + "=?") @@ -894,7 +894,7 @@ public class SQLite implements DataSource { public List getAllAuths() { List auths = new ArrayList<>(); try { - PreparedStatement st = getConnection().prepareStatement(new Query(this) + PreparedStatement st = getConnection().prepareStatement(new Query() .select("*") .from(tableName) .build() diff --git a/src/main/java/fr/xephi/authme/datasource/queries/Query.java b/src/main/java/fr/xephi/authme/datasource/queries/Query.java index 36260416..a7db9fd8 100644 --- a/src/main/java/fr/xephi/authme/datasource/queries/Query.java +++ b/src/main/java/fr/xephi/authme/datasource/queries/Query.java @@ -9,7 +9,6 @@ import fr.xephi.authme.datasource.DataSource; public class Query { - private DataSource source; private String selector = null; private String from = null; private HashMap where = new HashMap(); @@ -23,9 +22,8 @@ public class Query { * * @param source */ - public Query(DataSource source) + public Query() { - this.source = source; } /** From 0a2941ed4edf26b6deb36752a1a1695624587775 Mon Sep 17 00:00:00 2001 From: Xephi Date: Wed, 30 Dec 2015 13:14:32 +0100 Subject: [PATCH 05/10] Revert "Remove source from constructor" This reverts commit dbacf7754dc930896af4569b6b0aa6068b0ab238. --- .../fr/xephi/authme/datasource/MySQL.java | 46 +++++++++---------- .../fr/xephi/authme/datasource/SQLite.java | 44 +++++++++--------- .../authme/datasource/queries/Query.java | 4 +- 3 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index a2c503f8..881747c2 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -256,7 +256,7 @@ public class MySQL implements DataSource { @Override public synchronized boolean isAuthAvailable(String user) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query() + PreparedStatement pst = con.prepareStatement(new Query(this) .select(columnName) .from(tableName) .addWhere(columnName + "=?", null) @@ -283,7 +283,7 @@ public class MySQL implements DataSource { public synchronized PlayerAuth getAuth(String user) { PlayerAuth pAuth; try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query() + PreparedStatement pst = con.prepareStatement(new Query(this) .select("*") .from(tableName) .addWhere(columnName + "=?", null) @@ -314,7 +314,7 @@ public class MySQL implements DataSource { rs.close(); pst.close(); if (Settings.getPasswordHash == HashAlgorithm.XENFORO) { - pst = con.prepareStatement(new Query() + pst = con.prepareStatement(new Query(this) .select("data") .from("xf_user_authenticate") .addWhere(columnID + "=?", null) @@ -611,7 +611,7 @@ public class MySQL implements DataSource { @Override public synchronized boolean updateSession(PlayerAuth auth) { try(Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query() + PreparedStatement pst = con.prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnIp + "=?") @@ -646,7 +646,7 @@ public class MySQL implements DataSource { public synchronized int purgeDatabase(long until) { int result = 0; try(Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query() + PreparedStatement pst = con.prepareStatement(new Query(this) .delete() .from(tableName) .addWhere(columnLastLogin + " autoPurgeDatabase(long until) { List list = new ArrayList<>(); try(Connection con = getConnection()) { - PreparedStatement st = con.prepareStatement(new Query() + PreparedStatement st = con.prepareStatement(new Query(this) .select(columnName) .from(tableName) .addWhere(columnLastLogin + "<" + until, null) @@ -686,7 +686,7 @@ public class MySQL implements DataSource { } rs.close(); st.close(); - st = con.prepareStatement(new Query() + st = con.prepareStatement(new Query(this) .delete() .from(tableName) .addWhere(columnLastLogin + "<" + until, null) @@ -754,7 +754,7 @@ public class MySQL implements DataSource { @Override public synchronized boolean updateQuitLoc(PlayerAuth auth) { try(Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query() + PreparedStatement pst = con.prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(lastlocX + "=?") @@ -792,7 +792,7 @@ public class MySQL implements DataSource { public synchronized int getIps(String ip) { int countIp = 0; try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query() + PreparedStatement pst = con.prepareStatement(new Query(this) .select("COUNT(*)") .from(tableName) .addWhere(columnIp + "=?", null) @@ -824,7 +824,7 @@ public class MySQL implements DataSource { @Override public synchronized boolean updateEmail(PlayerAuth auth) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query() + PreparedStatement pst = con.prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnEmail + "=?") @@ -858,7 +858,7 @@ public class MySQL implements DataSource { return false; } try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query() + PreparedStatement pst = con.prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnSalt + "=?") @@ -919,7 +919,7 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByName(PlayerAuth auth) { List result = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) .addWhere(columnIp + "=?", null) @@ -951,7 +951,7 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByIp(String ip) { List result = new ArrayList<>(); try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) .addWhere(columnIp + "=?", null) @@ -983,7 +983,7 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByEmail(String email){ List countEmail = new ArrayList<>(); try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) .addWhere(columnEmail + "=?", null) @@ -1012,7 +1012,7 @@ public class MySQL implements DataSource { @Override public synchronized void purgeBanned(List banned) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query() + PreparedStatement pst = con.prepareStatement(new Query(this) .delete() .from(tableName) .addWhere(columnName + "=?", null) @@ -1050,7 +1050,7 @@ public class MySQL implements DataSource { public boolean isLogged(String user) { boolean isLogged = false; try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnLogged) .from(tableName) .addWhere(columnName + "=?", null) @@ -1076,7 +1076,7 @@ public class MySQL implements DataSource { @Override public void setLogged(String user) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnLogged + "='1'") @@ -1101,7 +1101,7 @@ public class MySQL implements DataSource { @Override public void setUnlogged(String user) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnLogged + "='0'") @@ -1124,7 +1124,7 @@ public class MySQL implements DataSource { @Override public void purgeLogged() { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query() + PreparedStatement pst = con.prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnLogged + "=" + 0) @@ -1150,7 +1150,7 @@ public class MySQL implements DataSource { public int getAccountsRegistered() { int result = 0; try (Connection con = getConnection()) { - PreparedStatement st = con.prepareStatement(new Query() + PreparedStatement st = con.prepareStatement(new Query(this) .select("COUNT(*)") .from(tableName) .build() @@ -1180,7 +1180,7 @@ public class MySQL implements DataSource { public void updateName(String oldOne, String newOne) { try (Connection con = getConnection()) { PreparedStatement pst = - con.prepareStatement(new Query() + con.prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnName + "=?") @@ -1208,14 +1208,14 @@ public class MySQL implements DataSource { public List getAllAuths() { List auths = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement st = con.prepareStatement(new Query() + PreparedStatement st = con.prepareStatement(new Query(this) .select("*") .from(tableName) .build() .getQuery()); ResultSet rs = st .executeQuery(); - PreparedStatement pst = con.prepareStatement(new Query() + PreparedStatement pst = con.prepareStatement(new Query(this) .select("data") .from("xf_user_authenticate") .addWhere(columnID + "=?", null) diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java index a58a71b6..e541baf5 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -164,7 +164,7 @@ public class SQLite implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - pst = getConnection().prepareStatement(new Query() + pst = getConnection().prepareStatement(new Query(this) .select("*") .from(tableName) .addWhere("LOWER(" + columnName + ")=LOWER(?)", null) @@ -194,7 +194,7 @@ public class SQLite implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - pst = getConnection().prepareStatement(new Query() + pst = getConnection().prepareStatement(new Query(this) .select("*") .from(tableName) .addWhere("LOWER(" + columnName + ")=LOWER(?)", null) @@ -272,7 +272,7 @@ public class SQLite implements DataSource { @Override public synchronized boolean updatePassword(PlayerAuth auth) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnPassword + "=?") @@ -302,7 +302,7 @@ public class SQLite implements DataSource { @Override public synchronized boolean updateSession(PlayerAuth auth) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnIp + "=?") @@ -336,7 +336,7 @@ public class SQLite implements DataSource { public synchronized int purgeDatabase(long until) { int result = 0; try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .delete() .from(tableName) .addWhere(columnLastLogin + " autoPurgeDatabase(long until) { List list = new ArrayList<>(); try { - PreparedStatement st = getConnection().prepareStatement(new Query() + PreparedStatement st = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) .addWhere(columnLastLogin + "<" + until, null) @@ -375,7 +375,7 @@ public class SQLite implements DataSource { list.add(rs.getString(columnName)); } rs.close(); - st = getConnection().prepareStatement(new Query() + st = getConnection().prepareStatement(new Query(this) .delete() .from(tableName) .addWhere(columnLastLogin + "<" + until, null) @@ -424,7 +424,7 @@ public class SQLite implements DataSource { @Override public synchronized boolean updateQuitLoc(PlayerAuth auth) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(lastlocX + "=?") @@ -461,7 +461,7 @@ public class SQLite implements DataSource { public synchronized int getIps(String ip) { int countIp = 0; try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select("COUNT(*)") .from(tableName) .addWhere(columnIp + "=?", null) @@ -492,7 +492,7 @@ public class SQLite implements DataSource { @Override public synchronized boolean updateEmail(PlayerAuth auth) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnEmail + "=?") @@ -525,7 +525,7 @@ public class SQLite implements DataSource { return false; } try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnSalt + "=?") @@ -609,7 +609,7 @@ public class SQLite implements DataSource { public synchronized List getAllAuthsByName(PlayerAuth auth) { List result = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) .addWhere(columnIp + "=?", null) @@ -641,7 +641,7 @@ public class SQLite implements DataSource { public synchronized List getAllAuthsByIp(String ip) { List result = new ArrayList<>(); try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) .addWhere(columnIp + "=?", null) @@ -673,7 +673,7 @@ public class SQLite implements DataSource { public synchronized List getAllAuthsByEmail(String email){ List countEmail = new ArrayList<>(); try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) .addWhere(columnEmail + "=?", null) @@ -702,7 +702,7 @@ public class SQLite implements DataSource { @Override public synchronized void purgeBanned(List banned) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .delete() .from(tableName) .addWhere(columnName + "=?", null) @@ -739,7 +739,7 @@ public class SQLite implements DataSource { public boolean isLogged(String user) { boolean isLogged = false; try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnLogged) .from(tableName) .addWhere(columnName + "=?", null) @@ -765,7 +765,7 @@ public class SQLite implements DataSource { @Override public void setLogged(String user) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnLogged + "='1'") @@ -790,7 +790,7 @@ public class SQLite implements DataSource { @Override public void setUnlogged(String user) { try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnLogged + "='0'") @@ -813,7 +813,7 @@ public class SQLite implements DataSource { @Override public void purgeLogged() { try { - PreparedStatement pst = getConnection().prepareStatement(new Query() + PreparedStatement pst = getConnection().prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnLogged + "='0'") @@ -838,7 +838,7 @@ public class SQLite implements DataSource { public int getAccountsRegistered() { int result = 0; try { - PreparedStatement st = getConnection().prepareStatement(new Query() + PreparedStatement st = getConnection().prepareStatement(new Query(this) .select("COUNT(*)") .from(tableName) .build() @@ -867,7 +867,7 @@ public class SQLite implements DataSource { public void updateName(String oldOne, String newOne) { try (Connection con = getConnection()) { PreparedStatement pst = - con.prepareStatement(new Query() + con.prepareStatement(new Query(this) .update() .from(tableName) .addUpdateSet(columnName + "=?") @@ -894,7 +894,7 @@ public class SQLite implements DataSource { public List getAllAuths() { List auths = new ArrayList<>(); try { - PreparedStatement st = getConnection().prepareStatement(new Query() + PreparedStatement st = getConnection().prepareStatement(new Query(this) .select("*") .from(tableName) .build() diff --git a/src/main/java/fr/xephi/authme/datasource/queries/Query.java b/src/main/java/fr/xephi/authme/datasource/queries/Query.java index a7db9fd8..36260416 100644 --- a/src/main/java/fr/xephi/authme/datasource/queries/Query.java +++ b/src/main/java/fr/xephi/authme/datasource/queries/Query.java @@ -9,6 +9,7 @@ import fr.xephi.authme.datasource.DataSource; public class Query { + private DataSource source; private String selector = null; private String from = null; private HashMap where = new HashMap(); @@ -22,8 +23,9 @@ public class Query { * * @param source */ - public Query() + public Query(DataSource source) { + this.source = source; } /** From 668535d93f605208b5bd4bf0edcb2d1609b76a57 Mon Sep 17 00:00:00 2001 From: Xephi Date: Wed, 30 Dec 2015 13:14:41 +0100 Subject: [PATCH 06/10] Revert "Pass args through PreparedStatement" This reverts commit 14e130eaee89382e40e3c5b88f475bf2ceba72ea. --- .../fr/xephi/authme/datasource/MySQL.java | 55 +++++++++---------- .../fr/xephi/authme/datasource/SQLite.java | 28 ++++------ 2 files changed, 36 insertions(+), 47 deletions(-) diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 881747c2..713901bf 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -919,18 +919,18 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByName(PlayerAuth auth) { List result = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + PreparedStatement pst = con.prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnIp + "=?", null) + .addWhere(columnIp + "='" + auth.getIp() + "'", null) .build() .getQuery()); - pst.setString(1, auth.getIp()); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); } rs.close(); + pst.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -950,19 +950,19 @@ public class MySQL implements DataSource { @Override public synchronized List getAllAuthsByIp(String ip) { List result = new ArrayList<>(); - try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + try (Connection con = getConnection()) { + PreparedStatement pst = con.prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnIp + "=?", null) + .addWhere(columnIp + "='" + ip + "'", null) .build() .getQuery()); - pst.setString(1, ip); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); } rs.close(); + pst.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -982,19 +982,19 @@ public class MySQL implements DataSource { @Override public synchronized List getAllAuthsByEmail(String email){ List countEmail = new ArrayList<>(); - try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + try (Connection con = getConnection()) { + PreparedStatement pst = con.prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnEmail + "=?", null) + .addWhere(columnEmail + "='" + email + "'", null) .build() .getQuery()); - pst.setString(1, email); ResultSet rs = pst.executeQuery(); while (rs.next()) { countEmail.add(rs.getString(columnName)); } rs.close(); + pst.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -1049,14 +1049,13 @@ public class MySQL implements DataSource { @Override public boolean isLogged(String user) { boolean isLogged = false; - try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + try (Connection con = getConnection()) { + PreparedStatement pst = con.prepareStatement(new Query(this) .select(columnLogged) .from(tableName) - .addWhere(columnName + "=?", null) + .addWhere(columnName + "='" + user + "'", null) .build() .getQuery()); - pst.setString(1, user); ResultSet rs = pst.executeQuery(); isLogged = rs.next() && (rs.getInt(columnLogged) == 1); } catch (SQLException ex) { @@ -1075,16 +1074,16 @@ public class MySQL implements DataSource { */ @Override public void setLogged(String user) { - try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + try (Connection con = getConnection()) { + PreparedStatement pst = con.prepareStatement(new Query(this) .update() .from(tableName) - .addUpdateSet(columnLogged + "='1'") - .addWhere(columnName + "=?", null) + .addUpdateSet(columnLogged + "=" + 1) + .addWhere(columnName + "='" + user.toLowerCase() + "'", null) .build() .getQuery()); - pst.setString(1, user.toLowerCase()); pst.executeUpdate(); + pst.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -1100,16 +1099,16 @@ public class MySQL implements DataSource { */ @Override public void setUnlogged(String user) { - try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) + try (Connection con = getConnection()) { + PreparedStatement pst = con.prepareStatement(new Query(this) .update() .from(tableName) - .addUpdateSet(columnLogged + "='0'") - .addWhere(columnName + "=?", null) + .addUpdateSet(columnLogged + "=" + 0) + .addWhere(columnName + "='" + user.toLowerCase() + "'", null) .build() .getQuery()); - pst.setString(1, user.toLowerCase()); pst.executeUpdate(); + pst.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -1183,12 +1182,10 @@ public class MySQL implements DataSource { con.prepareStatement(new Query(this) .update() .from(tableName) - .addUpdateSet(columnName + "=?") - .addWhere(columnName + "=?", null) + .addUpdateSet(columnName + "='" + newOne + "'") + .addWhere(columnName + "='" + oldOne + "'", null) .build() .getQuery()); - pst.setString(1, newOne); - pst.setString(2, oldOne); pst.executeUpdate(); pst.close(); } catch (Exception ex) { diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java index e541baf5..b753ba24 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -612,10 +612,9 @@ public class SQLite implements DataSource { PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnIp + "=?", null) + .addWhere(columnIp + "='" + auth.getIp() + "'", null) .build() .getQuery()); - pst.setString(1, auth.getIp()); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); @@ -644,10 +643,9 @@ public class SQLite implements DataSource { PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnIp + "=?", null) + .addWhere(columnIp + "='" + ip + "'", null) .build() .getQuery()); - pst.setString(1, ip); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); @@ -676,10 +674,9 @@ public class SQLite implements DataSource { PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnName) .from(tableName) - .addWhere(columnEmail + "=?", null) + .addWhere(columnEmail + "='" + email + "'", null) .build() .getQuery()); - pst.setString(1, email); ResultSet rs = pst.executeQuery(); while (rs.next()) { countEmail.add(rs.getString(columnName)); @@ -742,10 +739,9 @@ public class SQLite implements DataSource { PreparedStatement pst = getConnection().prepareStatement(new Query(this) .select(columnLogged) .from(tableName) - .addWhere(columnName + "=?", null) + .addWhere(columnName + "='" + user + "'", null) .build() .getQuery()); - pst.setString(1, user); ResultSet rs = pst.executeQuery(); isLogged = rs.next() && (rs.getInt(columnLogged) == 1); } catch (SQLException ex) { @@ -769,10 +765,9 @@ public class SQLite implements DataSource { .update() .from(tableName) .addUpdateSet(columnLogged + "='1'") - .addWhere(columnName + "=?", null) + .addWhere(columnName + "='" + user.toLowerCase() + "'", null) .build() .getQuery()); - pst.setString(1, user.toLowerCase()); pst.executeUpdate(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); @@ -794,10 +789,9 @@ public class SQLite implements DataSource { .update() .from(tableName) .addUpdateSet(columnLogged + "='0'") - .addWhere(columnName + "=?", null) + .addWhere(columnName + "='" + user.toLowerCase() + "'", null) .build() .getQuery()); - pst.setString(1, user.toLowerCase()); pst.executeUpdate(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); @@ -865,17 +859,15 @@ public class SQLite implements DataSource { */ @Override public void updateName(String oldOne, String newOne) { - try (Connection con = getConnection()) { + try { PreparedStatement pst = - con.prepareStatement(new Query(this) + getConnection().prepareStatement(new Query(this) .update() .from(tableName) - .addUpdateSet(columnName + "=?") - .addWhere(columnName + "=?", null) + .addUpdateSet(columnName + "='" + newOne + "'") + .addWhere(columnName + "='" + oldOne + "'", null) .build() .getQuery()); - pst.setString(1, newOne); - pst.setString(2, oldOne); pst.executeUpdate(); } catch (Exception ex) { ConsoleLogger.showError(ex.getMessage()); From bfbddd466a45182d522c38d460b0f0f26f89b89a Mon Sep 17 00:00:00 2001 From: Xephi Date: Wed, 30 Dec 2015 13:14:48 +0100 Subject: [PATCH 07/10] Revert "Change to QueryType" This reverts commit f7010f1d3b8ce719157a738483aa6f1eab867624. --- .../authme/datasource/queries/Query.java | 108 ++++++++++-------- .../authme/datasource/queries/QueryType.java | 9 -- 2 files changed, 63 insertions(+), 54 deletions(-) delete mode 100644 src/main/java/fr/xephi/authme/datasource/queries/QueryType.java diff --git a/src/main/java/fr/xephi/authme/datasource/queries/Query.java b/src/main/java/fr/xephi/authme/datasource/queries/Query.java index 36260416..08511e00 100644 --- a/src/main/java/fr/xephi/authme/datasource/queries/Query.java +++ b/src/main/java/fr/xephi/authme/datasource/queries/Query.java @@ -1,10 +1,15 @@ package fr.xephi.authme.datasource.queries; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; +import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.datasource.DataSource; public class Query { @@ -16,7 +21,10 @@ public class Query { private List into = new ArrayList(); private List values = new ArrayList(); private List updateSet = new ArrayList(); - private QueryType type; + private boolean isSelect = false; + private boolean isDelete = false; + private boolean isUpdate = false; + private boolean isInsert = false; private String buildQuery = ""; /** @@ -36,7 +44,10 @@ public class Query { public Query select(String selector) { this.selector = selector; - type = QueryType.SELECT; + isSelect = true; + isDelete = false; + isUpdate = false; + isInsert = false; return this; } @@ -46,7 +57,10 @@ public class Query { */ public Query update() { - type = QueryType.UPDATE; + isSelect = false; + isDelete = false; + isUpdate = true; + isInsert = false; return this; } @@ -56,7 +70,10 @@ public class Query { */ public Query delete() { - type = QueryType.DELETE; + isSelect = false; + isDelete = true; + isUpdate = false; + isInsert = false; return this; } @@ -67,7 +84,10 @@ public class Query { */ public Query insert() { - type = QueryType.INSERT; + isSelect = false; + isDelete = false; + isUpdate = false; + isInsert = true; return this; } @@ -129,51 +149,49 @@ public class Query { public Query build(){ StringBuilder str = new StringBuilder(); - switch (type) { - case SELECT: + if (isSelect) + { + str.append("SELECT ").append(selector).append(" FROM ").append(from); + } + else if (isDelete) + { + str.append("DELETE FROM ").append(from); + } + else if (isUpdate) + { + str.append("UPDATE ").append(from).append(" SET "); + Iterator iter = updateSet.iterator(); + while (iter.hasNext()) { - str.append("SELECT ").append(selector).append(" FROM ").append(from); + String s = iter.next(); + str.append(s); + if (iter.hasNext()) + str.append(", "); } - case DELETE: + } + else if (isInsert) + { + str.append("INSERT INTO ").append(from).append(" ('"); + Iterator iter = into.iterator(); + while (iter.hasNext()) { - str.append("DELETE FROM ").append(from); + String s = iter.next(); + str.append(s); + if (iter.hasNext()) + str.append("', '"); + else + str.append("')"); } - case UPDATE: + str.append(" VALUES ('"); + iter = values.iterator(); + while (iter.hasNext()) { - str.append("UPDATE ").append(from).append(" SET "); - Iterator iter = updateSet.iterator(); - while (iter.hasNext()) - { - String s = iter.next(); - str.append(s); - if (iter.hasNext()) - str.append(", "); - } - } - case INSERT: - { - str.append("INSERT INTO ").append(from).append(" ('"); - Iterator iter = into.iterator(); - while (iter.hasNext()) - { - String s = iter.next(); - str.append(s); - if (iter.hasNext()) - str.append("', '"); - else - str.append("')"); - } - str.append(" VALUES ('"); - iter = values.iterator(); - while (iter.hasNext()) - { - String s = iter.next(); - str.append(s); - if (iter.hasNext()) - str.append("', '"); - else - str.append("')"); - } + String s = iter.next(); + str.append(s); + if (iter.hasNext()) + str.append("', '"); + else + str.append("')"); } } if (!where.isEmpty()) diff --git a/src/main/java/fr/xephi/authme/datasource/queries/QueryType.java b/src/main/java/fr/xephi/authme/datasource/queries/QueryType.java deleted file mode 100644 index 1f1575ed..00000000 --- a/src/main/java/fr/xephi/authme/datasource/queries/QueryType.java +++ /dev/null @@ -1,9 +0,0 @@ -package fr.xephi.authme.datasource.queries; - -public enum QueryType { - - DELETE, - INSERT, - SELECT, - UPDATE; -} From ac0225c62143ec728f3bd51cdcd8ec363ec634af Mon Sep 17 00:00:00 2001 From: Xephi Date: Wed, 30 Dec 2015 13:14:53 +0100 Subject: [PATCH 08/10] Revert "Start a rework of some SQL Queries, add a Query builder" This reverts commit da1adb632ecfb4e891dd11f8a969ba338fd37cfd. --- .../authme/datasource/CacheDataSource.java | 7 - .../xephi/authme/datasource/DataSource.java | 4 - .../fr/xephi/authme/datasource/FlatFile.java | 7 - .../fr/xephi/authme/datasource/MySQL.java | 228 +++----- .../fr/xephi/authme/datasource/SQLite.java | 493 +++++++----------- .../authme/datasource/queries/Query.java | 215 -------- 6 files changed, 260 insertions(+), 694 deletions(-) delete mode 100644 src/main/java/fr/xephi/authme/datasource/queries/Query.java diff --git a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java index 85c0c50a..8b26b08c 100644 --- a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java @@ -10,8 +10,6 @@ import com.google.common.cache.RemovalNotification; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; -import java.sql.Connection; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; @@ -480,9 +478,4 @@ public class CacheDataSource implements DataSource { public List getLoggedPlayers() { return new ArrayList<>(PlayerCache.getInstance().getCache().values()); } - - @Override - public Connection getConnection() throws SQLException { - return source.getConnection(); - } } diff --git a/src/main/java/fr/xephi/authme/datasource/DataSource.java b/src/main/java/fr/xephi/authme/datasource/DataSource.java index 1917c91f..e43dffa8 100644 --- a/src/main/java/fr/xephi/authme/datasource/DataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/DataSource.java @@ -2,8 +2,6 @@ package fr.xephi.authme.datasource; import fr.xephi.authme.cache.auth.PlayerAuth; -import java.sql.Connection; -import java.sql.SQLException; import java.util.List; /** @@ -217,8 +215,6 @@ public interface DataSource { */ List getLoggedPlayers(); - Connection getConnection() throws SQLException; - enum DataSourceType { MYSQL, FILE, diff --git a/src/main/java/fr/xephi/authme/datasource/FlatFile.java b/src/main/java/fr/xephi/authme/datasource/FlatFile.java index e97d8f39..b7fb5517 100644 --- a/src/main/java/fr/xephi/authme/datasource/FlatFile.java +++ b/src/main/java/fr/xephi/authme/datasource/FlatFile.java @@ -7,8 +7,6 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.sql.Connection; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -931,9 +929,4 @@ public class FlatFile implements DataSource { public List getLoggedPlayers() { return new ArrayList<>(); } - - @Override - public Connection getConnection() throws SQLException { - return null; - } } diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 713901bf..e779afff 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -5,7 +5,6 @@ import com.zaxxer.hikari.pool.HikariPool.PoolInitializationException; import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; -import fr.xephi.authme.datasource.queries.Query; import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.settings.Settings; @@ -135,8 +134,7 @@ public class MySQL implements DataSource { * * @return Connection * @throws SQLException */ - @Override - public synchronized Connection getConnection() throws SQLException { + private synchronized Connection getConnection() throws SQLException { return ds.getConnection(); } @@ -256,12 +254,8 @@ public class MySQL implements DataSource { @Override public synchronized boolean isAuthAvailable(String user) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .select(columnName) - .from(tableName) - .addWhere(columnName + "=?", null) - .build() - .getQuery()); + String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnName + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); pst.setString(1, user.toLowerCase()); ResultSet rs = pst.executeQuery(); return rs.next(); @@ -283,12 +277,8 @@ public class MySQL implements DataSource { public synchronized PlayerAuth getAuth(String user) { PlayerAuth pAuth; try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .select("*") - .from(tableName) - .addWhere(columnName + "=?", null) - .build() - .getQuery()); + String sql = "SELECT * FROM " + tableName + " WHERE " + columnName + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); pst.setString(1, user.toLowerCase()); ResultSet rs = pst.executeQuery(); if (!rs.next()) { @@ -314,12 +304,7 @@ public class MySQL implements DataSource { rs.close(); pst.close(); if (Settings.getPasswordHash == HashAlgorithm.XENFORO) { - pst = con.prepareStatement(new Query(this) - .select("data") - .from("xf_user_authenticate") - .addWhere(columnID + "=?", null) - .build() - .getQuery()); + pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + columnID + "=?;"); pst.setInt(1, id); rs = pst.executeQuery(); if (rs.next()) { @@ -610,16 +595,10 @@ public class MySQL implements DataSource { */ @Override public synchronized boolean updateSession(PlayerAuth auth) { - try(Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnIp + "=?") - .addUpdateSet(columnLastLogin + "=?") - .addUpdateSet(columnRealName + "=?") - .addWhere(columnName + "=?", null) - .build() - .getQuery()); + try (Connection con = getConnection()) { + String sql = "UPDATE " + tableName + " SET " + + columnIp + "=?, " + columnLastLogin + "=?, " + columnRealName + "=? WHERE " + columnName + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); pst.setString(1, auth.getIp()); pst.setLong(2, auth.getLastLogin()); pst.setString(3, auth.getRealName()); @@ -645,13 +624,9 @@ public class MySQL implements DataSource { @Override public synchronized int purgeDatabase(long until) { int result = 0; - try(Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .delete() - .from(tableName) - .addWhere(columnLastLogin + " autoPurgeDatabase(long until) { List list = new ArrayList<>(); - try(Connection con = getConnection()) { - PreparedStatement st = con.prepareStatement(new Query(this) - .select(columnName) - .from(tableName) - .addWhere(columnLastLogin + "<" + until, null) - .build() - .getQuery()); - ResultSet rs = st.executeQuery(); + try (Connection con = getConnection()) { + String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnLastLogin + "<" + until; + Statement st = con.createStatement(); + ResultSet rs = st.executeQuery(sql); while (rs.next()) { list.add(rs.getString(columnName)); } rs.close(); - st.close(); - st = con.prepareStatement(new Query(this) - .delete() - .from(tableName) - .addWhere(columnLastLogin + "<" + until, null) - .build() - .getQuery()); - st.executeUpdate(); + sql = "DELETE FROM " + tableName + " WHERE " + columnLastLogin + "<" + until; + st.executeUpdate(sql); st.close(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); @@ -753,17 +718,11 @@ public class MySQL implements DataSource { */ @Override public synchronized boolean updateQuitLoc(PlayerAuth auth) { - try(Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(lastlocX + "=?") - .addUpdateSet(lastlocY + "=?") - .addUpdateSet(lastlocZ + "=?") - .addUpdateSet(lastlocWorld + "=?") - .addWhere(columnName + "=?", null) - .build() - .getQuery()); + try (Connection con = getConnection()) { + String sql = "UPDATE " + tableName + + " SET " + lastlocX + " =?, " + lastlocY + "=?, " + lastlocZ + "=?, " + lastlocWorld + "=?" + + " WHERE " + columnName + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); pst.setDouble(1, auth.getQuitLocX()); pst.setDouble(2, auth.getQuitLocY()); pst.setDouble(3, auth.getQuitLocZ()); @@ -792,12 +751,8 @@ public class MySQL implements DataSource { public synchronized int getIps(String ip) { int countIp = 0; try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .select("COUNT(*)") - .from(tableName) - .addWhere(columnIp + "=?", null) - .build() - .getQuery()); + String sql = "SELECT COUNT(*) FROM " + tableName + " WHERE " + columnIp + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); pst.setString(1, ip); ResultSet rs = pst.executeQuery(); while (rs.next()) { @@ -824,13 +779,8 @@ public class MySQL implements DataSource { @Override public synchronized boolean updateEmail(PlayerAuth auth) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnEmail + "=?") - .addWhere(columnName + "=?", null) - .build() - .getQuery()); + String sql = "UPDATE " + tableName + " SET " + columnEmail + " =? WHERE " + columnName + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); pst.setString(1, auth.getEmail()); pst.setString(2, auth.getNickname()); pst.executeUpdate(); @@ -858,13 +808,8 @@ public class MySQL implements DataSource { return false; } try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnSalt + "=?") - .addWhere(columnName + "=?", null) - .build() - .getQuery()); + String sql = "UPDATE " + tableName + " SET " + columnSalt + " =? WHERE " + columnName + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); pst.setString(1, auth.getSalt()); pst.setString(2, auth.getNickname()); pst.executeUpdate(); @@ -919,12 +864,9 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByName(PlayerAuth auth) { List result = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .select(columnName) - .from(tableName) - .addWhere(columnIp + "='" + auth.getIp() + "'", null) - .build() - .getQuery()); + String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnIp + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); + pst.setString(1, auth.getIp()); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); @@ -951,12 +893,9 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByIp(String ip) { List result = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .select(columnName) - .from(tableName) - .addWhere(columnIp + "='" + ip + "'", null) - .build() - .getQuery()); + String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnIp + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); + pst.setString(1, ip); ResultSet rs = pst.executeQuery(); while (rs.next()) { result.add(rs.getString(columnName)); @@ -983,12 +922,9 @@ public class MySQL implements DataSource { public synchronized List getAllAuthsByEmail(String email){ List countEmail = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .select(columnName) - .from(tableName) - .addWhere(columnEmail + "='" + email + "'", null) - .build() - .getQuery()); + String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnEmail + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); + pst.setString(1, email); ResultSet rs = pst.executeQuery(); while (rs.next()) { countEmail.add(rs.getString(columnName)); @@ -1012,12 +948,7 @@ public class MySQL implements DataSource { @Override public synchronized void purgeBanned(List banned) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .delete() - .from(tableName) - .addWhere(columnName + "=?", null) - .build() - .getQuery()); + PreparedStatement pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + columnName + "=?;"); for (String name : banned) { pst.setString(1, name); pst.executeUpdate(); @@ -1050,12 +981,9 @@ public class MySQL implements DataSource { public boolean isLogged(String user) { boolean isLogged = false; try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .select(columnLogged) - .from(tableName) - .addWhere(columnName + "='" + user + "'", null) - .build() - .getQuery()); + String sql = "SELECT " + columnLogged + " FROM " + tableName + " WHERE " + columnName + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); + pst.setString(1, user); ResultSet rs = pst.executeQuery(); isLogged = rs.next() && (rs.getInt(columnLogged) == 1); } catch (SQLException ex) { @@ -1075,13 +1003,10 @@ public class MySQL implements DataSource { @Override public void setLogged(String user) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnLogged + "=" + 1) - .addWhere(columnName + "='" + user.toLowerCase() + "'", null) - .build() - .getQuery()); + String sql = "UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnName + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); + pst.setInt(1, 1); + pst.setString(2, user.toLowerCase()); pst.executeUpdate(); pst.close(); } catch (SQLException ex) { @@ -1100,13 +1025,10 @@ public class MySQL implements DataSource { @Override public void setUnlogged(String user) { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnLogged + "=" + 0) - .addWhere(columnName + "='" + user.toLowerCase() + "'", null) - .build() - .getQuery()); + String sql = "UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnName + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); + pst.setInt(1, 0); + pst.setString(2, user.toLowerCase()); pst.executeUpdate(); pst.close(); } catch (SQLException ex) { @@ -1123,13 +1045,10 @@ public class MySQL implements DataSource { @Override public void purgeLogged() { try (Connection con = getConnection()) { - PreparedStatement pst = con.prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnLogged + "=" + 0) - .addWhere(columnLogged + "=" + 1, null) - .build() - .getQuery()); + String sql = "UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnLogged + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); + pst.setInt(1, 0); + pst.setInt(2, 1); pst.executeUpdate(); pst.close(); } catch (Exception ex) { @@ -1149,12 +1068,8 @@ public class MySQL implements DataSource { public int getAccountsRegistered() { int result = 0; try (Connection con = getConnection()) { - PreparedStatement st = con.prepareStatement(new Query(this) - .select("COUNT(*)") - .from(tableName) - .build() - .getQuery()); - ResultSet rs = st.executeQuery(); + Statement st = con.createStatement(); + ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM " + tableName); if (rs.next()) { result = rs.getInt(1); } @@ -1178,16 +1093,11 @@ public class MySQL implements DataSource { @Override public void updateName(String oldOne, String newOne) { try (Connection con = getConnection()) { - PreparedStatement pst = - con.prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnName + "='" + newOne + "'") - .addWhere(columnName + "='" + oldOne + "'", null) - .build() - .getQuery()); + String sql = "UPDATE " + tableName + " SET " + columnName + "=? WHERE " + columnName + "=?;"; + PreparedStatement pst = con.prepareStatement(sql); + pst.setString(1, newOne); + pst.setString(2, oldOne); pst.executeUpdate(); - pst.close(); } catch (Exception ex) { ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.writeStackTrace(ex); @@ -1205,19 +1115,9 @@ public class MySQL implements DataSource { public List getAllAuths() { List auths = new ArrayList<>(); try (Connection con = getConnection()) { - PreparedStatement st = con.prepareStatement(new Query(this) - .select("*") - .from(tableName) - .build() - .getQuery()); - ResultSet rs = st - .executeQuery(); - PreparedStatement pst = con.prepareStatement(new Query(this) - .select("data") - .from("xf_user_authenticate") - .addWhere(columnID + "=?", null) - .build() - .getQuery()); + Statement st = con.createStatement(); + ResultSet rs = st.executeQuery("SELECT * FROM " + tableName); + PreparedStatement pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + columnID + "=?;"); while (rs.next()) { String salt = !columnSalt.isEmpty() ? rs.getString(columnSalt) : ""; int group = !salt.isEmpty() && !columnGroup.isEmpty() ? rs.getInt(columnGroup) : -1; diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java index b753ba24..9c4301ae 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -2,8 +2,6 @@ package fr.xephi.authme.datasource; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; -import fr.xephi.authme.datasource.queries.Query; -import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.settings.Settings; import java.sql.*; @@ -76,23 +74,6 @@ public class SQLite implements DataSource { } - private synchronized void reconnect() throws ClassNotFoundException, SQLException { - Class.forName("org.sqlite.JDBC"); - this.con = DriverManager.getConnection("jdbc:sqlite:plugins/AuthMe/" + database + ".db"); - } - - @Override - public synchronized Connection getConnection() throws SQLException - { - if (this.con.isClosed()) - try { - reconnect(); - } catch (ClassNotFoundException e) { - ConsoleLogger.writeStackTrace(e); - } - return this.con; - } - /** * Method setup. * @@ -164,12 +145,7 @@ public class SQLite implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - pst = getConnection().prepareStatement(new Query(this) - .select("*") - .from(tableName) - .addWhere("LOWER(" + columnName + ")=LOWER(?)", null) - .build() - .getQuery()); + pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);"); pst.setString(1, user); rs = pst.executeQuery(); return rs.next(); @@ -194,12 +170,7 @@ public class SQLite implements DataSource { PreparedStatement pst = null; ResultSet rs = null; try { - pst = getConnection().prepareStatement(new Query(this) - .select("*") - .from(tableName) - .addWhere("LOWER(" + columnName + ")=LOWER(?)", null) - .build() - .getQuery()); + pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);"); pst.setString(1, user); rs = pst.executeQuery(); if (rs.next()) { @@ -271,14 +242,8 @@ public class SQLite implements DataSource { */ @Override public synchronized boolean updatePassword(PlayerAuth auth) { + PreparedStatement pst = null; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnPassword + "=?") - .addWhere(columnName + "=?", null) - .build() - .getQuery()); pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnPassword + "=? WHERE " + columnName + "=?;"); pst.setString(1, auth.getHash()); pst.setString(2, auth.getNickname()); @@ -286,6 +251,8 @@ public class SQLite implements DataSource { } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); return false; + } finally { + close(pst); } return true; } @@ -295,32 +262,25 @@ public class SQLite implements DataSource { * * @param auth PlayerAuth * - * @return boolean - * - * @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth) + * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth) */ @Override - public synchronized boolean updateSession(PlayerAuth auth) { + public boolean updateSession(PlayerAuth auth) { + PreparedStatement pst = null; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnIp + "=?") - .addUpdateSet(columnLastLogin + "=?") - .addUpdateSet(columnRealName + "=?") - .addWhere(columnName + "=?", null) - .build() - .getQuery()); + pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnIp + "=?, " + columnLastLogin + "=?, " + columnRealName + "=? WHERE " + columnName + "=?;"); pst.setString(1, auth.getIp()); pst.setLong(2, auth.getLastLogin()); pst.setString(3, auth.getRealName()); pst.setString(4, auth.getNickname()); pst.executeUpdate(); - return true; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); + return false; + } finally { + close(pst); } - return false; + return true; } /** @@ -328,27 +288,22 @@ public class SQLite implements DataSource { * * @param until long * - * @return int - * - * @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long) + * @return int * @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long) */ @Override - public synchronized int purgeDatabase(long until) { - int result = 0; + public int purgeDatabase(long until) { + PreparedStatement pst = null; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .delete() - .from(tableName) - .addWhere(columnLastLogin + " * @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long) */ @Override - public synchronized List autoPurgeDatabase(long until) { + public List autoPurgeDatabase(long until) { + PreparedStatement pst = null; + ResultSet rs = null; List list = new ArrayList<>(); try { - PreparedStatement st = getConnection().prepareStatement(new Query(this) - .select(columnName) - .from(tableName) - .addWhere(columnLastLogin + "<" + until, null) - .build() - .getQuery()); - ResultSet rs = st.executeQuery(); + pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + "(); + } finally { + close(rs); + close(pst); } - return list; } /** @@ -417,35 +363,26 @@ public class SQLite implements DataSource { * * @param auth PlayerAuth * - * @return boolean - * - * @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth) + * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth) */ @Override - public synchronized boolean updateQuitLoc(PlayerAuth auth) { + public boolean updateQuitLoc(PlayerAuth auth) { + PreparedStatement pst = null; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(lastlocX + "=?") - .addUpdateSet(lastlocY + "=?") - .addUpdateSet(lastlocZ + "=?") - .addUpdateSet(lastlocWorld + "=?") - .addWhere(columnName + "=?", null) - .build() - .getQuery()); + pst = con.prepareStatement("UPDATE " + tableName + " SET " + lastlocX + "=?, " + lastlocY + "=?, " + lastlocZ + "=?, " + lastlocWorld + "=? WHERE " + columnName + "=?;"); pst.setDouble(1, auth.getQuitLocX()); pst.setDouble(2, auth.getQuitLocY()); pst.setDouble(3, auth.getQuitLocZ()); pst.setString(4, auth.getWorld()); pst.setString(5, auth.getNickname()); pst.executeUpdate(); - return true; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + return false; + } finally { + close(pst); } - return false; + return true; } /** @@ -453,31 +390,28 @@ public class SQLite implements DataSource { * * @param ip String * - * @return int - * - * @see fr.xephi.authme.datasource.DataSource#getIps(String) + * @return int * @see fr.xephi.authme.datasource.DataSource#getIps(String) */ @Override - public synchronized int getIps(String ip) { + public int getIps(String ip) { + PreparedStatement pst = null; + ResultSet rs = null; int countIp = 0; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .select("COUNT(*)") - .from(tableName) - .addWhere(columnIp + "=?", null) - .build() - .getQuery()); + pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); pst.setString(1, ip); - ResultSet rs = pst.executeQuery(); + rs = pst.executeQuery(); while (rs.next()) { - countIp = rs.getInt(1); + countIp++; } - rs.close(); + return countIp; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + return 0; + } finally { + close(rs); + close(pst); } - return countIp; } /** @@ -485,29 +419,23 @@ public class SQLite implements DataSource { * * @param auth PlayerAuth * - * @return boolean - * - * @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth) + * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth) */ @Override - public synchronized boolean updateEmail(PlayerAuth auth) { + public boolean updateEmail(PlayerAuth auth) { + PreparedStatement pst = null; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnEmail + "=?") - .addWhere(columnName + "=?", null) - .build() - .getQuery()); + pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnEmail + "=? WHERE " + columnName + "=?;"); pst.setString(1, auth.getEmail()); pst.setString(2, auth.getNickname()); pst.executeUpdate(); - return true; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + return false; + } finally { + close(pst); } - return false; + return true; } /** @@ -515,32 +443,26 @@ public class SQLite implements DataSource { * * @param auth PlayerAuth * - * @return boolean - * - * @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth) + * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth) */ @Override - public synchronized boolean updateSalt(PlayerAuth auth) { + public boolean updateSalt(PlayerAuth auth) { if (columnSalt.isEmpty()) { return false; } + PreparedStatement pst = null; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnSalt + "=?") - .addWhere(columnName + "=?", null) - .build() - .getQuery()); + pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnSalt + "=? WHERE " + columnName + "=?;"); pst.setString(1, auth.getSalt()); pst.setString(2, auth.getNickname()); pst.executeUpdate(); - return true; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + return false; + } finally { + close(pst); } - return false; + return true; } /** @@ -601,30 +523,30 @@ public class SQLite implements DataSource { * * @param auth PlayerAuth * - * @return List - * - * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth) + * @return List * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth) */ @Override - public synchronized List getAllAuthsByName(PlayerAuth auth) { - List result = new ArrayList<>(); - try (Connection con = getConnection()) { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .select(columnName) - .from(tableName) - .addWhere(columnIp + "='" + auth.getIp() + "'", null) - .build() - .getQuery()); - ResultSet rs = pst.executeQuery(); + public List getAllAuthsByName(PlayerAuth auth) { + PreparedStatement pst = null; + ResultSet rs = null; + List countIp = new ArrayList<>(); + try { + pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); + pst.setString(1, auth.getIp()); + rs = pst.executeQuery(); while (rs.next()) { - result.add(rs.getString(columnName)); + countIp.add(rs.getString(columnName)); } - rs.close(); + return countIp; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + return new ArrayList<>(); + } catch (NullPointerException npe) { + return new ArrayList<>(); + } finally { + close(rs); + close(pst); } - return result; } /** @@ -632,30 +554,30 @@ public class SQLite implements DataSource { * * @param ip String * - * @return List - * - * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String) + * @return List * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String) */ @Override - public synchronized List getAllAuthsByIp(String ip) { - List result = new ArrayList<>(); + public List getAllAuthsByIp(String ip) { + PreparedStatement pst = null; + ResultSet rs = null; + List countIp = new ArrayList<>(); try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .select(columnName) - .from(tableName) - .addWhere(columnIp + "='" + ip + "'", null) - .build() - .getQuery()); - ResultSet rs = pst.executeQuery(); + pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;"); + pst.setString(1, ip); + rs = pst.executeQuery(); while (rs.next()) { - result.add(rs.getString(columnName)); + countIp.add(rs.getString(columnName)); } - rs.close(); + return countIp; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + return new ArrayList<>(); + } catch (NullPointerException npe) { + return new ArrayList<>(); + } finally { + close(rs); + close(pst); } - return result; } /** @@ -663,30 +585,30 @@ public class SQLite implements DataSource { * * @param email String * - * @return List - * - * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String) + * @return List * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String) */ @Override - public synchronized List getAllAuthsByEmail(String email){ + public List getAllAuthsByEmail(String email) { + PreparedStatement pst = null; + ResultSet rs = null; List countEmail = new ArrayList<>(); try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .select(columnName) - .from(tableName) - .addWhere(columnEmail + "='" + email + "'", null) - .build() - .getQuery()); - ResultSet rs = pst.executeQuery(); + pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;"); + pst.setString(1, email); + rs = pst.executeQuery(); while (rs.next()) { countEmail.add(rs.getString(columnName)); } - rs.close(); + return countEmail; } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + return new ArrayList<>(); + } catch (NullPointerException npe) { + return new ArrayList<>(); + } finally { + close(rs); + close(pst); } - return countEmail; } /** @@ -694,24 +616,21 @@ public class SQLite implements DataSource { * * @param banned List * - * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List) + * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List) */ @Override - public synchronized void purgeBanned(List banned) { + public void purgeBanned(List banned) { + PreparedStatement pst = null; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .delete() - .from(tableName) - .addWhere(columnName + "=?", null) - .build() - .getQuery()); for (String name : banned) { + pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + columnName + "=?;"); pst.setString(1, name); pst.executeUpdate(); } } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + } finally { + close(pst); } } @@ -734,21 +653,22 @@ public class SQLite implements DataSource { */ @Override public boolean isLogged(String user) { - boolean isLogged = false; + PreparedStatement pst = null; + ResultSet rs = null; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .select(columnLogged) - .from(tableName) - .addWhere(columnName + "='" + user + "'", null) - .build() - .getQuery()); - ResultSet rs = pst.executeQuery(); - isLogged = rs.next() && (rs.getInt(columnLogged) == 1); + pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=?;"); + pst.setString(1, user); + rs = pst.executeQuery(); + if (rs.next()) + return (rs.getInt(columnLogged) == 1); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + return false; + } finally { + close(rs); + close(pst); } - return isLogged; + return false; } /** @@ -760,18 +680,16 @@ public class SQLite implements DataSource { */ @Override public void setLogged(String user) { + PreparedStatement pst = null; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnLogged + "='1'") - .addWhere(columnName + "='" + user.toLowerCase() + "'", null) - .build() - .getQuery()); + pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE LOWER(" + columnName + ")=?;"); + pst.setInt(1, 1); + pst.setString(2, user); pst.executeUpdate(); } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + } finally { + close(pst); } } @@ -784,19 +702,18 @@ public class SQLite implements DataSource { */ @Override public void setUnlogged(String user) { - try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnLogged + "='0'") - .addWhere(columnName + "='" + user.toLowerCase() + "'", null) - .build() - .getQuery()); - pst.executeUpdate(); - } catch (SQLException ex) { - ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); - } + PreparedStatement pst = null; + if (user != null) + try { + pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE LOWER(" + columnName + ")=?;"); + pst.setInt(1, 0); + pst.setString(2, user); + pst.executeUpdate(); + } catch (SQLException ex) { + ConsoleLogger.showError(ex.getMessage()); + } finally { + close(pst); + } } /** @@ -806,45 +723,40 @@ public class SQLite implements DataSource { */ @Override public void purgeLogged() { + PreparedStatement pst = null; try { - PreparedStatement pst = getConnection().prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnLogged + "='0'") - .addWhere(columnLogged + "='1'", null) - .build() - .getQuery()); + pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnLogged + "=?;"); + pst.setInt(1, 0); + pst.setInt(2, 1); pst.executeUpdate(); - } catch (Exception ex) { + } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + } finally { + close(pst); } } /** * Method getAccountsRegistered. * - * @return int - * - * @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered() + * @return int * @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered() */ @Override public int getAccountsRegistered() { int result = 0; + PreparedStatement pst = null; + ResultSet rs; try { - PreparedStatement st = getConnection().prepareStatement(new Query(this) - .select("COUNT(*)") - .from(tableName) - .build() - .getQuery()); - ResultSet rs = st.executeQuery(); - if (rs.next()) { + pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";"); + rs = pst.executeQuery(); + if (rs != null && rs.next()) { result = rs.getInt(1); } - rs.close(); - } catch (Exception ex) { + } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + return result; + } finally { + close(pst); } return result; } @@ -859,63 +771,50 @@ public class SQLite implements DataSource { */ @Override public void updateName(String oldOne, String newOne) { + PreparedStatement pst = null; try { - PreparedStatement pst = - getConnection().prepareStatement(new Query(this) - .update() - .from(tableName) - .addUpdateSet(columnName + "='" + newOne + "'") - .addWhere(columnName + "='" + oldOne + "'", null) - .build() - .getQuery()); + pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnName + "=? WHERE " + columnName + "=?;"); + pst.setString(1, newOne); + pst.setString(2, oldOne); pst.executeUpdate(); - } catch (Exception ex) { + } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + } finally { + close(pst); } } /** * Method getAllAuths. * - * @return List - * - * @see fr.xephi.authme.datasource.DataSource#getAllAuths() + * @return List * @see fr.xephi.authme.datasource.DataSource#getAllAuths() */ @Override public List getAllAuths() { List auths = new ArrayList<>(); + PreparedStatement pst = null; + ResultSet rs; try { - PreparedStatement st = getConnection().prepareStatement(new Query(this) - .select("*") - .from(tableName) - .build() - .getQuery()); - ResultSet rs = st - .executeQuery(); + pst = con.prepareStatement("SELECT * FROM " + tableName + ";"); + rs = pst.executeQuery(); while (rs.next()) { - String salt = !columnSalt.isEmpty() ? rs.getString(columnSalt) : ""; - int group = !salt.isEmpty() && !columnGroup.isEmpty() ? rs.getInt(columnGroup) : -1; - PlayerAuth pAuth = PlayerAuth.builder() - .name(rs.getString(columnName)) - .realName(rs.getString(columnRealName)) - .hash(rs.getString(columnPassword)) - .lastLogin(rs.getLong(columnLastLogin)) - .ip(rs.getString(columnIp)) - .locWorld(rs.getString(lastlocWorld)) - .locX(rs.getDouble(lastlocX)) - .locY(rs.getDouble(lastlocY)) - .locZ(rs.getDouble(lastlocZ)) - .email(rs.getString(columnEmail)) - .salt(salt) - .groupId(group) - .build(); + PlayerAuth pAuth; + if (rs.getString(columnIp).isEmpty()) { + pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), "127.0.0.1", rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); + } else { + if (!columnSalt.isEmpty()) { + pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnSalt), rs.getInt(columnGroup), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); + } else { + pAuth = new PlayerAuth(rs.getString(columnName), rs.getString(columnPassword), rs.getString(columnIp), rs.getLong(columnLastLogin), rs.getDouble(lastlocX), rs.getDouble(lastlocY), rs.getDouble(lastlocZ), rs.getString(lastlocWorld), rs.getString(columnEmail), rs.getString(columnRealName)); + } + } auths.add(pAuth); } - rs.close(); - } catch (Exception ex) { + } catch (SQLException ex) { ConsoleLogger.showError(ex.getMessage()); - ConsoleLogger.writeStackTrace(ex); + return auths; + } finally { + close(pst); } return auths; } diff --git a/src/main/java/fr/xephi/authme/datasource/queries/Query.java b/src/main/java/fr/xephi/authme/datasource/queries/Query.java deleted file mode 100644 index 08511e00..00000000 --- a/src/main/java/fr/xephi/authme/datasource/queries/Query.java +++ /dev/null @@ -1,215 +0,0 @@ -package fr.xephi.authme.datasource.queries; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; - -import fr.xephi.authme.ConsoleLogger; -import fr.xephi.authme.datasource.DataSource; - -public class Query { - - private DataSource source; - private String selector = null; - private String from = null; - private HashMap where = new HashMap(); - private List into = new ArrayList(); - private List values = new ArrayList(); - private List updateSet = new ArrayList(); - private boolean isSelect = false; - private boolean isDelete = false; - private boolean isUpdate = false; - private boolean isInsert = false; - private String buildQuery = ""; - - /** - * - * @param source - */ - public Query(DataSource source) - { - this.source = source; - } - - /** - * - * @param selector - * @return Query instance - */ - public Query select(String selector) - { - this.selector = selector; - isSelect = true; - isDelete = false; - isUpdate = false; - isInsert = false; - return this; - } - - /** - * - * @return Query instance - */ - public Query update() - { - isSelect = false; - isDelete = false; - isUpdate = true; - isInsert = false; - return this; - } - - /** - * - * @return Query instance - */ - public Query delete() - { - isSelect = false; - isDelete = true; - isUpdate = false; - isInsert = false; - return this; - } - - /** - * - * @param selector - * @return Query instance - */ - public Query insert() - { - isSelect = false; - isDelete = false; - isUpdate = false; - isInsert = true; - return this; - } - - /** - * - * @param column - * @return - */ - public Query addInsertInto(String column) - { - into.add(column); - return this; - } - - /** - * - * @param value - * @return - */ - public Query addInsertValue(String value) - { - values.add(value); - return this; - } - - /** - * - * @param set - * @return - */ - public Query addUpdateSet(String set) - { - updateSet.add(set); - return this; - } - - /** - * - * @param from - * @return Query instance - */ - public Query from(String from) - { - this.from = from; - return this; - } - - /** - * - * @param where - * @param String and/or/null - * @return Query instance - */ - public Query addWhere(String where, String logic) - { - this.where.put(where, logic); - return this; - } - - public Query build(){ - StringBuilder str = new StringBuilder(); - if (isSelect) - { - str.append("SELECT ").append(selector).append(" FROM ").append(from); - } - else if (isDelete) - { - str.append("DELETE FROM ").append(from); - } - else if (isUpdate) - { - str.append("UPDATE ").append(from).append(" SET "); - Iterator iter = updateSet.iterator(); - while (iter.hasNext()) - { - String s = iter.next(); - str.append(s); - if (iter.hasNext()) - str.append(", "); - } - } - else if (isInsert) - { - str.append("INSERT INTO ").append(from).append(" ('"); - Iterator iter = into.iterator(); - while (iter.hasNext()) - { - String s = iter.next(); - str.append(s); - if (iter.hasNext()) - str.append("', '"); - else - str.append("')"); - } - str.append(" VALUES ('"); - iter = values.iterator(); - while (iter.hasNext()) - { - String s = iter.next(); - str.append(s); - if (iter.hasNext()) - str.append("', '"); - else - str.append("')"); - } - } - if (!where.isEmpty()) - { - str.append(" WHERE"); - for (String key : where.keySet()) - { - if (where.get(key) != null) - str.append(" ").append(where.get(key)); - str.append(" ").append(key); - } - } - str.append(";"); - this.buildQuery = str.toString(); - return this; - } - - public String getQuery() { - return this.buildQuery; - } -} From edffae3a36aa4b00ec27d1a664ce3be7f1262a39 Mon Sep 17 00:00:00 2001 From: Xephi Date: Wed, 30 Dec 2015 13:20:24 +0100 Subject: [PATCH 09/10] Add TabComplete support through ProtocolLib --- src/main/java/fr/xephi/authme/AuthMe.java | 7 +++ .../AuthMeTabCompletePacketAdapter.java | 45 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/main/java/fr/xephi/authme/listener/AuthMeTabCompletePacketAdapter.java diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 32188d23..cae30a44 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -31,6 +31,7 @@ import fr.xephi.authme.listener.AuthMePlayerListener16; import fr.xephi.authme.listener.AuthMePlayerListener18; import fr.xephi.authme.listener.AuthMeServerListener; import fr.xephi.authme.listener.AuthMeServerStop; +import fr.xephi.authme.listener.AuthMeTabCompletePacketAdapter; import fr.xephi.authme.mail.SendMailSSL; import fr.xephi.authme.modules.ModuleManager; import fr.xephi.authme.output.ConsoleFilter; @@ -117,6 +118,7 @@ public class AuthMe extends JavaPlugin { public MultiverseCore multiverse; public CombatTagPlus combatTagPlus; public AuthMeInventoryPacketAdapter inventoryProtector; + public AuthMeTabCompletePacketAdapter tabComplete; /* * Maps and stuff @@ -710,6 +712,11 @@ public class AuthMe extends JavaPlugin { inventoryProtector = null; } } + if (tabComplete == null) + { + tabComplete = new AuthMeTabCompletePacketAdapter(this); + tabComplete.register(); + } } // Save Player Data diff --git a/src/main/java/fr/xephi/authme/listener/AuthMeTabCompletePacketAdapter.java b/src/main/java/fr/xephi/authme/listener/AuthMeTabCompletePacketAdapter.java new file mode 100644 index 00000000..3c385070 --- /dev/null +++ b/src/main/java/fr/xephi/authme/listener/AuthMeTabCompletePacketAdapter.java @@ -0,0 +1,45 @@ +package fr.xephi.authme.listener; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.ListenerPriority; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketEvent; +import com.comphenix.protocol.reflect.FieldAccessException; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.cache.auth.PlayerCache; + +public class AuthMeTabCompletePacketAdapter extends PacketAdapter { + + public AuthMeTabCompletePacketAdapter(AuthMe plugin) { + super(plugin, ListenerPriority.NORMAL, PacketType.Play.Client.TAB_COMPLETE); + } + + @Override + public void onPacketReceiving(PacketEvent event) + { + if (event.getPacketType() == PacketType.Play.Client.TAB_COMPLETE) { + try + { + String message = ((String)event.getPacket().getSpecificModifier(String.class).read(0)).toLowerCase(); + if ((message.startsWith("")) && (!message.contains(" ")) && !PlayerCache.getInstance().isAuthenticated(event.getPlayer().getName().toLowerCase())) { + event.setCancelled(true); + } + } + catch (FieldAccessException e) + { + ConsoleLogger.showError("Couldn't access field."); + } + } + } + + public void register() { + ProtocolLibrary.getProtocolManager().addPacketListener(this); + } + + public void unregister() { + ProtocolLibrary.getProtocolManager().removePacketListener(this); + } +} From 712f09cf801600abb200f2193388140ceb689ab3 Mon Sep 17 00:00:00 2001 From: Alexandre Vanhecke Date: Wed, 30 Dec 2015 13:50:18 +0100 Subject: [PATCH 10/10] Update team.txt --- team.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/team.txt b/team.txt index 98b60aa5..7a346aa1 100644 --- a/team.txt +++ b/team.txt @@ -8,9 +8,6 @@ TimVisee - Developer games647 - Developer Gabriele C. (sgdc3) - Project Manager, Contributor -AuthMeBridge staff: -CryLegend - Main developer, We need to contact him! - Retired staff: Maxetto - Ticket Manager, IT translator darkwarriors (d4rkwarriors) - Original AuthMeReloaded Author (Inactive)