Split MySQL into a SQLDataSource abstract class
* Create an abstract class with current MySQL Methods * Provide a way to init HikariCP DataSource for each classes * Make MySQL extends correctly this class * Create PostgreSQL which extends MySQL due to same grammar
This commit is contained in:
parent
9b60c26a1d
commit
69754ccec0
|
@ -247,7 +247,7 @@ class MySqlDefaultChanger implements DebugSection {
|
|||
@VisibleForTesting
|
||||
Connection getConnection(MySQL mySql) {
|
||||
try {
|
||||
Method method = MySQL.class.getDeclaredMethod("getConnection");
|
||||
Method method = MySQL.class.getSuperclass().getDeclaredMethod("getConnection");
|
||||
method.setAccessible(true);
|
||||
return (Connection) method.invoke(mySql);
|
||||
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
|
|
|
@ -7,6 +7,8 @@ public enum DataSourceType {
|
|||
|
||||
MYSQL,
|
||||
|
||||
POSTGRESQL,
|
||||
|
||||
SQLITE,
|
||||
|
||||
@Deprecated
|
||||
|
|
|
@ -4,48 +4,21 @@ import com.google.common.annotations.VisibleForTesting;
|
|||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import com.zaxxer.hikari.pool.HikariPool.PoolInitializationException;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtension;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtensionsFactory;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import fr.xephi.authme.datasource.sqlextensions.SqlExtensionsFactory;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
import fr.xephi.authme.settings.properties.HooksSettings;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import fr.xephi.authme.util.Utils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.getNullableLong;
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException;
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.isColumnMissing;
|
||||
|
||||
public class MySQL implements DataSource {
|
||||
public class MySQL extends SqlDataSource {
|
||||
|
||||
private boolean useSsl;
|
||||
private String host;
|
||||
private String port;
|
||||
private String username;
|
||||
private String password;
|
||||
private String database;
|
||||
private String tableName;
|
||||
private int poolSize;
|
||||
private int maxLifetime;
|
||||
private List<String> columnOthers;
|
||||
private Columns col;
|
||||
private MySqlExtension sqlExtension;
|
||||
private HikariDataSource ds;
|
||||
|
||||
public MySQL(Settings settings, MySqlExtensionsFactory extensionsFactory) throws SQLException {
|
||||
public MySQL(Settings settings, SqlExtensionsFactory extensionsFactory) throws SQLException {
|
||||
setParameters(settings, extensionsFactory);
|
||||
|
||||
// Set the connection arguments (and check if connection is ok)
|
||||
|
@ -69,14 +42,14 @@ public class MySQL implements DataSource {
|
|||
checkTablesAndColumns();
|
||||
} catch (SQLException e) {
|
||||
closeConnection();
|
||||
ConsoleLogger.logException("Can't initialize the MySQL database:", e);
|
||||
ConsoleLogger.logException("Can't initialize the database:", e);
|
||||
ConsoleLogger.warning("Please check your database settings in the config.yml file!");
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
MySQL(Settings settings, HikariDataSource hikariDataSource, MySqlExtensionsFactory extensionsFactory) {
|
||||
MySQL(Settings settings, HikariDataSource hikariDataSource, SqlExtensionsFactory extensionsFactory) {
|
||||
ds = hikariDataSource;
|
||||
setParameters(settings, extensionsFactory);
|
||||
}
|
||||
|
@ -87,7 +60,7 @@ public class MySQL implements DataSource {
|
|||
* @param settings the settings to read properties from
|
||||
* @param extensionsFactory factory to create the MySQL extension
|
||||
*/
|
||||
private void setParameters(Settings settings, MySqlExtensionsFactory extensionsFactory) {
|
||||
private void setParameters(Settings settings, SqlExtensionsFactory extensionsFactory) {
|
||||
this.host = settings.getProperty(DatabaseSettings.MYSQL_HOST);
|
||||
this.port = settings.getProperty(DatabaseSettings.MYSQL_PORT);
|
||||
this.username = settings.getProperty(DatabaseSettings.MYSQL_USERNAME);
|
||||
|
@ -102,20 +75,26 @@ public class MySQL implements DataSource {
|
|||
this.useSsl = settings.getProperty(DatabaseSettings.MYSQL_USE_SSL);
|
||||
}
|
||||
|
||||
void initHikariDataSource() {
|
||||
|
||||
ds = new HikariDataSource();
|
||||
ds.setPoolName("AuthMeMYSQLPool");
|
||||
|
||||
// Database URL
|
||||
ds.setJdbcUrl("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the connection arguments to the database.
|
||||
*/
|
||||
private void setConnectionArguments() {
|
||||
ds = new HikariDataSource();
|
||||
ds.setPoolName("AuthMeMYSQLPool");
|
||||
void setConnectionArguments() {
|
||||
|
||||
initHikariDataSource();
|
||||
|
||||
// Pool Settings
|
||||
ds.setMaximumPoolSize(poolSize);
|
||||
ds.setMaxLifetime(maxLifetime * 1000);
|
||||
|
||||
// Database URL
|
||||
ds.setJdbcUrl("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database);
|
||||
|
||||
// Auth
|
||||
ds.setUsername(this.username);
|
||||
ds.setPassword(this.password);
|
||||
|
@ -140,19 +119,6 @@ public class MySQL implements DataSource {
|
|||
ConsoleLogger.info("Connection arguments loaded, Hikari ConnectionPool ready!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() {
|
||||
if (ds != null) {
|
||||
ds.close();
|
||||
}
|
||||
setConnectionArguments();
|
||||
ConsoleLogger.info("Hikari ConnectionPool arguments reloaded!");
|
||||
}
|
||||
|
||||
private Connection getConnection() throws SQLException {
|
||||
return ds.getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the table or any of its required columns if they don't exist.
|
||||
*/
|
||||
|
@ -166,49 +132,49 @@ public class MySQL implements DataSource {
|
|||
st.executeUpdate(sql);
|
||||
|
||||
DatabaseMetaData md = con.getMetaData();
|
||||
if (isColumnMissing(md, col.NAME)) {
|
||||
if (isColumnMissing(md, col.NAME, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.NAME + " VARCHAR(255) NOT NULL UNIQUE AFTER " + col.ID + ";");
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.REAL_NAME)) {
|
||||
if (isColumnMissing(md, col.REAL_NAME, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.REAL_NAME + " VARCHAR(255) NOT NULL AFTER " + col.NAME + ";");
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.PASSWORD)) {
|
||||
if (isColumnMissing(md, col.PASSWORD, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.PASSWORD + " VARCHAR(255) CHARACTER SET ascii COLLATE ascii_bin NOT NULL;");
|
||||
}
|
||||
|
||||
if (!col.SALT.isEmpty() && isColumnMissing(md, col.SALT)) {
|
||||
if (!col.SALT.isEmpty() && isColumnMissing(md, col.SALT, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.SALT + " VARCHAR(255);");
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.LAST_IP)) {
|
||||
if (isColumnMissing(md, col.LAST_IP, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.LAST_IP + " VARCHAR(40) CHARACTER SET ascii COLLATE ascii_bin;");
|
||||
} else {
|
||||
MySqlMigrater.migrateLastIpColumn(st, md, tableName, col);
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.LAST_LOGIN)) {
|
||||
if (isColumnMissing(md, col.LAST_LOGIN, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.LAST_LOGIN + " BIGINT;");
|
||||
} else {
|
||||
MySqlMigrater.migrateLastLoginColumn(st, md, tableName, col);
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.REGISTRATION_DATE)) {
|
||||
if (isColumnMissing(md, col.REGISTRATION_DATE, tableName)) {
|
||||
MySqlMigrater.addRegistrationDateColumn(st, tableName, col);
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.REGISTRATION_IP)) {
|
||||
if (isColumnMissing(md, col.REGISTRATION_IP, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName
|
||||
+ " ADD COLUMN " + col.REGISTRATION_IP + " VARCHAR(40) CHARACTER SET ascii COLLATE ascii_bin;");
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.LASTLOC_X)) {
|
||||
if (isColumnMissing(md, col.LASTLOC_X, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
|
||||
+ col.LASTLOC_X + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LAST_LOGIN + " , ADD "
|
||||
+ col.LASTLOC_Y + " DOUBLE NOT NULL DEFAULT '0.0' AFTER " + col.LASTLOC_X + " , ADD "
|
||||
|
@ -220,32 +186,32 @@ public class MySQL implements DataSource {
|
|||
+ col.LASTLOC_Z + " DOUBLE NOT NULL DEFAULT '0.0';");
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.LASTLOC_WORLD)) {
|
||||
if (isColumnMissing(md, col.LASTLOC_WORLD, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
|
||||
+ col.LASTLOC_WORLD + " VARCHAR(255) NOT NULL DEFAULT 'world' AFTER " + col.LASTLOC_Z);
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.LASTLOC_YAW)) {
|
||||
if (isColumnMissing(md, col.LASTLOC_YAW, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
|
||||
+ col.LASTLOC_YAW + " FLOAT;");
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.LASTLOC_PITCH)) {
|
||||
if (isColumnMissing(md, col.LASTLOC_PITCH, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
|
||||
+ col.LASTLOC_PITCH + " FLOAT;");
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.EMAIL)) {
|
||||
if (isColumnMissing(md, col.EMAIL, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
|
||||
+ col.EMAIL + " VARCHAR(255);");
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.IS_LOGGED)) {
|
||||
if (isColumnMissing(md, col.IS_LOGGED, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
|
||||
+ col.IS_LOGGED + " SMALLINT NOT NULL DEFAULT '0' AFTER " + col.EMAIL);
|
||||
}
|
||||
|
||||
if (isColumnMissing(md, col.HAS_SESSION)) {
|
||||
if (isColumnMissing(md, col.HAS_SESSION, tableName)) {
|
||||
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
|
||||
+ col.HAS_SESSION + " SMALLINT NOT NULL DEFAULT '0' AFTER " + col.IS_LOGGED);
|
||||
}
|
||||
|
@ -253,500 +219,6 @@ public class MySQL implements DataSource {
|
|||
ConsoleLogger.info("MySQL setup finished");
|
||||
}
|
||||
|
||||
private boolean isColumnMissing(DatabaseMetaData metaData, String columnName) throws SQLException {
|
||||
try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) {
|
||||
return !rs.next();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAuthAvailable(String user) {
|
||||
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user.toLowerCase());
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
return rs.next();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashedPassword getPassword(String user) {
|
||||
boolean useSalt = !col.SALT.isEmpty();
|
||||
String sql = "SELECT " + col.PASSWORD
|
||||
+ (useSalt ? ", " + col.SALT : "")
|
||||
+ " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user.toLowerCase());
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
return new HashedPassword(rs.getString(col.PASSWORD),
|
||||
useSalt ? rs.getString(col.SALT) : null);
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerAuth getAuth(String user) {
|
||||
String sql = "SELECT * FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
PlayerAuth auth;
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user.toLowerCase());
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
int id = rs.getInt(col.ID);
|
||||
auth = buildAuthFromResultSet(rs);
|
||||
sqlExtension.extendAuth(auth, id, con);
|
||||
return auth;
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveAuth(PlayerAuth auth) {
|
||||
try (Connection con = getConnection()) {
|
||||
// TODO ljacqu 20171104: Replace with generic columns util to clean this up
|
||||
boolean useSalt = !col.SALT.isEmpty() || !StringUtils.isEmpty(auth.getPassword().getSalt());
|
||||
boolean hasEmail = auth.getEmail() != null;
|
||||
String emailPlaceholder = hasEmail ? "?" : "DEFAULT";
|
||||
|
||||
String sql = "INSERT INTO " + tableName + "("
|
||||
+ col.NAME + "," + col.PASSWORD + "," + col.REAL_NAME
|
||||
+ "," + col.EMAIL + "," + col.REGISTRATION_DATE + "," + col.REGISTRATION_IP
|
||||
+ (useSalt ? "," + col.SALT : "")
|
||||
+ ") VALUES (?,?,?," + emailPlaceholder + ",?,?" + (useSalt ? ",?" : "") + ");";
|
||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
pst.setString(index++, auth.getNickname());
|
||||
pst.setString(index++, auth.getPassword().getHash());
|
||||
pst.setString(index++, auth.getRealName());
|
||||
if (hasEmail) {
|
||||
pst.setString(index++, auth.getEmail());
|
||||
}
|
||||
pst.setObject(index++, auth.getRegistrationDate());
|
||||
pst.setString(index++, auth.getRegistrationIp());
|
||||
if (useSalt) {
|
||||
pst.setString(index++, auth.getPassword().getSalt());
|
||||
}
|
||||
pst.executeUpdate();
|
||||
}
|
||||
|
||||
if (!columnOthers.isEmpty()) {
|
||||
for (String column : columnOthers) {
|
||||
try (PreparedStatement pst = con.prepareStatement("UPDATE " + tableName + " SET " + column + "=? WHERE " + col.NAME + "=?;")) {
|
||||
pst.setString(1, auth.getRealName());
|
||||
pst.setString(2, auth.getNickname());
|
||||
pst.executeUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlExtension.saveAuth(auth, con);
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePassword(PlayerAuth auth) {
|
||||
return updatePassword(auth.getNickname(), auth.getPassword());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePassword(String user, HashedPassword password) {
|
||||
user = user.toLowerCase();
|
||||
try (Connection con = getConnection()) {
|
||||
boolean useSalt = !col.SALT.isEmpty();
|
||||
if (useSalt) {
|
||||
String sql = String.format("UPDATE %s SET %s = ?, %s = ? WHERE %s = ?;",
|
||||
tableName, col.PASSWORD, col.SALT, col.NAME);
|
||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, password.getHash());
|
||||
pst.setString(2, password.getSalt());
|
||||
pst.setString(3, user);
|
||||
pst.executeUpdate();
|
||||
}
|
||||
} else {
|
||||
String sql = String.format("UPDATE %s SET %s = ? WHERE %s = ?;",
|
||||
tableName, col.PASSWORD, col.NAME);
|
||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, password.getHash());
|
||||
pst.setString(2, user);
|
||||
pst.executeUpdate();
|
||||
}
|
||||
}
|
||||
sqlExtension.changePassword(user, password, con);
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateSession(PlayerAuth auth) {
|
||||
String sql = "UPDATE " + tableName + " SET "
|
||||
+ col.LAST_IP + "=?, " + col.LAST_LOGIN + "=?, " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, auth.getLastIp());
|
||||
pst.setObject(2, auth.getLastLogin());
|
||||
pst.setString(3, auth.getRealName());
|
||||
pst.setString(4, auth.getNickname());
|
||||
pst.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getRecordsToPurge(long until) {
|
||||
Set<String> list = new HashSet<>();
|
||||
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE GREATEST("
|
||||
+ " COALESCE(" + col.LAST_LOGIN + ", 0),"
|
||||
+ " COALESCE(" + col.REGISTRATION_DATE + ", 0)"
|
||||
+ ") < ?;";
|
||||
try (Connection con = getConnection();
|
||||
PreparedStatement selectPst = con.prepareStatement(select)) {
|
||||
selectPst.setLong(1, until);
|
||||
try (ResultSet rs = selectPst.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
list.add(rs.getString(col.NAME));
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAuth(String user) {
|
||||
user = user.toLowerCase();
|
||||
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
sqlExtension.removeAuth(user, con);
|
||||
pst.setString(1, user.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateQuitLoc(PlayerAuth auth) {
|
||||
String sql = "UPDATE " + tableName
|
||||
+ " SET " + col.LASTLOC_X + " =?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, "
|
||||
+ col.LASTLOC_WORLD + "=?, " + col.LASTLOC_YAW + "=?, " + col.LASTLOC_PITCH + "=?"
|
||||
+ " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setDouble(1, auth.getQuitLocX());
|
||||
pst.setDouble(2, auth.getQuitLocY());
|
||||
pst.setDouble(3, auth.getQuitLocZ());
|
||||
pst.setString(4, auth.getWorld());
|
||||
pst.setFloat(5, auth.getYaw());
|
||||
pst.setFloat(6, auth.getPitch());
|
||||
pst.setString(7, auth.getNickname());
|
||||
pst.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEmail(PlayerAuth auth) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.EMAIL + " =? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, auth.getEmail());
|
||||
pst.setString(2, auth.getNickname());
|
||||
pst.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeConnection() {
|
||||
if (ds != null && !ds.isClosed()) {
|
||||
ds.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAllAuthsByIp(String ip) {
|
||||
List<String> result = new ArrayList<>();
|
||||
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_IP + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, ip);
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
result.add(rs.getString(col.NAME));
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int countAuthsByEmail(String email) {
|
||||
String sql = "SELECT COUNT(1) FROM " + tableName + " WHERE UPPER(" + col.EMAIL + ") = UPPER(?)";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, email);
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1);
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void purgeRecords(Collection<String> toPurge) {
|
||||
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
for (String name : toPurge) {
|
||||
pst.setString(1, name.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceType getType() {
|
||||
return DataSourceType.MYSQL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLogged(String user) {
|
||||
String sql = "SELECT " + col.IS_LOGGED + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user);
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
return rs.next() && (rs.getInt(col.IS_LOGGED) == 1);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLogged(String user) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setInt(1, 1);
|
||||
pst.setString(2, user.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUnlogged(String user) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setInt(1, 0);
|
||||
pst.setString(2, user.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSession(String user) {
|
||||
String sql = "SELECT " + col.HAS_SESSION + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user.toLowerCase());
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
return rs.next() && (rs.getInt(col.HAS_SESSION) == 1);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void grantSession(String user) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.HAS_SESSION + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setInt(1, 1);
|
||||
pst.setString(2, user.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void revokeSession(String user) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.HAS_SESSION + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setInt(1, 0);
|
||||
pst.setString(2, user.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void purgeLogged() {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.IS_LOGGED + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setInt(1, 0);
|
||||
pst.setInt(2, 1);
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAccountsRegistered() {
|
||||
int result = 0;
|
||||
String sql = "SELECT COUNT(*) FROM " + tableName;
|
||||
try (Connection con = getConnection();
|
||||
Statement st = con.createStatement();
|
||||
ResultSet rs = st.executeQuery(sql)) {
|
||||
if (rs.next()) {
|
||||
result = rs.getInt(1);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateRealName(String user, String realName) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, realName);
|
||||
pst.setString(2, user);
|
||||
pst.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceResult<String> getEmail(String user) {
|
||||
String sql = "SELECT " + col.EMAIL + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user);
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
return DataSourceResult.of(rs.getString(1));
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return DataSourceResult.unknownPlayer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PlayerAuth> getAllAuths() {
|
||||
List<PlayerAuth> auths = new ArrayList<>();
|
||||
try (Connection con = getConnection(); Statement st = con.createStatement()) {
|
||||
try (ResultSet rs = st.executeQuery("SELECT * FROM " + tableName)) {
|
||||
while (rs.next()) {
|
||||
PlayerAuth auth = buildAuthFromResultSet(rs);
|
||||
sqlExtension.extendAuth(auth, rs.getInt(col.ID), con);
|
||||
auths.add(auth);
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return auths;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getLoggedPlayersWithEmptyMail() {
|
||||
List<String> players = new ArrayList<>();
|
||||
String sql = "SELECT " + col.REAL_NAME + " FROM " + tableName + " WHERE " + col.IS_LOGGED + " = 1"
|
||||
+ " AND (" + col.EMAIL + " = 'your@email.com' OR " + col.EMAIL + " IS NULL);";
|
||||
try (Connection con = getConnection();
|
||||
Statement st = con.createStatement();
|
||||
ResultSet rs = st.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
players.add(rs.getString(1));
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PlayerAuth> getRecentlyLoggedInPlayers() {
|
||||
List<PlayerAuth> players = new ArrayList<>();
|
||||
String sql = "SELECT * FROM " + tableName + " ORDER BY " + col.LAST_LOGIN + " DESC LIMIT 10;";
|
||||
try (Connection con = getConnection();
|
||||
Statement st = con.createStatement();
|
||||
ResultSet rs = st.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
players.add(buildAuthFromResultSet(rs));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
logSqlException(e);
|
||||
}
|
||||
return players;
|
||||
}
|
||||
|
||||
private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException {
|
||||
String salt = col.SALT.isEmpty() ? null : row.getString(col.SALT);
|
||||
int group = col.GROUP.isEmpty() ? -1 : row.getInt(col.GROUP);
|
||||
return PlayerAuth.builder()
|
||||
.name(row.getString(col.NAME))
|
||||
.realName(row.getString(col.REAL_NAME))
|
||||
.password(row.getString(col.PASSWORD), salt)
|
||||
.lastLogin(getNullableLong(row, col.LAST_LOGIN))
|
||||
.lastIp(row.getString(col.LAST_IP))
|
||||
.email(row.getString(col.EMAIL))
|
||||
.registrationDate(row.getLong(col.REGISTRATION_DATE))
|
||||
.registrationIp(row.getString(col.REGISTRATION_IP))
|
||||
.groupId(group)
|
||||
.locWorld(row.getString(col.LASTLOC_WORLD))
|
||||
.locX(row.getDouble(col.LASTLOC_X))
|
||||
.locY(row.getDouble(col.LASTLOC_Y))
|
||||
.locZ(row.getDouble(col.LASTLOC_Z))
|
||||
.locYaw(row.getFloat(col.LASTLOC_YAW))
|
||||
.locPitch(row.getFloat(col.LASTLOC_PITCH))
|
||||
.build();
|
||||
}
|
||||
public DataSourceType getType() { return DataSourceType.MYSQL; }
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package fr.xephi.authme.datasource;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import fr.xephi.authme.datasource.sqlextensions.SqlExtensionsFactory;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class PostgreSQL extends MySQL {
|
||||
|
||||
public PostgreSQL(Settings settings, SqlExtensionsFactory extensionsFactory) throws SQLException {
|
||||
super(settings, extensionsFactory);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
PostgreSQL(Settings settings, HikariDataSource hikariDataSource, SqlExtensionsFactory extensionsFactory) {
|
||||
super(settings, hikariDataSource, extensionsFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
void initHikariDataSource() {
|
||||
|
||||
ds = new HikariDataSource();
|
||||
ds.setPoolName("AuthMePostgreSQLPool");
|
||||
|
||||
// Database URL
|
||||
ds.setJdbcUrl("jdbc:postgresql://" + this.host + ":" + this.port + "/" + this.database);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceType getType() { return DataSourceType.POSTGRESQL; }
|
||||
}
|
|
@ -0,0 +1,541 @@
|
|||
package fr.xephi.authme.datasource;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.sqlextensions.SqlExtension;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.getNullableLong;
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException;
|
||||
|
||||
public abstract class SqlDataSource implements DataSource {
|
||||
|
||||
boolean useSsl;
|
||||
String host;
|
||||
String port;
|
||||
String username;
|
||||
String password;
|
||||
String database;
|
||||
String tableName;
|
||||
int poolSize;
|
||||
int maxLifetime;
|
||||
List<String> columnOthers;
|
||||
Columns col;
|
||||
SqlExtension sqlExtension;
|
||||
HikariDataSource ds;
|
||||
|
||||
abstract void setConnectionArguments();
|
||||
abstract void initHikariDataSource();
|
||||
|
||||
@Override
|
||||
public void reload() {
|
||||
if (ds != null) {
|
||||
ds.close();
|
||||
}
|
||||
setConnectionArguments();
|
||||
ConsoleLogger.info("Hikari ConnectionPool arguments reloaded!");
|
||||
}
|
||||
|
||||
Connection getConnection() throws SQLException {
|
||||
return ds.getConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAuthAvailable(String user) {
|
||||
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user.toLowerCase());
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
return rs.next();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashedPassword getPassword(String user) {
|
||||
boolean useSalt = !col.SALT.isEmpty();
|
||||
String sql = "SELECT " + col.PASSWORD
|
||||
+ (useSalt ? ", " + col.SALT : "")
|
||||
+ " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user.toLowerCase());
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
return new HashedPassword(rs.getString(col.PASSWORD),
|
||||
useSalt ? rs.getString(col.SALT) : null);
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlayerAuth getAuth(String user) {
|
||||
String sql = "SELECT * FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
PlayerAuth auth;
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user.toLowerCase());
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
int id = rs.getInt(col.ID);
|
||||
auth = buildAuthFromResultSet(rs);
|
||||
sqlExtension.extendAuth(auth, id, con);
|
||||
return auth;
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveAuth(PlayerAuth auth) {
|
||||
try (Connection con = getConnection()) {
|
||||
// TODO ljacqu 20171104: Replace with generic columns util to clean this up
|
||||
boolean useSalt = !col.SALT.isEmpty() || !StringUtils.isEmpty(auth.getPassword().getSalt());
|
||||
boolean hasEmail = auth.getEmail() != null;
|
||||
String emailPlaceholder = hasEmail ? "?" : "DEFAULT";
|
||||
|
||||
String sql = "INSERT INTO " + tableName + "("
|
||||
+ col.NAME + "," + col.PASSWORD + "," + col.REAL_NAME
|
||||
+ "," + col.EMAIL + "," + col.REGISTRATION_DATE + "," + col.REGISTRATION_IP
|
||||
+ (useSalt ? "," + col.SALT : "")
|
||||
+ ") VALUES (?,?,?," + emailPlaceholder + ",?,?" + (useSalt ? ",?" : "") + ");";
|
||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
int index = 1;
|
||||
pst.setString(index++, auth.getNickname());
|
||||
pst.setString(index++, auth.getPassword().getHash());
|
||||
pst.setString(index++, auth.getRealName());
|
||||
if (hasEmail) {
|
||||
pst.setString(index++, auth.getEmail());
|
||||
}
|
||||
pst.setObject(index++, auth.getRegistrationDate());
|
||||
pst.setString(index++, auth.getRegistrationIp());
|
||||
if (useSalt) {
|
||||
pst.setString(index++, auth.getPassword().getSalt());
|
||||
}
|
||||
pst.executeUpdate();
|
||||
}
|
||||
|
||||
if (!columnOthers.isEmpty()) {
|
||||
for (String column : columnOthers) {
|
||||
try (PreparedStatement pst = con.prepareStatement("UPDATE " + tableName + " SET " + column + "=? WHERE " + col.NAME + "=?;")) {
|
||||
pst.setString(1, auth.getRealName());
|
||||
pst.setString(2, auth.getNickname());
|
||||
pst.executeUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlExtension.saveAuth(auth, con);
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePassword(PlayerAuth auth) {
|
||||
return updatePassword(auth.getNickname(), auth.getPassword());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updatePassword(String user, HashedPassword password) {
|
||||
user = user.toLowerCase();
|
||||
try (Connection con = getConnection()) {
|
||||
boolean useSalt = !col.SALT.isEmpty();
|
||||
if (useSalt) {
|
||||
String sql = String.format("UPDATE %s SET %s = ?, %s = ? WHERE %s = ?;",
|
||||
tableName, col.PASSWORD, col.SALT, col.NAME);
|
||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, password.getHash());
|
||||
pst.setString(2, password.getSalt());
|
||||
pst.setString(3, user);
|
||||
pst.executeUpdate();
|
||||
}
|
||||
} else {
|
||||
String sql = String.format("UPDATE %s SET %s = ? WHERE %s = ?;",
|
||||
tableName, col.PASSWORD, col.NAME);
|
||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, password.getHash());
|
||||
pst.setString(2, user);
|
||||
pst.executeUpdate();
|
||||
}
|
||||
}
|
||||
sqlExtension.changePassword(user, password, con);
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateSession(PlayerAuth auth) {
|
||||
String sql = "UPDATE " + tableName + " SET "
|
||||
+ col.LAST_IP + "=?, " + col.LAST_LOGIN + "=?, " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, auth.getLastIp());
|
||||
pst.setObject(2, auth.getLastLogin());
|
||||
pst.setString(3, auth.getRealName());
|
||||
pst.setString(4, auth.getNickname());
|
||||
pst.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getRecordsToPurge(long until) {
|
||||
Set<String> list = new HashSet<>();
|
||||
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE GREATEST("
|
||||
+ " COALESCE(" + col.LAST_LOGIN + ", 0),"
|
||||
+ " COALESCE(" + col.REGISTRATION_DATE + ", 0)"
|
||||
+ ") < ?;";
|
||||
try (Connection con = getConnection();
|
||||
PreparedStatement selectPst = con.prepareStatement(select)) {
|
||||
selectPst.setLong(1, until);
|
||||
try (ResultSet rs = selectPst.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
list.add(rs.getString(col.NAME));
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAuth(String user) {
|
||||
user = user.toLowerCase();
|
||||
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
sqlExtension.removeAuth(user, con);
|
||||
pst.setString(1, user.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateQuitLoc(PlayerAuth auth) {
|
||||
String sql = "UPDATE " + tableName
|
||||
+ " SET " + col.LASTLOC_X + " =?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, "
|
||||
+ col.LASTLOC_WORLD + "=?, " + col.LASTLOC_YAW + "=?, " + col.LASTLOC_PITCH + "=?"
|
||||
+ " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setDouble(1, auth.getQuitLocX());
|
||||
pst.setDouble(2, auth.getQuitLocY());
|
||||
pst.setDouble(3, auth.getQuitLocZ());
|
||||
pst.setString(4, auth.getWorld());
|
||||
pst.setFloat(5, auth.getYaw());
|
||||
pst.setFloat(6, auth.getPitch());
|
||||
pst.setString(7, auth.getNickname());
|
||||
pst.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateEmail(PlayerAuth auth) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.EMAIL + " =? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, auth.getEmail());
|
||||
pst.setString(2, auth.getNickname());
|
||||
pst.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeConnection() {
|
||||
if (ds != null && !ds.isClosed()) {
|
||||
ds.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAllAuthsByIp(String ip) {
|
||||
List<String> result = new ArrayList<>();
|
||||
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_IP + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, ip);
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
result.add(rs.getString(col.NAME));
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int countAuthsByEmail(String email) {
|
||||
String sql = "SELECT COUNT(1) FROM " + tableName + " WHERE UPPER(" + col.EMAIL + ") = UPPER(?)";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, email);
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1);
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void purgeRecords(Collection<String> toPurge) {
|
||||
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
for (String name : toPurge) {
|
||||
pst.setString(1, name.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLogged(String user) {
|
||||
String sql = "SELECT " + col.IS_LOGGED + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user);
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
return rs.next() && (rs.getInt(col.IS_LOGGED) == 1);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLogged(String user) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setInt(1, 1);
|
||||
pst.setString(2, user.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUnlogged(String user) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setInt(1, 0);
|
||||
pst.setString(2, user.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSession(String user) {
|
||||
String sql = "SELECT " + col.HAS_SESSION + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user.toLowerCase());
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
return rs.next() && (rs.getInt(col.HAS_SESSION) == 1);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void grantSession(String user) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.HAS_SESSION + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setInt(1, 1);
|
||||
pst.setString(2, user.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void revokeSession(String user) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.HAS_SESSION + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setInt(1, 0);
|
||||
pst.setString(2, user.toLowerCase());
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void purgeLogged() {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.IS_LOGGED + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setInt(1, 0);
|
||||
pst.setInt(2, 1);
|
||||
pst.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAccountsRegistered() {
|
||||
int result = 0;
|
||||
String sql = "SELECT COUNT(*) FROM " + tableName;
|
||||
try (Connection con = getConnection();
|
||||
Statement st = con.createStatement();
|
||||
ResultSet rs = st.executeQuery(sql)) {
|
||||
if (rs.next()) {
|
||||
result = rs.getInt(1);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateRealName(String user, String realName) {
|
||||
String sql = "UPDATE " + tableName + " SET " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, realName);
|
||||
pst.setString(2, user);
|
||||
pst.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSourceResult<String> getEmail(String user) {
|
||||
String sql = "SELECT " + col.EMAIL + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||
pst.setString(1, user);
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
return DataSourceResult.of(rs.getString(1));
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return DataSourceResult.unknownPlayer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PlayerAuth> getAllAuths() {
|
||||
List<PlayerAuth> auths = new ArrayList<>();
|
||||
try (Connection con = getConnection(); Statement st = con.createStatement()) {
|
||||
try (ResultSet rs = st.executeQuery("SELECT * FROM " + tableName)) {
|
||||
while (rs.next()) {
|
||||
PlayerAuth auth = buildAuthFromResultSet(rs);
|
||||
sqlExtension.extendAuth(auth, rs.getInt(col.ID), con);
|
||||
auths.add(auth);
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return auths;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getLoggedPlayersWithEmptyMail() {
|
||||
List<String> players = new ArrayList<>();
|
||||
String sql = "SELECT " + col.REAL_NAME + " FROM " + tableName + " WHERE " + col.IS_LOGGED + " = 1"
|
||||
+ " AND (" + col.EMAIL + " = 'your@email.com' OR " + col.EMAIL + " IS NULL);";
|
||||
try (Connection con = getConnection();
|
||||
Statement st = con.createStatement();
|
||||
ResultSet rs = st.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
players.add(rs.getString(1));
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
logSqlException(ex);
|
||||
}
|
||||
return players;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PlayerAuth> getRecentlyLoggedInPlayers() {
|
||||
List<PlayerAuth> players = new ArrayList<>();
|
||||
String sql = "SELECT * FROM " + tableName + " ORDER BY " + col.LAST_LOGIN + " DESC LIMIT 10;";
|
||||
try (Connection con = getConnection();
|
||||
Statement st = con.createStatement();
|
||||
ResultSet rs = st.executeQuery(sql)) {
|
||||
while (rs.next()) {
|
||||
players.add(buildAuthFromResultSet(rs));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
logSqlException(e);
|
||||
}
|
||||
return players;
|
||||
}
|
||||
|
||||
private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException {
|
||||
String salt = col.SALT.isEmpty() ? null : row.getString(col.SALT);
|
||||
int group = col.GROUP.isEmpty() ? -1 : row.getInt(col.GROUP);
|
||||
return PlayerAuth.builder()
|
||||
.name(row.getString(col.NAME))
|
||||
.realName(row.getString(col.REAL_NAME))
|
||||
.password(row.getString(col.PASSWORD), salt)
|
||||
.lastLogin(getNullableLong(row, col.LAST_LOGIN))
|
||||
.lastIp(row.getString(col.LAST_IP))
|
||||
.email(row.getString(col.EMAIL))
|
||||
.registrationDate(row.getLong(col.REGISTRATION_DATE))
|
||||
.registrationIp(row.getString(col.REGISTRATION_IP))
|
||||
.groupId(group)
|
||||
.locWorld(row.getString(col.LASTLOC_WORLD))
|
||||
.locX(row.getDouble(col.LASTLOC_X))
|
||||
.locY(row.getDouble(col.LASTLOC_Y))
|
||||
.locZ(row.getDouble(col.LASTLOC_Z))
|
||||
.locYaw(row.getFloat(col.LASTLOC_YAW))
|
||||
.locPitch(row.getFloat(col.LASTLOC_PITCH))
|
||||
.build();
|
||||
}
|
||||
}
|
|
@ -83,4 +83,10 @@ public final class SqlDataSourceUtils {
|
|||
return rs.getObject("COLUMN_DEF");
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isColumnMissing(DatabaseMetaData metaData, String columnName, String tableName) throws SQLException {
|
||||
try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) {
|
||||
return !rs.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package fr.xephi.authme.datasource.converter;
|
|||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.datasource.DataSourceType;
|
||||
import fr.xephi.authme.datasource.MySQL;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtensionsFactory;
|
||||
import fr.xephi.authme.datasource.sqlextensions.SqlExtensionsFactory;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
@ -15,17 +15,17 @@ import java.sql.SQLException;
|
|||
public class MySqlToSqlite extends AbstractDataSourceConverter<MySQL> {
|
||||
|
||||
private final Settings settings;
|
||||
private final MySqlExtensionsFactory mySqlExtensionsFactory;
|
||||
private final SqlExtensionsFactory sqlExtensionsFactory;
|
||||
|
||||
@Inject
|
||||
MySqlToSqlite(DataSource dataSource, Settings settings, MySqlExtensionsFactory mySqlExtensionsFactory) {
|
||||
MySqlToSqlite(DataSource dataSource, Settings settings, SqlExtensionsFactory sqlExtensionsFactory) {
|
||||
super(dataSource, DataSourceType.SQLITE);
|
||||
this.settings = settings;
|
||||
this.mySqlExtensionsFactory = mySqlExtensionsFactory;
|
||||
this.sqlExtensionsFactory = sqlExtensionsFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MySQL getSource() throws SQLException {
|
||||
return new MySQL(settings, mySqlExtensionsFactory);
|
||||
return new MySQL(settings, sqlExtensionsFactory);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
|
@ -12,7 +12,7 @@ import java.sql.SQLException;
|
|||
/**
|
||||
* Extension for IPB4.
|
||||
*/
|
||||
class Ipb4Extension extends MySqlExtension {
|
||||
class Ipb4Extension extends SqlExtension {
|
||||
|
||||
private final String ipbPrefix;
|
||||
private final int ipbGroup;
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
@ -6,7 +6,7 @@ import fr.xephi.authme.settings.Settings;
|
|||
/**
|
||||
* Extension implementation that does not do anything.
|
||||
*/
|
||||
class NoOpExtension extends MySqlExtension {
|
||||
class NoOpExtension extends SqlExtension {
|
||||
|
||||
NoOpExtension(Settings settings, Columns col) {
|
||||
super(settings, col);
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
|
@ -13,7 +13,7 @@ import java.util.OptionalInt;
|
|||
/**
|
||||
* Extensions for phpBB when MySQL is used as data source.
|
||||
*/
|
||||
class PhpBbExtension extends MySqlExtension {
|
||||
class PhpBbExtension extends SqlExtension {
|
||||
|
||||
private final String phpBbPrefix;
|
||||
private final int phpBbGroup;
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
|
@ -16,12 +16,12 @@ import java.util.OptionalInt;
|
|||
* Extension for the MySQL data source for forums. For certain password hashes (e.g. phpBB), we want
|
||||
* to hook into the forum board and execute some actions specific to the forum software.
|
||||
*/
|
||||
public abstract class MySqlExtension {
|
||||
public abstract class SqlExtension {
|
||||
|
||||
protected final Columns col;
|
||||
protected final String tableName;
|
||||
|
||||
MySqlExtension(Settings settings, Columns col) {
|
||||
SqlExtension(Settings settings, Columns col) {
|
||||
this.col = col;
|
||||
this.tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
import fr.xephi.authme.security.HashAlgorithm;
|
||||
|
@ -8,20 +8,20 @@ import fr.xephi.authme.settings.properties.SecuritySettings;
|
|||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Creates the appropriate {@link MySqlExtension}, depending on the configured password hashing algorithm.
|
||||
* Creates the appropriate {@link SqlExtension}, depending on the configured password hashing algorithm.
|
||||
*/
|
||||
public class MySqlExtensionsFactory {
|
||||
public class SqlExtensionsFactory {
|
||||
|
||||
@Inject
|
||||
private Settings settings;
|
||||
|
||||
/**
|
||||
* Creates a new {@link MySqlExtension} object according to the configured hash algorithm.
|
||||
* Creates a new {@link SqlExtension} object according to the configured hash algorithm.
|
||||
*
|
||||
* @param columnsConfig the columns configuration
|
||||
* @return the extension the MySQL data source should use
|
||||
*/
|
||||
public MySqlExtension buildExtension(Columns columnsConfig) {
|
||||
public SqlExtension buildExtension(Columns columnsConfig) {
|
||||
HashAlgorithm hash = settings.getProperty(SecuritySettings.PASSWORD_HASH);
|
||||
switch (hash) {
|
||||
case IPB4:
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
|
@ -13,7 +13,7 @@ import java.util.OptionalInt;
|
|||
/**
|
||||
* MySQL extensions for Wordpress.
|
||||
*/
|
||||
class WordpressExtension extends MySqlExtension {
|
||||
class WordpressExtension extends SqlExtension {
|
||||
|
||||
private final String wordpressPrefix;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
|
@ -17,7 +17,7 @@ import java.util.OptionalInt;
|
|||
/**
|
||||
* Extension for XFBCRYPT.
|
||||
*/
|
||||
class XfBcryptExtension extends MySqlExtension {
|
||||
class XfBcryptExtension extends SqlExtension {
|
||||
|
||||
private final String xfPrefix;
|
||||
private final int xfGroup;
|
|
@ -9,7 +9,7 @@ import fr.xephi.authme.datasource.FlatFile;
|
|||
import fr.xephi.authme.datasource.MySQL;
|
||||
import fr.xephi.authme.datasource.SQLite;
|
||||
import fr.xephi.authme.datasource.converter.ForceFlatToSqlite;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtensionsFactory;
|
||||
import fr.xephi.authme.datasource.sqlextensions.SqlExtensionsFactory;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
|
@ -38,7 +38,7 @@ public class DataSourceProvider implements Provider<DataSource> {
|
|||
@Inject
|
||||
private PlayerCache playerCache;
|
||||
@Inject
|
||||
private MySqlExtensionsFactory mySqlExtensionsFactory;
|
||||
private SqlExtensionsFactory sqlExtensionsFactory;
|
||||
|
||||
DataSourceProvider() {
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ public class DataSourceProvider implements Provider<DataSource> {
|
|||
dataSource = new FlatFile(source);
|
||||
break;
|
||||
case MYSQL:
|
||||
dataSource = new MySQL(settings, mySqlExtensionsFactory);
|
||||
dataSource = new MySQL(settings, sqlExtensionsFactory);
|
||||
break;
|
||||
case SQLITE:
|
||||
dataSource = new SQLite(settings, dataFolder);
|
||||
|
|
|
@ -10,7 +10,7 @@ import static ch.jalu.configme.properties.PropertyInitializer.newProperty;
|
|||
public final class DatabaseSettings implements SettingsHolder {
|
||||
|
||||
@Comment({"What type of database do you want to use?",
|
||||
"Valid values: SQLITE, MYSQL"})
|
||||
"Valid values: SQLITE, MYSQL, POSTGRESQL"})
|
||||
public static final Property<DataSourceType> BACKEND =
|
||||
newProperty(DataSourceType.class, "DataSource.backend", DataSourceType.SQLITE);
|
||||
|
||||
|
|
|
@ -6,7 +6,8 @@ import com.google.common.collect.ImmutableMap;
|
|||
import com.google.common.collect.ImmutableSet;
|
||||
import fr.xephi.authme.data.captcha.CaptchaCodeStorage;
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtension;
|
||||
import fr.xephi.authme.datasource.SqlDataSource;
|
||||
import fr.xephi.authme.datasource.sqlextensions.SqlExtension;
|
||||
import fr.xephi.authme.initialization.HasCleanup;
|
||||
import fr.xephi.authme.process.register.executors.RegistrationMethod;
|
||||
import fr.xephi.authme.security.crypts.Whirlpool;
|
||||
|
@ -57,8 +58,9 @@ public class ClassesConsistencyTest {
|
|||
/** Classes excluded from the field visibility test. */
|
||||
private static final Set<Class<?>> CLASSES_EXCLUDED_FROM_VISIBILITY_TEST = ImmutableSet.of(
|
||||
Whirlpool.class, // not our implementation, so we don't touch it
|
||||
MySqlExtension.class, // has immutable protected fields used by all children
|
||||
Columns.class // uses non-static String constants, which is safe
|
||||
SqlExtension.class, // has immutable protected fields used by all children
|
||||
Columns.class, // uses non-static String constants, which is safe
|
||||
SqlDataSource.class // abstract sql datasource class with non-static package-private fields
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package fr.xephi.authme.datasource;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtension;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtensionsFactory;
|
||||
import fr.xephi.authme.datasource.sqlextensions.SqlExtension;
|
||||
import fr.xephi.authme.datasource.sqlextensions.SqlExtensionsFactory;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
@ -25,8 +25,8 @@ public class MySqlResourceClosingTest extends AbstractSqlDataSourceResourceClosi
|
|||
protected DataSource createDataSource(Settings settings, Connection connection) throws Exception {
|
||||
HikariDataSource hikariDataSource = mock(HikariDataSource.class);
|
||||
given(hikariDataSource.getConnection()).willReturn(connection);
|
||||
MySqlExtensionsFactory extensionsFactory = mock(MySqlExtensionsFactory.class);
|
||||
given(extensionsFactory.buildExtension(any(Columns.class))).willReturn(mock(MySqlExtension.class));
|
||||
SqlExtensionsFactory extensionsFactory = mock(SqlExtensionsFactory.class);
|
||||
given(extensionsFactory.buildExtension(any(Columns.class))).willReturn(mock(SqlExtension.class));
|
||||
return new MySQL(settings, hikariDataSource, extensionsFactory);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package fr.xephi.authme.datasource;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtension;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtensionsFactory;
|
||||
import fr.xephi.authme.datasource.sqlextensions.SqlExtension;
|
||||
import fr.xephi.authme.datasource.sqlextensions.SqlExtensionsFactory;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -22,8 +22,8 @@ public final class SqlDataSourceTestUtil {
|
|||
}
|
||||
|
||||
public static MySQL createMySql(Settings settings, HikariDataSource hikariDataSource) {
|
||||
MySqlExtensionsFactory extensionsFactory = mock(MySqlExtensionsFactory.class);
|
||||
given(extensionsFactory.buildExtension(any())).willReturn(mock(MySqlExtension.class));
|
||||
SqlExtensionsFactory extensionsFactory = mock(SqlExtensionsFactory.class);
|
||||
given(extensionsFactory.buildExtension(any())).willReturn(mock(SqlExtension.class));
|
||||
return new MySQL(settings, hikariDataSource, extensionsFactory);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.datasource.AbstractResourceClosingTest;
|
||||
|
@ -16,14 +16,14 @@ import java.util.stream.Collectors;
|
|||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Checks that SQL resources are closed properly in {@link MySqlExtension} implementations.
|
||||
* Checks that SQL resources are closed properly in {@link SqlExtension} implementations.
|
||||
*/
|
||||
public abstract class AbstractMySqlExtensionResourceClosingTest extends AbstractResourceClosingTest {
|
||||
public abstract class AbstractSqlExtensionResourceClosingTest extends AbstractResourceClosingTest {
|
||||
|
||||
private static Settings settings;
|
||||
private static Columns columns;
|
||||
|
||||
public AbstractMySqlExtensionResourceClosingTest(Method method, String name) {
|
||||
public AbstractSqlExtensionResourceClosingTest(Method method, String name) {
|
||||
super(method, name);
|
||||
}
|
||||
|
||||
|
@ -35,15 +35,15 @@ public abstract class AbstractMySqlExtensionResourceClosingTest extends Abstract
|
|||
}
|
||||
|
||||
@Override
|
||||
protected MySqlExtension getObjectUnderTest() {
|
||||
protected SqlExtension getObjectUnderTest() {
|
||||
return createExtension(settings, columns);
|
||||
}
|
||||
|
||||
protected abstract MySqlExtension createExtension(Settings settings, Columns columns);
|
||||
protected abstract SqlExtension createExtension(Settings settings, Columns columns);
|
||||
|
||||
@Parameterized.Parameters(name = "{1}")
|
||||
public static List<Object[]> createParameters() {
|
||||
return Arrays.stream(MySqlExtension.class.getDeclaredMethods())
|
||||
return Arrays.stream(SqlExtension.class.getDeclaredMethods())
|
||||
.filter(m -> Modifier.isPublic(m.getModifiers()))
|
||||
.map(m -> new Object[]{m, m.getName()})
|
||||
.collect(Collectors.toList());
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
@ -8,14 +8,14 @@ import java.lang.reflect.Method;
|
|||
/**
|
||||
* Resource closing test for {@link Ipb4Extension}.
|
||||
*/
|
||||
public class Ipb4ExtensionResourceClosingTest extends AbstractMySqlExtensionResourceClosingTest {
|
||||
public class Ipb4ExtensionResourceClosingTest extends AbstractSqlExtensionResourceClosingTest {
|
||||
|
||||
public Ipb4ExtensionResourceClosingTest(Method method, String name) {
|
||||
super(method, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MySqlExtension createExtension(Settings settings, Columns columns) {
|
||||
protected SqlExtension createExtension(Settings settings, Columns columns) {
|
||||
return new Ipb4Extension(settings, columns);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
@ -8,14 +8,14 @@ import java.lang.reflect.Method;
|
|||
/**
|
||||
* Resource closing test for {@link PhpBbExtension}.
|
||||
*/
|
||||
public class PhpBbExtensionResourceClosingTest extends AbstractMySqlExtensionResourceClosingTest {
|
||||
public class PhpBbExtensionResourceClosingTest extends AbstractSqlExtensionResourceClosingTest {
|
||||
|
||||
public PhpBbExtensionResourceClosingTest(Method method, String name) {
|
||||
super(method, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MySqlExtension createExtension(Settings settings, Columns columns) {
|
||||
protected SqlExtension createExtension(Settings settings, Columns columns) {
|
||||
return new PhpBbExtension(settings, columns);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
@ -8,14 +8,14 @@ import java.lang.reflect.Method;
|
|||
/**
|
||||
* Resource closing test for {@link WordpressExtension}.
|
||||
*/
|
||||
public class WordpressExtensionResourceClosingTest extends AbstractMySqlExtensionResourceClosingTest {
|
||||
public class WordpressExtensionResourceClosingTest extends AbstractSqlExtensionResourceClosingTest {
|
||||
|
||||
public WordpressExtensionResourceClosingTest(Method method, String name) {
|
||||
super(method, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MySqlExtension createExtension(Settings settings, Columns columns) {
|
||||
protected SqlExtension createExtension(Settings settings, Columns columns) {
|
||||
return new WordpressExtension(settings, columns);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package fr.xephi.authme.datasource.mysqlextensions;
|
||||
package fr.xephi.authme.datasource.sqlextensions;
|
||||
|
||||
import fr.xephi.authme.datasource.Columns;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
@ -8,14 +8,14 @@ import java.lang.reflect.Method;
|
|||
/**
|
||||
* Resource closing test for {@link XfBcryptExtension}.
|
||||
*/
|
||||
public class XfBcryptExtensionResourceClosingTest extends AbstractMySqlExtensionResourceClosingTest {
|
||||
public class XfBcryptExtensionResourceClosingTest extends AbstractSqlExtensionResourceClosingTest {
|
||||
|
||||
public XfBcryptExtensionResourceClosingTest(Method method, String name) {
|
||||
super(method, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MySqlExtension createExtension(Settings settings, Columns columns) {
|
||||
protected SqlExtension createExtension(Settings settings, Columns columns) {
|
||||
return new XfBcryptExtension(settings, columns);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue