You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Fixed a bug where the In conditions could render incorrectly in certain circumstances. ([#239](https://github.com/mybatis/mybatis-dynamic-sql/issues/239))
12
+
13
+
### Added
14
+
15
+
- Added a callback capability to the "In" conditions that will be called before rendering when the conditions are empty. Also, removed the option that forced the library to render invalid SQL in that case.
@@ -14,7 +26,7 @@ This release includes a significant refactoring of the classes in the "org.mybat
14
26
15
27
With this release, we deprecated several insert methods because they were inconsistently named or awkward. All deprecated methods have documented direct replacements.
16
28
17
-
All deprecated code will be removed in the next release.
29
+
All deprecated code will be removed in the next minor release.
Copy file name to clipboardExpand all lines: src/site/markdown/docs/conditions.md
+11-13Lines changed: 11 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,22 +124,20 @@ The library supplies several specializations of optional conditions to be used i
124
124
### Optionality with the "In" Conditions
125
125
Optionality with the "in" and "not in" conditions is a bit more complex than the other types of conditions. The first thing to know is that no "in" or "not in" condition will render if the list of values is empty. For example, there will never be rendered SQL like `where name in ()`. So optionality of the "in" conditions is more about optionality of the *values* of the condition. The library comes with functions that will filter out null values, and will upper case String values to enable case insensitive queries. There are extension points to add additional filtering and mapping if you so desire.
126
126
127
-
We think it is a good thing that the library will not render invalid SQL. Normally an "in" condition will be dropped from rendering if the list of values is empty - either through filtering or from the creation of the list. But there is some danger with this stance. Because the condition could be dropped from the rendered SQL, more rows could be impacted than expected if the list ends up empty for whatever reason. Our recommended solution is to make sure that you validate list values - especially if they are coming from direct user input. Another option is to force the conditions to render even if they are empty - which will cause a database error in most cases. If you want to force "in" conditions to render even if they are empty, you will need to create your own condition and configure it to render when empty. This is easily done by subclassing one of the existing conditions. For example:
127
+
We think it is a good thing that the library will not render invalid SQL. An "in" condition will always be dropped from rendering if the list of values is empty. But there is some danger with this stance. Because the condition could be dropped from the rendered SQL, more rows could be impacted than expected if the list ends up empty for whatever reason. Our recommended solution is to make sure that you validate list values - especially if they are coming from direct user input. Another option is to take some action when the list is empty. This can be especially helpful when you are applying filters to the value list or using one of the built in "when present" conditions. In that case, it is possible that the list of values could end up empty after a validation check.
128
+
129
+
The "In" conditions support a callback that will be invoked when the value list is empty and just before the statement is rendered. You could use the callback to create a condition that will throw an exception when the list is empty. For example:
128
130
129
131
```java
130
-
publicclassIsInRequired<T> extendsIsIn<T> {
131
-
protectedIsInRequired(Collection<T>values) {
132
-
super(values);
133
-
forceRenderingWhenEmpty(); // calling this method will force the condition to render even if the values list is empty
returnIsIn.of(values).withListEmptyCallback(() -> { thrownewRuntimeException("In values cannot be empty"); } );
139
134
}
140
-
```
141
135
142
-
Note that we do not supply conditions like this as a part of the standard library because we believe that forcing the library to render invalid SQL is an extreme measure and should be undertaken with care.
136
+
// Alternatively, there is a convenience method in the Callback interface
returnIsIn.of(values).withListEmptyCallback(Callback.exceptionThrowingCallback("In values cannot be empty"));
139
+
}
140
+
```
143
141
144
142
The following table shows the different supplied In conditions and how they will render for different sets of inputs. The table assumes the following types of input:
145
143
@@ -201,4 +199,4 @@ Then the condition could be used in a query as follows:
201
199
.render(RenderingStrategies.MYBATIS3);
202
200
```
203
201
204
-
You can apply value stream operations to the conditions `IsIn`, `IsInCaseInsensitive`, `IsNotIn`, and `IsNotInCaseInsensitive`. With the case-insensitive conditions, the library will automatically convert non-null strings to upper case after any value stream operation you specify.
202
+
You can apply value stream operations to the conditions `IsIn` and `IsNotIn`. The built in conditions `IsInCaseInsensitive`, `IsInCaseInsensitiveWhenPresent`, `IsNotInCaseInsensitive`, and `IsNotInCaseInsensitiveWhenPresent` do not support additional value stream operations as they already implement a value stream operation as part of the condition.
0 commit comments