Skip to content

Commit 7a43621

Browse files
committed
Add error handling and tests
1 parent 39935c1 commit 7a43621

19 files changed

+240
-146
lines changed

doma-core/src/main/java/org/seasar/doma/jdbc/CommandImplementors.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ default <RESULT> SelectCommand<RESULT> createSelectCommand(
6969
return new SelectCommand<>(query, resultSetHandler);
7070
}
7171

72+
/**
73+
* Creates an {@link AggregateCommand} object for aggregating entities based on a specified query
74+
* and strategy.
75+
*
76+
* @param <RESULT> the result type of the aggregation
77+
* @param <ENTITY> the entity type used in the aggregation
78+
* @param method the DAO method associated with the command
79+
* @param query the select query to be executed
80+
* @param entityType the type of the root entity for aggregation
81+
* @param resultReducer the reducer used to process the stream of aggregated entities into a final
82+
* result
83+
* @param aggregateStrategyType the strategy type used for aggregation logic
84+
* @return a new {@link AggregateCommand} instance configured with the provided parameters
85+
*/
7286
default <RESULT, ENTITY> AggregateCommand<RESULT, ENTITY> createAggregateCommand(
7387
Method method,
7488
SelectQuery query,
@@ -111,7 +125,16 @@ default UpdateCommand createUpdateCommand(Method method, UpdateQuery query) {
111125
return new UpdateCommand(query);
112126
}
113127

114-
// TODO
128+
/**
129+
* Creates a {@link DeleteReturningCommand} object.
130+
*
131+
* @param <RESULT> the result type of the command
132+
* @param method the DAO method
133+
* @param query the delete query
134+
* @param resultSetHandler the handler for processing the result set
135+
* @param emptyResultSupplier the supplier for providing an empty result
136+
* @return the created {@link DeleteReturningCommand} object
137+
*/
115138
default <RESULT> DeleteReturningCommand<RESULT> createDeleteReturningCommand(
116139
Method method,
117140
DeleteQuery query,
@@ -136,7 +159,16 @@ default <RESULT> InsertReturningCommand<RESULT> createInsertReturningCommand(
136159
return new InsertReturningCommand<>(query, resultSetHandler, emptyResultSupplier);
137160
}
138161

139-
// TODO
162+
/**
163+
* Creates an {@link UpdateReturningCommand} object.
164+
*
165+
* @param <RESULT> the result type of the command
166+
* @param method the DAO method
167+
* @param query the update query
168+
* @param resultSetHandler the handler for processing the result set
169+
* @param emptyResultSupplier the supplier for providing an empty result
170+
* @return the created {@link UpdateReturningCommand} object
171+
*/
140172
default <RESULT> UpdateReturningCommand<RESULT> createUpdateReturningCommand(
141173
Method method,
142174
UpdateQuery query,

doma-core/src/main/java/org/seasar/doma/jdbc/criteria/statement/SpecificMetamodels.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
import java.util.HashSet;
2020
import java.util.List;
2121
import java.util.Objects;
22+
import org.seasar.doma.DomaException;
2223
import org.seasar.doma.jdbc.criteria.metamodel.EntityMetamodel;
2324
import org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel;
2425
import org.seasar.doma.jdbc.entity.EntityPropertyType;
2526
import org.seasar.doma.jdbc.entity.EntityType;
2627
import org.seasar.doma.jdbc.query.ReturningProperties;
28+
import org.seasar.doma.message.Message;
2729

2830
record SpecificMetamodels(
2931
EntityMetamodel<?> entityMetamodel, List<PropertyMetamodel<?>> propertyMetamodels)
@@ -33,9 +35,10 @@ record SpecificMetamodels(
3335
public List<? extends EntityPropertyType<?, ?>> resolve(EntityType<?> entityType) {
3436
Objects.requireNonNull(entityType);
3537
if (!entityMetamodel.asType().equals(entityType)) {
36-
// TODO
37-
throw new IllegalArgumentException(
38-
"The specified entity type is not equal to the entity type of the entity metamodel.");
38+
throw new DomaException(
39+
Message.DOMA6013,
40+
entityMetamodel.asType().getEntityClass().getName(),
41+
entityType.getEntityClass().getName());
3942
}
4043
return propertyMetamodels.stream().map(PropertyMetamodel::asType).toList();
4144
}
@@ -50,11 +53,8 @@ static ReturningProperties of(
5053
for (int i = 0; i < propertyMetamodels.length; i++) {
5154
var p = propertyMetamodels[i];
5255
if (!allProperties.contains(p)) {
53-
// TODO
54-
throw new IllegalArgumentException(
55-
String.format(
56-
"The specified properties are not included in the entity. name=%s, index=%d",
57-
p.getName(), i));
56+
throw new DomaException(
57+
Message.DOMA6012, p.getName(), i, entityMetamodel.asType().getEntityClass().getName());
5858
}
5959
}
6060
return new SpecificMetamodels(entityMetamodel, Arrays.stream(propertyMetamodels).toList());

doma-core/src/main/java/org/seasar/doma/jdbc/dialect/Dialect.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,24 +393,58 @@ Sql<?> getIdentityReservationSql(
393393
UpsertAssembler getUpsertAssembler(UpsertAssemblerContext context);
394394

395395
/**
396-
* Returns the MultiInsertAssembler implementation for the given context.
396+
* Provides an implementation of {@code MultiInsertAssembler} based on the given context.
397397
*
398-
* @param <ENTITY> the entity type
399-
* @param context the MultiInsertAssemblerContext object
400-
* @return the MultiInsertAssembler object for the given context
398+
* @param <ENTITY> the type of the entity to be handled by the assembler
399+
* @param context the context that holds the information required to create the {@code
400+
* MultiInsertAssembler}
401+
* @return an instance of {@code MultiInsertAssembler} specific to the provided entity context
401402
*/
402403
<ENTITY> MultiInsertAssembler getMultiInsertAssembler(
403404
MultiInsertAssemblerContext<ENTITY> context);
404405

406+
/**
407+
* Provides an implementation of {@code InsertAssembler} based on the given context.
408+
*
409+
* @param <ENTITY> the type of the entity to be handled by the assembler
410+
* @param context the context that holds the information required to create the {@code
411+
* InsertAssembler}
412+
* @return an instance of {@code InsertAssembler} specific to the provided entity context
413+
*/
405414
default <ENTITY> InsertAssembler getInsertAssembler(InsertAssemblerContext<ENTITY> context) {
406415
return new DefaultInsertAssembler<>(context);
407416
}
408417

418+
/**
419+
* Provides an implementation of {@code UpdateAssembler} based on the given context.
420+
*
421+
* @param <ENTITY> the type of the entity to be handled by the assembler
422+
* @param context the context that holds the information required to create the {@code
423+
* UpdateAssembler}
424+
* @return an instance of {@code UpdateAssembler} specific to the provided entity context
425+
*/
409426
default <ENTITY> UpdateAssembler getUpdateAssembler(UpdateAssemblerContext<ENTITY> context) {
410427
return new DefaultUpdateAssembler<>(context);
411428
}
412429

430+
/**
431+
* Provides an implementation of {@code DeleteAssembler} based on the given context.
432+
*
433+
* @param <ENTITY> the type of the entity to be handled by the assembler
434+
* @param context the context that holds the information required to create the {@code
435+
* DeleteAssembler}
436+
* @return an instance of {@code DeleteAssembler} specific to the provided entity context
437+
*/
413438
default <ENTITY> DeleteAssembler getDeleteAssembler(DeleteAssemblerContext<ENTITY> context) {
414439
return new DefaultDeleteAssembler<>(context);
415440
}
441+
442+
/**
443+
* Determines if the current implementation supports the RETURNING feature in database operations.
444+
*
445+
* @return true if the RETURNING feature is supported, false otherwise
446+
*/
447+
default boolean supportsReturning() {
448+
return true;
449+
}
416450
}

doma-core/src/main/java/org/seasar/doma/jdbc/dialect/MysqlDialect.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ public UpsertAssembler getUpsertAssembler(UpsertAssemblerContext context) {
289289
return new MysqlUpsertAssembler(context, version);
290290
}
291291

292+
@Override
293+
public boolean supportsReturning() {
294+
return false;
295+
}
296+
292297
public enum MySqlVersion {
293298
V5,
294299
V8

doma-core/src/main/java/org/seasar/doma/jdbc/dialect/OracleDialect.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,9 @@ public <ENTITY> MultiInsertAssembler getMultiInsertAssembler(
116116
MultiInsertAssemblerContext<ENTITY> context) {
117117
return new OracleMultiInsertAssembler<>(context);
118118
}
119+
120+
@Override
121+
public boolean supportsReturning() {
122+
return false;
123+
}
119124
}

doma-core/src/main/java/org/seasar/doma/jdbc/query/AutoModifyQuery.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ protected AutoModifyQuery(EntityType<ENTITY> entityType) {
6666
this.entityType = entityType;
6767
}
6868

69+
@Override
70+
public void prepare() {
71+
super.prepare();
72+
if (!returning.isNone() && !config.getDialect().supportsReturning()) {
73+
throw new JdbcException(Message.DOMA2240, config.getDialect().getName());
74+
}
75+
}
76+
6977
protected void prepareSpecialPropertyTypes() {
7078
idPropertyTypes = entityType.getIdPropertyTypes();
7179
versionPropertyType = entityType.getVersionPropertyType();

doma-core/src/main/java/org/seasar/doma/jdbc/query/DeleteAssemblerContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.seasar.doma.jdbc.dialect.Dialect;
2323
import org.seasar.doma.jdbc.entity.EntityPropertyType;
2424
import org.seasar.doma.jdbc.entity.EntityType;
25+
import org.seasar.doma.jdbc.entity.TenantIdPropertyType;
2526
import org.seasar.doma.jdbc.entity.VersionPropertyType;
2627

2728
/**
@@ -36,7 +37,7 @@ public class DeleteAssemblerContext<ENTITY> {
3637
public final Dialect dialect;
3738
public final List<EntityPropertyType<ENTITY, ?>> idPropertyTypes;
3839
public final VersionPropertyType<ENTITY, ?, ?> versionPropertyType;
39-
public final EntityPropertyType<ENTITY, ?> tenantIdPropertyType;
40+
public final TenantIdPropertyType<ENTITY, ?, ?> tenantIdPropertyType;
4041
public boolean versionIgnored;
4142
public final ENTITY entity;
4243
public ReturningProperties returning;
@@ -48,7 +49,7 @@ public class DeleteAssemblerContext<ENTITY> {
4849
Dialect dialect,
4950
List<EntityPropertyType<ENTITY, ?>> idPropertyTypes,
5051
VersionPropertyType<ENTITY, ?, ?> versionPropertyType,
51-
EntityPropertyType<ENTITY, ?> tenantIdPropertyType,
52+
TenantIdPropertyType<ENTITY, ?, ?> tenantIdPropertyType,
5253
boolean versionIgnored,
5354
ENTITY entity,
5455
ReturningProperties returning) {

doma-core/src/main/java/org/seasar/doma/jdbc/query/DeleteAssemblerContextBuilder.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
package org.seasar.doma.jdbc.query;
1717

1818
import java.util.List;
19+
import java.util.Objects;
1920
import org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder;
2021
import org.seasar.doma.jdbc.Naming;
2122
import org.seasar.doma.jdbc.dialect.Dialect;
2223
import org.seasar.doma.jdbc.entity.EntityPropertyType;
2324
import org.seasar.doma.jdbc.entity.EntityType;
25+
import org.seasar.doma.jdbc.entity.TenantIdPropertyType;
2426
import org.seasar.doma.jdbc.entity.VersionPropertyType;
2527

2628
public class DeleteAssemblerContextBuilder {
@@ -32,23 +34,21 @@ public static <ENTITY> DeleteAssemblerContext<ENTITY> build(
3234
Dialect dialect,
3335
List<EntityPropertyType<ENTITY, ?>> idPropertyTypes,
3436
VersionPropertyType<ENTITY, ?, ?> versionPropertyType,
35-
EntityPropertyType<ENTITY, ?> tenantIdPropertyType,
37+
TenantIdPropertyType<ENTITY, ?, ?> tenantIdPropertyType,
3638
boolean versionIgnored,
3739
ENTITY entity,
3840
ReturningProperties returning) {
3941

40-
// TODO: check arguments
41-
4242
return new DeleteAssemblerContext<>(
43-
buf,
44-
entityType,
45-
naming,
46-
dialect,
47-
idPropertyTypes,
48-
versionPropertyType,
49-
tenantIdPropertyType,
43+
Objects.requireNonNull(buf),
44+
Objects.requireNonNull(entityType),
45+
Objects.requireNonNull(naming),
46+
Objects.requireNonNull(dialect),
47+
Objects.requireNonNull(idPropertyTypes),
48+
versionPropertyType, // nullable
49+
tenantIdPropertyType, // nullable
5050
versionIgnored,
51-
entity,
52-
returning);
51+
Objects.requireNonNull(entity),
52+
Objects.requireNonNull(returning));
5353
}
5454
}

doma-core/src/main/java/org/seasar/doma/jdbc/query/InsertAssemblerContext.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class InsertAssemblerContext<ENTITY> {
4646
* @param dialect the SQL dialect
4747
* @param insertPropertyTypes the property types that are targets for the insert
4848
* @param entity the entity
49-
* @param returning whether a returning clause is required
49+
* @param returning the properties to be returned after insert execution
5050
*/
5151
InsertAssemblerContext(
5252
PreparedSqlBuilder buf,
@@ -56,19 +56,12 @@ public class InsertAssemblerContext<ENTITY> {
5656
List<EntityPropertyType<ENTITY, ?>> insertPropertyTypes,
5757
ENTITY entity,
5858
ReturningProperties returning) {
59-
Objects.requireNonNull(buf);
60-
Objects.requireNonNull(entityType);
61-
Objects.requireNonNull(naming);
62-
Objects.requireNonNull(dialect);
63-
Objects.requireNonNull(insertPropertyTypes);
64-
Objects.requireNonNull(entity);
65-
Objects.requireNonNull(returning);
66-
this.buf = buf;
67-
this.entityType = entityType;
68-
this.naming = naming;
69-
this.dialect = dialect;
70-
this.insertPropertyTypes = insertPropertyTypes;
71-
this.entity = entity;
72-
this.returning = returning;
59+
this.buf = Objects.requireNonNull(buf);
60+
this.entityType = Objects.requireNonNull(entityType);
61+
this.naming = Objects.requireNonNull(naming);
62+
this.dialect = Objects.requireNonNull(dialect);
63+
this.insertPropertyTypes = Objects.requireNonNull(insertPropertyTypes);
64+
this.entity = Objects.requireNonNull(entity);
65+
this.returning = Objects.requireNonNull(returning);
7366
}
7467
}

doma-core/src/main/java/org/seasar/doma/jdbc/query/InsertAssemblerContextBuilder.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.seasar.doma.jdbc.query;
1717

1818
import java.util.List;
19+
import java.util.Objects;
1920
import org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder;
2021
import org.seasar.doma.jdbc.Naming;
2122
import org.seasar.doma.jdbc.dialect.Dialect;
@@ -33,9 +34,13 @@ public static <ENTITY> InsertAssemblerContext<ENTITY> build(
3334
ENTITY entity,
3435
ReturningProperties returning) {
3536

36-
// TODO: check arguments
37-
3837
return new InsertAssemblerContext<>(
39-
buf, entityType, naming, dialect, insertPropertyTypes, entity, returning);
38+
Objects.requireNonNull(buf),
39+
Objects.requireNonNull(entityType),
40+
Objects.requireNonNull(naming),
41+
Objects.requireNonNull(dialect),
42+
Objects.requireNonNull(insertPropertyTypes),
43+
Objects.requireNonNull(entity),
44+
Objects.requireNonNull(returning));
4045
}
4146
}

0 commit comments

Comments
 (0)