#1539 Use columns handler in more datasource methods; fix case-insensitivity for SQLite
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package fr.xephi.authme.datasource;
|
||||
|
||||
import ch.jalu.datasourcecolumns.data.DataSourceValues;
|
||||
import ch.jalu.datasourcecolumns.predicate.AlwaysTruePredicate;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import com.zaxxer.hikari.pool.HikariPool.PoolInitializationException;
|
||||
@@ -23,11 +24,14 @@ import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static ch.jalu.datasourcecolumns.data.UpdateValues.with;
|
||||
import static ch.jalu.datasourcecolumns.predicate.StandardPredicates.eq;
|
||||
import static ch.jalu.datasourcecolumns.predicate.StandardPredicates.eqIgnoreCase;
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.getNullableLong;
|
||||
import static fr.xephi.authme.datasource.SqlDataSourceUtils.logSqlException;
|
||||
|
||||
@@ -269,24 +273,20 @@ public class MySQL implements DataSource {
|
||||
|
||||
@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);
|
||||
try {
|
||||
return columnsHandler.retrieve(user, AuthMeColumns.NAME).rowExists();
|
||||
} catch (SQLException e) {
|
||||
logSqlException(e);
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashedPassword getPassword(String user) {
|
||||
try {
|
||||
DataSourceValues passwordResult = columnsHandler.retrieve(user, AuthMeColumns.PASSWORD, AuthMeColumns.SALT);
|
||||
if (passwordResult.rowExists()) {
|
||||
return new HashedPassword(passwordResult.get(AuthMeColumns.PASSWORD), passwordResult.get(AuthMeColumns.SALT));
|
||||
DataSourceValues values = columnsHandler.retrieve(user, AuthMeColumns.PASSWORD, AuthMeColumns.SALT);
|
||||
if (values.rowExists()) {
|
||||
return new HashedPassword(values.get(AuthMeColumns.PASSWORD), values.get(AuthMeColumns.SALT));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
logSqlException(e);
|
||||
@@ -354,19 +354,7 @@ public class MySQL implements DataSource {
|
||||
|
||||
@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;
|
||||
return columnsHandler.update(auth, AuthMeColumns.LAST_IP, AuthMeColumns.LAST_LOGIN, AuthMeColumns.NICK_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -427,35 +415,17 @@ public class MySQL implements DataSource {
|
||||
|
||||
@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);
|
||||
try {
|
||||
return columnsHandler.retrieve(eq(AuthMeColumns.LAST_IP, ip), AuthMeColumns.NAME);
|
||||
} catch (SQLException e) {
|
||||
logSqlException(e);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
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;
|
||||
return columnsHandler.count(eqIgnoreCase(AuthMeColumns.EMAIL, email));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -566,18 +536,7 @@ public class MySQL implements DataSource {
|
||||
|
||||
@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;
|
||||
return columnsHandler.count(new AlwaysTruePredicate<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user