@@ -6,7 +6,7 @@ overload functions, infix functions, and Kotlin receiver functions.
66
77## Simple Where Clauses
88
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 :
1010
1111``` kotlin
1212select(foo) {
@@ -36,8 +36,8 @@ select(foo) {
3636```
3737
3838Most, 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:
4141
4242``` kotlin
4343select(foo) {
@@ -56,30 +56,30 @@ select(foo) {
5656Many conditions support ` filter ` and ` map ` functions that can be used to test whether the condition should be rendered
5757or to change the value of the condition parameter(s). If you need to use the ` filter ` or ` map ` functions, then you
5858cannot 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:
6060
6161``` kotlin
6262select(foo) {
6363 from(bar)
64- where { id (isEqualTo( 3 ).map { it + 2 }) }
64+ where { firstName (isLike( " fred " ).map { " % $it % " }) } // add wildcards for like
6565}
6666```
6767
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:
7171
7272``` kotlin
7373select(foo) {
7474 from(bar)
75- where { id .invoke(isEqualTo( 3 ).map { it + 2 }) }
75+ where { firstName .invoke(isLike( " fred " ).map { " % $it % " }) } // add wildcards for like
7676}
7777```
7878
7979## Compound Where Clauses
8080
8181Of 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:
8383
8484``` kotlin
8585select(foo) {
@@ -94,8 +94,9 @@ select(foo) {
9494```
9595
9696The ` 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.
99100
100101## Initial and Subsequent Conditions
101102
@@ -110,19 +111,21 @@ Everything is optional - if you don't specify an initial condition, or any subse
110111render.
111112
112113For 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 .
115116
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.
118121
119122### Initial Condition Types
120123
121124There are four types of initial conditions. Only one of the initial condition types may be specified in any
122125given context. Others must be enclosed in an ` and ` or an ` or ` block. The four types are as follows:
123126
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
1261293 . ** Exists** - for executing an exists sub-query:
127130
128131 ``` kotlin
@@ -173,11 +176,11 @@ given context. Others must be enclosed in an `and` or an `or` block. The four ty
173176
174177## Extending Where Clauses
175178
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.
179182
180183At this time, it is not possible to add infix functions for custom conditions to the library. This is due to an
181184underlying 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
183186progress of that enhancement here: https: // youtrack.jetbrains.com/issue/KT-42435
0 commit comments