Skip to content

Commit

Permalink
Changed format of DatabaseContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim203 committed Aug 25, 2024
1 parent 341fcd4 commit 4ac3a54
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 44 deletions.
43 changes: 8 additions & 35 deletions core/src/main/java/org/geysermc/databaseutils/DatabaseContext.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
/*
* Copyright (c) 2024 GeyserMC <https://geysermc.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* Copyright (c) 2024 GeyserMC
* Licensed under the MIT license
* @link https://github.com/GeyserMC/DatabaseUtils
*/
package org.geysermc.databaseutils;
Expand All @@ -28,11 +9,8 @@
import org.geysermc.databaseutils.codec.TypeCodecRegistry;

public record DatabaseContext(
String url,
String username,
String password,
DatabaseConfig config,
String poolName,
int connectionPoolSize,
DatabaseType type,
ExecutorService service,
TypeCodecRegistry registry) {
Expand All @@ -44,19 +22,14 @@ public record DatabaseContext(
}

public DatabaseContext(
DatabaseConfig config,
String url,
String username,
String password,
int connectionPoolSize,
String poolName,
DatabaseType type,
ExecutorService service,
TypeCodecRegistry registry) {
this(
config.url(),
config.username(),
config.password(),
poolName,
config.connectionPoolSize(),
type,
service,
registry);
this(new DatabaseConfig(url, username, password, connectionPoolSize), poolName, type, service, registry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ public final class MongodbDatabase extends Database {
@Override
public void start(DatabaseContext context, Class<?> databaseImpl) {
super.start(context, databaseImpl);
var config = context.config();

var connectionString = new ConnectionString(context.url());
var connectionString = new ConnectionString(config.url());
Objects.requireNonNull(connectionString.getDatabase(), "Database has to be specified!");

var settings = MongoClientSettings.builder().applyConnectionString(connectionString);

if (connectionString.getCredential() == null && (context.username() != null || context.password() != null)) {
if (connectionString.getCredential() == null && (config.username() != null || config.password() != null)) {
settings.credential(MongoCredential.createCredential(
context.username() != null ? context.username() : "",
config.username() != null ? config.username() : "",
connectionString.getDatabase(),
context.password() != null ? context.password().toCharArray() : new char[0]));
config.password() != null ? config.password().toCharArray() : new char[0]));
}

settings.codecRegistry(CodecRegistries.fromRegistries(
Expand All @@ -65,10 +66,10 @@ public MongoDatabase mongoDatabase() {
return mongoDatabase;
}

@SuppressWarnings("unchecked")
private CodecRegistry customCodecRegistry(DatabaseContext context) {
var codecs = new ArrayList<Codec<?>>();
for (TypeCodec<?> codec : context.registry().typeCodecs()) {
//noinspection unchecked
codecs.add(new CustomTypeCodec((TypeCodec<Object>) codec));
}
return CodecRegistries.fromCodecs(codecs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ public void start(DatabaseContext context, Class<?> databaseImpl) {
throw new IllegalStateException("The driver for the selected dialect '" + dialect + "' is not present!");
}

var config = context.config();

var hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(context.url());
hikariConfig.setUsername(context.username());
hikariConfig.setPassword(context.password());
hikariConfig.setJdbcUrl(config.url());
hikariConfig.setUsername(config.username());
hikariConfig.setPassword(config.password());
hikariConfig.setPoolName(context.poolName());
hikariConfig.setMaximumPoolSize(context.connectionPoolSize());
hikariConfig.setMaximumPoolSize(config.connectionPoolSize());

this.dataSource = new HikariDataSource(hikariConfig);
}
Expand Down

0 comments on commit 4ac3a54

Please sign in to comment.