Skip to content

Commit 1f9c37a

Browse files
committed
Kotlin extensions for Spring and tests
1 parent 0aa2918 commit 1f9c37a

File tree

9 files changed

+823
-6
lines changed

9 files changed

+823
-6
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ package org.mybatis.dynamic.sql.util.kotlin
1717

1818
import org.mybatis.dynamic.sql.BindableColumn
1919
import org.mybatis.dynamic.sql.VisitableCondition
20+
import org.mybatis.dynamic.sql.insert.InsertDSL
21+
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL
2022
import org.mybatis.dynamic.sql.update.UpdateDSL
2123
import org.mybatis.dynamic.sql.update.UpdateModel
2224
import org.mybatis.dynamic.sql.util.Buildable
2325

26+
// insert completers are here because sonar doesn't see them as covered if they are in a file by themselves
27+
typealias InsertCompleter<T> = InsertDSL<T>.() -> InsertDSL<T>
28+
typealias MultiRowInsertCompleter<T> = MultiRowInsertDSL<T>.() -> MultiRowInsertDSL<T>
29+
2430
typealias UpdateCompleter = UpdateDSL<UpdateModel>.() -> Buildable<UpdateModel>
2531

2632
fun <T> UpdateDSL<UpdateModel>.where(column: BindableColumn<T>, condition: VisitableCondition<T>, collect: CriteriaReceiver) =
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2016-2019 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.spring
17+
18+
import org.mybatis.dynamic.sql.SqlBuilder
19+
import org.mybatis.dynamic.sql.SqlTable
20+
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider
21+
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider
22+
import org.mybatis.dynamic.sql.render.RenderingStrategies
23+
import org.mybatis.dynamic.sql.select.QueryExpressionDSL
24+
import org.mybatis.dynamic.sql.select.SelectModel
25+
import org.mybatis.dynamic.sql.util.kotlin.*
26+
27+
fun count(table: SqlTable, completer: CountCompleter) =
28+
completer(SqlBuilder.countFrom(table)).build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
29+
30+
fun deleteFrom(table: SqlTable, completer: DeleteCompleter) =
31+
completer(SqlBuilder.deleteFrom(table)).build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
32+
33+
fun <T> insert(record: T, table: SqlTable, completer: InsertCompleter<T>): InsertStatementProvider<T> =
34+
completer(SqlBuilder.insert(record).into(table)).build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
35+
36+
//fun <T> insertMultiple(records: Collection<T>, table: SqlTable, completer: MultiRowInsertCompleter<T>): MultiRowInsertStatementProvider<T> =
37+
// completer(SqlBuilder.insertMultiple(records).into(table)).build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
38+
39+
fun QueryExpressionDSL.FromGatherer<SelectModel>.from(table: SqlTable, completer: SelectCompleter) =
40+
completer(from(table)).build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
41+
42+
fun QueryExpressionDSL.FromGatherer<SelectModel>.from(table: SqlTable, alias: String, completer: SelectCompleter) =
43+
completer(from(table, alias)).build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
44+
45+
fun update(table: SqlTable, completer: UpdateCompleter) =
46+
completer(SqlBuilder.update(table)).build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2016-2019 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 examples.kotlin.spring.canonical
17+
18+
import org.mybatis.dynamic.sql.SqlTable
19+
import java.sql.JDBCType
20+
21+
object AddressDynamicSqlSupport {
22+
object Address : SqlTable("Address") {
23+
val id = column<Int>("address_id", JDBCType.INTEGER)
24+
val streetAddress = column<String>("street_address", JDBCType.VARCHAR)
25+
val city = column<String>("city", JDBCType.VARCHAR)
26+
val state = column<String>("state", JDBCType.VARCHAR)
27+
}
28+
}

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/InsertDSLExtensions.kt renamed to src/test/kotlin/examples/kotlin/spring/canonical/AddressRecord.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.mybatis.dynamic.sql.util.kotlin
16+
package examples.kotlin.spring.canonical
1717

18-
import org.mybatis.dynamic.sql.insert.InsertDSL
19-
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL
20-
21-
typealias InsertCompleter<T> = InsertDSL<T>.() -> InsertDSL<T>
22-
typealias MultiRowInsertCompleter<T> = MultiRowInsertDSL<T>.() -> MultiRowInsertDSL<T>
18+
data class AddressRecord(var id: Int? = null, var streetAddress: String? = null, var city: String? = null, var state: String? = null)

0 commit comments

Comments
 (0)