Skip to content

Commit af036e9

Browse files
combine conditions
1 parent 62175a5 commit af036e9

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

ktorm-core/src/main/kotlin/me/liuwj/ktorm/dsl/Query.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package me.liuwj.ktorm.dsl
33
import me.liuwj.ktorm.database.Database
44
import me.liuwj.ktorm.database.prepareStatement
55
import me.liuwj.ktorm.expression.*
6-
import me.liuwj.ktorm.schema.ColumnDeclaring
7-
import me.liuwj.ktorm.schema.DoubleSqlType
8-
import me.liuwj.ktorm.schema.IntSqlType
9-
import me.liuwj.ktorm.schema.Table
6+
import me.liuwj.ktorm.schema.*
107
import java.sql.ResultSet
118
import javax.sql.rowset.RowSetProvider
129

@@ -192,6 +189,14 @@ inline fun Query.whereWithOrConditions(block: (MutableList<ScalarExpression<Bool
192189
}
193190
}
194191

192+
fun Iterable<ScalarExpression<Boolean>>.combineConditions(): ScalarExpression<Boolean> {
193+
if (this.any()) {
194+
return this.reduce { a, b -> a and b }
195+
} else {
196+
return ArgumentExpression(true, BooleanSqlType)
197+
}
198+
}
199+
195200
fun Query.groupBy(vararg columns: ColumnDeclaring<*>): Query {
196201
return this.withExpression { expr ->
197202
when (expr) {

ktorm-core/src/test/kotlin/me/liuwj/ktorm/dsl/QueryTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.liuwj.ktorm.dsl
22

33
import me.liuwj.ktorm.BaseTest
4+
import me.liuwj.ktorm.expression.ScalarExpression
45
import org.junit.Test
56

67
/**
@@ -57,6 +58,21 @@ class QueryTest : BaseTest() {
5758
assert(name == "vince")
5859
}
5960

61+
@Test
62+
fun testCombineConditions() {
63+
val t = Employees.aliased("t")
64+
65+
val names = t
66+
.select(t.name)
67+
.where { emptyList<ScalarExpression<Boolean>>().combineConditions() }
68+
.orderBy(t.id.asc())
69+
.map { it.getString(1) }
70+
71+
assert(names.size == 4)
72+
assert(names[0] == "vince")
73+
assert(names[1] == "marry")
74+
}
75+
6076
@Test
6177
fun testOrderBy() {
6278
val names = Employees

0 commit comments

Comments
 (0)