Skip to content

Commit d9b237a

Browse files
committed
1 parent 4456d10 commit d9b237a

File tree

8 files changed

+119
-26
lines changed

8 files changed

+119
-26
lines changed

project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/KSqlClient.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.babyfish.jimmer.lang.NewChain
55
import org.babyfish.jimmer.sql.JSqlClient
66
import org.babyfish.jimmer.sql.ast.mutation.DeleteMode
77
import org.babyfish.jimmer.sql.event.binlog.BinLog
8+
import org.babyfish.jimmer.sql.exception.DatabaseValidationException
89
import org.babyfish.jimmer.sql.exception.EmptyResultException
910
import org.babyfish.jimmer.sql.exception.TooManyResultsException
1011
import org.babyfish.jimmer.sql.fetcher.DtoMetadata
@@ -265,6 +266,29 @@ interface KSqlClient : KDeprecatedMoreSaveOperations {
265266
*/
266267
fun <R> transaction(propagation: Propagation = Propagation.REQUIRED, block: () -> R): R
267268

269+
/**
270+
* Validate the database manually.
271+
*
272+
* User can either automatically validate the database or manually validate it.
273+
*
274+
* - Automatically:
275+
* Specify the `databaseValidationMode` as
276+
* [DatabaseValidationMode.ERROR] or [DatabaseValidationMode.WARNING]
277+
* when building the `KSqlClient` object, so there is **NO** need
278+
* to call this function
279+
*
280+
* - Manually: Specify the `databaseValidationMode` as
281+
* [DatabaseValidationMode.NONE] and call this method after obtaining
282+
* the `KSqlClient` object
283+
*
284+
* > Note: If there are any database validation errors, the relevant
285+
* exceptions will be returned as the result of this method
286+
* instead of being thrown directly.
287+
*
288+
* @return The validation error or null
289+
*/
290+
fun validateDatabase(): DatabaseValidationException?
291+
268292
val javaClient: JSqlClientImplementor
269293
}
270294

project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/di/AbstractKSqlClientDelegate.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.babyfish.jimmer.sql.kt.di
22

33
import org.babyfish.jimmer.sql.event.binlog.BinLog
4+
import org.babyfish.jimmer.sql.exception.DatabaseValidationException
45
import org.babyfish.jimmer.sql.kt.*
56
import org.babyfish.jimmer.sql.kt.ast.KExecutable
67
import org.babyfish.jimmer.sql.kt.ast.mutation.KMutableDelete
@@ -83,6 +84,9 @@ abstract class AbstractKSqlClientDelegate : KSqlClientImplementor {
8384
override fun <R> transaction(propagation: Propagation, block: () -> R): R =
8485
sqlClient().transaction(propagation, block)
8586

87+
override fun validateDatabase(): DatabaseValidationException? =
88+
sqlClient().validateDatabase()
89+
8690
override val javaClient: JSqlClientImplementor
8791
get() = sqlClient().javaClient
8892
}

project/jimmer-sql-kotlin/src/main/kotlin/org/babyfish/jimmer/sql/kt/impl/KSqlClientImpl.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.babyfish.jimmer.sql.ast.impl.query.FilterLevel
99
import org.babyfish.jimmer.sql.ast.impl.query.MutableRootQueryImpl
1010
import org.babyfish.jimmer.sql.ast.table.Table
1111
import org.babyfish.jimmer.sql.event.binlog.BinLog
12+
import org.babyfish.jimmer.sql.exception.DatabaseValidationException
1213
import org.babyfish.jimmer.sql.kt.*
1314
import org.babyfish.jimmer.sql.kt.ast.KExecutable
1415
import org.babyfish.jimmer.sql.kt.ast.mutation.*
@@ -152,6 +153,9 @@ internal class KSqlClientImpl(
152153
override fun <R> transaction(propagation: Propagation, block: () -> R): R =
153154
javaClient.transaction(propagation, block)
154155

156+
override fun validateDatabase(): DatabaseValidationException? =
157+
javaClient.validateDatabase()
158+
155159
override val entityManager: EntityManager
156160
get() = javaClient.entityManager
157161

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.babyfish.jimmer.sql.event.Triggers;
1717
import org.babyfish.jimmer.sql.event.binlog.BinLog;
1818
import org.babyfish.jimmer.sql.event.binlog.BinLogPropReader;
19+
import org.babyfish.jimmer.sql.exception.DatabaseValidationException;
1920
import org.babyfish.jimmer.sql.fetcher.Fetcher;
2021
import org.babyfish.jimmer.sql.fetcher.ReferenceFetchType;
2122
import org.babyfish.jimmer.sql.filter.Filter;
@@ -244,6 +245,30 @@ default <R> R transaction(Supplier<R> block) {
244245
*/
245246
<R> R transaction(Propagation propagation, Supplier<R> block);
246247

248+
/**
249+
* Validate the database manually.
250+
*
251+
* <p>User can either automatically validate the database or manually validate it.</p>
252+
*
253+
* <ul>
254+
* <li>Automatically: Specify the {@code databaseValidationMode} as
255+
* {@link DatabaseValidationMode#ERROR} or {@link DatabaseValidationMode#ERROR}
256+
* when building the {@code JSqlClient} object, so there is <b>NO</b> need
257+
* to call this method</li>
258+
* <li>Manually: Specify the {@code databaseValidationMode} as
259+
* {@link DatabaseValidationMode#NONE} and call this method after obtaining
260+
* the {@code JSqlClient} object</li>
261+
* </ul>
262+
*
263+
* <p>Note: If there are any database validation errors, the relevant
264+
* exceptions will be returned as the result of this method
265+
* instead of being thrown directly.</p>
266+
*
267+
* @return The validation error or null
268+
*/
269+
@Nullable
270+
DatabaseValidationException validateDatabase();
271+
247272
interface Builder {
248273

249274
int DEFAULT_BATCH_SIZE = 128;

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

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,34 @@ public <R> R transaction(Propagation propagation, Supplier<R> block) {
495495
return txConnectionManager.executeTransaction(propagation, con -> block.get());
496496
}
497497

498+
@Nullable
499+
@Override
500+
public DatabaseValidationException validateDatabase() {
501+
ConnectionManager cm = connectionManager;
502+
if (cm == null) {
503+
throw new IllegalStateException(
504+
"The `connectionManager` of must be configured when `validate` is configured"
505+
);
506+
}
507+
return cm.execute(con -> {
508+
try {
509+
return DatabaseValidators.validate(
510+
entityManager,
511+
microServiceName,
512+
defaultDissociationActionCheckable,
513+
metadataStrategy,
514+
null,
515+
con
516+
);
517+
} catch (SQLException ex) {
518+
throw new ExecutionException(
519+
"Cannot validate the database because of SQL exception",
520+
ex
521+
);
522+
}
523+
});
524+
}
525+
498526
@Override
499527
public Entities getEntities() {
500528
return entities;
@@ -1838,7 +1866,7 @@ public JSqlClient build() {
18381866
);
18391867
}
18401868
}
1841-
validateDatabase(metadataStrategy);
1869+
validateDatabase(sqlClient);
18421870
return sqlClient;
18431871
}
18441872

@@ -1912,31 +1940,9 @@ private void validateAssociations(FilterManager filterManager) {
19121940
}
19131941
}
19141942

1915-
private void validateDatabase(MetadataStrategy metadataStrategy) {
1943+
private void validateDatabase(JSqlClient sqlClient) {
19161944
if (databaseValidationMode != DatabaseValidationMode.NONE) {
1917-
ConnectionManager cm = connectionManager;
1918-
if (cm == null) {
1919-
throw new IllegalStateException(
1920-
"The `connectionManager` of must be configured when `validate` is configured"
1921-
);
1922-
}
1923-
DatabaseValidationException validationException = cm.execute(con -> {
1924-
try {
1925-
return DatabaseValidators.validate(
1926-
entityManager(),
1927-
microServiceName,
1928-
defaultDissociationActionCheckable,
1929-
metadataStrategy,
1930-
null,
1931-
con
1932-
);
1933-
} catch (SQLException ex) {
1934-
throw new ExecutionException(
1935-
"Cannot validate the database because of SQL exception",
1936-
ex
1937-
);
1938-
}
1939-
});
1945+
DatabaseValidationException validationException = sqlClient.validateDatabase();
19401946
if (validationException != null) {
19411947
if (databaseValidationMode == DatabaseValidationMode.ERROR) {
19421948
throw validationException;

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/di/AbstractJSqlClientDelegate.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.babyfish.jimmer.sql.event.TriggerType;
2121
import org.babyfish.jimmer.sql.event.Triggers;
2222
import org.babyfish.jimmer.sql.event.binlog.BinLog;
23+
import org.babyfish.jimmer.sql.exception.DatabaseValidationException;
2324
import org.babyfish.jimmer.sql.fetcher.Fetcher;
2425
import org.babyfish.jimmer.sql.fetcher.ReferenceFetchType;
2526
import org.babyfish.jimmer.sql.filter.FilterConfig;
@@ -430,6 +431,12 @@ public <R> R transaction(Propagation propagation, Supplier<R> block) {
430431
return sqlClient().transaction(propagation, block);
431432
}
432433

434+
@Override
435+
@Nullable
436+
public DatabaseValidationException validateDatabase() {
437+
return sqlClient().validateDatabase();
438+
}
439+
433440
@Override
434441
public MutableSubQuery createSubQuery(TableProxy<?> table) {
435442
return sqlClient().createSubQuery(table);

project/jimmer-sql/src/main/java/org/babyfish/jimmer/sql/runtime/ConnectionManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import javax.sql.DataSource;
88
import java.sql.Connection;
99
import java.sql.SQLException;
10-
import java.util.Objects;
1110
import java.util.function.Function;
1211

1312
@FunctionalInterface
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
package org.babyfish.jimmer.sql.runtime;
22

33
public enum DatabaseValidationMode {
4+
5+
/**
6+
* Default option, no automatic database validation is required.
7+
*
8+
* <p>You need this configuration in the following two cases:</p>
9+
* <ul>
10+
* <li>You do not want to validate the database.</li>
11+
* <li>You want to manually validate the database for specific purposes,
12+
* such as precisely controlling the timing of validation.
13+
* <i>In this case, you need to manually call the `validateDatabase`
14+
* method of the `sqlClient` object.</i>
15+
* </li>
16+
* </ul>
17+
*/
418
NONE,
19+
20+
/**
21+
* Automatically validate the database,
22+
* and if there are validation errors, throw an exception.
23+
*/
524
WARNING,
25+
26+
/**
27+
* Automatically validate the database,
28+
* and if there are validation errors, print a warning.
29+
*/
630
ERROR
731
}

0 commit comments

Comments
 (0)