mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-28 19:47:35 +01:00
Start a rework of some SQL Queries, add a Query builder
This commit is contained in:
parent
f3a5e2edd8
commit
da1adb632e
@ -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<PlayerAuth> getLoggedPlayers() {
|
||||
return new ArrayList<>(PlayerCache.getInstance().getCache().values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
return source.getConnection();
|
||||
}
|
||||
}
|
||||
|
@ -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<PlayerAuth> getLoggedPlayers();
|
||||
|
||||
Connection getConnection() throws SQLException;
|
||||
|
||||
enum DataSourceType {
|
||||
MYSQL,
|
||||
FILE,
|
||||
|
@ -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<PlayerAuth> getLoggedPlayers() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -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 + "<?;";
|
||||
PreparedStatement pst = con.prepareStatement(sql);
|
||||
try(Connection con = getConnection()) {
|
||||
PreparedStatement pst = con.prepareStatement(new Query(this)
|
||||
.delete()
|
||||
.from(tableName)
|
||||
.addWhere(columnLastLogin + "<?", null)
|
||||
.build()
|
||||
.getQuery());
|
||||
pst.setLong(1, until);
|
||||
result = pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
@ -648,16 +673,26 @@ public class MySQL implements DataSource {
|
||||
@Override
|
||||
public synchronized List<String> autoPurgeDatabase(long until) {
|
||||
List<String> 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<String> getAllAuthsByName(PlayerAuth auth) {
|
||||
List<String> 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<String> getAllAuthsByIp(String ip) {
|
||||
List<String> 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<String> getAllAuthsByEmail(String email){
|
||||
List<String> 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<String> 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<PlayerAuth> getAllAuths() {
|
||||
List<PlayerAuth> 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;
|
||||
|
@ -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 + "<?;");
|
||||
PreparedStatement pst = getConnection().prepareStatement(new Query(this)
|
||||
.delete()
|
||||
.from(tableName)
|
||||
.addWhere(columnLastLogin + "<?", null)
|
||||
.build()
|
||||
.getQuery());
|
||||
pst.setLong(1, until);
|
||||
return pst.executeUpdate();
|
||||
result = pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
ConsoleLogger.showError(ex.getMessage());
|
||||
return 0;
|
||||
} finally {
|
||||
close(pst);
|
||||
ConsoleLogger.writeStackTrace(ex);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -311,28 +356,37 @@ public class SQLite implements DataSource {
|
||||
*
|
||||
* @param until long
|
||||
*
|
||||
* @return List<String> * @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long)
|
||||
* @return List
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long)
|
||||
*/
|
||||
@Override
|
||||
public List<String> autoPurgeDatabase(long until) {
|
||||
PreparedStatement pst = null;
|
||||
ResultSet rs = null;
|
||||
public synchronized List<String> autoPurgeDatabase(long until) {
|
||||
List<String> list = new ArrayList<>();
|
||||
try {
|
||||
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnLastLogin + "<?;");
|
||||
pst.setLong(1, until);
|
||||
rs = pst.executeQuery();
|
||||
PreparedStatement st = getConnection().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));
|
||||
}
|
||||
return list;
|
||||
rs.close();
|
||||
st = getConnection().prepareStatement(new Query(this)
|
||||
.delete()
|
||||
.from(tableName)
|
||||
.addWhere(columnLastLogin + "<" + until, null)
|
||||
.build()
|
||||
.getQuery());
|
||||
st.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
ConsoleLogger.showError(ex.getMessage());
|
||||
return new ArrayList<>();
|
||||
} 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<String> * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth)
|
||||
* @return List
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth)
|
||||
*/
|
||||
@Override
|
||||
public List<String> getAllAuthsByName(PlayerAuth auth) {
|
||||
PreparedStatement pst = null;
|
||||
ResultSet rs = null;
|
||||
List<String> countIp = new ArrayList<>();
|
||||
try {
|
||||
pst = con.prepareStatement("SELECT * FROM " + tableName + " WHERE " + columnIp + "=?;");
|
||||
pst.setString(1, auth.getIp());
|
||||
rs = pst.executeQuery();
|
||||
public synchronized List<String> getAllAuthsByName(PlayerAuth auth) {
|
||||
List<String> 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<String> * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String)
|
||||
* @return List
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String)
|
||||
*/
|
||||
@Override
|
||||
public List<String> getAllAuthsByIp(String ip) {
|
||||
PreparedStatement pst = null;
|
||||
ResultSet rs = null;
|
||||
List<String> countIp = new ArrayList<>();
|
||||
public synchronized List<String> getAllAuthsByIp(String ip) {
|
||||
List<String> 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<String> * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String)
|
||||
* @return List
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String)
|
||||
*/
|
||||
@Override
|
||||
public List<String> getAllAuthsByEmail(String email) {
|
||||
PreparedStatement pst = null;
|
||||
ResultSet rs = null;
|
||||
public synchronized List<String> getAllAuthsByEmail(String email){
|
||||
List<String> 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<String>
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List<String>)
|
||||
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List)
|
||||
*/
|
||||
@Override
|
||||
public void purgeBanned(List<String> banned) {
|
||||
PreparedStatement pst = null;
|
||||
public synchronized void purgeBanned(List<String> 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<PlayerAuth> * @see fr.xephi.authme.datasource.DataSource#getAllAuths()
|
||||
* @return List
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#getAllAuths()
|
||||
*/
|
||||
@Override
|
||||
public List<PlayerAuth> getAllAuths() {
|
||||
List<PlayerAuth> 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;
|
||||
}
|
||||
|
215
src/main/java/fr/xephi/authme/datasource/queries/Query.java
Normal file
215
src/main/java/fr/xephi/authme/datasource/queries/Query.java
Normal file
@ -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<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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user