Skip to content

Commit be6ec44

Browse files
authored
Merge pull request #448 from jeffgbutler/update-dsl
[Kotlin] Minor DSL Improvements
2 parents 82ffdfa + 26e546b commit be6ec44

19 files changed

+280
-229
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,21 @@ abstract class KotlinBaseBuilder<D : AbstractWhereSupport<*>> {
145145
or(existsPredicate, sc)
146146
}
147147

148-
fun allRows() {}
148+
/**
149+
* This function does nothing, but it can be used to make some code snippets more understandable.
150+
*
151+
* For example, to count all rows in a table you can write either of the following:
152+
*
153+
* val rows = countFrom(foo) { }
154+
*
155+
* or
156+
*
157+
* val rows = countFrom(foo) { allRows() }
158+
*/
159+
@SuppressWarnings("EmptyFunctionBlock")
160+
fun allRows() {
161+
// intentionally empty - this function exists for code beautification and clarity only
162+
}
149163

150164
private fun applyToWhere(block: AbstractWhereDSL<*>.() -> Unit) {
151165
getDsl().where().apply(block)

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,45 +42,44 @@ class KotlinGeneralInsertBuilder(private val dsl: GeneralInsertDSL) : Buildable<
4242

4343
@MyBatisDslMarker
4444
inner class GeneralInsertSetClauseFinisher<T>(private val column: SqlColumn<T>) {
45-
fun toNull(): KotlinGeneralInsertBuilder =
45+
fun toNull(): Unit =
4646
applyToDsl {
4747
set(column).toNull()
4848
}
4949

50-
fun toConstant(constant: String): KotlinGeneralInsertBuilder =
50+
infix fun toConstant(constant: String): Unit =
5151
applyToDsl {
5252
set(column).toConstant(constant)
5353
}
5454

55-
fun toStringConstant(constant: String): KotlinGeneralInsertBuilder =
55+
infix fun toStringConstant(constant: String): Unit =
5656
applyToDsl {
5757
set(column).toStringConstant(constant)
5858
}
5959

60-
fun toValue(value: T): KotlinGeneralInsertBuilder = toValue { value }
60+
infix fun toValue(value: T): Unit = toValue { value }
6161

62-
fun toValue(value: () -> T): KotlinGeneralInsertBuilder =
62+
infix fun toValue(value: () -> T): Unit =
6363
applyToDsl {
6464
set(column).toValue(value)
6565
}
6666

67-
fun toValueOrNull(value: T?): KotlinGeneralInsertBuilder = toValueOrNull { value }
67+
infix fun toValueOrNull(value: T?): Unit = toValueOrNull { value }
6868

69-
fun toValueOrNull(value: () -> T?): KotlinGeneralInsertBuilder =
69+
infix fun toValueOrNull(value: () -> T?): Unit =
7070
applyToDsl {
7171
set(column).toValueOrNull(value)
7272
}
7373

74-
fun toValueWhenPresent(value: T?): KotlinGeneralInsertBuilder = toValueWhenPresent { value }
74+
infix fun toValueWhenPresent(value: T?): Unit = toValueWhenPresent { value }
7575

76-
fun toValueWhenPresent(value: () -> T?): KotlinGeneralInsertBuilder =
76+
infix fun toValueWhenPresent(value: () -> T?): Unit =
7777
applyToDsl {
7878
set(column).toValueWhenPresent(value)
7979
}
8080

81-
private fun applyToDsl(block: GeneralInsertDSL.() -> Unit): KotlinGeneralInsertBuilder =
82-
this@KotlinGeneralInsertBuilder.apply {
83-
dsl.apply(block)
84-
}
81+
private fun applyToDsl(block: GeneralInsertDSL.() -> Unit) {
82+
this@KotlinGeneralInsertBuilder.dsl.apply(block)
83+
}
8584
}
8685
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,46 +40,46 @@ class KotlinUpdateBuilder(private val dsl: UpdateDSL<UpdateModel>) :
4040
set(column).equalToNull()
4141
}
4242

43-
fun equalToConstant(constant: String): Unit =
43+
infix fun equalToConstant(constant: String): Unit =
4444
applyToDsl {
4545
set(column).equalToConstant(constant)
4646
}
4747

48-
fun equalToStringConstant(constant: String): Unit =
48+
infix fun equalToStringConstant(constant: String): Unit =
4949
applyToDsl {
5050
set(column).equalToStringConstant(constant)
5151
}
5252

53-
fun equalTo(value: T): Unit = equalTo { value }
53+
infix fun equalTo(value: T): Unit = equalTo { value }
5454

55-
fun equalTo(value: () -> T): Unit =
55+
infix fun equalTo(value: () -> T): Unit =
5656
applyToDsl {
5757
set(column).equalTo(value)
5858
}
5959

60-
fun equalTo(rightColumn: BasicColumn): Unit =
60+
infix fun equalTo(rightColumn: BasicColumn): Unit =
6161
applyToDsl {
6262
set(column).equalTo(rightColumn)
6363
}
6464

65-
fun equalToOrNull(value: T?): Unit = equalToOrNull { value }
65+
infix fun equalToOrNull(value: T?): Unit = equalToOrNull { value }
6666

67-
fun equalToOrNull(value: () -> T?): Unit =
67+
infix fun equalToOrNull(value: () -> T?): Unit =
6868
applyToDsl {
6969
set(column).equalToOrNull(value)
7070
}
7171

72-
fun equalToQueryResult(subQuery: KotlinSubQueryBuilder.() -> Unit): Unit =
72+
infix fun equalToQueryResult(subQuery: KotlinSubQueryBuilder.() -> Unit): Unit =
7373
applyToDsl {
7474
set(column).equalTo(KotlinSubQueryBuilder().apply(subQuery))
7575
}
7676

77-
fun equalToWhenPresent(value: () -> T?): Unit =
77+
infix fun equalToWhenPresent(value: () -> T?): Unit =
7878
applyToDsl {
7979
set(column).equalToWhenPresent(value)
8080
}
8181

82-
fun equalToWhenPresent(value: T?): Unit = equalToWhenPresent { value }
82+
infix fun equalToWhenPresent(value: T?): Unit = equalToWhenPresent { value }
8383

8484
private fun applyToDsl(block: UpdateDSL<UpdateModel>.() -> Unit) {
8585
this@KotlinUpdateBuilder.dsl.apply(block)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2016-2022 the original author or 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+
* http://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.mybatis.dynamic.sql.util.kotlin.elements
17+
18+
import org.mybatis.dynamic.sql.DerivedColumn
19+
import org.mybatis.dynamic.sql.SqlColumn
20+
21+
infix fun <T> DerivedColumn<T>.`as`(alias: String): DerivedColumn<T> = this.`as`(alias)
22+
23+
infix fun <T> SqlColumn<T>.`as`(alias: String): SqlColumn<T> = this.`as`(alias)
24+
25+
infix fun <T> SqlColumn<T>.qualifiedWith(tableQualifier: String): SqlColumn<T> = this.qualifiedWith(tableQualifier)

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/SqlTableExtensions.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ import kotlin.reflect.KClass
2727
* This function preserves the non-nullable column type which is lost with the Java
2828
* native versions.
2929
*/
30+
@SuppressWarnings("LongParameterList")
3031
fun <T : Any> SqlTable.column(
3132
name: String,
3233
jdbcType: JDBCType? = null,

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/NamedParameterJdbcTemplateExtensions.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -188,6 +188,7 @@ fun NamedParameterJdbcTemplate.selectOne(
188188
this
189189
)
190190

191+
@SuppressWarnings("SwallowedException")
191192
fun <T> NamedParameterJdbcTemplate.selectOne(
192193
selectStatement: SelectStatementProvider,
193194
rowMapper: (rs: ResultSet, rowNum: Int) -> T
@@ -197,6 +198,7 @@ fun <T> NamedParameterJdbcTemplate.selectOne(
197198
null
198199
}
199200

201+
@SuppressWarnings("SwallowedException")
200202
fun <T : Any> NamedParameterJdbcTemplate.selectOne(
201203
selectStatement: SelectStatementProvider,
202204
type: KClass<T>

0 commit comments

Comments
 (0)