Skip to content

Commit 3478d20

Browse files
committed
Added deleteBy
1 parent 6aaa6d7 commit 3478d20

File tree

7 files changed

+121
-20
lines changed

7 files changed

+121
-20
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828

2929
public final class ActionRegistry {
3030
private static final Set<Action> REGISTERED_ACTIONS = Set.of(
31-
new FindByAction(), new ExistsByAction(), new InsertAction(), new UpdateAction(), new DeleteAction());
31+
new FindByAction(),
32+
new ExistsByAction(),
33+
new DeleteByAction(),
34+
new InsertAction(),
35+
new UpdateAction(),
36+
new DeleteAction());
3237

3338
public static Action actionMatching(String name) {
3439
for (Action action : REGISTERED_ACTIONS) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
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
23+
* @link https://github.com/GeyserMC/DatabaseUtils
24+
*/
25+
package org.geysermc.databaseutils.processor.action;
26+
27+
import com.squareup.javapoet.MethodSpec;
28+
import javax.lang.model.element.ExecutableElement;
29+
import javax.lang.model.element.TypeElement;
30+
import org.geysermc.databaseutils.processor.info.EntityInfo;
31+
import org.geysermc.databaseutils.processor.query.QueryInfo;
32+
import org.geysermc.databaseutils.processor.type.RepositoryGenerator;
33+
import org.geysermc.databaseutils.processor.util.InvalidRepositoryException;
34+
import org.geysermc.databaseutils.processor.util.TypeUtils;
35+
36+
final class DeleteByAction extends ByAction {
37+
DeleteByAction() {
38+
super("deleteBy");
39+
}
40+
41+
@Override
42+
protected void validate(ExecutableElement element, TypeElement returnType, EntityInfo info) {
43+
if (!TypeUtils.isTypeOf(Void.class, returnType)) {
44+
throw new InvalidRepositoryException(
45+
"Expected Void as return type for %s, got %s", element.getSimpleName(), returnType);
46+
}
47+
}
48+
49+
@Override
50+
protected void addToSingle(
51+
RepositoryGenerator generator, QueryInfo queryInfo, MethodSpec.Builder spec, boolean async) {
52+
generator.addDeleteBy(queryInfo, spec, async);
53+
}
54+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void addTo(
5656
EntityInfo info,
5757
Types typeUtils,
5858
boolean async) {
59-
if (!TypeUtils.isTypeOf(Void.class, TypeUtils.toBoxedTypeElement(returnType.asType(), typeUtils))) {
59+
if (!TypeUtils.isTypeOf(Void.class, returnType)) {
6060
throw new InvalidRepositoryException(
6161
"Expected Void as return type for %s, got %s", element.getSimpleName(), returnType);
6262
}

ap/src/main/java/org/geysermc/databaseutils/processor/type/RepositoryGenerator.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ public abstract class RepositoryGenerator {
4444

4545
protected void onConstructorBuilder(MethodSpec.Builder builder) {}
4646

47-
public abstract void addFindBy(QueryInfo queryInfo, MethodSpec.Builder spec, boolean async);
47+
public abstract void addFindBy(QueryInfo info, MethodSpec.Builder spec, boolean async);
4848

49-
public abstract void addExistsBy(QueryInfo queryInfo, MethodSpec.Builder spec, boolean async);
49+
public abstract void addExistsBy(QueryInfo info, MethodSpec.Builder spec, boolean async);
50+
51+
public abstract void addDeleteBy(QueryInfo info, MethodSpec.Builder spec, boolean async);
5052

5153
public abstract void addInsert(EntityInfo info, VariableElement parameter, MethodSpec.Builder spec, boolean async);
5254

ap/src/main/java/org/geysermc/databaseutils/processor/type/SqlRepositoryGenerator.java

+20-16
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,15 @@ protected void onConstructorBuilder(MethodSpec.Builder builder) {
6363
}
6464

6565
@Override
66-
public void addFindBy(QueryInfo queryInfo, MethodSpec.Builder spec, boolean async) {
67-
var query = "select * from %s where %s"
68-
.formatted(queryInfo.tableName(), createWhereForSections(queryInfo.sections()));
69-
addActionedData(spec, async, query, queryInfo.parameterNames(), "%s", queryInfo::columnFor, () -> {
66+
public void addFindBy(QueryInfo info, MethodSpec.Builder spec, boolean async) {
67+
var query = "select * from %s where %s".formatted(info.tableName(), createWhereFor(info));
68+
addActionedData(spec, async, query, info.parameterNames(), "%s", info::columnFor, () -> {
7069
spec.beginControlFlow("if (!result.next())");
7170
spec.addStatement("return null");
7271
spec.endControlFlow();
7372

7473
var arguments = new ArrayList<String>();
75-
for (ColumnInfo column : queryInfo.columns()) {
74+
for (ColumnInfo column : info.columns()) {
7675
var columnType = ClassName.bestGuess(column.typeName().toString());
7776
spec.addStatement(
7877
jdbcGetFor(column.typeName(), "$T _$L = result.%s(%s)"),
@@ -83,25 +82,30 @@ public void addFindBy(QueryInfo queryInfo, MethodSpec.Builder spec, boolean asyn
8382
}
8483
spec.addStatement(
8584
"return new $T($L)",
86-
ClassName.bestGuess(queryInfo.entityType().toString()),
85+
ClassName.bestGuess(info.entityType().toString()),
8786
String.join(", ", arguments));
8887
});
8988
}
9089

9190
@Override
92-
public void addExistsBy(QueryInfo queryInfo, MethodSpec.Builder spec, boolean async) {
93-
var query = "select 1 from %s where %s"
94-
.formatted(queryInfo.tableName(), createWhereForSections(queryInfo.sections()));
91+
public void addExistsBy(QueryInfo info, MethodSpec.Builder spec, boolean async) {
92+
var query = "select 1 from %s where %s".formatted(info.tableName(), createWhereFor(info));
9593
addActionedData(
9694
spec,
9795
async,
9896
query,
99-
queryInfo.parameterNames(),
97+
info.parameterNames(),
10098
"%s",
101-
queryInfo::columnFor,
99+
info::columnFor,
102100
() -> spec.addStatement("return result.next()"));
103101
}
104102

103+
@Override
104+
public void addDeleteBy(QueryInfo info, MethodSpec.Builder spec, boolean async) {
105+
var query = "delete from %s where %s".formatted(info.tableName(), createWhereFor(info));
106+
addActionedData(spec, async, query, info.parameterNames(), "%s", info::columnFor, null);
107+
}
108+
105109
@Override
106110
public void addInsert(EntityInfo info, VariableElement parameter, MethodSpec.Builder spec, boolean async) {
107111
var columnNames =
@@ -113,15 +117,15 @@ public void addInsert(EntityInfo info, VariableElement parameter, MethodSpec.Bui
113117

114118
@Override
115119
public void addUpdate(EntityInfo info, VariableElement parameter, MethodSpec.Builder spec, boolean async) {
116-
var query = "update %s set %s where %s".formatted(info.name(), createSetFor(info), createWhereForSimple(info));
120+
var query = "update %s set %s where %s".formatted(info.name(), createSetFor(info), createWhereFor(info));
117121
var parameters = new ArrayList<>(info.notKeyColumns());
118122
parameters.addAll(info.keyColumns());
119123
addSimple(info, parameter, query, parameters, spec, async);
120124
}
121125

122126
@Override
123127
public void addDelete(EntityInfo info, VariableElement parameter, MethodSpec.Builder spec, boolean async) {
124-
var query = "delete from %s where %s".formatted(info.name(), createWhereForSimple(info));
128+
var query = "delete from %s where %s".formatted(info.name(), createWhereFor(info));
125129
addSimple(info, parameter, query, info.keyColumns(), spec, async);
126130
}
127131

@@ -179,12 +183,12 @@ private String createSetFor(EntityInfo info) {
179183
return createParametersForColumns(info.notKeyColumns(), ',');
180184
}
181185

182-
private String createWhereForSimple(EntityInfo info) {
186+
private String createWhereFor(EntityInfo info) {
183187
return createParametersForColumns(info.keyColumns(), ' ');
184188
}
185189

186-
private String createWhereForSections(List<QuerySection> sections) {
187-
return createParametersForSections(sections, ' ');
190+
private String createWhereFor(QueryInfo info) {
191+
return createParametersForSections(info.sections(), ' ');
188192
}
189193

190194
private String createParametersForColumns(List<ColumnInfo> columns, char separator) {

ap/src/test/resources/test/basic/BasicRepository.java

+4
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ public interface BasicRepository extends IRepository<TestEntity> {
1313
CompletableFuture<Void> update(TestEntity entity);
1414

1515
CompletableFuture<Void> insert(TestEntity entity);
16+
17+
CompletableFuture<Void> delete(TestEntity entity);
18+
19+
CompletableFuture<Void> deleteByAAndB(int a, String b);
1620
}

ap/src/test/resources/test/basic/BasicRepositorySqlImpl.java

+32
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,36 @@ public CompletableFuture<Void> insert(TestEntity entity) {
9797
}
9898
} , this.database.executorService());
9999
}
100+
101+
@Override
102+
public CompletableFuture<Void> delete(TestEntity entity) {
103+
return CompletableFuture.supplyAsync(() -> {
104+
try (Connection connection = dataSource.getConnection()) {
105+
try (PreparedStatement statement = connection.prepareStatement("delete from hello where a=? and b=?")) {
106+
statement.setInt(1, entity.a());
107+
statement.setString(2, entity.b());
108+
statement.executeUpdate();
109+
return null;
110+
}
111+
} catch (SQLException exception) {
112+
throw new CompletionException("Unexpected error occurred", exception);
113+
}
114+
} , this.database.executorService());
115+
}
116+
117+
@Override
118+
public CompletableFuture<Void> deleteByAAndB(int a, String b) {
119+
return CompletableFuture.supplyAsync(() -> {
120+
try (Connection connection = dataSource.getConnection()) {
121+
try (PreparedStatement statement = connection.prepareStatement("delete from hello where a=? and b=?")) {
122+
statement.setInt(1, a);
123+
statement.setString(2, b);
124+
statement.executeUpdate();
125+
return null;
126+
}
127+
} catch (SQLException exception) {
128+
throw new CompletionException("Unexpected error occurred", exception);
129+
}
130+
} , this.database.executorService());
131+
}
100132
}

0 commit comments

Comments
 (0)