Skip to content

Commit f172e7b

Browse files
committed
Added first version of MongoDB support
1 parent e32e6e7 commit f172e7b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1516
-741
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ currently examples can be found in the tests of the AP module and the tests of t
66

77
# What's left to do?
88
- make 'simple' actions like `insert` more flexible
9-
- allow it to return something else than void, e.g. ~~the input entity~~ or whether there was a row added
9+
- allow it to return whether there was a row added
1010
- support adding every variable of the entity as parameter
1111
- add `save` which either inserts the entity if it's not present or updates the already existing entity
12-
- implementing MongoDB support
1312
- adding migrations
1413
- and plenty more
1514

ap/src/main/java/org/geysermc/databaseutils/processor/EntityManager.java

+12-26
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
11
/*
2-
* Copyright (c) 2024 GeyserMC <https://geysermc.org>
3-
*
4-
* Permission is hereby granted, free of charge, to any person obtaining a copy
5-
* of this software and associated documentation files (the "Software"), to deal
6-
* in the Software without restriction, including without limitation the rights
7-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8-
* copies of the Software, and to permit persons to whom the Software is
9-
* furnished to do so, subject to the following conditions:
10-
*
11-
* The above copyright notice and this permission notice shall be included in
12-
* all copies or substantial portions of the Software.
13-
*
14-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20-
* THE SOFTWARE.
21-
*
22-
* @author GeyserMC
2+
* Copyright (c) 2024 GeyserMC
3+
* Licensed under the MIT license
234
* @link https://github.com/GeyserMC/DatabaseUtils
245
*/
256
package org.geysermc.databaseutils.processor;
267

278
import static org.geysermc.databaseutils.processor.util.AnnotationUtils.hasAnnotation;
289

10+
import com.google.auto.common.MoreTypes;
2911
import java.util.ArrayList;
3012
import java.util.Arrays;
3113
import java.util.Collection;
@@ -38,7 +20,9 @@
3820
import javax.lang.model.element.Modifier;
3921
import javax.lang.model.element.TypeElement;
4022
import javax.lang.model.element.VariableElement;
23+
import javax.lang.model.type.TypeMirror;
4124
import org.geysermc.databaseutils.meta.Entity;
25+
import org.geysermc.databaseutils.meta.Index;
4226
import org.geysermc.databaseutils.meta.Key;
4327
import org.geysermc.databaseutils.processor.info.ColumnInfo;
4428
import org.geysermc.databaseutils.processor.info.EntityInfo;
@@ -57,7 +41,9 @@ Collection<EntityInfo> processedEntities() {
5741
return entityInfoByClassName.values();
5842
}
5943

60-
EntityInfo processEntity(TypeElement type) {
44+
EntityInfo processEntity(TypeMirror typeMirror) {
45+
var type = MoreTypes.asTypeElement(typeMirror);
46+
6147
var cached = entityInfoByClassName.get(type.getQualifiedName());
6248
if (cached != null) {
6349
return cached;
@@ -78,7 +64,7 @@ EntityInfo processEntity(TypeElement type) {
7864
var indexes = new ArrayList<IndexInfo>();
7965
var columns = new ArrayList<ColumnInfo>();
8066

81-
Arrays.stream(type.getAnnotationsByType(org.geysermc.databaseutils.meta.Index.class))
67+
Arrays.stream(type.getAnnotationsByType(Index.class))
8268
.map(index -> new IndexInfo(index.name(), index.columns(), index.unique()))
8369
.forEach(indexes::add);
8470

@@ -100,14 +86,14 @@ EntityInfo processEntity(TypeElement type) {
10086
}
10187

10288
TypeElement typeElement = typeUtils.toBoxedTypeElement(field.asType());
103-
columns.add(new ColumnInfo(field.getSimpleName(), typeElement.getQualifiedName()));
89+
columns.add(new ColumnInfo(field.getSimpleName(), typeElement, typeElement.getQualifiedName()));
10490

10591
if (hasAnnotation(field, Key.class)) {
10692
keys.add(field.getSimpleName());
10793
}
108-
var index = field.getAnnotation(org.geysermc.databaseutils.meta.Index.class);
94+
var index = field.getAnnotation(Index.class);
10995
if (index != null) {
110-
indexes.add(new IndexInfo(index.name(), index.columns(), index.unique()));
96+
indexes.add(new IndexInfo(index.name(), index.columns(), index.unique(), index.direction()));
11197
}
11298
}
11399

Original file line numberDiff line numberDiff line change
@@ -1,55 +1,47 @@
11
/*
2-
* Copyright (c) 2024 GeyserMC <https://geysermc.org>
3-
*
4-
* Permission is hereby granted, free of charge, to any person obtaining a copy
5-
* of this software and associated documentation files (the "Software"), to deal
6-
* in the Software without restriction, including without limitation the rights
7-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8-
* copies of the Software, and to permit persons to whom the Software is
9-
* furnished to do so, subject to the following conditions:
10-
*
11-
* The above copyright notice and this permission notice shall be included in
12-
* all copies or substantial portions of the Software.
13-
*
14-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20-
* THE SOFTWARE.
21-
*
22-
* @author GeyserMC
2+
* Copyright (c) 2024 GeyserMC
3+
* Licensed under the MIT license
234
* @link https://github.com/GeyserMC/DatabaseUtils
245
*/
256
package org.geysermc.databaseutils.processor;
267

8+
import java.util.HashMap;
279
import java.util.List;
10+
import java.util.Map;
2811
import java.util.function.Supplier;
2912
import java.util.stream.Collectors;
13+
import org.geysermc.databaseutils.DatabaseType;
3014
import org.geysermc.databaseutils.processor.type.DatabaseGenerator;
3115
import org.geysermc.databaseutils.processor.type.RepositoryGenerator;
32-
import org.geysermc.databaseutils.processor.type.SqlDatabaseGenerator;
33-
import org.geysermc.databaseutils.processor.type.SqlRepositoryGenerator;
16+
import org.geysermc.databaseutils.processor.type.mongo.MongoDatabaseGenerator;
17+
import org.geysermc.databaseutils.processor.type.mongo.MongoRepositoryGenerator;
18+
import org.geysermc.databaseutils.processor.type.sql.SqlDatabaseGenerator;
19+
import org.geysermc.databaseutils.processor.type.sql.SqlRepositoryGenerator;
3420

3521
final class RegisteredGenerators {
36-
// both the database and the repository generators have to be on the same indexes
37-
38-
private static final List<Supplier<DatabaseGenerator>> DATABASE_GENERATORS = List.of(SqlDatabaseGenerator::new);
39-
private static final List<Supplier<RepositoryGenerator>> REPOSITORY_GENERATORS =
40-
List.of(SqlRepositoryGenerator::new);
22+
private static final Map<DatabaseType, Supplier<DatabaseGenerator>> DATABASE_GENERATORS = new HashMap<>();
23+
private static final Map<DatabaseType, Supplier<RepositoryGenerator>> REPOSITORY_GENERATORS = new HashMap<>();
4124

4225
private RegisteredGenerators() {}
4326

4427
public static List<DatabaseGenerator> databaseGenerators() {
45-
return DATABASE_GENERATORS.stream().map(Supplier::get).collect(Collectors.toList());
28+
return DATABASE_GENERATORS.values().stream().map(Supplier::get).collect(Collectors.toList());
4629
}
4730

4831
public static List<RepositoryGenerator> repositoryGenerators() {
49-
return REPOSITORY_GENERATORS.stream().map(Supplier::get).collect(Collectors.toList());
32+
return REPOSITORY_GENERATORS.values().stream().map(Supplier::get).collect(Collectors.toList());
5033
}
5134

5235
public static int generatorCount() {
5336
return DATABASE_GENERATORS.size();
5437
}
38+
39+
static {
40+
// todo make it less cursed by using one map/list with everything for each database category
41+
DATABASE_GENERATORS.put(DatabaseType.SQL, SqlDatabaseGenerator::new);
42+
DATABASE_GENERATORS.put(DatabaseType.MONGODB, MongoDatabaseGenerator::new);
43+
44+
REPOSITORY_GENERATORS.put(DatabaseType.SQL, SqlRepositoryGenerator::new);
45+
REPOSITORY_GENERATORS.put(DatabaseType.MONGODB, MongoRepositoryGenerator::new);
46+
}
5547
}

ap/src/main/java/org/geysermc/databaseutils/processor/RepositoryProcessor.java

+4-23
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
/*
2-
* Copyright (c) 2024 GeyserMC <https://geysermc.org>
3-
*
4-
* Permission is hereby granted, free of charge, to any person obtaining a copy
5-
* of this software and associated documentation files (the "Software"), to deal
6-
* in the Software without restriction, including without limitation the rights
7-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8-
* copies of the Software, and to permit persons to whom the Software is
9-
* furnished to do so, subject to the following conditions:
10-
*
11-
* The above copyright notice and this permission notice shall be included in
12-
* all copies or substantial portions of the Software.
13-
*
14-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20-
* THE SOFTWARE.
21-
*
22-
* @author GeyserMC
2+
* Copyright (c) 2024 GeyserMC
3+
* Licensed under the MIT license
234
* @link https://github.com/GeyserMC/DatabaseUtils
245
*/
256
package org.geysermc.databaseutils.processor;
@@ -136,7 +117,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
136117
repositoryClasses.add(result.packageName() + "." + build.name);
137118
}
138119

139-
var spec = TypeSpec.classBuilder(generator.databaseClass().getSimpleName() + "Generated");
120+
var spec = TypeSpec.classBuilder(generator.databaseType().upperCamelCaseName() + "DatabaseGenerated");
140121
generator.init(spec, hasAsync);
141122
generator.addEntities(entityManager.processedEntities());
142123
generator.addRepositories(repositoryClasses);
@@ -178,7 +159,7 @@ private List<RepositoryGenerator> processRepository(TypeElement repository) {
178159
throw new InvalidRepositoryException("Repository has to extend IRepository<EntityClass>");
179160
}
180161

181-
var entity = entityManager.processEntity(MoreTypes.asTypeElement(entityType));
162+
var entity = entityManager.processEntity(entityType);
182163

183164
var generators = RegisteredGenerators.repositoryGenerators();
184165
for (var generator : generators) {

ap/src/main/java/org/geysermc/databaseutils/processor/action/Action.java

+4-22
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
/*
2-
* Copyright (c) 2024 GeyserMC <https://geysermc.org>
3-
*
4-
* Permission is hereby granted, free of charge, to any person obtaining a copy
5-
* of this software and associated documentation files (the "Software"), to deal
6-
* in the Software without restriction, including without limitation the rights
7-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8-
* copies of the Software, and to permit persons to whom the Software is
9-
* furnished to do so, subject to the following conditions:
10-
*
11-
* The above copyright notice and this permission notice shall be included in
12-
* all copies or substantial portions of the Software.
13-
*
14-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20-
* THE SOFTWARE.
21-
*
22-
* @author GeyserMC
2+
* Copyright (c) 2024 GeyserMC
3+
* Licensed under the MIT license
234
* @link https://github.com/GeyserMC/DatabaseUtils
245
*/
256
package org.geysermc.databaseutils.processor.action;
@@ -146,7 +127,8 @@ public void validate(
146127

147128
public void addTo(List<RepositoryGenerator> generators, QueryContext context) {
148129
if (!context.hasBySection() && !context.parametersInfo().isNoneOrAnySelf()) {
149-
throw new InvalidRepositoryException("Expected at most one parameter, with type %s", context.entityType());
130+
throw new InvalidRepositoryException(
131+
"Expected at most one parameter, with type %s", context.entityTypeName());
150132
}
151133

152134
for (RepositoryGenerator generator : generators) {
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
/*
2-
* Copyright (c) 2024 GeyserMC <https://geysermc.org>
3-
*
4-
* Permission is hereby granted, free of charge, to any person obtaining a copy
5-
* of this software and associated documentation files (the "Software"), to deal
6-
* in the Software without restriction, including without limitation the rights
7-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8-
* copies of the Software, and to permit persons to whom the Software is
9-
* furnished to do so, subject to the following conditions:
10-
*
11-
* The above copyright notice and this permission notice shall be included in
12-
* all copies or substantial portions of the Software.
13-
*
14-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20-
* THE SOFTWARE.
21-
*
22-
* @author GeyserMC
2+
* Copyright (c) 2024 GeyserMC
3+
* Licensed under the MIT license
234
* @link https://github.com/GeyserMC/DatabaseUtils
245
*/
256
package org.geysermc.databaseutils.processor.info;
267

278
import javax.lang.model.element.Name;
9+
import javax.lang.model.element.TypeElement;
2810

29-
public record ColumnInfo(Name name, Name typeName) {}
11+
public record ColumnInfo(Name name, TypeElement type, Name typeName) {}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
/*
2-
* Copyright (c) 2024 GeyserMC <https://geysermc.org>
3-
*
4-
* Permission is hereby granted, free of charge, to any person obtaining a copy
5-
* of this software and associated documentation files (the "Software"), to deal
6-
* in the Software without restriction, including without limitation the rights
7-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8-
* copies of the Software, and to permit persons to whom the Software is
9-
* furnished to do so, subject to the following conditions:
10-
*
11-
* The above copyright notice and this permission notice shall be included in
12-
* all copies or substantial portions of the Software.
13-
*
14-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20-
* THE SOFTWARE.
21-
*
22-
* @author GeyserMC
2+
* Copyright (c) 2024 GeyserMC
3+
* Licensed under the MIT license
234
* @link https://github.com/GeyserMC/DatabaseUtils
245
*/
256
package org.geysermc.databaseutils.processor.info;
@@ -28,6 +9,11 @@
289
import java.util.List;
2910
import java.util.stream.Collectors;
3011
import javax.lang.model.element.TypeElement;
12+
import org.checkerframework.checker.nullness.qual.NonNull;
13+
import org.checkerframework.checker.nullness.qual.Nullable;
14+
import org.geysermc.databaseutils.processor.query.section.by.keyword.EqualsKeyword;
15+
import org.geysermc.databaseutils.processor.query.section.factor.Factor;
16+
import org.geysermc.databaseutils.processor.query.section.factor.VariableByFactor;
3117

3218
public record EntityInfo(
3319
String name, TypeElement type, List<ColumnInfo> columns, List<IndexInfo> indexes, List<CharSequence> keys) {
@@ -54,10 +40,27 @@ public List<ColumnInfo> notKeyColumns() {
5440
.toList();
5541
}
5642

57-
public List<ColumnInfo> notKeyFirstColumns() {
58-
var combined = new ArrayList<ColumnInfo>();
59-
combined.addAll(notKeyColumns());
60-
combined.addAll(keyColumns());
61-
return combined;
43+
public List<Factor> keyColumnsAsFactors(@Nullable Factor separator, @NonNull CharSequence parameterName) {
44+
return asFactors(keyColumns(), separator, parameterName);
45+
}
46+
47+
public List<Factor> notKeyColumnsAsFactors(@Nullable Factor separator, @NonNull CharSequence parameterName) {
48+
return asFactors(notKeyColumns(), separator, parameterName);
49+
}
50+
51+
private List<Factor> asFactors(
52+
@NonNull List<ColumnInfo> columns, @Nullable Factor separator, @Nullable CharSequence parameterName) {
53+
var factors = new ArrayList<Factor>();
54+
for (ColumnInfo column : columns) {
55+
if (!factors.isEmpty() && separator != null) {
56+
factors.add(separator);
57+
}
58+
59+
if (parameterName != null) {
60+
var name = "%s.%s()".formatted(parameterName, column.name());
61+
factors.add(new VariableByFactor(column.name(), new EqualsKeyword().addParameterName(name)));
62+
}
63+
}
64+
return factors;
6265
}
6366
}

0 commit comments

Comments
 (0)