Skip to content

Commit c0838a2

Browse files
committed
[Kotlin] Deprecate the general and/or methods
These methods always impact the where clause, but given their visibility they could be misunderstood as methods that would impact a having clause. The replacement is to only use the and/or methods inside the where lambda.
1 parent 696136c commit c0838a2

File tree

8 files changed

+378
-258
lines changed

8 files changed

+378
-258
lines changed

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinBaseBuilders.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,24 @@ abstract class KotlinBaseBuilder<D : AbstractWhereStarter<*,*>> {
5151
getDsl().where(criteria)
5252
}
5353

54+
@Deprecated("Please move the \"and\" function into the where lambda. If the where lambda has more than one condition, you may need to surround the existing conditions with \"group\" first.")
5455
fun and(criteria: GroupingCriteriaReceiver): Unit =
5556
GroupingCriteriaCollector().apply(criteria).let {
5657
getDsl().where().and(it.initialCriterion, it.subCriteria)
5758
}
5859

60+
@Deprecated("Please move the \"and\" function into the where lambda. If the where lambda has more than one condition, you may need to surround the existing conditions with \"group\" first.")
5961
fun and(criteria: List<AndOrCriteriaGroup>) {
6062
getDsl().where().and(criteria)
6163
}
6264

65+
@Deprecated("Please move the \"or\" function into the where lambda. If the where lambda has more than one condition, you may need to surround the existing conditions with \"group\" first.")
6366
fun or(criteria: GroupingCriteriaReceiver): Unit =
6467
GroupingCriteriaCollector().apply(criteria).let {
6568
getDsl().where().or(it.initialCriterion, it.subCriteria)
6669
}
6770

71+
@Deprecated("Please move the \"or\" function into the where lambda. If the where lambda has more than one condition, you may need to surround the existing conditions with \"group\" first.")
6872
fun or(criteria: List<AndOrCriteriaGroup>) {
6973
getDsl().where().or(criteria)
7074
}

src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ class PersonMapperTest {
6363
val mapper = session.getMapper(PersonMapper::class.java)
6464

6565
val rows = mapper.select {
66-
where { id isEqualTo 1 }
67-
or { occupation.isNull() }
66+
where {
67+
id isEqualTo 1
68+
or { occupation.isNull() }
69+
}
6870
}
6971

7072
assertThat(rows).hasSize(3)
@@ -106,8 +108,10 @@ class PersonMapperTest {
106108
val mapper = session.getMapper(PersonMapper::class.java)
107109

108110
val rows = mapper.selectDistinct {
109-
where { id isGreaterThan 1 }
110-
or { occupation.isNull() }
111+
where {
112+
id isGreaterThan 1
113+
or { occupation.isNull() }
114+
}
111115
}
112116

113117
assertThat(rows).hasSize(5)
@@ -375,8 +379,10 @@ class PersonMapperTest {
375379

376380
rows = mapper.update {
377381
set(occupation) equalTo "Programmer"
378-
where { id isEqualTo 100 }
379-
and { firstName isEqualTo "Joe" }
382+
where {
383+
id isEqualTo 100
384+
and { firstName isEqualTo "Joe" }
385+
}
380386
}
381387

382388
assertThat(rows).isEqualTo(1)
@@ -494,8 +500,10 @@ class PersonMapperTest {
494500
val mapper = session.getMapper(PersonMapper::class.java)
495501

496502
val rows = mapper.count {
497-
where { employed.isTrue() }
498-
and { occupation isEqualTo "Brontosaurus Operator" }
503+
where {
504+
employed.isTrue()
505+
and { occupation isEqualTo "Brontosaurus Operator" }
506+
}
499507
}
500508

501509
assertThat(rows).isEqualTo(2L)
@@ -524,10 +532,12 @@ class PersonMapperTest {
524532
val mapper = session.getMapper(PersonMapper::class.java)
525533

526534
val rows = mapper.count {
527-
where { id isEqualTo 1 }
528-
or {
529-
id isEqualTo 2
530-
or { id isEqualTo 3 }
535+
where {
536+
id isEqualTo 1
537+
or {
538+
id isEqualTo 2
539+
or { id isEqualTo 3 }
540+
}
531541
}
532542
}
533543

@@ -541,10 +551,12 @@ class PersonMapperTest {
541551
val mapper = session.getMapper(PersonMapper::class.java)
542552

543553
val rows = mapper.count {
544-
where { id isLessThan 5 }
545-
and {
546-
id isLessThan 3
547-
and { id isEqualTo 1 }
554+
where {
555+
id isLessThan 5
556+
and {
557+
id isLessThan 3
558+
and { id isEqualTo 1 }
559+
}
548560
}
549561
}
550562

src/test/kotlin/examples/kotlin/mybatis3/general/GeneralKotlinTest.kt

Lines changed: 91 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ class GeneralKotlinTest {
163163
val mapper = session.getMapper(PersonMapper::class.java)
164164

165165
val deleteStatement = deleteFrom(person) {
166-
where { id isLessThan 4 }
167-
and { occupation.isNotNull() }
166+
where {
167+
id isLessThan 4
168+
and { occupation.isNotNull() }
169+
}
168170
}
169171

170172
assertThat(deleteStatement.deleteStatement).isEqualTo(
@@ -183,8 +185,10 @@ class GeneralKotlinTest {
183185
val mapper = session.getMapper(PersonMapper::class.java)
184186

185187
val deleteStatement = deleteFrom(person) {
186-
where { id isLessThan 4 }
187-
or { occupation.isNotNull() }
188+
where {
189+
id isLessThan 4
190+
or { occupation.isNotNull() }
191+
}
188192
}
189193

190194
assertThat(deleteStatement.deleteStatement).isEqualTo(
@@ -204,10 +208,12 @@ class GeneralKotlinTest {
204208

205209
val deleteStatement = deleteFrom(person) {
206210
where {
207-
id isLessThan 4
208-
or { occupation.isNotNull() }
211+
group {
212+
id isLessThan 4
213+
or { occupation.isNotNull() }
214+
}
215+
and { employed isEqualTo true }
209216
}
210-
and { employed isEqualTo true }
211217
}
212218

213219
val expected = "delete from Person " +
@@ -229,10 +235,12 @@ class GeneralKotlinTest {
229235
val mapper = session.getMapper(PersonMapper::class.java)
230236

231237
val deleteStatement = deleteFrom(person) {
232-
where { id isLessThan 4 }
233-
or {
234-
occupation.isNotNull()
235-
and { employed isEqualTo true }
238+
where {
239+
id isLessThan 4
240+
or {
241+
occupation.isNotNull()
242+
and { employed isEqualTo true }
243+
}
236244
}
237245
}
238246

@@ -255,10 +263,12 @@ class GeneralKotlinTest {
255263
val mapper = session.getMapper(PersonMapper::class.java)
256264

257265
val deleteStatement = deleteFrom(person) {
258-
where { id isLessThan 4 }
259-
and {
260-
occupation.isNotNull()
261-
and { employed isEqualTo true }
266+
where {
267+
id isLessThan 4
268+
and {
269+
occupation.isNotNull()
270+
and { employed isEqualTo true }
271+
}
262272
}
263273
}
264274

@@ -286,10 +296,12 @@ class GeneralKotlinTest {
286296
) {
287297
from(person)
288298
where {
289-
id isLessThan 4
299+
group {
300+
id isLessThan 4
301+
and { occupation.isNotNull() }
302+
}
290303
and { occupation.isNotNull() }
291304
}
292-
and { occupation.isNotNull() }
293305
orderBy(id)
294306
limit(3)
295307
}
@@ -320,10 +332,12 @@ class GeneralKotlinTest {
320332
) {
321333
from(person)
322334
where {
323-
id isLessThan 4
335+
group {
336+
id isLessThan 4
337+
and { occupation.isNotNull() }
338+
}
324339
and { occupation.isNotNull() }
325340
}
326-
and { occupation.isNotNull() }
327341
orderBy(id)
328342
limit(3)
329343
}
@@ -392,12 +406,14 @@ class GeneralKotlinTest {
392406
join(address) {
393407
on(addressId) equalTo address.id
394408
}
395-
where { id isLessThan 5 }
396-
and {
397-
id isLessThan 4
409+
where {
410+
id isLessThan 5
398411
and {
399-
id isLessThan 3
400-
and { id isLessThan 2 }
412+
id isLessThan 4
413+
and {
414+
id isLessThan 3
415+
and { id isLessThan 2 }
416+
}
401417
}
402418
}
403419
}
@@ -433,12 +449,14 @@ class GeneralKotlinTest {
433449
join(address) {
434450
on(addressId) equalTo address.id
435451
}
436-
where { id isEqualTo 5 }
437-
or {
438-
id isEqualTo 4
452+
where {
453+
id isEqualTo 5
439454
or {
440-
id isEqualTo 3
441-
or { id isEqualTo 2 }
455+
id isEqualTo 4
456+
or {
457+
id isEqualTo 3
458+
or { id isEqualTo 2 }
459+
}
442460
}
443461
}
444462
orderBy(id)
@@ -473,12 +491,14 @@ class GeneralKotlinTest {
473491
addressId
474492
) {
475493
from(person)
476-
where { id isLessThan 5 }
477-
and {
478-
id isLessThan 4
494+
where {
495+
id isLessThan 5
479496
and {
480-
id isLessThan 3
481-
and { id isLessThan 2 }
497+
id isLessThan 4
498+
and {
499+
id isLessThan 3
500+
and { id isLessThan 2 }
501+
}
482502
}
483503
}
484504
orderBy(id)
@@ -519,12 +539,14 @@ class GeneralKotlinTest {
519539
addressId
520540
) {
521541
from(person)
522-
where { id isEqualTo 5 }
523-
or {
524-
id isEqualTo 4
542+
where {
543+
id isEqualTo 5
525544
or {
526-
id isEqualTo 3
527-
or { id isEqualTo 2 }
545+
id isEqualTo 4
546+
or {
547+
id isEqualTo 3
548+
or { id isEqualTo 2 }
549+
}
528550
}
529551
}
530552
orderBy(id)
@@ -559,12 +581,14 @@ class GeneralKotlinTest {
559581
fun testRawSelectWithoutFrom() {
560582
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
561583
select(id `as` "A_ID", firstName, lastName, birthDate, employed, occupation, addressId) {
562-
where { id isEqualTo 5 }
563-
or {
564-
id isEqualTo 4
584+
where {
585+
id isEqualTo 5
565586
or {
566-
id isEqualTo 3
567-
or { id isEqualTo 2 }
587+
id isEqualTo 4
588+
or {
589+
id isEqualTo 3
590+
or { id isEqualTo 2 }
591+
}
568592
}
569593
}
570594
orderBy(id)
@@ -577,12 +601,14 @@ class GeneralKotlinTest {
577601
fun testRawCountWithoutFrom() {
578602
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
579603
count(id) {
580-
where { id isEqualTo 5 }
581-
or {
582-
id isEqualTo 4
604+
where {
605+
id isEqualTo 5
583606
or {
584-
id isEqualTo 3
585-
or { id isEqualTo 2 }
607+
id isEqualTo 4
608+
or {
609+
id isEqualTo 3
610+
or { id isEqualTo 2 }
611+
}
586612
}
587613
}
588614
}
@@ -711,10 +737,12 @@ class GeneralKotlinTest {
711737

712738
val updateStatement = update(person) {
713739
set(firstName) equalTo "Sam"
714-
where { firstName isEqualTo "Fred" }
715-
or {
716-
id isEqualTo 5
717-
or { id isEqualTo 6 }
740+
where {
741+
firstName isEqualTo "Fred"
742+
or {
743+
id isEqualTo 5
744+
or { id isEqualTo 6 }
745+
}
718746
}
719747
}
720748

@@ -738,10 +766,12 @@ class GeneralKotlinTest {
738766

739767
val updateStatement = update(person) {
740768
set(firstName) equalTo "Sam"
741-
where { firstName isEqualTo "Fred" }
742-
and {
743-
id isEqualTo 1
744-
or { id isEqualTo 6 }
769+
where {
770+
firstName isEqualTo "Fred"
771+
and {
772+
id isEqualTo 1
773+
or { id isEqualTo 6 }
774+
}
745775
}
746776
}
747777

@@ -765,8 +795,10 @@ class GeneralKotlinTest {
765795

766796
val updateStatement = update(person) {
767797
set(firstName) equalTo "Sam"
768-
where { firstName isEqualTo "Fred" }
769-
or { id isEqualTo 3 }
798+
where {
799+
firstName isEqualTo "Fred"
800+
or { id isEqualTo 3 }
801+
}
770802
}
771803

772804
assertThat(updateStatement.updateStatement).isEqualTo(

0 commit comments

Comments
 (0)