Skip to content

Commit 66847ec

Browse files
committed
Support UserDefinedCriteria for kotlin
1 parent 8ea1c87 commit 66847ec

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

doma-kotlin/src/main/kotlin/org/seasar/doma/kotlin/jdbc/criteria/declaration/KComparisonDeclaration.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.seasar.doma.kotlin.jdbc.criteria.declaration
1717

18+
import org.seasar.doma.jdbc.criteria.declaration.UserDefinedCriteriaContext
1819
import org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel
20+
import org.seasar.doma.jdbc.dialect.Dialect
1921

2022
abstract class KComparisonDeclaration<DECLARATION : org.seasar.doma.jdbc.criteria.declaration.ComparisonDeclaration>(protected val declaration: DECLARATION) {
2123

@@ -94,4 +96,29 @@ abstract class KComparisonDeclaration<DECLARATION : org.seasar.doma.jdbc.criteri
9496
fun not(block: () -> Unit) {
9597
declaration.not(block)
9698
}
99+
100+
fun <EXTENSION> extension(
101+
construct: (KUserDefinedCriteriaContext) -> EXTENSION,
102+
extensionDeclaration: EXTENSION.() -> Unit,
103+
) {
104+
declaration.extension(
105+
{ context -> construct(context.toKotlin()) },
106+
extensionDeclaration
107+
)
108+
}
109+
110+
private fun UserDefinedCriteriaContext.toKotlin(): KUserDefinedCriteriaContext {
111+
return KUserDefinedCriteriaContext { builderBlock ->
112+
this.add { builder ->
113+
builderBlock(object : KUserDefinedCriteriaContext.Builder {
114+
override fun appendSql(sql: String) = builder.appendSql(sql)
115+
override fun cutBackSql(length: Int) = builder.cutBackSql(length)
116+
override fun appendExpression(propertyMetamodel: PropertyMetamodel<*>) =
117+
builder.appendExpression(propertyMetamodel)
118+
override val dialect: Dialect
119+
get() = builder.dialect
120+
})
121+
}
122+
}
123+
}
97124
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Doma Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.seasar.doma.kotlin.jdbc.criteria.declaration
17+
18+
import org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel
19+
import org.seasar.doma.jdbc.dialect.Dialect
20+
21+
fun interface KUserDefinedCriteriaContext {
22+
fun add(builderBlock: Builder.() -> Unit)
23+
24+
interface Builder {
25+
fun appendSql(sql: String)
26+
27+
fun cutBackSql(length: Int)
28+
29+
fun appendExpression(propertyMetamodel: PropertyMetamodel<*>)
30+
31+
val dialect: Dialect
32+
}
33+
}

doma-kotlin/src/test/kotlin/org/seasar/doma/jdbc/criteria/KQueryDslSqlSelectTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,26 @@ internal class KQueryDslSqlSelectTest {
687687
)
688688
}
689689

690+
@Test
691+
fun where_extension() {
692+
val e = Emp_()
693+
val stmt = dsl
694+
.from(e)
695+
.where {
696+
eq(e.id, 1)
697+
extension(::MyExtension) {
698+
likeMultiple(e.name, "A", "B", "C")
699+
eq(e.id, 1)
700+
}
701+
}
702+
.select(e.id)
703+
val sql = stmt.asSql()
704+
assertEquals(
705+
"select t0_.ID from EMP t0_ where t0_.ID = 1 and t0_.NAME like '%A%' escape '\\' and t0_.NAME like '%B%' escape '\\' and t0_.NAME like '%C%' escape '\\' and t0_.ID = 1",
706+
sql.formattedSql,
707+
)
708+
}
709+
690710
@Test
691711
fun innerJoin() {
692712
val e = Emp_()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright Doma Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.seasar.doma.jdbc.criteria
17+
18+
import org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel
19+
import org.seasar.doma.kotlin.jdbc.criteria.declaration.KUserDefinedCriteriaContext
20+
import org.seasar.doma.kotlin.jdbc.criteria.expression.KExpressions
21+
22+
data class MyExtension(val context: KUserDefinedCriteriaContext) {
23+
fun likeMultiple(entityMetamodel: PropertyMetamodel<*>, vararg patterns: String) {
24+
context.add {
25+
patterns.forEach { pattern ->
26+
appendExpression(entityMetamodel)
27+
appendSql(" like ")
28+
appendExpression(KExpressions.literal("%" + pattern + "%"))
29+
appendSql(" escape '\\'")
30+
appendSql(" and ")
31+
}
32+
cutBackSql(5)
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)