Skip to content

Commit 7d4ee7c

Browse files
committed
1 parent 6198350 commit 7d4ee7c

File tree

12 files changed

+322
-30
lines changed

12 files changed

+322
-30
lines changed

project/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
group=org.babyfish.jimmer
2-
version=0.9.39
2+
version=0.9.40

project/jimmer-spring-boot-starter/src/main/java/org/babyfish/jimmer/spring/JSpringSqlClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.babyfish.jimmer.spring;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import org.babyfish.jimmer.spring.cfg.JimmerProperties;
45
import org.babyfish.jimmer.spring.cfg.support.SpringConnectionManager;
56
import org.babyfish.jimmer.spring.cfg.support.SpringLogicalDeletedValueGeneratorProvider;
@@ -98,6 +99,7 @@ protected JSqlClient.Builder createBuilder() {
9899
DialectDetector dialectDetector = getOptionalBean(DialectDetector.class);
99100
Executor executor = getOptionalBean(Executor.class);
100101
SqlFormatter sqlFormatter = getOptionalBean(SqlFormatter.class);
102+
ObjectMapper objectMapper = getOptionalBean(ObjectMapper.class);
101103
CacheFactory cacheFactory = getOptionalBean(CacheFactory.class);
102104
CacheOperator cacheOperator = getOptionalBean(CacheOperator.class);
103105
MicroServiceExchange exchange = getOptionalBean(MicroServiceExchange.class);
@@ -174,6 +176,7 @@ protected JSqlClient.Builder createBuilder() {
174176
.setDatabaseValidationMode(properties.getDatabaseValidation().getMode())
175177
.setDatabaseValidationCatalog(properties.getDatabaseValidation().getCatalog())
176178
.setDatabaseValidationSchema(properties.getDatabaseValidation().getSchema())
179+
.setDefaultSerializedTypeObjectMapper(objectMapper)
177180
.setCacheFactory(cacheFactory)
178181
.setCacheOperator(cacheOperator)
179182
.addCacheAbandonedCallbacks(callbacks);

project/jimmer-sql-kotlin/src/test/kotlin/org/babyfish/jimmer/sql/kt/filter/SaveTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class SaveTest : AbstractMutationTest() {
248248
"where tb_1_.NAME = ? and tb_1_.DELETED_TIME is null"
249249
)
250250
variables("Andrew")
251-
queryReason(QueryReason.IDENTITY_GENERATOR_REQUIRED)
251+
queryReason(QueryReason.UPSERT_NOT_SUPPORTED)
252252
}
253253
statement {
254254
sql(

project/jimmer-sql-kotlin/src/test/kotlin/org/babyfish/jimmer/sql/kt/mutation/SaveCommandTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SaveCommandTest : AbstractMutationTest() {
4141
)
4242
}) {
4343
statement {
44-
queryReason(QueryReason.IDENTITY_GENERATOR_REQUIRED)
44+
queryReason(QueryReason.UPSERT_NOT_SUPPORTED)
4545
sql(
4646
"""select tb_1_.ID, tb_1_.NAME, tb_1_.EDITION
4747
|from BOOK tb_1_
@@ -86,7 +86,7 @@ class SaveCommandTest : AbstractMutationTest() {
8686
}
8787
}) {
8888
statement {
89-
queryReason(QueryReason.IDENTITY_GENERATOR_REQUIRED)
89+
queryReason(QueryReason.UPSERT_NOT_SUPPORTED)
9090
sql(
9191
"""select tb_1_.ID, tb_1_.NAME, tb_1_.EDITION
9292
|from BOOK tb_1_

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/JSqlClientImpl.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,11 @@ public Builder setDefaultSerializedTypeObjectMapper(ObjectMapper mapper) {
11651165

11661166
@Override
11671167
public Builder setSerializedTypeObjectMapper(Class<?> type, ObjectMapper mapper) {
1168-
serializedTypeObjectMapperMap.put(type != null ? type : Object.class, mapper);
1168+
if (mapper != null) {
1169+
serializedTypeObjectMapperMap.put(type != null ? type : Object.class, mapper);
1170+
} else {
1171+
serializedTypeObjectMapperMap.remove(type != null ? type : Object.class);
1172+
}
11691173
return this;
11701174
}
11711175

@@ -1185,7 +1189,11 @@ public Builder setSerializedPropObjectMapper(ImmutableProp prop, ObjectMapper ma
11851189
"\""
11861190
);
11871191
}
1188-
serializedPropObjectMapperMap.put(prop, mapper);
1192+
if (mapper != null) {
1193+
serializedPropObjectMapperMap.put(prop, mapper);
1194+
} else {
1195+
serializedTypeObjectMapperMap.remove(prop);
1196+
}
11891197
return this;
11901198
}
11911199

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/impl/mutation/PreHandler.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -297,23 +297,29 @@ final QueryReason queryReason(boolean hasId, Collection<DraftSpi> drafts) {
297297
}
298298
JSqlClientImplementor sqlClient = ctx.options.getSqlClient();
299299
SaveMode saveMode = ctx.options.getMode();
300-
if (!hasId && saveMode != SaveMode.UPDATE_ONLY) {
301-
IdGenerator idGenerator = ctx.options.getSqlClient().getIdGenerator(ctx.path.getType().getJavaClass());
302-
if (idGenerator == null) {
303-
ctx.throwNoIdGenerator();
304-
}
305-
ImmutableProp prop = ctx.path.getProp();
306-
if (prop != null && ctx.options.isKeyOnlyAsReference(prop) && isKeyOnly(drafts)) {
307-
return QueryReason.KEY_ONLY_AS_REFERENCE;
308-
}
309-
if (!(idGenerator instanceof IdentityIdGenerator)) {
310-
return QueryReason.IDENTITY_GENERATOR_REQUIRED;
300+
boolean clearMode = saveMode == SaveMode.INSERT_ONLY || saveMode == SaveMode.UPDATE_ONLY;
301+
if (!clearMode && !sqlClient.getDialect().isUpsertSupported()) {
302+
return QueryReason.UPSERT_NOT_SUPPORTED;
303+
}
304+
if (!hasId) {
305+
if (!clearMode && !ctx.options.getSqlClient().getDialect().isNoIdUpsertSupported()) {
306+
return QueryReason.NO_ID_UPSERT_NOT_SUPPORTED;
307+
}
308+
if (saveMode != SaveMode.UPDATE_ONLY) {
309+
IdGenerator idGenerator = ctx.options.getSqlClient().getIdGenerator(ctx.path.getType().getJavaClass());
310+
if (idGenerator == null) {
311+
ctx.throwNoIdGenerator();
312+
}
313+
ImmutableProp prop = ctx.path.getProp();
314+
if (prop != null && ctx.options.isKeyOnlyAsReference(prop) && isKeyOnly(drafts)) {
315+
return QueryReason.KEY_ONLY_AS_REFERENCE;
316+
}
317+
if (!(idGenerator instanceof IdentityIdGenerator)) {
318+
return QueryReason.IDENTITY_GENERATOR_REQUIRED;
319+
}
311320
}
312321
}
313-
if (saveMode != SaveMode.INSERT_ONLY && saveMode != SaveMode.UPDATE_ONLY) {
314-
if (!sqlClient.getDialect().isUpsertSupported()) {
315-
return QueryReason.UPSERT_NOT_SUPPORTED;
316-
}
322+
if (!clearMode) {
317323
if (saveMode != SaveMode.INSERT_IF_ABSENT &&
318324
!sqlClient.getDialect().isUpsertWithOptimisticLockSupported()) {
319325
UserOptimisticLock<?, ?> userLock = ctx.options.getUserOptimisticLock(ctx.path.getType());

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/ast/mutation/QueryReason.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ public enum QueryReason {
184184
*/
185185
UPSERT_NOT_SUPPORTED,
186186

187+
/**
188+
* For some databases such as SqlLite, when using
189+
* database-level upsert(insert or update) operations,
190+
* if the object id is not specified, JDBC cannot
191+
* get the id allocated by the database.
192+
* Therefore, data-level upsert operations without
193+
* id should be abandoned.
194+
*/
195+
NO_ID_UPSERT_NOT_SUPPORTED,
196+
187197
/**
188198
* The current database does not support mixing optimistic locking
189199
* checks in upsert operations (so far, among the dialects implemented

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/dialect/Dialect.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ default boolean isUpsertSupported() {
143143
return false;
144144
}
145145

146+
default boolean isNoIdUpsertSupported() {
147+
return isUpsertSupported();
148+
}
149+
146150
default boolean isUpsertWithOptimisticLockSupported() {
147151
return false;
148152
}

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/dialect/SQLiteDialect.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public boolean isUpsertSupported() {
2727
return true;
2828
}
2929

30+
@Override
31+
public boolean isNoIdUpsertSupported() {
32+
return false;
33+
}
34+
35+
@Override
36+
public boolean isBatchDumb() {
37+
return true;
38+
}
39+
3040
@Override
3141
public void upsert(UpsertContext ctx) {
3242
ctx.sql("insert into ")

project/jimmer-sql/src/test/java/org/babyfish/jimmer/sql/mutation/CascadeSaveTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.babyfish.jimmer.sql.ast.mutation.SaveMode;
1111
import org.babyfish.jimmer.sql.common.AbstractMutationTest;
1212
import org.babyfish.jimmer.sql.common.Constants;
13+
import org.babyfish.jimmer.sql.common.NativeDatabases;
1314
import org.babyfish.jimmer.sql.dialect.H2Dialect;
1415
import org.babyfish.jimmer.sql.meta.UserIdGenerator;
1516
import org.babyfish.jimmer.sql.meta.impl.IdentityIdGenerator;
@@ -21,6 +22,7 @@
2122
import org.babyfish.jimmer.sql.model.wild.*;
2223
import org.babyfish.jimmer.sql.runtime.DbLiteral;
2324
import org.jetbrains.annotations.NotNull;
25+
import org.junit.jupiter.api.Assertions;
2426
import org.junit.jupiter.api.Test;
2527

2628
import java.math.BigDecimal;

0 commit comments

Comments
 (0)