@@ -6,7 +6,7 @@ overload functions, infix functions, and Kotlin receiver functions.
6
6
7
7
## Simple Where Clauses
8
8
9
- The simplest form of where clause includes a single condition. An example follows :
9
+ The simplest form of where clause includes a single condition. See the following examples :
10
10
11
11
``` kotlin
12
12
select(foo) {
@@ -36,8 +36,8 @@ select(foo) {
36
36
```
37
37
38
38
Most, but not all, of the built-in conditions can be expressed as infix functions. Conditions without parameters,
39
- or varargs conditions, cannot be called with infix. Good examples of those conditions are ` isNull ` and ` isIn ` . In
40
- those cases you will need to call the function directly as follows:
39
+ or varargs conditions, cannot be called as an infix function . Good examples of conditions that cannot be called via
40
+ an infix function are ` isNull ` and ` isIn ` . In those cases you will need to call the function directly as follows:
41
41
42
42
``` kotlin
43
43
select(foo) {
@@ -56,30 +56,30 @@ select(foo) {
56
56
Many conditions support ` filter ` and ` map ` functions that can be used to test whether the condition should be rendered
57
57
or to change the value of the condition parameter(s). If you need to use the ` filter ` or ` map ` functions, then you
58
58
cannot use the infix functions. In this case you can use a function that creates the condition and then apply
59
- that condition to the where clause. For example:
59
+ that condition to the where clause via the invoke operator . For example:
60
60
61
61
``` kotlin
62
62
select(foo) {
63
63
from(bar)
64
- where { id (isEqualTo( 3 ).map { it + 2 }) }
64
+ where { firstName (isLike( " fred " ).map { " % $it % " }) } // add wildcards for like
65
65
}
66
66
```
67
67
68
- In this case, ` isEqualTo ` is a function in the ` org.mybatis.dynamic.sql.util.kotlin.elements ` package, not the infix
69
- function. Note that the condition is enclosed in parentheses. This is actually a function call using a Kotlin invoke
70
- operator overload. This can also be called explicitly without the operator overload as follows:
68
+ In this case, ` isLike ` is a function in the ` org.mybatis.dynamic.sql.util.kotlin.elements ` package, not the infix
69
+ function. Note also that the condition is enclosed in parentheses. This is actually a function call using a Kotlin
70
+ invoke operator overload. This can also be called explicitly without the operator overload as follows:
71
71
72
72
``` kotlin
73
73
select(foo) {
74
74
from(bar)
75
- where { id .invoke(isEqualTo( 3 ).map { it + 2 }) }
75
+ where { firstName .invoke(isLike( " fred " ).map { " % $it % " }) } // add wildcards for like
76
76
}
77
77
```
78
78
79
79
## Compound Where Clauses
80
80
81
81
Of course many where clauses are composed of more than one condition. The where DSL supports arbitrarily complex
82
- where clauses with and/or/not phrases.
82
+ where clauses with and/or/not phrases. See the following example of a complex where clause:
83
83
84
84
``` kotlin
85
85
select(foo) {
@@ -94,8 +94,9 @@ select(foo) {
94
94
```
95
95
96
96
The ` and ` , ` or ` , and ` not ` functions each create a new context that can in turn include ` and ` , ` or ` , and ` not `
97
- functions. The DSL has no limit on the depth of nesting of these functions. When there are nested ` and ` and ` or `
98
- functions, the curly braces will essentially be rendered as parentheses in the final SQL.
97
+ functions. The DSL has no practical limit on the depth of nesting of these functions. When there are nested
98
+ ` and ` and ` or ` functions, the curly braces will be rendered as parentheses in the final SQL if the context contains
99
+ more than one condition.
99
100
100
101
## Initial and Subsequent Conditions
101
102
@@ -110,19 +111,21 @@ Everything is optional - if you don't specify an initial condition, or any subse
110
111
render.
111
112
112
113
For each context, the renderer will add parenthesis around the rendered context if there is more than one condition in
113
- the context. Remember that the ` filter ` function can be used to remove a condition from rendering, so the parentheses
114
- are added only if the enclosed conditions all render .
114
+ the context. Remember that a ` filter ` function can be used to remove some conditions from rendering, so the
115
+ parentheses are added only if there is more than one condition that renders .
115
116
116
- If you forget to specify an initial condition and only specify ` and ` and ` or ` groups, then the first "and" or "or"
117
- will be removed during rendering. This to avoid a rendered where clause like "where and id = 3".
117
+ If you neglect to specify an initial condition and only specify ` and ` and ` or ` groups, then the first "and" or "or"
118
+ will be removed during rendering. This to avoid a rendered where clause like "where and id = 3". This can be useful
119
+ in situations where a where clause is composed by a number of different functions - there is no need to keep track
120
+ of who goes first as the renderer will automatically strip the first connector.
118
121
119
122
### Initial Condition Types
120
123
121
124
There are four types of initial conditions. Only one of the initial condition types may be specified in any
122
125
given context. Others must be enclosed in an ` and ` or an ` or ` block. The four types are as follows:
123
126
124
- 1 . ** Column and Criterion** ( ` id isEqualTo 3 ` - as shown above)
125
- 2 . ** Not** ( ` not { id isEqualTo 3 } ` - as shown above)
127
+ 1 . ** Column and Criterion** - either with the infix functions, or the invoke function as shown above
128
+ 2 . ** Not** - appends " not" to a group of criteria or a single criterion as shown above
126
129
3 . ** Exists** - for executing an exists sub-query:
127
130
128
131
``` kotlin
@@ -173,11 +176,11 @@ given context. Others must be enclosed in an `and` or an `or` block. The four ty
173
176
174
177
## Extending Where Clauses
175
178
176
- In addition to the built- in conditions supplied with the library, it is possible to write your own custom
177
- conditions. Any custom condition can be used with the " invoke operator" method shown above in the " Using Filter And Map "
178
- section above.
179
+ In addition to the built- in conditions supplied with the library, it is also possible to write your own custom
180
+ conditions. Any custom condition can be used with the " invoke operator" method shown above in the
181
+ " Using Filter And Map " section above.
179
182
180
183
At this time, it is not possible to add infix functions for custom conditions to the library. This is due to an
181
184
underlying limitation in Kotlin itself. There is a Kotlin language enhancement on the roadmap that will likely
182
- remove this limitation. The enhancement will allow multiple receivers for an extension function. You can follow
185
+ remove this limitation. That enhancement will allow multiple receivers for an extension function. You can follow
183
186
progress of that enhancement here: https: // youtrack.jetbrains.com/issue/KT-42435
0 commit comments