Skip to content

Commit

Permalink
Embdded DBへの布石
Browse files Browse the repository at this point in the history
refs #10
  • Loading branch information
love2hina-net committed Dec 29, 2021
1 parent 4ba2735 commit 035de8a
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 9 deletions.
17 changes: 16 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ buildscript {
ext.versions = [
'target_jvm': 11,
'junit': '5.8.2',
'mockito': '4.1.0'
'mockito': '4.1.0',
'doma': '2.50.0'
]
}

plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version "$kotlin_version"
id "org.jetbrains.kotlin.kapt" version "$kotlin_version"
id 'application'
id "org.seasar.doma.codegen" version "1.4.1"
id "org.seasar.doma.compile" version "1.1.0"
}

group 'net.love2hina'
Expand Down Expand Up @@ -44,6 +48,17 @@ dependencies {
// Java Parser
implementation 'com.github.javaparser:javaparser-core:3.23.1'

// H2 Database engine
runtimeOnly 'com.h2database:h2:2.0.204'

// logback
runtimeOnly 'ch.qos.logback:logback-classic:1.2.10'

// Doma2
implementation "org.seasar.doma:doma-kotlin:${versions.doma}"
implementation "org.seasar.doma:doma-slf4j:${versions.doma}"
kapt "org.seasar.doma:doma-processor:${versions.doma}"

// テスト
testImplementation "org.junit.jupiter:junit-jupiter-api:${versions.junit}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${versions.junit}"
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
kotlin.code.style=official
kotlin.code.style=official
kapt.incremental.apt=false
kapt.include.compile.classpath=false
4 changes: 4 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module sharon.main {
requires java.base;
requires java.sql;
requires java.xml;
requires kotlin.stdlib;
requires kotlin.stdlib.jdk7;
requires kotlin.stdlib.jdk8;
requires com.github.javaparser.core;
requires org.seasar.doma.core;
requires org.seasar.doma.slf4j;
}
26 changes: 26 additions & 0 deletions src/main/kotlin/net/love2hina/kotlin/sharon/AppDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.love2hina.kotlin.sharon

import org.seasar.doma.Dao
import org.seasar.doma.Script
import org.seasar.doma.Sql

@Dao
interface AppDao {

@Sql("""
CREATE TABLE FILEMAP (
ID UUID NOT NULL PRIMARY KEY,
FILE VARCHAR(2048) NOT NULL,
PACKAGE VARCHAR(2048)
);
""")
@Script
fun create(): Unit

@Sql("""
DROP TABLE FILEMAP;
""")
@Script
fun drop(): Unit

}
39 changes: 39 additions & 0 deletions src/main/kotlin/net/love2hina/kotlin/sharon/DbConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.love2hina.kotlin.sharon

import org.seasar.doma.jdbc.Config
import org.seasar.doma.jdbc.JdbcLogger
import org.seasar.doma.jdbc.dialect.Dialect
import org.seasar.doma.jdbc.dialect.H2Dialect
import org.seasar.doma.jdbc.tx.LocalTransactionDataSource
import org.seasar.doma.jdbc.tx.LocalTransactionManager
import org.seasar.doma.jdbc.tx.TransactionManager
import org.seasar.doma.slf4j.Slf4jJdbcLogger
import javax.sql.DataSource

data class DbConfig(
private val dialect: Dialect,
private val dataSource: DataSource,
private val jdbcLogger: JdbcLogger,
private val transactionManager: TransactionManager
): Config {

override fun getDialect(): Dialect = dialect
override fun getDataSource(): DataSource = dataSource
override fun getJdbcLogger(): JdbcLogger = jdbcLogger
override fun getTransactionManager(): TransactionManager = transactionManager

companion object {
fun create(): DbConfig {
val datasource = LocalTransactionDataSource("jdbc:h2:mem:sharon;DB_CLOSE_DELAY=-1", "sa", null)
val logger = Slf4jJdbcLogger()

return DbConfig(
H2Dialect(),
datasource,
logger,
LocalTransactionManager(datasource.getLocalTransaction(logger))
)
}
}

}
14 changes: 13 additions & 1 deletion src/main/kotlin/net/love2hina/kotlin/sharon/FileMapper.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package net.love2hina.kotlin.sharon

import java.io.Closeable
import java.io.File
import java.util.*
import kotlin.collections.HashMap

internal class FileMapper {
internal class FileMapper: Closeable {

private val config = DbConfig.create()

private val map = HashMap<String, File>()

init {
// setup
val dao = AppDaoImpl(config)
config.transactionManager.required { dao.create() }
}

override fun close() {
}

fun assign(file: File): String {
var id: String

Expand Down
6 changes: 6 additions & 0 deletions src/main/kotlin/net/love2hina/kotlin/sharon/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import java.util.*

internal class Parser(val file: File) {

companion object Default {
fun parse(file: File, xml: File) {
Parser(file).parse(xml)
}
}

fun parse(xml: File) {
val unit = StaticJavaParser.parse(file)

Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/net/love2hina/kotlin/sharon/SharonApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ internal class SharonApplication(val args: Array<String>) {
}

fun processFiles() {
val mapper = FileMapper()
FileMapper().use { mapper->
files.parallelStream().forEach {
val xmlName = mapper.assign(it)

files.parallelStream().forEach {
val xmlName = mapper.assign(it)
val parser = Parser(it)

parser.parse(pathOutputDir.resolve(xmlName).toFile())
Parser.parse(it, pathOutputDir.resolve(xmlName).toFile())
}
}
}

Expand Down

0 comments on commit 035de8a

Please sign in to comment.