Skip to content

Commit 9da977e

Browse files
committed
Kotlin extensions for Spring JDBC Template
1 parent 69bb1d3 commit 9da977e

File tree

4 files changed

+51
-87
lines changed

4 files changed

+51
-87
lines changed

pom.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@
173173
<version>${kotlin.version}</version>
174174
<scope>provided</scope>
175175
</dependency>
176+
<dependency>
177+
<groupId>org.springframework</groupId>
178+
<artifactId>spring-jdbc</artifactId>
179+
<version>5.1.9.RELEASE</version>
180+
<scope>provided</scope>
181+
</dependency>
182+
176183
<dependency>
177184
<groupId>org.junit.jupiter</groupId>
178185
<artifactId>junit-jupiter-api</artifactId>
@@ -191,7 +198,6 @@
191198
<version>${junit.platform.version}</version>
192199
<scope>test</scope>
193200
</dependency>
194-
195201
<dependency>
196202
<groupId>org.assertj</groupId>
197203
<artifactId>assertj-core</artifactId>
@@ -216,12 +222,6 @@
216222
<version>2.5.0</version>
217223
<scope>test</scope>
218224
</dependency>
219-
<dependency>
220-
<groupId>org.springframework</groupId>
221-
<artifactId>spring-jdbc</artifactId>
222-
<version>5.1.9.RELEASE</version>
223-
<scope>test</scope>
224-
</dependency>
225225
<dependency>
226226
<groupId>org.springframework.batch</groupId>
227227
<artifactId>spring-batch-core</artifactId>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.delete.render.DeleteStatementProvider
19+
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider
20+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider
21+
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider
22+
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource
23+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
24+
import java.sql.ResultSet
25+
26+
fun NamedParameterJdbcTemplate.count(selectStatement: SelectStatementProvider) =
27+
queryForObject(selectStatement.selectStatement, selectStatement.parameters, Long::class.java)!!
28+
29+
fun NamedParameterJdbcTemplate.delete(deleteStatement: DeleteStatementProvider) =
30+
update(deleteStatement.deleteStatement, deleteStatement.parameters)
31+
32+
fun <T> NamedParameterJdbcTemplate.insert(insertStatement: InsertStatementProvider<T>) =
33+
update(insertStatement.insertStatement, BeanPropertySqlParameterSource(insertStatement.record))
34+
35+
fun <T> NamedParameterJdbcTemplate.selectMany(selectStatement: SelectStatementProvider, rowMapper: (rs: ResultSet, rowNum: Int) -> T): List<T> =
36+
query(selectStatement.selectStatement, selectStatement.parameters, rowMapper)
37+
38+
fun <T> NamedParameterJdbcTemplate.selectOne(selectStatement: SelectStatementProvider, rowMapper: (rs: ResultSet, rowNum: Int) -> T): T? =
39+
queryForObject(selectStatement.selectStatement, selectStatement.parameters, rowMapper)
40+
41+
fun NamedParameterJdbcTemplate.update(updateStatement: UpdateStatementProvider) =
42+
update(updateStatement.updateStatement, updateStatement.parameters)

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ fun deleteFrom(table: SqlTable, completer: DeleteCompleter) =
3333
fun <T> insert(record: T, table: SqlTable, completer: InsertCompleter<T>): InsertStatementProvider<T> =
3434
completer(SqlBuilder.insert(record).into(table)).build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
3535

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-
3936
fun QueryExpressionDSL.FromGatherer<SelectModel>.from(table: SqlTable, completer: SelectCompleter) =
4037
completer(from(table)).build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
4138

src/test/kotlin/examples/kotlin/spring/canonical/CanonicalSpringKotlinTest.kt

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -25,56 +25,23 @@ import examples.kotlin.spring.canonical.PersonDynamicSqlSupport.Person.id
2525
import examples.kotlin.spring.canonical.PersonDynamicSqlSupport.Person.lastName
2626
import examples.kotlin.spring.canonical.PersonDynamicSqlSupport.Person.occupation
2727
import org.assertj.core.api.Assertions.assertThat
28-
import org.junit.jupiter.api.AfterEach
2928
import org.junit.jupiter.api.BeforeEach
3029
import org.junit.jupiter.api.Test
3130
import org.mybatis.dynamic.sql.SqlBuilder.*
32-
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider
33-
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider
34-
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider
35-
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider
3631
import org.mybatis.dynamic.sql.util.kotlin.*
3732
import org.mybatis.dynamic.sql.util.kotlin.spring.*
3833
import org.mybatis.dynamic.sql.util.kotlin.spring.from
39-
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource
4034
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
41-
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase
4235
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder
4336
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType
44-
import java.sql.ResultSet
4537
import java.util.*
4638

47-
fun NamedParameterJdbcTemplate.count(selectStatement: SelectStatementProvider) =
48-
queryForObject(selectStatement.selectStatement, selectStatement.parameters, Long::class.java)
49-
?: 0
50-
51-
fun NamedParameterJdbcTemplate.delete(deleteStatement: DeleteStatementProvider) =
52-
update(deleteStatement.deleteStatement, deleteStatement.parameters)
53-
54-
fun <T> NamedParameterJdbcTemplate.insert(insertStatement: InsertStatementProvider<T>) =
55-
update(insertStatement.insertStatement, BeanPropertySqlParameterSource(insertStatement.record))
56-
57-
fun <T> NamedParameterJdbcTemplate.selectMany(selectStatement: SelectStatementProvider, rowMapper: (rs: ResultSet, rowNum: Int) -> T): List<T> =
58-
query(selectStatement.selectStatement, selectStatement.parameters, rowMapper)
59-
60-
fun <T> NamedParameterJdbcTemplate.selectOne(selectStatement: SelectStatementProvider, rowMapper: (rs: ResultSet, rowNum: Int) -> T): T =
61-
queryForObject(selectStatement.selectStatement, selectStatement.parameters, rowMapper)!!
62-
63-
fun NamedParameterJdbcTemplate.update(updateStatement: UpdateStatementProvider) =
64-
update(updateStatement.updateStatement, updateStatement.parameters)
65-
6639
class CanonicalSpringKotlinTest {
67-
private lateinit var db: EmbeddedDatabase
6840
private lateinit var template: NamedParameterJdbcTemplate
6941

70-
@AfterEach
71-
fun teardown() {
72-
db.shutdown()
73-
}
74-
7542
@BeforeEach
7643
fun setup() {
77-
db = EmbeddedDatabaseBuilder()
44+
val db = EmbeddedDatabaseBuilder()
7845
.setType(EmbeddedDatabaseType.HSQL)
7946
.generateUniqueName(true)
8047
.addScript("classpath:/examples/kotlin/spring/CreateSimpleDB.sql")
@@ -245,48 +212,6 @@ class CanonicalSpringKotlinTest {
245212
assertThat(rows).isEqualTo(1)
246213
}
247214

248-
// @Test
249-
// fun testInsertMultiple() {
250-
// val record1 = PersonRecord(100, "Joe", "Jones", Date(), "Yes", "Developer", 1)
251-
// val record2 = PersonRecord(101, "Sarah", "Smith", Date(), "Yes", "Architect", 2)
252-
//
253-
// val insertStatement = insertMultiple(listOf(record1, record2), Person) {
254-
// map(id).toProperty("id")
255-
// map(firstName).toProperty("firstName")
256-
// map(lastName).toProperty("lastName")
257-
// map(birthDate).toProperty("birthDate")
258-
// map(employed).toProperty("employed")
259-
// map(occupation).toProperty("occupation")
260-
// map(addressId).toProperty("addressId")
261-
// }
262-
//
263-
// val expected = "insert into Person (id, first_name, last_name, birth_date, employed, occupation, address_id)" +
264-
// " values" +
265-
// " (#{records[0].id,jdbcType=INTEGER}," +
266-
// " #{records[0].firstName,jdbcType=VARCHAR}," +
267-
// " #{records[0].lastName,jdbcType=VARCHAR,typeHandler=examples.kotlin.mybatis3.canonical.LastNameTypeHandler}," +
268-
// " #{records[0].birthDate,jdbcType=DATE}," +
269-
// " #{records[0].employed,jdbcType=VARCHAR,typeHandler=examples.kotlin.mybatis3.canonical.YesNoTypeHandler}," +
270-
// " #{records[0].occupation,jdbcType=VARCHAR}," +
271-
// " #{records[0].addressId,jdbcType=INTEGER})" +
272-
// ", (#{records[1].id,jdbcType=INTEGER}," +
273-
// " #{records[1].firstName,jdbcType=VARCHAR}," +
274-
// " #{records[1].lastName,jdbcType=VARCHAR,typeHandler=examples.kotlin.mybatis3.canonical.LastNameTypeHandler}," +
275-
// " #{records[1].birthDate,jdbcType=DATE}," +
276-
// " #{records[1].employed,jdbcType=VARCHAR,typeHandler=examples.kotlin.mybatis3.canonical.YesNoTypeHandler}," +
277-
// " #{records[1].occupation,jdbcType=VARCHAR}," +
278-
// " #{records[1].addressId,jdbcType=INTEGER})"
279-
//
280-
// assertThat(insertStatement.insertStatement).isEqualTo(expected)
281-
//
282-
// val b = BeanPropertySqlParameterSource(insertStatement.records)
283-
// val names = b.parameterNames
284-
//
285-
// val rows = template.update(insertStatement.insertStatement, BeanPropertySqlParameterSource(insertStatement.records))
286-
//
287-
// assertThat(rows).isEqualTo(2)
288-
// }
289-
290215
@Test
291216
fun testRawSelect() {
292217
val selectStatement = select(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation,
@@ -342,7 +267,7 @@ class CanonicalSpringKotlinTest {
342267
record
343268
}
344269

345-
with(record) {
270+
with(record!!) {
346271
assertThat(id).isEqualTo(1)
347272
assertThat(firstName).isEqualTo("Fred")
348273
assertThat(lastName).isEqualTo("Flintstone")

0 commit comments

Comments
 (0)