parent
ecdf1b24ba
commit
dfeb045f1a
@ -1,49 +0,0 @@
|
||||
package wanijo.wanijo2.db
|
||||
|
||||
import org.jdbi.v3.core.Jdbi
|
||||
import org.springframework.stereotype.Service
|
||||
import wanijo.wanijo2.domain.Document
|
||||
import wanijo.wanijo2.domain.DocumentDao
|
||||
import kotlin.jvm.optionals.getOrNull
|
||||
|
||||
@Service
|
||||
class DocumentDaoAdapter(
|
||||
private val jdbi: Jdbi
|
||||
) : DocumentDao {
|
||||
|
||||
override fun all(): List<Document> =
|
||||
jdbi.open().use {
|
||||
it.createQuery(
|
||||
"""
|
||||
SELECT tdoc.id,
|
||||
tdoc.created_at,
|
||||
tdoc.updated_at,
|
||||
tdoc.name,
|
||||
tdoc.description
|
||||
FROM t_document tdoc
|
||||
ORDER BY tdoc.updated_at DESC
|
||||
""".trimIndent()
|
||||
)
|
||||
.mapTo(Document::class.java)
|
||||
.list()
|
||||
}
|
||||
|
||||
override fun forId(id: Int): Document? =
|
||||
jdbi.open().use {
|
||||
it.createQuery(
|
||||
"""
|
||||
SELECT tdoc.id,
|
||||
tdoc.created_at,
|
||||
tdoc.updated_at,
|
||||
tdoc.name,
|
||||
tdoc.description
|
||||
FROM t_document tdoc
|
||||
WHERE tdoc.id = :id
|
||||
""".trimIndent()
|
||||
)
|
||||
.bind("id", id)
|
||||
.mapTo(Document::class.java)
|
||||
.findOne()
|
||||
}.getOrNull()
|
||||
|
||||
}
|
@ -1,11 +1,15 @@
|
||||
package wanijo.wanijo2.domain
|
||||
|
||||
import org.springframework.data.annotation.Id
|
||||
import org.springframework.data.relational.core.mapping.Table
|
||||
import java.time.ZonedDateTime
|
||||
|
||||
@Table("T_DOCUMENT")
|
||||
data class Document(
|
||||
val id: Int,
|
||||
val createdAt: ZonedDateTime,
|
||||
val updatedAt: ZonedDateTime,
|
||||
@Id
|
||||
val id: Int = 0,
|
||||
val createdAt: ZonedDateTime = ZonedDateTime.now(),
|
||||
val updatedAt: ZonedDateTime = ZonedDateTime.now(),
|
||||
val name: String,
|
||||
val description: String
|
||||
)
|
||||
|
@ -1,9 +1,15 @@
|
||||
package wanijo.wanijo2.domain
|
||||
|
||||
interface DocumentDao {
|
||||
import org.springframework.data.repository.Repository
|
||||
|
||||
fun all(): List<Document>
|
||||
interface DocumentDao: Repository<Document, Int> {
|
||||
|
||||
fun forId(id: Int): Document?
|
||||
fun findAll(): List<Document>
|
||||
|
||||
fun findById(id: Int): Document?
|
||||
|
||||
fun findByName(name: String): List<Document>
|
||||
|
||||
fun save(doc: Document)
|
||||
|
||||
}
|
||||
|
@ -1,51 +0,0 @@
|
||||
package wanijo.wanijo2.spring
|
||||
|
||||
import org.jdbi.v3.core.Jdbi
|
||||
import org.jdbi.v3.core.kotlin.KotlinPlugin
|
||||
import org.jdbi.v3.core.statement.SqlLogger
|
||||
import org.jdbi.v3.core.statement.StatementContext
|
||||
import org.jdbi.v3.sqlobject.kotlin.KotlinSqlObjectPlugin
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import javax.sql.DataSource
|
||||
|
||||
@Configuration
|
||||
class JdbiConfig {
|
||||
|
||||
@Bean
|
||||
fun jdbi(ds: DataSource): Jdbi =
|
||||
Jdbi.create(ds).run {
|
||||
installPlugin(KotlinPlugin())
|
||||
installPlugin(KotlinSqlObjectPlugin())
|
||||
setSqlLogger(object : SqlLogger {
|
||||
private val logger = LoggerFactory.getLogger("org.jdbi.sql")
|
||||
|
||||
override fun logBeforeExecution(context: StatementContext) {
|
||||
if (logger.isDebugEnabled) {
|
||||
logger.debug(
|
||||
"Query: {}, Params: {}",
|
||||
context.parsedSql.sql.lines()
|
||||
.joinToString(separator = " ") { it.trim() },
|
||||
context.binding
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun logAfterExecution(context: StatementContext) {
|
||||
if (logger.isDebugEnabled) {
|
||||
logger.debug(
|
||||
"Duration: {}ms, Query: {}, Params: {}",
|
||||
context.completionMoment.toEpochMilli() - context.executionMoment.toEpochMilli(),
|
||||
context.parsedSql.sql.lines()
|
||||
.joinToString(separator = " ") { it.trim() },
|
||||
context.binding
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
this
|
||||
}
|
||||
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
spring.datasource.url=jdbc:h2:file:./wanijo2-dev.h2
|
||||
|
||||
logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
|
||||
|
||||
spring.banner.location=banner-dev.txt
|
||||
|
@ -0,0 +1,74 @@
|
||||
package wanijo.wanijo2.domain
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertNotNull
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
import org.springframework.test.annotation.DirtiesContext
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@SpringBootTest
|
||||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
||||
class DocumentDaoTest {
|
||||
|
||||
@Autowired
|
||||
lateinit var adapter: DocumentDao
|
||||
|
||||
@Test
|
||||
fun insertWorks() {
|
||||
assertEquals(0, adapter.findAll().count())
|
||||
|
||||
adapter.save(
|
||||
Document(
|
||||
name = "name",
|
||||
description = "desc"
|
||||
)
|
||||
)
|
||||
|
||||
assertEquals(1, adapter.findAll().count())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun findByIdWorks() {
|
||||
val doc = Document(
|
||||
name = "name",
|
||||
description = "desc"
|
||||
)
|
||||
|
||||
adapter.save(doc)
|
||||
|
||||
val savedDoc = adapter.findById(1)
|
||||
assertNotNull(savedDoc)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun idsAreGeneratedSequentially() {
|
||||
adapter.save(
|
||||
Document(
|
||||
name = "name1",
|
||||
description = "desc"
|
||||
)
|
||||
)
|
||||
|
||||
adapter.save(
|
||||
Document(
|
||||
name = "name2",
|
||||
description = "desc"
|
||||
)
|
||||
)
|
||||
|
||||
assertNotNull(adapter.findById(1))
|
||||
assertNotNull(adapter.findById(2))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun findByNameWorks()
|
||||
{
|
||||
adapter.save(
|
||||
Document(name = "findmich", description = "")
|
||||
)
|
||||
|
||||
assertNotNull(adapter.findByName("findmich"))
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
|
||||
logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
|
||||
|
||||
spring.banner.location=banner-test.txt
|
@ -0,0 +1,4 @@
|
||||
▗▄▄▄▖▗▄▄▄▖ ▗▄▄▖▗▄▄▄▖▗▄▄▄▖▗▖ ▗▖ ▗▄▄▖
|
||||
█ ▐▌ ▐▌ █ █ ▐▛▚▖▐▌▐▌
|
||||
█ ▐▛▀▀▘ ▝▀▚▖ █ █ ▐▌ ▝▜▌▐▌▝▜▌
|
||||
█ ▐▙▄▄▖▗▄▄▞▘ █ ▗▄█▄▖▐▌ ▐▌▝▚▄▞▘
|
Loading…
Reference in new issue