99 lines
4.0 KiB
Java
99 lines
4.0 KiB
Java
|
|
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(DatabaseSettings.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 discord 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);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|