[Editor] Use SQL file for project's database instead of *.mv.db and *.trace.db H2 files
This commit is contained in:
@@ -16,7 +16,7 @@ class DatabaseController : Controller() {
|
||||
private val databaseService: DatabaseService by di()
|
||||
|
||||
fun execute(statement: String, name: String, schema: SchemaTable? = null): Query? = try {
|
||||
databaseService.execute(statement, name, schema)
|
||||
databaseService.execute(statement, name, schema).also { databaseService.dump() }
|
||||
} catch (e: SQLException) {
|
||||
sqlErrorAlert(e)
|
||||
null
|
||||
@@ -25,6 +25,7 @@ class DatabaseController : Controller() {
|
||||
fun execute(op: Connection.() -> Unit): Boolean = databaseService.run {
|
||||
try {
|
||||
op(this)
|
||||
databaseService.dump()
|
||||
true
|
||||
} catch (e: SQLException) {
|
||||
sqlErrorAlert(e)
|
||||
@@ -40,6 +41,7 @@ class DatabaseController : Controller() {
|
||||
.forEach(PreparedStatement::execute)
|
||||
|
||||
commit()
|
||||
databaseService.dump()
|
||||
}
|
||||
|
||||
private fun sqlErrorAlert(e: SQLException) =
|
||||
|
||||
@@ -11,4 +11,6 @@ interface DatabaseService {
|
||||
fun <T> run(op: Connection.() -> T): T?
|
||||
|
||||
fun execute(statement: String, name: String, schema: SchemaTable? = null): Query?
|
||||
|
||||
fun dump()
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import com.bartlomiejpluta.base.editor.database.model.data.Query
|
||||
import com.bartlomiejpluta.base.editor.database.model.schema.SchemaDatabase
|
||||
import com.bartlomiejpluta.base.editor.database.model.schema.SchemaTable
|
||||
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
|
||||
import org.h2.tools.Script
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.stereotype.Service
|
||||
import tornadofx.observableListOf
|
||||
@@ -32,10 +33,10 @@ class H2DatabaseService : DatabaseService {
|
||||
val results = prepareStatement("SHOW COLUMNS FROM ${table.name}").executeQuery()
|
||||
while (results.next()) {
|
||||
table.addColumn(
|
||||
results.getString("FIELD"),
|
||||
results.getString("TYPE"),
|
||||
results.getBoolean("NULL"),
|
||||
results.getString("KEY") == "PRI"
|
||||
results.getString("FIELD"),
|
||||
results.getString("TYPE"),
|
||||
results.getBoolean("NULL"),
|
||||
results.getString("KEY") == "PRI"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -80,4 +81,12 @@ class H2DatabaseService : DatabaseService {
|
||||
|
||||
return@run null
|
||||
}
|
||||
|
||||
override fun dump() {
|
||||
projectContext.project?.databaseFile?.let { file ->
|
||||
run {
|
||||
Script.process(this, file.absolutePath, "", "")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,21 @@ package com.bartlomiejpluta.base.editor.database.source
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
import org.h2.tools.RunScript
|
||||
import org.h2.tools.Script
|
||||
import java.io.File
|
||||
import java.sql.Connection
|
||||
|
||||
class DataSource(dbFile: File) {
|
||||
class H2DBDataSource(dbFile: File) {
|
||||
private val config = HikariConfig()
|
||||
private val source: HikariDataSource
|
||||
|
||||
init {
|
||||
config.jdbcUrl = "jdbc:h2:file:${dbFile.absolutePath.replace("\\", "/")}"
|
||||
config.jdbcUrl = "jdbc:h2:mem:data;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1"
|
||||
source = HikariDataSource(config)
|
||||
source.connection.use {
|
||||
RunScript.execute(it, dbFile.reader())
|
||||
}
|
||||
}
|
||||
|
||||
val connection: Connection
|
||||
@@ -3,7 +3,7 @@ package com.bartlomiejpluta.base.editor.project.model
|
||||
import com.bartlomiejpluta.base.editor.animation.asset.AnimationAsset
|
||||
import com.bartlomiejpluta.base.editor.audio.asset.SoundAsset
|
||||
import com.bartlomiejpluta.base.editor.autotile.asset.AutoTileAsset
|
||||
import com.bartlomiejpluta.base.editor.database.source.DataSource
|
||||
import com.bartlomiejpluta.base.editor.database.source.H2DBDataSource
|
||||
import com.bartlomiejpluta.base.editor.characterset.asset.CharacterSetAsset
|
||||
import com.bartlomiejpluta.base.editor.file.model.FileSystemNode
|
||||
import com.bartlomiejpluta.base.editor.gui.font.asset.FontAsset
|
||||
@@ -128,7 +128,7 @@ class Project {
|
||||
createObjectBinding({ File(buildOutDirectory, PROJECT_OUTPUT_JAR_FILE) }, buildOutDirectoryProperty)
|
||||
val buildOutputJarFile by buildOutputJarFileProperty
|
||||
|
||||
lateinit var database: DataSource
|
||||
lateinit var database: H2DBDataSource
|
||||
|
||||
init {
|
||||
sourceDirectoryProperty.addListener { _, _, dir ->
|
||||
@@ -155,7 +155,7 @@ class Project {
|
||||
}
|
||||
|
||||
fun init() {
|
||||
database = DataSource(databaseFile)
|
||||
database = H2DBDataSource(databaseFile)
|
||||
mkdirs()
|
||||
}
|
||||
|
||||
@@ -180,7 +180,7 @@ class Project {
|
||||
|
||||
companion object {
|
||||
const val PROJECT_FILE = "project.bep"
|
||||
const val DATABASE_FILE = "data"
|
||||
const val DATABASE_FILE = "data.sql"
|
||||
const val DATABASE_DUMP_FILE = "data.sql"
|
||||
const val PROJECT_OUTPUT_JAR_FILE = "game.jar"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user