From 4ac3a5432f31ad3f39eb620e1fe118aaeac861dc Mon Sep 17 00:00:00 2001 From: Tim203 Date: Sun, 25 Aug 2024 19:26:44 +0200 Subject: [PATCH] Changed format of DatabaseContext --- .../databaseutils/DatabaseContext.java | 43 ++++--------------- .../databaseutils/mongo/MongodbDatabase.java | 11 ++--- .../databaseutils/sql/SqlDatabase.java | 10 +++-- 3 files changed, 20 insertions(+), 44 deletions(-) diff --git a/core/src/main/java/org/geysermc/databaseutils/DatabaseContext.java b/core/src/main/java/org/geysermc/databaseutils/DatabaseContext.java index b09fbd0..8448110 100644 --- a/core/src/main/java/org/geysermc/databaseutils/DatabaseContext.java +++ b/core/src/main/java/org/geysermc/databaseutils/DatabaseContext.java @@ -1,25 +1,6 @@ /* - * Copyright (c) 2024 GeyserMC - * - * 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; @@ -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) { @@ -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); } } diff --git a/core/src/main/java/org/geysermc/databaseutils/mongo/MongodbDatabase.java b/core/src/main/java/org/geysermc/databaseutils/mongo/MongodbDatabase.java index b23e582..0e383e6 100644 --- a/core/src/main/java/org/geysermc/databaseutils/mongo/MongodbDatabase.java +++ b/core/src/main/java/org/geysermc/databaseutils/mongo/MongodbDatabase.java @@ -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( @@ -65,10 +66,10 @@ public MongoDatabase mongoDatabase() { return mongoDatabase; } + @SuppressWarnings("unchecked") private CodecRegistry customCodecRegistry(DatabaseContext context) { var codecs = new ArrayList>(); for (TypeCodec codec : context.registry().typeCodecs()) { - //noinspection unchecked codecs.add(new CustomTypeCodec((TypeCodec) codec)); } return CodecRegistries.fromCodecs(codecs); diff --git a/core/src/main/java/org/geysermc/databaseutils/sql/SqlDatabase.java b/core/src/main/java/org/geysermc/databaseutils/sql/SqlDatabase.java index 08b15c0..e7ae3e5 100644 --- a/core/src/main/java/org/geysermc/databaseutils/sql/SqlDatabase.java +++ b/core/src/main/java/org/geysermc/databaseutils/sql/SqlDatabase.java @@ -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); }