Refactor database structure and improve player handling.
Reorganized database-related code into a dedicated module, added mappings for UUID handling, and updated SQL queries for clarity. Enhanced team members API to use player data directly, ensuring consistency and better handling of UUIDs. Introduced new database table for connection settings and adjusted Gradle configurations for modularization.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
plugins {
|
||||
id("java")
|
||||
}
|
||||
|
||||
group = "com.alttd.altitudeweb"
|
||||
version = "0.0.1-SNAPSHOT"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":open_api"))
|
||||
compileOnly("org.projectlombok:lombok:1.18.38")
|
||||
annotationProcessor("org.projectlombok:lombok:1.18.38")
|
||||
implementation("org.mybatis:mybatis:3.5.13")
|
||||
compileOnly("org.slf4j:slf4j-api:2.0.17")
|
||||
compileOnly("org.slf4j:slf4j-simple:2.0.17")
|
||||
}
|
||||
|
||||
tasks.test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.alttd.altitudeweb;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.alttd.altitudeweb.database;
|
||||
|
||||
import com.alttd.altitudeweb.database.web_db.DatabaseSettings;
|
||||
import com.alttd.altitudeweb.database.web_db.SettingsMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.datasource.pooled.PooledDataSource;
|
||||
import org.apache.ibatis.mapping.Environment;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
|
||||
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@Slf4j
|
||||
public class Connection {
|
||||
|
||||
private static final HashMap<Databases, Connection> connections = new HashMap<>();
|
||||
private SqlSessionFactory sqlSessionFactory;
|
||||
private final DatabaseSettings settings;
|
||||
private final AddMappers addMappers;
|
||||
|
||||
private Connection(DatabaseSettings settings, AddMappers addMappers) {
|
||||
this.settings = settings;
|
||||
this.addMappers = addMappers;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface AddMappers {
|
||||
void apply(Configuration configuration);
|
||||
}
|
||||
|
||||
public static CompletableFuture<Connection> getConnection(Databases database, AddMappers addMappers) {
|
||||
if (connections.containsKey(database)) {
|
||||
return CompletableFuture.completedFuture(connections.get(database));
|
||||
}
|
||||
if (database == Databases.DEFAULT) {
|
||||
return loadDefaultDatabase(addMappers);
|
||||
}
|
||||
CompletableFuture<DatabaseSettings> settingsFuture = new CompletableFuture<>();
|
||||
getConnection(Databases.DEFAULT, (mapper -> mapper.addMapper(SettingsMapper.class))).thenApply(connection -> {
|
||||
connection.runQuery(session -> {
|
||||
DatabaseSettings loadedSettings = session.getMapper(SettingsMapper.class).getSettings(database.getInternalName());
|
||||
settingsFuture.complete(loadedSettings);
|
||||
});
|
||||
return null;
|
||||
});
|
||||
return settingsFuture.thenApply(loadedSettings -> {
|
||||
Connection connection = new Connection(loadedSettings, addMappers);
|
||||
connections.put(database, connection);
|
||||
return connection;
|
||||
});
|
||||
}
|
||||
|
||||
private static CompletableFuture<Connection> loadDefaultDatabase(AddMappers addMappers) {
|
||||
DatabaseSettings databaseSettings = new DatabaseSettings(
|
||||
System.getenv("DB_HOST"),
|
||||
Integer.parseInt(System.getenv("DB_PORT")),
|
||||
System.getenv("DB_NAME"),
|
||||
System.getenv("DB_USER"),
|
||||
System.getenv("DB_PASS")
|
||||
);
|
||||
Connection connection = new Connection(databaseSettings, addMappers);
|
||||
return CompletableFuture.completedFuture(connection);
|
||||
}
|
||||
|
||||
public void runQuery(Consumer<SqlSession> consumer) {
|
||||
new Thread(() -> {
|
||||
if (sqlSessionFactory == null) {
|
||||
sqlSessionFactory = createSqlSessionFactory(settings, addMappers);
|
||||
}
|
||||
|
||||
try (SqlSession session = sqlSessionFactory.openSession()) {
|
||||
consumer.accept(session);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to run query", e);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
private SqlSessionFactory createSqlSessionFactory(DatabaseSettings settings, AddMappers addMappers) {
|
||||
PooledDataSource dataSource = new PooledDataSource();
|
||||
dataSource.setDriver("com.mysql.cj.jdbc.Driver");
|
||||
dataSource.setUrl(String.format("jdbc:mysql://%s:%d/%s", settings.host(),
|
||||
settings.port(), settings.name()));
|
||||
dataSource.setUsername(settings.username());
|
||||
dataSource.setPassword(settings.password());
|
||||
Environment environment = new Environment("production", new JdbcTransactionFactory(), dataSource);
|
||||
Configuration configuration = new Configuration(environment);
|
||||
addMappers.apply(configuration);
|
||||
|
||||
return new SqlSessionFactoryBuilder().build(configuration);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.alttd.altitudeweb.database;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum Databases {
|
||||
DEFAULT("web_db"),
|
||||
LUCK_PERMS("luckperms");
|
||||
|
||||
private final String internalName;
|
||||
|
||||
Databases(String internalName) {
|
||||
this.internalName = internalName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.alttd.altitudeweb.database.luckperms;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record Player(String username, UUID uuid) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.alttd.altitudeweb.database.luckperms;
|
||||
|
||||
import com.alttd.altitudeweb.type_handler.UUIDTypeHandler;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface TeamMemberMapper {
|
||||
@ConstructorArgs({
|
||||
@Arg(column = "username", javaType = String.class),
|
||||
@Arg(column = "uuid", javaType = UUID.class, typeHandler = UUIDTypeHandler.class)
|
||||
})
|
||||
@Select("""
|
||||
SELECT players.username, players.uuid
|
||||
FROM luckperms_user_permissions AS permissions
|
||||
INNER JOIN luckperms_players AS players ON players.uuid = permissions.uuid
|
||||
WHERE permission = #{groupPermission}
|
||||
""")
|
||||
List<Player> getTeamMembers(@Param("groupPermission") String groupPermission);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.alttd.altitudeweb.database.web_db;
|
||||
|
||||
public record DatabaseSettings(String host, int port, String name, String username, String password) {
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.alttd.altitudeweb.database.web_db;
|
||||
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
public interface SettingsMapper {
|
||||
@Select("SELECT host, port, name, username, password FROM db_connection_settings WHERE name = #{database}")
|
||||
DatabaseSettings getSettings(String database);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.alttd.altitudeweb.type_handler;
|
||||
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
|
||||
@MappedTypes(UUID.class)
|
||||
public class UUIDTypeHandler extends BaseTypeHandler<UUID> {
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, UUID parameter, JdbcType jdbcType) throws SQLException {
|
||||
ps.setString(i, parameter.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||
String uuid = rs.getString(columnName);
|
||||
return uuid == null ? null : UUID.fromString(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
String uuid = rs.getString(columnIndex);
|
||||
return uuid == null ? null : UUID.fromString(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
String uuid = cs.getString(columnIndex);
|
||||
return uuid == null ? null : UUID.fromString(uuid);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE db_connection_settings
|
||||
(
|
||||
internal_name VARCHAR(255) NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
username VARCHAR(255) NOT NULL,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
host VARCHAR(255) NOT NULL,
|
||||
port INT NOT NULL,
|
||||
CONSTRAINT pk_internal_name PRIMARY KEY (internal_name)
|
||||
);
|
||||
Reference in New Issue
Block a user