Revert changes about H2

This commit is contained in:
HaHaWTH 2023-11-18 20:52:25 +08:00
parent 5cd8791fee
commit 1f88b3bc12
4 changed files with 413 additions and 414 deletions

View File

@ -1083,7 +1083,7 @@
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>
<version>2.1.214</version> <version>2.1.214</version>
<scope>compile</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -4,7 +4,7 @@ package fr.xephi.authme.datasource;
* DataSource type. * DataSource type.
*/ */
public enum DataSourceType { public enum DataSourceType {
H2, // H2,
MYSQL, MYSQL,

View File

@ -1,408 +1,408 @@
package fr.xephi.authme.datasource; //package fr.xephi.authme.datasource;
//
import com.google.common.annotations.VisibleForTesting; //import com.google.common.annotations.VisibleForTesting;
import fr.xephi.authme.ConsoleLogger; //import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerAuth; //import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.datasource.columnshandler.AuthMeColumnsHandler; //import fr.xephi.authme.datasource.columnshandler.AuthMeColumnsHandler;
import fr.xephi.authme.output.ConsoleLoggerFactory; //import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.settings.Settings; //import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.DatabaseSettings; //import fr.xephi.authme.settings.properties.DatabaseSettings;
//
import java.io.File; //import java.io.File;
import java.sql.Connection; //import java.sql.Connection;
import java.sql.DatabaseMetaData; //import java.sql.DatabaseMetaData;
import java.sql.DriverManager; //import java.sql.DriverManager;
import java.sql.PreparedStatement; //import java.sql.PreparedStatement;
import java.sql.ResultSet; //import java.sql.ResultSet;
import java.sql.SQLException; //import java.sql.SQLException;
import java.sql.Statement; //import java.sql.Statement;
import java.util.ArrayList; //import java.util.ArrayList;
import java.util.Collection; //import java.util.Collection;
import java.util.HashSet; //import java.util.HashSet;
import java.util.List; //import java.util.List;
import java.util.Locale; //import java.util.Locale;
import java.util.Set; //import java.util.Set;
//
import static fr.xephi.authme.datasource.SqlDataSourceUtils.getNullableLong; //import static fr.xephi.authme.datasource.SqlDataSourceUtils.getNullableLong;
import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException; //import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException;
//
/** ///**
* H2 data source. // * H2 data source.
*/ // */
@SuppressWarnings({"checkstyle:AbbreviationAsWordInName"}) // Justification: Class name cannot be changed anymore //@SuppressWarnings({"checkstyle:AbbreviationAsWordInName"}) // Justification: Class name cannot be changed anymore
public class H2 extends AbstractSqlDataSource { //public class H2 extends AbstractSqlDataSource {
//
private final ConsoleLogger logger = ConsoleLoggerFactory.get(H2.class); // private final ConsoleLogger logger = ConsoleLoggerFactory.get(H2.class);
private final Settings settings; // private final Settings settings;
private final File dataFolder; // private final File dataFolder;
private final String database; // private final String database;
private final String tableName; // private final String tableName;
private final Columns col; // private final Columns col;
private Connection con; // private Connection con;
//
/** // /**
* Constructor for H2. // * Constructor for H2.
* // *
* @param settings The settings instance // * @param settings The settings instance
* @param dataFolder The data folder // * @param dataFolder The data folder
* @throws SQLException when initialization of a SQL datasource failed // * @throws SQLException when initialization of a SQL datasource failed
*/ // */
public H2(Settings settings, File dataFolder) throws SQLException { // public H2(Settings settings, File dataFolder) throws SQLException {
this.settings = settings; // this.settings = settings;
this.dataFolder = dataFolder; // this.dataFolder = dataFolder;
this.database = settings.getProperty(DatabaseSettings.MYSQL_DATABASE); // this.database = settings.getProperty(DatabaseSettings.MYSQL_DATABASE);
this.tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE); // this.tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
this.col = new Columns(settings); // this.col = new Columns(settings);
//
try { // try {
this.connect(); // this.connect();
this.setup(); // this.setup();
} catch (Exception ex) { // } catch (Exception ex) {
logger.logException("Error during H2 initialization:", ex); // logger.logException("Error during H2 initialization:", ex);
throw ex; // throw ex;
} // }
} // }
//
@VisibleForTesting // @VisibleForTesting
H2(Settings settings, File dataFolder, Connection connection) { // H2(Settings settings, File dataFolder, Connection connection) {
this.settings = settings; // this.settings = settings;
this.dataFolder = dataFolder; // this.dataFolder = dataFolder;
this.database = settings.getProperty(DatabaseSettings.MYSQL_DATABASE); // this.database = settings.getProperty(DatabaseSettings.MYSQL_DATABASE);
this.tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE); // this.tableName = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
this.col = new Columns(settings); // this.col = new Columns(settings);
this.con = connection; // this.con = connection;
this.columnsHandler = AuthMeColumnsHandler.createForSqlite(con, settings); // this.columnsHandler = AuthMeColumnsHandler.createForSqlite(con, settings);
} // }
//
/** // /**
* Initializes the connection to the H2 database. // * Initializes the connection to the H2 database.
* // *
* @throws SQLException when an SQL error occurs while connecting // * @throws SQLException when an SQL error occurs while connecting
*/ // */
protected void connect() throws SQLException { // protected void connect() throws SQLException {
try { // try {
Class.forName("org.h2.Driver"); // Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) { // } catch (ClassNotFoundException e) {
throw new IllegalStateException("Failed to load H2 JDBC class", e); // throw new IllegalStateException("Failed to load H2 JDBC class", e);
} // }
//
logger.debug("H2 driver loaded"); // logger.debug("H2 driver loaded");
this.con = DriverManager.getConnection(this.getJdbcUrl(this.dataFolder.getAbsolutePath(), "", this.database)); // this.con = DriverManager.getConnection(this.getJdbcUrl(this.dataFolder.getAbsolutePath(), "", this.database));
this.columnsHandler = AuthMeColumnsHandler.createForSqlite(con, settings); // this.columnsHandler = AuthMeColumnsHandler.createForSqlite(con, settings);
} // }
//
/** // /**
* Creates the table if necessary, or adds any missing columns to the table. // * Creates the table if necessary, or adds any missing columns to the table.
* // *
* @throws SQLException when an SQL error occurs while initializing the database // * @throws SQLException when an SQL error occurs while initializing the database
*/ // */
@VisibleForTesting // @VisibleForTesting
@SuppressWarnings("checkstyle:CyclomaticComplexity") // @SuppressWarnings("checkstyle:CyclomaticComplexity")
protected void setup() throws SQLException { // protected void setup() throws SQLException {
try (Statement st = con.createStatement()) { // try (Statement st = con.createStatement()) {
// Note: cannot add unique fields later on in SQLite, so we add it on initialization // // Note: cannot add unique fields later on in SQLite, so we add it on initialization
st.executeUpdate("CREATE TABLE IF NOT EXISTS " + tableName + " (" // st.executeUpdate("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ col.ID + " INTEGER AUTO_INCREMENT, " // + col.ID + " INTEGER AUTO_INCREMENT, "
+ col.NAME + " VARCHAR(255) NOT NULL UNIQUE, " // + col.NAME + " VARCHAR(255) NOT NULL UNIQUE, "
+ "CONSTRAINT table_const_prim PRIMARY KEY (" + col.ID + "));"); // + "CONSTRAINT table_const_prim PRIMARY KEY (" + col.ID + "));");
//
DatabaseMetaData md = con.getMetaData(); // DatabaseMetaData md = con.getMetaData();
//
if (isColumnMissing(md, col.REAL_NAME)) { // if (isColumnMissing(md, col.REAL_NAME)) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " // st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
+ col.REAL_NAME + " VARCHAR(255) NOT NULL DEFAULT 'Player';"); // + col.REAL_NAME + " VARCHAR(255) NOT NULL DEFAULT 'Player';");
} // }
//
if (isColumnMissing(md, col.PASSWORD)) { // if (isColumnMissing(md, col.PASSWORD)) {
st.executeUpdate("ALTER TABLE " + tableName // st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.PASSWORD + " VARCHAR(255) NOT NULL DEFAULT '';"); // + " ADD COLUMN " + col.PASSWORD + " VARCHAR(255) NOT NULL DEFAULT '';");
} // }
//
if (!col.SALT.isEmpty() && isColumnMissing(md, col.SALT)) { // if (!col.SALT.isEmpty() && isColumnMissing(md, col.SALT)) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.SALT + " VARCHAR(255);"); // st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.SALT + " VARCHAR(255);");
} // }
//
if (isColumnMissing(md, col.LAST_IP)) { // if (isColumnMissing(md, col.LAST_IP)) {
st.executeUpdate("ALTER TABLE " + tableName // st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.LAST_IP + " VARCHAR(40);"); // + " ADD COLUMN " + col.LAST_IP + " VARCHAR(40);");
} // }
//
if (isColumnMissing(md, col.LAST_LOGIN)) { // if (isColumnMissing(md, col.LAST_LOGIN)) {
st.executeUpdate("ALTER TABLE " + tableName // st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.LAST_LOGIN + " TIMESTAMP;"); // + " ADD COLUMN " + col.LAST_LOGIN + " TIMESTAMP;");
} // }
//
if (isColumnMissing(md, col.REGISTRATION_IP)) { // if (isColumnMissing(md, col.REGISTRATION_IP)) {
st.executeUpdate("ALTER TABLE " + tableName // st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.REGISTRATION_IP + " VARCHAR(40);"); // + " ADD COLUMN " + col.REGISTRATION_IP + " VARCHAR(40);");
} // }
//
if (isColumnMissing(md, col.REGISTRATION_DATE)) { // if (isColumnMissing(md, col.REGISTRATION_DATE)) {
addRegistrationDateColumn(st); // addRegistrationDateColumn(st);
} // }
//
if (isColumnMissing(md, col.LASTLOC_X)) { // if (isColumnMissing(md, col.LASTLOC_X)) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_X // st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_X
+ " DOUBLE NOT NULL DEFAULT '0.0';"); // + " DOUBLE NOT NULL DEFAULT '0.0';");
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_Y // st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_Y
+ " DOUBLE NOT NULL DEFAULT '0.0';"); // + " DOUBLE NOT NULL DEFAULT '0.0';");
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_Z // st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + col.LASTLOC_Z
+ " DOUBLE NOT NULL DEFAULT '0.0';"); // + " DOUBLE NOT NULL DEFAULT '0.0';");
} // }
//
if (isColumnMissing(md, col.LASTLOC_WORLD)) { // if (isColumnMissing(md, col.LASTLOC_WORLD)) {
st.executeUpdate("ALTER TABLE " + tableName // st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.LASTLOC_WORLD + " VARCHAR(255) NOT NULL DEFAULT 'world';"); // + " ADD COLUMN " + col.LASTLOC_WORLD + " VARCHAR(255) NOT NULL DEFAULT 'world';");
} // }
//
if (isColumnMissing(md, col.LASTLOC_YAW)) { // if (isColumnMissing(md, col.LASTLOC_YAW)) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " // st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
+ col.LASTLOC_YAW + " FLOAT;"); // + col.LASTLOC_YAW + " FLOAT;");
} // }
//
if (isColumnMissing(md, col.LASTLOC_PITCH)) { // if (isColumnMissing(md, col.LASTLOC_PITCH)) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " // st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN "
+ col.LASTLOC_PITCH + " FLOAT;"); // + col.LASTLOC_PITCH + " FLOAT;");
} // }
//
if (isColumnMissing(md, col.EMAIL)) { // if (isColumnMissing(md, col.EMAIL)) {
st.executeUpdate("ALTER TABLE " + tableName // st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.EMAIL + " VARCHAR(255);"); // + " ADD COLUMN " + col.EMAIL + " VARCHAR(255);");
} // }
//
if (isColumnMissing(md, col.IS_LOGGED)) { // if (isColumnMissing(md, col.IS_LOGGED)) {
st.executeUpdate("ALTER TABLE " + tableName // st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.IS_LOGGED + " INT NOT NULL DEFAULT '0';"); // + " ADD COLUMN " + col.IS_LOGGED + " INT NOT NULL DEFAULT '0';");
} // }
//
if (isColumnMissing(md, col.HAS_SESSION)) { // if (isColumnMissing(md, col.HAS_SESSION)) {
st.executeUpdate("ALTER TABLE " + tableName // st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.HAS_SESSION + " INT NOT NULL DEFAULT '0';"); // + " ADD COLUMN " + col.HAS_SESSION + " INT NOT NULL DEFAULT '0';");
} // }
//
if (isColumnMissing(md, col.TOTP_KEY)) { // if (isColumnMissing(md, col.TOTP_KEY)) {
st.executeUpdate("ALTER TABLE " + tableName // st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(32);"); // + " ADD COLUMN " + col.TOTP_KEY + " VARCHAR(32);");
} // }
//
if (!col.PLAYER_UUID.isEmpty() && isColumnMissing(md, col.PLAYER_UUID)) { // if (!col.PLAYER_UUID.isEmpty() && isColumnMissing(md, col.PLAYER_UUID)) {
st.executeUpdate("ALTER TABLE " + tableName // st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.PLAYER_UUID + " VARCHAR(36)"); // + " ADD COLUMN " + col.PLAYER_UUID + " VARCHAR(36)");
} // }
} // }
logger.info("H2 Setup finished"); // logger.info("H2 Setup finished");
} // }
//
//
private boolean isColumnMissing(DatabaseMetaData metaData, String columnName) throws SQLException { // private boolean isColumnMissing(DatabaseMetaData metaData, String columnName) throws SQLException {
try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) { // try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) {
return !rs.next(); // return !rs.next();
} // }
} // }
//
@Override // @Override
public void reload() { // public void reload() {
close(con); // close(con);
try { // try {
this.connect(); // this.connect();
this.setup(); // this.setup();
} catch (SQLException ex) { // } catch (SQLException ex) {
logger.logException("Error while reloading H2:", ex); // logger.logException("Error while reloading H2:", ex);
} // }
} // }
//
@Override // @Override
public PlayerAuth getAuth(String user) { // public PlayerAuth getAuth(String user) {
String sql = "SELECT * FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=LOWER(?);"; // String sql = "SELECT * FROM " + tableName + " WHERE LOWER(" + col.NAME + ")=LOWER(?);";
try (PreparedStatement pst = con.prepareStatement(sql)) { // try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user); // pst.setString(1, user);
try (ResultSet rs = pst.executeQuery()) { // try (ResultSet rs = pst.executeQuery()) {
if (rs.next()) { // if (rs.next()) {
return buildAuthFromResultSet(rs); // return buildAuthFromResultSet(rs);
} // }
} // }
} catch (SQLException ex) { // } catch (SQLException ex) {
logSqlException(ex); // logSqlException(ex);
} // }
return null; // return null;
} // }
//
@Override // @Override
public Set<String> getRecordsToPurge(long until) { // public Set<String> getRecordsToPurge(long until) {
Set<String> list = new HashSet<>(); // Set<String> list = new HashSet<>();
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE MAX(" // String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE MAX("
+ " COALESCE(" + col.LAST_LOGIN + ", 0)," // + " COALESCE(" + col.LAST_LOGIN + ", 0),"
+ " COALESCE(" + col.REGISTRATION_DATE + ", 0)" // + " COALESCE(" + col.REGISTRATION_DATE + ", 0)"
+ ") < ?;"; // + ") < ?;";
try (PreparedStatement selectPst = con.prepareStatement(select)) { // try (PreparedStatement selectPst = con.prepareStatement(select)) {
selectPst.setLong(1, until); // selectPst.setLong(1, until);
try (ResultSet rs = selectPst.executeQuery()) { // try (ResultSet rs = selectPst.executeQuery()) {
while (rs.next()) { // while (rs.next()) {
list.add(rs.getString(col.NAME)); // list.add(rs.getString(col.NAME));
} // }
} // }
} catch (SQLException ex) { // } catch (SQLException ex) {
logSqlException(ex); // logSqlException(ex);
} // }
//
return list; // return list;
} // }
//
@Override // @Override
public void purgeRecords(Collection<String> toPurge) { // public void purgeRecords(Collection<String> toPurge) {
String delete = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;"; // String delete = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
try (PreparedStatement deletePst = con.prepareStatement(delete)) { // try (PreparedStatement deletePst = con.prepareStatement(delete)) {
for (String name : toPurge) { // for (String name : toPurge) {
deletePst.setString(1, name.toLowerCase(Locale.ROOT)); // deletePst.setString(1, name.toLowerCase(Locale.ROOT));
deletePst.executeUpdate(); // deletePst.executeUpdate();
} // }
} catch (SQLException ex) { // } catch (SQLException ex) {
logSqlException(ex); // logSqlException(ex);
} // }
} // }
//
@Override // @Override
public boolean removeAuth(String user) { // public boolean removeAuth(String user) {
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;"; // String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
try (PreparedStatement pst = con.prepareStatement(sql)) { // try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, user.toLowerCase(Locale.ROOT)); // pst.setString(1, user.toLowerCase(Locale.ROOT));
pst.executeUpdate(); // pst.executeUpdate();
return true; // return true;
} catch (SQLException ex) { // } catch (SQLException ex) {
logSqlException(ex); // logSqlException(ex);
} // }
return false; // return false;
} // }
//
@Override // @Override
public void closeConnection() { // public void closeConnection() {
try { // try {
if (con != null && !con.isClosed()) { // if (con != null && !con.isClosed()) {
con.close(); // con.close();
} // }
} catch (SQLException ex) { // } catch (SQLException ex) {
logSqlException(ex); // logSqlException(ex);
} // }
} // }
//
@Override // @Override
public DataSourceType getType() { // public DataSourceType getType() {
return DataSourceType.H2; // return DataSourceType.H2;
} // }
//
@Override // @Override
public List<PlayerAuth> getAllAuths() { // public List<PlayerAuth> getAllAuths() {
List<PlayerAuth> auths = new ArrayList<>(); // List<PlayerAuth> auths = new ArrayList<>();
String sql = "SELECT * FROM " + tableName + ";"; // String sql = "SELECT * FROM " + tableName + ";";
try (PreparedStatement pst = con.prepareStatement(sql); ResultSet rs = pst.executeQuery()) { // try (PreparedStatement pst = con.prepareStatement(sql); ResultSet rs = pst.executeQuery()) {
while (rs.next()) { // while (rs.next()) {
PlayerAuth auth = buildAuthFromResultSet(rs); // PlayerAuth auth = buildAuthFromResultSet(rs);
auths.add(auth); // auths.add(auth);
} // }
} catch (SQLException ex) { // } catch (SQLException ex) {
logSqlException(ex); // logSqlException(ex);
} // }
return auths; // return auths;
} // }
//
@Override // @Override
public List<String> getLoggedPlayersWithEmptyMail() { // public List<String> getLoggedPlayersWithEmptyMail() {
List<String> players = new ArrayList<>(); // List<String> players = new ArrayList<>();
String sql = "SELECT " + col.REAL_NAME + " FROM " + tableName + " WHERE " + col.IS_LOGGED + " = 1" // String sql = "SELECT " + col.REAL_NAME + " FROM " + tableName + " WHERE " + col.IS_LOGGED + " = 1"
+ " AND (" + col.EMAIL + " = 'your@email.com' OR " + col.EMAIL + " IS NULL);"; // + " AND (" + col.EMAIL + " = 'your@email.com' OR " + col.EMAIL + " IS NULL);";
try (Statement st = con.createStatement(); ResultSet rs = st.executeQuery(sql)) { // try (Statement st = con.createStatement(); ResultSet rs = st.executeQuery(sql)) {
while (rs.next()) { // while (rs.next()) {
players.add(rs.getString(1)); // players.add(rs.getString(1));
} // }
} catch (SQLException ex) { // } catch (SQLException ex) {
logSqlException(ex); // logSqlException(ex);
} // }
return players; // return players;
} // }
//
@Override // @Override
public List<PlayerAuth> getRecentlyLoggedInPlayers() { // public List<PlayerAuth> getRecentlyLoggedInPlayers() {
List<PlayerAuth> players = new ArrayList<>(); // List<PlayerAuth> players = new ArrayList<>();
String sql = "SELECT * FROM " + tableName + " ORDER BY " + col.LAST_LOGIN + " DESC LIMIT 10;"; // String sql = "SELECT * FROM " + tableName + " ORDER BY " + col.LAST_LOGIN + " DESC LIMIT 10;";
try (Statement st = con.createStatement(); ResultSet rs = st.executeQuery(sql)) { // try (Statement st = con.createStatement(); ResultSet rs = st.executeQuery(sql)) {
while (rs.next()) { // while (rs.next()) {
players.add(buildAuthFromResultSet(rs)); // players.add(buildAuthFromResultSet(rs));
} // }
} catch (SQLException e) { // } catch (SQLException e) {
logSqlException(e); // logSqlException(e);
} // }
return players; // return players;
} // }
//
//
@Override // @Override
public boolean setTotpKey(String user, String totpKey) { // public boolean setTotpKey(String user, String totpKey) {
String sql = "UPDATE " + tableName + " SET " + col.TOTP_KEY + " = ? WHERE " + col.NAME + " = ?"; // String sql = "UPDATE " + tableName + " SET " + col.TOTP_KEY + " = ? WHERE " + col.NAME + " = ?";
try (PreparedStatement pst = con.prepareStatement(sql)) { // try (PreparedStatement pst = con.prepareStatement(sql)) {
pst.setString(1, totpKey); // pst.setString(1, totpKey);
pst.setString(2, user.toLowerCase(Locale.ROOT)); // pst.setString(2, user.toLowerCase(Locale.ROOT));
pst.executeUpdate(); // pst.executeUpdate();
return true; // return true;
} catch (SQLException e) { // } catch (SQLException e) {
logSqlException(e); // logSqlException(e);
} // }
return false; // return false;
} // }
//
private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException { // private PlayerAuth buildAuthFromResultSet(ResultSet row) throws SQLException {
String salt = !col.SALT.isEmpty() ? row.getString(col.SALT) : null; // String salt = !col.SALT.isEmpty() ? row.getString(col.SALT) : null;
//
return PlayerAuth.builder() // return PlayerAuth.builder()
.name(row.getString(col.NAME)) // .name(row.getString(col.NAME))
.email(row.getString(col.EMAIL)) // .email(row.getString(col.EMAIL))
.realName(row.getString(col.REAL_NAME)) // .realName(row.getString(col.REAL_NAME))
.password(row.getString(col.PASSWORD), salt) // .password(row.getString(col.PASSWORD), salt)
.totpKey(row.getString(col.TOTP_KEY)) // .totpKey(row.getString(col.TOTP_KEY))
.lastLogin(getNullableLong(row, col.LAST_LOGIN)) // .lastLogin(getNullableLong(row, col.LAST_LOGIN))
.lastIp(row.getString(col.LAST_IP)) // .lastIp(row.getString(col.LAST_IP))
.registrationDate(row.getLong(col.REGISTRATION_DATE)) // .registrationDate(row.getLong(col.REGISTRATION_DATE))
.registrationIp(row.getString(col.REGISTRATION_IP)) // .registrationIp(row.getString(col.REGISTRATION_IP))
.locX(row.getDouble(col.LASTLOC_X)) // .locX(row.getDouble(col.LASTLOC_X))
.locY(row.getDouble(col.LASTLOC_Y)) // .locY(row.getDouble(col.LASTLOC_Y))
.locZ(row.getDouble(col.LASTLOC_Z)) // .locZ(row.getDouble(col.LASTLOC_Z))
.locWorld(row.getString(col.LASTLOC_WORLD)) // .locWorld(row.getString(col.LASTLOC_WORLD))
.locYaw(row.getFloat(col.LASTLOC_YAW)) // .locYaw(row.getFloat(col.LASTLOC_YAW))
.locPitch(row.getFloat(col.LASTLOC_PITCH)) // .locPitch(row.getFloat(col.LASTLOC_PITCH))
.build(); // .build();
} // }
//
/** // /**
* Creates the column for registration date and sets all entries to the current timestamp. // * Creates the column for registration date and sets all entries to the current timestamp.
* We do so in order to avoid issues with purging, where entries with 0 / NULL might get // * We do so in order to avoid issues with purging, where entries with 0 / NULL might get
* purged immediately on startup otherwise. // * purged immediately on startup otherwise.
* // *
* @param st Statement object to the database // * @param st Statement object to the database
*/ // */
private void addRegistrationDateColumn(Statement st) throws SQLException { // private void addRegistrationDateColumn(Statement st) throws SQLException {
st.executeUpdate("ALTER TABLE " + tableName // st.executeUpdate("ALTER TABLE " + tableName
+ " ADD COLUMN " + col.REGISTRATION_DATE + " TIMESTAMP NOT NULL DEFAULT '0';"); // + " ADD COLUMN " + col.REGISTRATION_DATE + " TIMESTAMP NOT NULL DEFAULT '0';");
//
// Use the timestamp from Java to avoid timezone issues in case JVM and database are out of sync // // Use the timestamp from Java to avoid timezone issues in case JVM and database are out of sync
long currentTimestamp = System.currentTimeMillis(); // long currentTimestamp = System.currentTimeMillis();
int updatedRows = st.executeUpdate(String.format("UPDATE %s SET %s = %d;", // int updatedRows = st.executeUpdate(String.format("UPDATE %s SET %s = %d;",
tableName, col.REGISTRATION_DATE, currentTimestamp)); // tableName, col.REGISTRATION_DATE, currentTimestamp));
logger.info("Created column '" + col.REGISTRATION_DATE + "' and set the current timestamp, " // logger.info("Created column '" + col.REGISTRATION_DATE + "' and set the current timestamp, "
+ currentTimestamp + ", to all " + updatedRows + " rows"); // + currentTimestamp + ", to all " + updatedRows + " rows");
} // }
//
@Override // @Override
String getJdbcUrl(String dataPath, String ignored, String database) { // String getJdbcUrl(String dataPath, String ignored, String database) {
return "jdbc:h2:" + dataPath + File.separator + database + ".db"; // return "jdbc:h2:" + dataPath + File.separator + database + ".db";
} // }
//
private static void close(Connection con) { // private static void close(Connection con) {
if (con != null) { // if (con != null) {
try { // try {
con.close(); // con.close();
} catch (SQLException ex) { // } catch (SQLException ex) {
logSqlException(ex); // logSqlException(ex);
} // }
} // }
} // }
} //}
//

View File

@ -5,7 +5,6 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.CacheDataSource; import fr.xephi.authme.datasource.CacheDataSource;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceType; import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.datasource.H2;
import fr.xephi.authme.datasource.MariaDB; import fr.xephi.authme.datasource.MariaDB;
import fr.xephi.authme.datasource.MySQL; import fr.xephi.authme.datasource.MySQL;
import fr.xephi.authme.datasource.PostgreSqlDataSource; import fr.xephi.authme.datasource.PostgreSqlDataSource;
@ -77,9 +76,9 @@ public class DataSourceProvider implements Provider<DataSource> {
case SQLITE: case SQLITE:
dataSource = new SQLite(settings, dataFolder); dataSource = new SQLite(settings, dataFolder);
break; break;
case H2: // case H2:
dataSource = new H2(settings, dataFolder); // dataSource = new H2(settings, dataFolder);
break; // break;
default: default:
throw new UnsupportedOperationException("Unknown data source type '" + dataSourceType + "'"); throw new UnsupportedOperationException("Unknown data source type '" + dataSourceType + "'");
} }