Skip to content

Commit bca74ad

Browse files
committed
Add migration helps to the documentation
1 parent 879b2c7 commit bca74ad

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

src/site/markdown/docs/kotlinWhereClauses.md

+184
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,187 @@ At this time, it is not possible to add infix functions for custom conditions to
184184
underlying limitation in Kotlin itself. There is a Kotlin language enhancement on the roadmap that will likely
185185
remove this limitation. That enhancement will allow multiple receivers for an extension function. You can follow
186186
progress of that enhancement here: https://youtrack.jetbrains.com/issue/KT-42435
187+
188+
## Migrating from Prior Releases
189+
190+
In version 1.4.0 the where DSL improved significantly and is now implemented as shown on this page. Many methods from
191+
previous releases are now deprecated. One of the primary motivations for this change was that compound criteria
192+
from prior releases were difficult to reason about - the Kotlin syntax was very different from the generated SQL.
193+
In complex where clauses, the code could become very difficult to understand.
194+
195+
With the updated DSL, the Kotlin code is much closer to the generated SQL and there is a consistent use of curly braces
196+
to denote where parentheses should be generated in SQL.
197+
198+
This section will detail the patterns for code updates from prior releases to the new DSL. The patterns below apply
199+
equally to "where", "and", and "or" methods from the prior releases.
200+
201+
### Migrating Single Column and Condition
202+
203+
In prior releases, a criterion with a single column and condition was written as follows:
204+
205+
```kotlin
206+
select(foo) {
207+
from(bar)
208+
where(id, isEqualTo(3))
209+
or(id, isEqualTo(4))
210+
}
211+
```
212+
213+
These criteria should be updated by moving the column and condition into a lambda and using an infix function:
214+
215+
```kotlin
216+
select(foo) {
217+
from(bar)
218+
where { id isEqualTo 3 }
219+
or { id isEqualTo 4 }
220+
}
221+
```
222+
223+
### Migrating Compound Column and Condition Criteria
224+
225+
In prior releases, a criterion with multiple column and conditions grouped together was written like the following:
226+
227+
```kotlin
228+
select(foo) {
229+
from(bar)
230+
where(id, isEqualTo(3)) {
231+
or(id, isEqualTo(4))
232+
}
233+
}
234+
```
235+
236+
These criteria should be updated by moving the first column and condition into the lambda, using infix functions,
237+
and updating the second criterion as well:
238+
239+
```kotlin
240+
select(foo) {
241+
from(bar)
242+
where {
243+
id isEqualTo 3
244+
or { id isEqualTo 4 }
245+
}
246+
}
247+
```
248+
249+
### Migrating Criteria Using Filter and Map
250+
251+
In prior releases, a criterion that used filter and map was written as follows:
252+
253+
```kotlin
254+
select(foo) {
255+
from(bar)
256+
where(firstName, isLike("fred").map { "%$it%" }) // add SQL wildcards
257+
}
258+
```
259+
260+
These criteria should be updated by moving the column and condition into a lambda and using the "invoke" operator
261+
function:
262+
263+
```kotlin
264+
select(foo) {
265+
from(bar)
266+
where { firstName (isLike("fred").map { "%$it%" }) } // add SQL wildcards
267+
}
268+
```
269+
270+
### Migrating Exists Criteria
271+
272+
In prior releases, a criterion that used an "exists" sub-query looked like this:
273+
274+
```kotlin
275+
select(foo) {
276+
from(bar)
277+
where(
278+
exists {
279+
select(baz) {
280+
from(bar)
281+
}
282+
}
283+
)
284+
}
285+
```
286+
287+
These criteria should be updated by moving the "exists" phrase into a lambda:
288+
289+
```kotlin
290+
select(foo) {
291+
from(bar)
292+
where {
293+
exists {
294+
select(baz) {
295+
from(bar)
296+
}
297+
}
298+
}
299+
}
300+
```
301+
302+
### Migrating Not Exists Criteria
303+
304+
In prior releases, a criterion that used a "not exists" sub-query looked like this:
305+
306+
```kotlin
307+
select(foo) {
308+
from(bar)
309+
where(
310+
notExists {
311+
select(baz) {
312+
from(bar)
313+
}
314+
}
315+
)
316+
}
317+
```
318+
319+
These criteria should be updated by moving the phrase into a lambda, and replacing "notExists" with a combination
320+
of "not" and "exists":
321+
322+
```kotlin
323+
select(foo) {
324+
from(bar)
325+
where {
326+
not {
327+
exists {
328+
select(baz) {
329+
from(bar)
330+
}
331+
}
332+
}
333+
}
334+
}
335+
```
336+
337+
### Migrating Compound Exists Criteria
338+
339+
In prior releases, a criterion that used a compound "exists" sub-query looked like this:
340+
341+
```kotlin
342+
select(foo) {
343+
from(bar)
344+
where(
345+
exists {
346+
select(baz) {
347+
from(bar)
348+
}
349+
}
350+
) {
351+
or(id, isEqualTo(3))
352+
}
353+
}
354+
```
355+
356+
These criteria should be updated by moving the "exists" phrase into the lambda and updating any other criteria:
357+
358+
```kotlin
359+
select(foo) {
360+
from(bar)
361+
where {
362+
exists {
363+
select(baz) {
364+
from(bar)
365+
}
366+
}
367+
or { id isEqualTo 3 }
368+
}
369+
}
370+
```

0 commit comments

Comments
 (0)