Load in users and create new users if needed.
This commit is contained in:
@@ -18,8 +18,12 @@ public abstract class StorageProvider {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public EssentiaUser loadUser(UUID uuid) {
|
||||
EssentiaUser user = load(uuid);
|
||||
public User loadUser(UUID uuid) {
|
||||
User user = load(uuid);
|
||||
|
||||
if (user == null) {
|
||||
user = plugin.userManager().createNewUser(uuid);
|
||||
}
|
||||
|
||||
plugin.userManager().addUser(user);
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ public class DatabaseQuery {
|
||||
this(statement, ps -> {});
|
||||
}
|
||||
|
||||
public ResultSet execute(Connection connection) {
|
||||
public ResultSet executeQuery(Connection connection) {
|
||||
try (PreparedStatement preparedStatement = connection.prepareStatement(statement)) {
|
||||
databaseTask.edit(preparedStatement);
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
@@ -31,12 +31,24 @@ public class DatabaseQuery {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void execute(Connection connection) {
|
||||
try (PreparedStatement preparedStatement = connection.prepareStatement(statement)) {
|
||||
databaseTask.edit(preparedStatement);
|
||||
preparedStatement.execute();
|
||||
databaseTask.onSuccess();
|
||||
} catch (SQLException e) {
|
||||
databaseTask.onFailure(e);
|
||||
}
|
||||
}
|
||||
|
||||
public interface DatabaseTask {
|
||||
|
||||
void edit(PreparedStatement preparedStatement) throws SQLException;
|
||||
|
||||
default void onSuccess(ResultSet resultSet) throws SQLException {};
|
||||
|
||||
default void onSuccess() throws SQLException {};
|
||||
|
||||
default void onFailure(SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public class DatabaseQueue extends BukkitRunnable {
|
||||
if (databaseQuery == null)
|
||||
return;
|
||||
|
||||
databaseQuery.execute(connection);
|
||||
databaseQuery.executeQuery(connection);
|
||||
}
|
||||
if (!connection.getAutoCommit()) {
|
||||
connection.commit();
|
||||
|
||||
@@ -34,10 +34,11 @@ public class SQLStorageProvider extends StorageProvider {
|
||||
private void createTables() {
|
||||
// TODO -- create table
|
||||
String userTable = "CREATE TABLE IF NOT EXISTS users(" +
|
||||
"id VARCHAR(36) NOT NULL, " +
|
||||
"PRIMARY KEY (id)" +
|
||||
"uuid VARCHAR(36) NOT NULL, " +
|
||||
"PRIMARY KEY (uuid)" +
|
||||
")";
|
||||
addDatabaseQuery(new DatabaseQuery(userTable), false);
|
||||
DatabaseQuery databaseQuery = new DatabaseQuery(userTable);
|
||||
databaseQuery.execute(getDatabaseConnection().get());
|
||||
}
|
||||
|
||||
public DatabaseConnection getDatabaseConnection() {
|
||||
@@ -100,7 +101,7 @@ public class SQLStorageProvider extends StorageProvider {
|
||||
if (queue) {
|
||||
databaseQueue.databaseQueryQueue().offer(databaseQuery);
|
||||
} else {
|
||||
databaseQuery.execute(getDatabaseConnection().get());
|
||||
databaseQuery.executeQuery(getDatabaseConnection().get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,11 +121,12 @@ public class SQLStorageProvider extends StorageProvider {
|
||||
return match;
|
||||
}
|
||||
|
||||
// TODO -- make this async
|
||||
@Override
|
||||
protected EssentiaUser load(UUID uuid) {
|
||||
String sql = "SELECT * FROM users WHERE uuid = ?";
|
||||
DatabaseQuery databaseQuery = new DatabaseQuery(sql, ps -> ps.setString(1, uuid.toString()));
|
||||
try (ResultSet resultSet = databaseQuery.execute(getDatabaseConnection().get())) {
|
||||
try (ResultSet resultSet = databaseQuery.executeQuery(getDatabaseConnection().get())) {
|
||||
if (!resultSet.next()) {
|
||||
return null; // user is not in the db
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user