Skip to content

Commit

Permalink
Renamed DatabaseWithDialectType to DatabaseType
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim203 committed Jun 30, 2024
1 parent f3e4baa commit 34b5108
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.geysermc.databaseutils.DatabaseType;
import org.geysermc.databaseutils.DatabaseCategory;
import org.geysermc.databaseutils.processor.type.DatabaseGenerator;
import org.geysermc.databaseutils.processor.type.RepositoryGenerator;
import org.geysermc.databaseutils.processor.type.mongo.MongoDatabaseGenerator;
Expand All @@ -19,8 +19,8 @@
import org.geysermc.databaseutils.processor.type.sql.SqlRepositoryGenerator;

final class RegisteredGenerators {
private static final Map<DatabaseType, Supplier<DatabaseGenerator>> DATABASE_GENERATORS = new HashMap<>();
private static final Map<DatabaseType, Supplier<RepositoryGenerator>> REPOSITORY_GENERATORS = new HashMap<>();
private static final Map<DatabaseCategory, Supplier<DatabaseGenerator>> DATABASE_GENERATORS = new HashMap<>();
private static final Map<DatabaseCategory, Supplier<RepositoryGenerator>> REPOSITORY_GENERATORS = new HashMap<>();

private RegisteredGenerators() {}

Expand All @@ -38,10 +38,10 @@ public static int generatorCount() {

static {
// todo make it less cursed by using one map/list with everything for each database category
DATABASE_GENERATORS.put(DatabaseType.SQL, SqlDatabaseGenerator::new);
DATABASE_GENERATORS.put(DatabaseType.MONGODB, MongoDatabaseGenerator::new);
DATABASE_GENERATORS.put(DatabaseCategory.SQL, SqlDatabaseGenerator::new);
DATABASE_GENERATORS.put(DatabaseCategory.MONGODB, MongoDatabaseGenerator::new);

REPOSITORY_GENERATORS.put(DatabaseType.SQL, SqlRepositoryGenerator::new);
REPOSITORY_GENERATORS.put(DatabaseType.MONGODB, MongoRepositoryGenerator::new);
REPOSITORY_GENERATORS.put(DatabaseCategory.SQL, SqlRepositoryGenerator::new);
REPOSITORY_GENERATORS.put(DatabaseCategory.MONGODB, MongoRepositoryGenerator::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
repositoryClasses.add(result.packageName() + "." + build.name);
}

var spec = TypeSpec.classBuilder(generator.databaseType().upperCamelCaseName() + "DatabaseGenerated");
var spec = TypeSpec.classBuilder(generator.databaseCategory().upperCamelCaseName() + "DatabaseGenerated");
generator.init(spec, hasAsync);
generator.addEntities(entityManager.processedEntities());
generator.addRepositories(repositoryClasses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
import java.util.List;
import java.util.function.BiFunction;
import javax.lang.model.element.Modifier;
import org.geysermc.databaseutils.DatabaseType;
import org.geysermc.databaseutils.DatabaseCategory;
import org.geysermc.databaseutils.IRepository;
import org.geysermc.databaseutils.codec.TypeCodecRegistry;
import org.geysermc.databaseutils.processor.info.EntityInfo;

public abstract class DatabaseGenerator {
private final DatabaseType databaseType;
private final DatabaseCategory databaseCategory;
protected TypeSpec.Builder spec;

public DatabaseGenerator(DatabaseType databaseType) {
this.databaseType = databaseType;
public DatabaseGenerator(DatabaseCategory databaseCategory) {
this.databaseCategory = databaseCategory;
}

public void init(TypeSpec.Builder spec, boolean hasAsync) {
Expand All @@ -40,8 +40,8 @@ public void init(TypeSpec.Builder spec, boolean hasAsync) {
.build());
}

public DatabaseType databaseType() {
return databaseType;
public DatabaseCategory databaseCategory() {
return databaseCategory;
}

public abstract Class<?> databaseClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.concurrent.CompletableFuture;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import org.geysermc.databaseutils.DatabaseType;
import org.geysermc.databaseutils.DatabaseCategory;
import org.geysermc.databaseutils.codec.TypeCodec;
import org.geysermc.databaseutils.codec.TypeCodecRegistry;
import org.geysermc.databaseutils.processor.info.ColumnInfo;
Expand All @@ -21,15 +21,15 @@
import org.geysermc.databaseutils.processor.util.TypeUtils;

public abstract class RepositoryGenerator {
private final DatabaseType type;
private final DatabaseCategory category;

protected TypeSpec.Builder typeSpec;
protected boolean hasAsync;
protected EntityInfo entityInfo;
private String packageName;

protected RepositoryGenerator(DatabaseType type) {
this.type = type;
protected RepositoryGenerator(DatabaseCategory category) {
this.category = category;
}

protected void onConstructorBuilder(MethodSpec.Builder builder) {}
Expand All @@ -49,7 +49,7 @@ public void init(TypeElement superType, EntityInfo entityInfo) {
throw new IllegalStateException("Cannot reinitialize RepositoryGenerator");
}
this.packageName = TypeUtils.packageNameFor(superType.getQualifiedName());
var className = superType.getSimpleName() + type.upperCamelCaseName() + "Impl";
var className = superType.getSimpleName() + category.upperCamelCaseName() + "Impl";
this.typeSpec = TypeSpec.classBuilder(className)
.addSuperinterface(ParameterizedTypeName.get(superType.asType()))
.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.bson.codecs.Codec;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.codecs.configuration.CodecRegistry;
import org.geysermc.databaseutils.DatabaseType;
import org.geysermc.databaseutils.DatabaseCategory;
import org.geysermc.databaseutils.meta.Index;
import org.geysermc.databaseutils.mongo.MongodbDatabase;
import org.geysermc.databaseutils.processor.info.EntityInfo;
Expand All @@ -33,7 +33,7 @@

public class MongoDatabaseGenerator extends DatabaseGenerator {
public MongoDatabaseGenerator() {
super(DatabaseType.MONGODB);
super(DatabaseCategory.MONGODB);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import javax.lang.model.element.Modifier;
import org.bson.Document;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.databaseutils.DatabaseType;
import org.geysermc.databaseutils.DatabaseCategory;
import org.geysermc.databaseutils.processor.query.QueryContext;
import org.geysermc.databaseutils.processor.query.section.by.keyword.EqualsKeyword;
import org.geysermc.databaseutils.processor.query.section.by.keyword.LessThanKeyword;
Expand All @@ -37,7 +37,7 @@

public class MongoRepositoryGenerator extends RepositoryGenerator {
public MongoRepositoryGenerator() {
super(DatabaseType.MONGODB);
super(DatabaseCategory.MONGODB);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import org.geysermc.databaseutils.DatabaseType;
import org.geysermc.databaseutils.DatabaseCategory;
import org.geysermc.databaseutils.processor.info.ColumnInfo;
import org.geysermc.databaseutils.processor.info.EntityInfo;
import org.geysermc.databaseutils.processor.type.DatabaseGenerator;
Expand All @@ -22,7 +22,7 @@

public class SqlDatabaseGenerator extends DatabaseGenerator {
public SqlDatabaseGenerator() {
super(DatabaseType.SQL);
super(DatabaseCategory.SQL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.List;
import java.util.concurrent.CompletionException;
import javax.lang.model.element.Modifier;
import org.geysermc.databaseutils.DatabaseType;
import org.geysermc.databaseutils.DatabaseCategory;
import org.geysermc.databaseutils.processor.info.ColumnInfo;
import org.geysermc.databaseutils.processor.query.QueryContext;
import org.geysermc.databaseutils.processor.query.section.by.keyword.EqualsKeyword;
Expand All @@ -43,7 +43,7 @@ public final class SqlRepositoryGenerator extends RepositoryGenerator {
private static final int BATCH_SIZE = 250;

public SqlRepositoryGenerator() {
super(DatabaseType.SQL);
super(DatabaseCategory.SQL);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.Properties;

final class CredentialsFileHandler {
public DatabaseConfig handle(DatabaseWithDialectType dialect, Path credentialsFile) {
public DatabaseConfig handle(DatabaseType dialect, Path credentialsFile) {
DatabaseConfig config = defaultValuesFor(dialect);
if (credentialsFile != null) {
if (Files.exists(credentialsFile)) {
Expand Down Expand Up @@ -55,7 +55,7 @@ private void createConfig(DatabaseConfig defaults, Path toStore) {
}
}

private DatabaseConfig defaultValuesFor(DatabaseWithDialectType type) {
private DatabaseConfig defaultValuesFor(DatabaseType type) {
return switch (type) {
case H2 -> configFor("jdbc:h2:./database", "sa");
case SQL_SERVER -> configFor("jdbc:sqlserver://localhost;encrypt=true;integratedSecurity=true;");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 GeyserMC
* Licensed under the MIT license
* @link https://github.com/GeyserMC/DatabaseUtils
*/
package org.geysermc.databaseutils;

public enum DatabaseCategory {
SQL("Sql"),
MONGODB("Mongo");

private final String upperCamelCaseName;

DatabaseCategory(String upperCamelCaseName) {
this.upperCamelCaseName = upperCamelCaseName;
}

public String upperCamelCaseName() {
return upperCamelCaseName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public record DatabaseContext(
String password,
String poolName,
int connectionPoolSize,
DatabaseWithDialectType type,
DatabaseType type,
ExecutorService service,
TypeCodecRegistry registry) {

Expand All @@ -46,7 +46,7 @@ public record DatabaseContext(
public DatabaseContext(
DatabaseConfig config,
String poolName,
DatabaseWithDialectType type,
DatabaseType type,
ExecutorService service,
TypeCodecRegistry registry) {
this(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class DatabaseLoader {
List<BiFunction<Database, TypeCodecRegistry, IRepository<?>>> repositoryCreators;
Method createEntitiesMethod;
try {
var className = context.type().databaseType().upperCamelCaseName() + "DatabaseGenerated";
var className = context.type().databaseCategory().upperCamelCaseName() + "DatabaseGenerated";
databaseImplClass = Class.forName(database.getClass().getPackageName() + "." + className);

hasAsync = access(databaseImplClass.getDeclaredField("HAS_ASYNC")).getBoolean(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import org.geysermc.databaseutils.sql.SqlDatabase;

final class DatabaseRegistry {
private static final Map<DatabaseType, Supplier<Database>> TYPES =
Map.of(DatabaseType.SQL, SqlDatabase::new, DatabaseType.MONGODB, MongodbDatabase::new);
private static final Map<DatabaseCategory, Supplier<Database>> TYPES =
Map.of(DatabaseCategory.SQL, SqlDatabase::new, DatabaseCategory.MONGODB, MongodbDatabase::new);

public static @Nullable Database databaseFor(@NonNull DatabaseWithDialectType type) {
var instanceSupplier = TYPES.get(type.databaseType());
public static @Nullable Database databaseFor(@NonNull DatabaseType type) {
var instanceSupplier = TYPES.get(type.databaseCategory());
if (instanceSupplier == null) {
return null;
}
Expand Down
66 changes: 57 additions & 9 deletions core/src/main/java/org/geysermc/databaseutils/DatabaseType.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,69 @@
/*
* Copyright (c) 2024 GeyserMC
* Licensed under the MIT license
* 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
* @link https://github.com/GeyserMC/DatabaseUtils
*/
package org.geysermc.databaseutils;

import java.util.Locale;
import java.util.Objects;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.databaseutils.sql.SqlDialect;

public enum DatabaseType {
SQL("Sql"),
MONGODB("Mongo");
H2(DatabaseCategory.SQL, SqlDialect.H2),
SQL_SERVER(DatabaseCategory.SQL, SqlDialect.SQL_SERVER),
MYSQL(DatabaseCategory.SQL, SqlDialect.MYSQL),
ORACLE_DATABASE(DatabaseCategory.SQL, SqlDialect.ORACLE_DATABASE),
POSTGRESQL(DatabaseCategory.SQL, SqlDialect.POSTGRESQL),
SQLITE(DatabaseCategory.SQL, SqlDialect.SQLITE),
MONGODB(DatabaseCategory.MONGODB, null);

private static final DatabaseType[] VALUES = values();

private final DatabaseCategory databaseCategory;
private final SqlDialect dialect;

private final String upperCamelCaseName;
DatabaseType(@NonNull DatabaseCategory databaseCategory, @Nullable SqlDialect dialect) {
this.databaseCategory = Objects.requireNonNull(databaseCategory);
this.dialect = dialect;
}

public static @Nullable DatabaseType byName(@NonNull String name) {
var normalized = name.replace('-', '_').replace(' ', '_').toUpperCase(Locale.ROOT);
for (DatabaseType value : VALUES) {
if (value.name().equals(normalized)) {
return value;
}
}
return null;
}

DatabaseType(String upperCamelCaseName) {
this.upperCamelCaseName = upperCamelCaseName;
public @NonNull DatabaseCategory databaseCategory() {
return databaseCategory;
}

public String upperCamelCaseName() {
return upperCamelCaseName;
public @Nullable SqlDialect dialect() {
return dialect;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static class Builder {
private String poolName = "database-utils";
private int connectionPoolSize = 0;

private DatabaseWithDialectType type;
private DatabaseType type;

private Path credentialsFile;
private boolean useDefaultCredentials = true;
Expand Down Expand Up @@ -158,11 +158,11 @@ public Builder connectionPoolSize(int connectionPoolSize) {
return this;
}

public DatabaseWithDialectType type() {
public DatabaseType type() {
return type;
}

public Builder type(DatabaseWithDialectType type) {
public Builder type(DatabaseType type) {
this.type = type;
return this;
}
Expand Down
Loading

0 comments on commit 34b5108

Please sign in to comment.