|
| 1 | +# Configuration of the Library |
| 2 | + |
| 3 | +Most behaviors of MyBatis Dynamic SQL are not configurable - the library does what it does. |
| 4 | +This page will detail the behaviors that can be modified. Configuration is available with version 1.4.1 and later |
| 5 | +of the library. |
| 6 | + |
| 7 | +The library can be configured globally - which will change the behavior for all statements - or each individual statement |
| 8 | +can be configured. There are sensible defaults for all configuration values, so configuration is not strictly necessary. |
| 9 | +If you want to change any of the default behaviors of the library, then the information on this page will help. |
| 10 | + |
| 11 | +Prior to version 1.4.1 of the library, there was no configurable behavior in the library. In version 1.4.1 we changed |
| 12 | +the default behavior of the library to throw an exception if a where clause fails to render. We did this out of an |
| 13 | +abundance of caution because the optional conditions in a where clause could lead to a statement that affected all |
| 14 | +rows in a table (for example, a delete statement could inadvertently delete all rows in a table). If you want the library |
| 15 | +to function as it did before version 1.4.1 where it was acceptable to have a where clause that didn't render, then you |
| 16 | +can change the global configuration as shown below. |
| 17 | + |
| 18 | +## Global Configuration |
| 19 | + |
| 20 | +On first use the library will initialize the global configuration. The global configuration can be changed via a property |
| 21 | +file named `mybatis-dynamic-sql.properties` in the root of the classpath. If you wish to use a different file name, |
| 22 | +you can specify the file name as a JVM property named `mybatis-dynamic-sql.configurationFile`. Note that the global |
| 23 | +configuration is created one time and shared for every statement in the same JVM. |
| 24 | + |
| 25 | +The configuration file is a standard Java properties file. The possible values are detailed in the next section. |
| 26 | + |
| 27 | +## Global Configuration Properties |
| 28 | + |
| 29 | +| Property | Default | Meaning | |
| 30 | +|--------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 31 | +| nonRenderingWhereClauseAllowed | false | If a where clause is specified, but fails to render, then the library will throw a `NonRenderingWhereClauseException` by default. If you set this value to true, then no exception will be thrown. This could cause statements to be rendered without where clauses that affect all rows in a table. | |
| 32 | + |
| 33 | +## Statement Configuration |
| 34 | + |
| 35 | +If the global configuration is not acceptable for any individual statement, you can also configure the statement in the |
| 36 | +DSL. Consider the following statement: |
| 37 | + |
| 38 | +```java |
| 39 | +DeleteStatementProvider deleteStatement = deleteFrom(animalData) |
| 40 | + .where(id, isNotIn(null, 22, null).filter(Objects::nonNull).filter(i -> i != 22)) |
| 41 | + .configureStatement(c -> c.setNonRenderingWhereClauseAllowed(true)) |
| 42 | + .build() |
| 43 | + .render(RenderingStrategies.MYBATIS3); |
| 44 | +``` |
| 45 | + |
| 46 | +In this case, the `isNotIn` condition has filters that will remove all values from the condition. In that case, the |
| 47 | +condition will not render and, subsequently, the where clause will not render. This means that the generated delete |
| 48 | +statement would delete all rows in the table. By default, the global configuration will block this statement from |
| 49 | +rendering and throw a `NonRenderingWhereClauseException`. If for some reason you would like to allow a statement |
| 50 | +like this to be rendered, then you can allow it as shown above with the `configureStatement` method. |
| 51 | + |
| 52 | +The Kotlin DSL contains the same function: |
| 53 | + |
| 54 | +```kotlin |
| 55 | +val deleteStatement = deleteFrom(person) { |
| 56 | + where { id isNotEqualToWhenPresent null } |
| 57 | + configureStatement { isNonRenderingWhereClauseAllowed = true } |
| 58 | +} |
| 59 | +``` |
| 60 | + |
0 commit comments