Revert "Start a rework of some SQL Queries, add a Query builder"

This reverts commit da1adb632e.
This commit is contained in:
Xephi 2015-12-30 13:14:53 +01:00
parent bfbddd466a
commit ac0225c621
6 changed files with 260 additions and 694 deletions

View File

@ -10,8 +10,6 @@ import com.google.common.cache.RemovalNotification;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -480,9 +478,4 @@ public class CacheDataSource implements DataSource {
public List<PlayerAuth> getLoggedPlayers() { public List<PlayerAuth> getLoggedPlayers() {
return new ArrayList<>(PlayerCache.getInstance().getCache().values()); return new ArrayList<>(PlayerCache.getInstance().getCache().values());
} }
@Override
public Connection getConnection() throws SQLException {
return source.getConnection();
}
} }

View File

@ -2,8 +2,6 @@ package fr.xephi.authme.datasource;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List; import java.util.List;
/** /**
@ -217,8 +215,6 @@ public interface DataSource {
*/ */
List<PlayerAuth> getLoggedPlayers(); List<PlayerAuth> getLoggedPlayers();
Connection getConnection() throws SQLException;
enum DataSourceType { enum DataSourceType {
MYSQL, MYSQL,
FILE, FILE,

View File

@ -7,8 +7,6 @@ import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -931,9 +929,4 @@ public class FlatFile implements DataSource {
public List<PlayerAuth> getLoggedPlayers() { public List<PlayerAuth> getLoggedPlayers() {
return new ArrayList<>(); return new ArrayList<>();
} }
@Override
public Connection getConnection() throws SQLException {
return null;
}
} }

View File

@ -5,7 +5,6 @@ import com.zaxxer.hikari.pool.HikariPool.PoolInitializationException;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.datasource.queries.Query;
import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
@ -135,8 +134,7 @@ public class MySQL implements DataSource {
* *
* @return Connection * @throws SQLException * @return Connection * @throws SQLException
*/ */
@Override private synchronized Connection getConnection() throws SQLException {
public synchronized Connection getConnection() throws SQLException {
return ds.getConnection(); return ds.getConnection();
} }
@ -256,12 +254,8 @@ public class MySQL implements DataSource {
@Override @Override
public synchronized boolean isAuthAvailable(String user) { public synchronized boolean isAuthAvailable(String user) {
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnName + "=?;";
.select(columnName) PreparedStatement pst = con.prepareStatement(sql);
.from(tableName)
.addWhere(columnName + "=?", null)
.build()
.getQuery());
pst.setString(1, user.toLowerCase()); pst.setString(1, user.toLowerCase());
ResultSet rs = pst.executeQuery(); ResultSet rs = pst.executeQuery();
return rs.next(); return rs.next();
@ -283,12 +277,8 @@ public class MySQL implements DataSource {
public synchronized PlayerAuth getAuth(String user) { public synchronized PlayerAuth getAuth(String user) {
PlayerAuth pAuth; PlayerAuth pAuth;
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "SELECT * FROM " + tableName + " WHERE " + columnName + "=?;";
.select("*") PreparedStatement pst = con.prepareStatement(sql);
.from(tableName)
.addWhere(columnName + "=?", null)
.build()
.getQuery());
pst.setString(1, user.toLowerCase()); pst.setString(1, user.toLowerCase());
ResultSet rs = pst.executeQuery(); ResultSet rs = pst.executeQuery();
if (!rs.next()) { if (!rs.next()) {
@ -314,12 +304,7 @@ public class MySQL implements DataSource {
rs.close(); rs.close();
pst.close(); pst.close();
if (Settings.getPasswordHash == HashAlgorithm.XENFORO) { if (Settings.getPasswordHash == HashAlgorithm.XENFORO) {
pst = con.prepareStatement(new Query(this) pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + columnID + "=?;");
.select("data")
.from("xf_user_authenticate")
.addWhere(columnID + "=?", null)
.build()
.getQuery());
pst.setInt(1, id); pst.setInt(1, id);
rs = pst.executeQuery(); rs = pst.executeQuery();
if (rs.next()) { if (rs.next()) {
@ -610,16 +595,10 @@ public class MySQL implements DataSource {
*/ */
@Override @Override
public synchronized boolean updateSession(PlayerAuth auth) { public synchronized boolean updateSession(PlayerAuth auth) {
try(Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "UPDATE " + tableName + " SET "
.update() + columnIp + "=?, " + columnLastLogin + "=?, " + columnRealName + "=? WHERE " + columnName + "=?;";
.from(tableName) PreparedStatement pst = con.prepareStatement(sql);
.addUpdateSet(columnIp + "=?")
.addUpdateSet(columnLastLogin + "=?")
.addUpdateSet(columnRealName + "=?")
.addWhere(columnName + "=?", null)
.build()
.getQuery());
pst.setString(1, auth.getIp()); pst.setString(1, auth.getIp());
pst.setLong(2, auth.getLastLogin()); pst.setLong(2, auth.getLastLogin());
pst.setString(3, auth.getRealName()); pst.setString(3, auth.getRealName());
@ -645,13 +624,9 @@ public class MySQL implements DataSource {
@Override @Override
public synchronized int purgeDatabase(long until) { public synchronized int purgeDatabase(long until) {
int result = 0; int result = 0;
try(Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "DELETE FROM " + tableName + " WHERE " + columnLastLogin + "<?;";
.delete() PreparedStatement pst = con.prepareStatement(sql);
.from(tableName)
.addWhere(columnLastLogin + "<?", null)
.build()
.getQuery());
pst.setLong(1, until); pst.setLong(1, until);
result = pst.executeUpdate(); result = pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
@ -673,26 +648,16 @@ public class MySQL implements DataSource {
@Override @Override
public synchronized List<String> autoPurgeDatabase(long until) { public synchronized List<String> autoPurgeDatabase(long until) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
try(Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement st = con.prepareStatement(new Query(this) String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnLastLogin + "<" + until;
.select(columnName) Statement st = con.createStatement();
.from(tableName) ResultSet rs = st.executeQuery(sql);
.addWhere(columnLastLogin + "<" + until, null)
.build()
.getQuery());
ResultSet rs = st.executeQuery();
while (rs.next()) { while (rs.next()) {
list.add(rs.getString(columnName)); list.add(rs.getString(columnName));
} }
rs.close(); rs.close();
st.close(); sql = "DELETE FROM " + tableName + " WHERE " + columnLastLogin + "<" + until;
st = con.prepareStatement(new Query(this) st.executeUpdate(sql);
.delete()
.from(tableName)
.addWhere(columnLastLogin + "<" + until, null)
.build()
.getQuery());
st.executeUpdate();
st.close(); st.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
@ -753,17 +718,11 @@ public class MySQL implements DataSource {
*/ */
@Override @Override
public synchronized boolean updateQuitLoc(PlayerAuth auth) { public synchronized boolean updateQuitLoc(PlayerAuth auth) {
try(Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "UPDATE " + tableName
.update() + " SET " + lastlocX + " =?, " + lastlocY + "=?, " + lastlocZ + "=?, " + lastlocWorld + "=?"
.from(tableName) + " WHERE " + columnName + "=?;";
.addUpdateSet(lastlocX + "=?") PreparedStatement pst = con.prepareStatement(sql);
.addUpdateSet(lastlocY + "=?")
.addUpdateSet(lastlocZ + "=?")
.addUpdateSet(lastlocWorld + "=?")
.addWhere(columnName + "=?", null)
.build()
.getQuery());
pst.setDouble(1, auth.getQuitLocX()); pst.setDouble(1, auth.getQuitLocX());
pst.setDouble(2, auth.getQuitLocY()); pst.setDouble(2, auth.getQuitLocY());
pst.setDouble(3, auth.getQuitLocZ()); pst.setDouble(3, auth.getQuitLocZ());
@ -792,12 +751,8 @@ public class MySQL implements DataSource {
public synchronized int getIps(String ip) { public synchronized int getIps(String ip) {
int countIp = 0; int countIp = 0;
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "SELECT COUNT(*) FROM " + tableName + " WHERE " + columnIp + "=?;";
.select("COUNT(*)") PreparedStatement pst = con.prepareStatement(sql);
.from(tableName)
.addWhere(columnIp + "=?", null)
.build()
.getQuery());
pst.setString(1, ip); pst.setString(1, ip);
ResultSet rs = pst.executeQuery(); ResultSet rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
@ -824,13 +779,8 @@ public class MySQL implements DataSource {
@Override @Override
public synchronized boolean updateEmail(PlayerAuth auth) { public synchronized boolean updateEmail(PlayerAuth auth) {
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "UPDATE " + tableName + " SET " + columnEmail + " =? WHERE " + columnName + "=?;";
.update() PreparedStatement pst = con.prepareStatement(sql);
.from(tableName)
.addUpdateSet(columnEmail + "=?")
.addWhere(columnName + "=?", null)
.build()
.getQuery());
pst.setString(1, auth.getEmail()); pst.setString(1, auth.getEmail());
pst.setString(2, auth.getNickname()); pst.setString(2, auth.getNickname());
pst.executeUpdate(); pst.executeUpdate();
@ -858,13 +808,8 @@ public class MySQL implements DataSource {
return false; return false;
} }
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "UPDATE " + tableName + " SET " + columnSalt + " =? WHERE " + columnName + "=?;";
.update() PreparedStatement pst = con.prepareStatement(sql);
.from(tableName)
.addUpdateSet(columnSalt + "=?")
.addWhere(columnName + "=?", null)
.build()
.getQuery());
pst.setString(1, auth.getSalt()); pst.setString(1, auth.getSalt());
pst.setString(2, auth.getNickname()); pst.setString(2, auth.getNickname());
pst.executeUpdate(); pst.executeUpdate();
@ -919,12 +864,9 @@ public class MySQL implements DataSource {
public synchronized List<String> getAllAuthsByName(PlayerAuth auth) { public synchronized List<String> getAllAuthsByName(PlayerAuth auth) {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnIp + "=?;";
.select(columnName) PreparedStatement pst = con.prepareStatement(sql);
.from(tableName) pst.setString(1, auth.getIp());
.addWhere(columnIp + "='" + auth.getIp() + "'", null)
.build()
.getQuery());
ResultSet rs = pst.executeQuery(); ResultSet rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
result.add(rs.getString(columnName)); result.add(rs.getString(columnName));
@ -951,12 +893,9 @@ public class MySQL implements DataSource {
public synchronized List<String> getAllAuthsByIp(String ip) { public synchronized List<String> getAllAuthsByIp(String ip) {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnIp + "=?;";
.select(columnName) PreparedStatement pst = con.prepareStatement(sql);
.from(tableName) pst.setString(1, ip);
.addWhere(columnIp + "='" + ip + "'", null)
.build()
.getQuery());
ResultSet rs = pst.executeQuery(); ResultSet rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
result.add(rs.getString(columnName)); result.add(rs.getString(columnName));
@ -983,12 +922,9 @@ public class MySQL implements DataSource {
public synchronized List<String> getAllAuthsByEmail(String email){ public synchronized List<String> getAllAuthsByEmail(String email){
List<String> countEmail = new ArrayList<>(); List<String> countEmail = new ArrayList<>();
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnEmail + "=?;";
.select(columnName) PreparedStatement pst = con.prepareStatement(sql);
.from(tableName) pst.setString(1, email);
.addWhere(columnEmail + "='" + email + "'", null)
.build()
.getQuery());
ResultSet rs = pst.executeQuery(); ResultSet rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
countEmail.add(rs.getString(columnName)); countEmail.add(rs.getString(columnName));
@ -1012,12 +948,7 @@ public class MySQL implements DataSource {
@Override @Override
public synchronized void purgeBanned(List<String> banned) { public synchronized void purgeBanned(List<String> banned) {
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) PreparedStatement pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + columnName + "=?;");
.delete()
.from(tableName)
.addWhere(columnName + "=?", null)
.build()
.getQuery());
for (String name : banned) { for (String name : banned) {
pst.setString(1, name); pst.setString(1, name);
pst.executeUpdate(); pst.executeUpdate();
@ -1050,12 +981,9 @@ public class MySQL implements DataSource {
public boolean isLogged(String user) { public boolean isLogged(String user) {
boolean isLogged = false; boolean isLogged = false;
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "SELECT " + columnLogged + " FROM " + tableName + " WHERE " + columnName + "=?;";
.select(columnLogged) PreparedStatement pst = con.prepareStatement(sql);
.from(tableName) pst.setString(1, user);
.addWhere(columnName + "='" + user + "'", null)
.build()
.getQuery());
ResultSet rs = pst.executeQuery(); ResultSet rs = pst.executeQuery();
isLogged = rs.next() && (rs.getInt(columnLogged) == 1); isLogged = rs.next() && (rs.getInt(columnLogged) == 1);
} catch (SQLException ex) { } catch (SQLException ex) {
@ -1075,13 +1003,10 @@ public class MySQL implements DataSource {
@Override @Override
public void setLogged(String user) { public void setLogged(String user) {
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnName + "=?;";
.update() PreparedStatement pst = con.prepareStatement(sql);
.from(tableName) pst.setInt(1, 1);
.addUpdateSet(columnLogged + "=" + 1) pst.setString(2, user.toLowerCase());
.addWhere(columnName + "='" + user.toLowerCase() + "'", null)
.build()
.getQuery());
pst.executeUpdate(); pst.executeUpdate();
pst.close(); pst.close();
} catch (SQLException ex) { } catch (SQLException ex) {
@ -1100,13 +1025,10 @@ public class MySQL implements DataSource {
@Override @Override
public void setUnlogged(String user) { public void setUnlogged(String user) {
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnName + "=?;";
.update() PreparedStatement pst = con.prepareStatement(sql);
.from(tableName) pst.setInt(1, 0);
.addUpdateSet(columnLogged + "=" + 0) pst.setString(2, user.toLowerCase());
.addWhere(columnName + "='" + user.toLowerCase() + "'", null)
.build()
.getQuery());
pst.executeUpdate(); pst.executeUpdate();
pst.close(); pst.close();
} catch (SQLException ex) { } catch (SQLException ex) {
@ -1123,13 +1045,10 @@ public class MySQL implements DataSource {
@Override @Override
public void purgeLogged() { public void purgeLogged() {
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = con.prepareStatement(new Query(this) String sql = "UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnLogged + "=?;";
.update() PreparedStatement pst = con.prepareStatement(sql);
.from(tableName) pst.setInt(1, 0);
.addUpdateSet(columnLogged + "=" + 0) pst.setInt(2, 1);
.addWhere(columnLogged + "=" + 1, null)
.build()
.getQuery());
pst.executeUpdate(); pst.executeUpdate();
pst.close(); pst.close();
} catch (Exception ex) { } catch (Exception ex) {
@ -1149,12 +1068,8 @@ public class MySQL implements DataSource {
public int getAccountsRegistered() { public int getAccountsRegistered() {
int result = 0; int result = 0;
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement st = con.prepareStatement(new Query(this) Statement st = con.createStatement();
.select("COUNT(*)") ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM " + tableName);
.from(tableName)
.build()
.getQuery());
ResultSet rs = st.executeQuery();
if (rs.next()) { if (rs.next()) {
result = rs.getInt(1); result = rs.getInt(1);
} }
@ -1178,16 +1093,11 @@ public class MySQL implements DataSource {
@Override @Override
public void updateName(String oldOne, String newOne) { public void updateName(String oldOne, String newOne) {
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement pst = String sql = "UPDATE " + tableName + " SET " + columnName + "=? WHERE " + columnName + "=?;";
con.prepareStatement(new Query(this) PreparedStatement pst = con.prepareStatement(sql);
.update() pst.setString(1, newOne);
.from(tableName) pst.setString(2, oldOne);
.addUpdateSet(columnName + "='" + newOne + "'")
.addWhere(columnName + "='" + oldOne + "'", null)
.build()
.getQuery());
pst.executeUpdate(); pst.executeUpdate();
pst.close();
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex); ConsoleLogger.writeStackTrace(ex);
@ -1205,19 +1115,9 @@ public class MySQL implements DataSource {
public List<PlayerAuth> getAllAuths() { public List<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<>(); List<PlayerAuth> auths = new ArrayList<>();
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
PreparedStatement st = con.prepareStatement(new Query(this) Statement st = con.createStatement();
.select("*") ResultSet rs = st.executeQuery("SELECT * FROM " + tableName);
.from(tableName) PreparedStatement pst = con.prepareStatement("SELECT data FROM xf_user_authenticate WHERE " + columnID + "=?;");
.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()) { while (rs.next()) {
String salt = !columnSalt.isEmpty() ? rs.getString(columnSalt) : ""; String salt = !columnSalt.isEmpty() ? rs.getString(columnSalt) : "";
int group = !salt.isEmpty() && !columnGroup.isEmpty() ? rs.getInt(columnGroup) : -1; int group = !salt.isEmpty() && !columnGroup.isEmpty() ? rs.getInt(columnGroup) : -1;

View File

@ -2,8 +2,6 @@ package fr.xephi.authme.datasource;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth; 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 fr.xephi.authme.settings.Settings;
import java.sql.*; 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. * Method setup.
* *
@ -164,12 +145,7 @@ public class SQLite implements DataSource {
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);");
.select("*")
.from(tableName)
.addWhere("LOWER(" + columnName + ")=LOWER(?)", null)
.build()
.getQuery());
pst.setString(1, user); pst.setString(1, user);
rs = pst.executeQuery(); rs = pst.executeQuery();
return rs.next(); return rs.next();
@ -194,12 +170,7 @@ public class SQLite implements DataSource {
PreparedStatement pst = null; PreparedStatement pst = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=LOWER(?);");
.select("*")
.from(tableName)
.addWhere("LOWER(" + columnName + ")=LOWER(?)", null)
.build()
.getQuery());
pst.setString(1, user); pst.setString(1, user);
rs = pst.executeQuery(); rs = pst.executeQuery();
if (rs.next()) { if (rs.next()) {
@ -271,14 +242,8 @@ public class SQLite implements DataSource {
*/ */
@Override @Override
public synchronized boolean updatePassword(PlayerAuth auth) { public synchronized boolean updatePassword(PlayerAuth auth) {
PreparedStatement pst = null;
try { 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 = con.prepareStatement("UPDATE " + tableName + " SET " + columnPassword + "=? WHERE " + columnName + "=?;");
pst.setString(1, auth.getHash()); pst.setString(1, auth.getHash());
pst.setString(2, auth.getNickname()); pst.setString(2, auth.getNickname());
@ -286,6 +251,8 @@ public class SQLite implements DataSource {
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
return false; return false;
} finally {
close(pst);
} }
return true; return true;
} }
@ -295,32 +262,25 @@ public class SQLite implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth)
*
* @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean updateSession(PlayerAuth auth) { public boolean updateSession(PlayerAuth auth) {
PreparedStatement pst = null;
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnIp + "=?, " + columnLastLogin + "=?, " + columnRealName + "=? WHERE " + columnName + "=?;");
.update()
.from(tableName)
.addUpdateSet(columnIp + "=?")
.addUpdateSet(columnLastLogin + "=?")
.addUpdateSet(columnRealName + "=?")
.addWhere(columnName + "=?", null)
.build()
.getQuery());
pst.setString(1, auth.getIp()); pst.setString(1, auth.getIp());
pst.setLong(2, auth.getLastLogin()); pst.setLong(2, auth.getLastLogin());
pst.setString(3, auth.getRealName()); pst.setString(3, auth.getRealName());
pst.setString(4, auth.getNickname()); pst.setString(4, auth.getNickname());
pst.executeUpdate(); pst.executeUpdate();
return true;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); 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 * @param until long
* *
* @return int * @return int * @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long)
*
* @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long)
*/ */
@Override @Override
public synchronized int purgeDatabase(long until) { public int purgeDatabase(long until) {
int result = 0; PreparedStatement pst = null;
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this)
.delete() pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + columnLastLogin + "<?;");
.from(tableName)
.addWhere(columnLastLogin + "<?", null)
.build()
.getQuery());
pst.setLong(1, until); pst.setLong(1, until);
result = pst.executeUpdate(); return pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex); return 0;
} finally {
close(pst);
} }
return result;
} }
/** /**
@ -356,37 +311,28 @@ public class SQLite implements DataSource {
* *
* @param until long * @param until long
* *
* @return List * @return List<String> * @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long)
*
* @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long)
*/ */
@Override @Override
public synchronized List<String> autoPurgeDatabase(long until) { public List<String> autoPurgeDatabase(long until) {
PreparedStatement pst = null;
ResultSet rs = null;
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
try { try {
PreparedStatement st = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + "<?;");
.select(columnName) pst.setLong(1, until);
.from(tableName) rs = pst.executeQuery();
.addWhere(columnLastLogin + "<" + until, null)
.build()
.getQuery());
ResultSet rs = st.executeQuery();
while (rs.next()) { while (rs.next()) {
list.add(rs.getString(columnName)); list.add(rs.getString(columnName));
} }
rs.close(); return list;
st = getConnection().prepareStatement(new Query(this)
.delete()
.from(tableName)
.addWhere(columnLastLogin + "<" + until, null)
.build()
.getQuery());
st.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex); return new ArrayList<>();
} finally {
close(rs);
close(pst);
} }
return list;
} }
/** /**
@ -417,35 +363,26 @@ public class SQLite implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth)
*
* @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean updateQuitLoc(PlayerAuth auth) { public boolean updateQuitLoc(PlayerAuth auth) {
PreparedStatement pst = null;
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("UPDATE " + tableName + " SET " + lastlocX + "=?, " + lastlocY + "=?, " + lastlocZ + "=?, " + lastlocWorld + "=? WHERE " + columnName + "=?;");
.update()
.from(tableName)
.addUpdateSet(lastlocX + "=?")
.addUpdateSet(lastlocY + "=?")
.addUpdateSet(lastlocZ + "=?")
.addUpdateSet(lastlocWorld + "=?")
.addWhere(columnName + "=?", null)
.build()
.getQuery());
pst.setDouble(1, auth.getQuitLocX()); pst.setDouble(1, auth.getQuitLocX());
pst.setDouble(2, auth.getQuitLocY()); pst.setDouble(2, auth.getQuitLocY());
pst.setDouble(3, auth.getQuitLocZ()); pst.setDouble(3, auth.getQuitLocZ());
pst.setString(4, auth.getWorld()); pst.setString(4, auth.getWorld());
pst.setString(5, auth.getNickname()); pst.setString(5, auth.getNickname());
pst.executeUpdate(); pst.executeUpdate();
return true;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); 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 * @param ip String
* *
* @return int * @return int * @see fr.xephi.authme.datasource.DataSource#getIps(String)
*
* @see fr.xephi.authme.datasource.DataSource#getIps(String)
*/ */
@Override @Override
public synchronized int getIps(String ip) { public int getIps(String ip) {
PreparedStatement pst = null;
ResultSet rs = null;
int countIp = 0; int countIp = 0;
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
.select("COUNT(*)")
.from(tableName)
.addWhere(columnIp + "=?", null)
.build()
.getQuery());
pst.setString(1, ip); pst.setString(1, ip);
ResultSet rs = pst.executeQuery(); rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
countIp = rs.getInt(1); countIp++;
} }
rs.close(); return countIp;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); 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 * @param auth PlayerAuth
* *
* @return boolean * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth)
*
* @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean updateEmail(PlayerAuth auth) { public boolean updateEmail(PlayerAuth auth) {
PreparedStatement pst = null;
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnEmail + "=? WHERE " + columnName + "=?;");
.update()
.from(tableName)
.addUpdateSet(columnEmail + "=?")
.addWhere(columnName + "=?", null)
.build()
.getQuery());
pst.setString(1, auth.getEmail()); pst.setString(1, auth.getEmail());
pst.setString(2, auth.getNickname()); pst.setString(2, auth.getNickname());
pst.executeUpdate(); pst.executeUpdate();
return true;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); 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 * @param auth PlayerAuth
* *
* @return boolean * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth)
*
* @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean updateSalt(PlayerAuth auth) { public boolean updateSalt(PlayerAuth auth) {
if (columnSalt.isEmpty()) { if (columnSalt.isEmpty()) {
return false; return false;
} }
PreparedStatement pst = null;
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnSalt + "=? WHERE " + columnName + "=?;");
.update()
.from(tableName)
.addUpdateSet(columnSalt + "=?")
.addWhere(columnName + "=?", null)
.build()
.getQuery());
pst.setString(1, auth.getSalt()); pst.setString(1, auth.getSalt());
pst.setString(2, auth.getNickname()); pst.setString(2, auth.getNickname());
pst.executeUpdate(); pst.executeUpdate();
return true;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); 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 * @param auth PlayerAuth
* *
* @return List * @return List<String> * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth)
*
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth)
*/ */
@Override @Override
public synchronized List<String> getAllAuthsByName(PlayerAuth auth) { public List<String> getAllAuthsByName(PlayerAuth auth) {
List<String> result = new ArrayList<>(); PreparedStatement pst = null;
try (Connection con = getConnection()) { ResultSet rs = null;
PreparedStatement pst = getConnection().prepareStatement(new Query(this) List<String> countIp = new ArrayList<>();
.select(columnName) try {
.from(tableName) pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
.addWhere(columnIp + "='" + auth.getIp() + "'", null) pst.setString(1, auth.getIp());
.build() rs = pst.executeQuery();
.getQuery());
ResultSet rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
result.add(rs.getString(columnName)); countIp.add(rs.getString(columnName));
} }
rs.close(); return countIp;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); 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 * @param ip String
* *
* @return List * @return List<String> * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String)
*
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String)
*/ */
@Override @Override
public synchronized List<String> getAllAuthsByIp(String ip) { public List<String> getAllAuthsByIp(String ip) {
List<String> result = new ArrayList<>(); PreparedStatement pst = null;
ResultSet rs = null;
List<String> countIp = new ArrayList<>();
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
.select(columnName) pst.setString(1, ip);
.from(tableName) rs = pst.executeQuery();
.addWhere(columnIp + "='" + ip + "'", null)
.build()
.getQuery());
ResultSet rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
result.add(rs.getString(columnName)); countIp.add(rs.getString(columnName));
} }
rs.close(); return countIp;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); 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 * @param email String
* *
* @return List * @return List<String> * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String)
*
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String)
*/ */
@Override @Override
public synchronized List<String> getAllAuthsByEmail(String email){ public List<String> getAllAuthsByEmail(String email) {
PreparedStatement pst = null;
ResultSet rs = null;
List<String> countEmail = new ArrayList<>(); List<String> countEmail = new ArrayList<>();
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnEmail + "=?;");
.select(columnName) pst.setString(1, email);
.from(tableName) rs = pst.executeQuery();
.addWhere(columnEmail + "='" + email + "'", null)
.build()
.getQuery());
ResultSet rs = pst.executeQuery();
while (rs.next()) { while (rs.next()) {
countEmail.add(rs.getString(columnName)); countEmail.add(rs.getString(columnName));
} }
rs.close(); return countEmail;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); 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<String> * @param banned List<String>
* *
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List) * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List<String>)
*/ */
@Override @Override
public synchronized void purgeBanned(List<String> banned) { public void purgeBanned(List<String> banned) {
PreparedStatement pst = null;
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this)
.delete()
.from(tableName)
.addWhere(columnName + "=?", null)
.build()
.getQuery());
for (String name : banned) { for (String name : banned) {
pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + columnName + "=?;");
pst.setString(1, name); pst.setString(1, name);
pst.executeUpdate(); pst.executeUpdate();
} }
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex); } finally {
close(pst);
} }
} }
@ -734,21 +653,22 @@ public class SQLite implements DataSource {
*/ */
@Override @Override
public boolean isLogged(String user) { public boolean isLogged(String user) {
boolean isLogged = false; PreparedStatement pst = null;
ResultSet rs = null;
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE LOWER(" + columnName + ")=?;");
.select(columnLogged) pst.setString(1, user);
.from(tableName) rs = pst.executeQuery();
.addWhere(columnName + "='" + user + "'", null) if (rs.next())
.build() return (rs.getInt(columnLogged) == 1);
.getQuery());
ResultSet rs = pst.executeQuery();
isLogged = rs.next() && (rs.getInt(columnLogged) == 1);
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); 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 @Override
public void setLogged(String user) { public void setLogged(String user) {
PreparedStatement pst = null;
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE LOWER(" + columnName + ")=?;");
.update() pst.setInt(1, 1);
.from(tableName) pst.setString(2, user);
.addUpdateSet(columnLogged + "='1'")
.addWhere(columnName + "='" + user.toLowerCase() + "'", null)
.build()
.getQuery());
pst.executeUpdate(); pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex); } finally {
close(pst);
} }
} }
@ -784,19 +702,18 @@ public class SQLite implements DataSource {
*/ */
@Override @Override
public void setUnlogged(String user) { public void setUnlogged(String user) {
try { PreparedStatement pst = null;
PreparedStatement pst = getConnection().prepareStatement(new Query(this) if (user != null)
.update() try {
.from(tableName) pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE LOWER(" + columnName + ")=?;");
.addUpdateSet(columnLogged + "='0'") pst.setInt(1, 0);
.addWhere(columnName + "='" + user.toLowerCase() + "'", null) pst.setString(2, user);
.build() pst.executeUpdate();
.getQuery()); } catch (SQLException ex) {
pst.executeUpdate(); ConsoleLogger.showError(ex.getMessage());
} catch (SQLException ex) { } finally {
ConsoleLogger.showError(ex.getMessage()); close(pst);
ConsoleLogger.writeStackTrace(ex); }
}
} }
/** /**
@ -806,45 +723,40 @@ public class SQLite implements DataSource {
*/ */
@Override @Override
public void purgeLogged() { public void purgeLogged() {
PreparedStatement pst = null;
try { try {
PreparedStatement pst = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnLogged + "=? WHERE " + columnLogged + "=?;");
.update() pst.setInt(1, 0);
.from(tableName) pst.setInt(2, 1);
.addUpdateSet(columnLogged + "='0'")
.addWhere(columnLogged + "='1'", null)
.build()
.getQuery());
pst.executeUpdate(); pst.executeUpdate();
} catch (Exception ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex); } finally {
close(pst);
} }
} }
/** /**
* Method getAccountsRegistered. * Method getAccountsRegistered.
* *
* @return int * @return int * @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered()
*
* @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered()
*/ */
@Override @Override
public int getAccountsRegistered() { public int getAccountsRegistered() {
int result = 0; int result = 0;
PreparedStatement pst = null;
ResultSet rs;
try { try {
PreparedStatement st = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("SELECT COUNT(*) FROM " + tableName + ";");
.select("COUNT(*)") rs = pst.executeQuery();
.from(tableName) if (rs != null && rs.next()) {
.build()
.getQuery());
ResultSet rs = st.executeQuery();
if (rs.next()) {
result = rs.getInt(1); result = rs.getInt(1);
} }
rs.close(); } catch (SQLException ex) {
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex); return result;
} finally {
close(pst);
} }
return result; return result;
} }
@ -859,63 +771,50 @@ public class SQLite implements DataSource {
*/ */
@Override @Override
public void updateName(String oldOne, String newOne) { public void updateName(String oldOne, String newOne) {
PreparedStatement pst = null;
try { try {
PreparedStatement pst = pst = con.prepareStatement("UPDATE " + tableName + " SET " + columnName + "=? WHERE " + columnName + "=?;");
getConnection().prepareStatement(new Query(this) pst.setString(1, newOne);
.update() pst.setString(2, oldOne);
.from(tableName)
.addUpdateSet(columnName + "='" + newOne + "'")
.addWhere(columnName + "='" + oldOne + "'", null)
.build()
.getQuery());
pst.executeUpdate(); pst.executeUpdate();
} catch (Exception ex) { } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex); } finally {
close(pst);
} }
} }
/** /**
* Method getAllAuths. * Method getAllAuths.
* *
* @return List * @return List<PlayerAuth> * @see fr.xephi.authme.datasource.DataSource#getAllAuths()
*
* @see fr.xephi.authme.datasource.DataSource#getAllAuths()
*/ */
@Override @Override
public List<PlayerAuth> getAllAuths() { public List<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<>(); List<PlayerAuth> auths = new ArrayList<>();
PreparedStatement pst = null;
ResultSet rs;
try { try {
PreparedStatement st = getConnection().prepareStatement(new Query(this) pst = con.prepareStatement("SELECT * FROM " + tableName + ";");
.select("*") rs = pst.executeQuery();
.from(tableName)
.build()
.getQuery());
ResultSet rs = st
.executeQuery();
while (rs.next()) { while (rs.next()) {
String salt = !columnSalt.isEmpty() ? rs.getString(columnSalt) : ""; PlayerAuth pAuth;
int group = !salt.isEmpty() && !columnGroup.isEmpty() ? rs.getInt(columnGroup) : -1; if (rs.getString(columnIp).isEmpty()) {
PlayerAuth pAuth = PlayerAuth.builder() 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));
.name(rs.getString(columnName)) } else {
.realName(rs.getString(columnRealName)) if (!columnSalt.isEmpty()) {
.hash(rs.getString(columnPassword)) 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));
.lastLogin(rs.getLong(columnLastLogin)) } else {
.ip(rs.getString(columnIp)) 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));
.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); auths.add(pAuth);
} }
rs.close(); } catch (SQLException ex) {
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage()); ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex); return auths;
} finally {
close(pst);
} }
return auths; return auths;
} }

View File

@ -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<String, String> where = new HashMap<String, String>();
private List<String> into = new ArrayList<String>();
private List<String> values = new ArrayList<String>();
private List<String> updateSet = new ArrayList<String>();
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<String> 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<String> 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;
}
}