[Editor] Add support inserting data from Query Result view
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.editor.database.controller
|
package com.bartlomiejpluta.base.editor.database.controller
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.editor.database.model.data.DataRecord
|
||||||
import com.bartlomiejpluta.base.editor.database.model.data.Query
|
import com.bartlomiejpluta.base.editor.database.model.data.Query
|
||||||
import com.bartlomiejpluta.base.editor.database.service.DatabaseService
|
import com.bartlomiejpluta.base.editor.database.service.DatabaseService
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
@@ -19,14 +20,27 @@ class DatabaseController : Controller() {
|
|||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun execute(op: Connection.() -> Unit) {
|
fun execute(op: Connection.() -> Unit): Boolean = databaseService.run {
|
||||||
databaseService.run<Unit> {
|
try {
|
||||||
try {
|
op(this)
|
||||||
op(this)
|
true
|
||||||
} catch (e: SQLException) {
|
} catch (e: SQLException) {
|
||||||
sqlErrorAlert(e)
|
sqlErrorAlert(e)
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} ?: false
|
||||||
|
|
||||||
|
fun submitBatch(table: String, records: List<DataRecord>) = execute {
|
||||||
|
autoCommit = false
|
||||||
|
records.forEach {
|
||||||
|
it.prepareStatement(table)?.let { sql ->
|
||||||
|
val statement = prepareStatement(sql)
|
||||||
|
it.fields.values.forEachIndexed { index, field -> statement.setObject(index + 1, field.value) }
|
||||||
|
statement.execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sqlErrorAlert(e: SQLException) =
|
private fun sqlErrorAlert(e: SQLException) =
|
||||||
|
|||||||
@@ -3,10 +3,8 @@ package com.bartlomiejpluta.base.editor.database.model.data
|
|||||||
import tornadofx.getValue
|
import tornadofx.getValue
|
||||||
import tornadofx.setValue
|
import tornadofx.setValue
|
||||||
import tornadofx.toProperty
|
import tornadofx.toProperty
|
||||||
import kotlin.collections.Map
|
|
||||||
import kotlin.collections.component1
|
import kotlin.collections.component1
|
||||||
import kotlin.collections.component2
|
import kotlin.collections.component2
|
||||||
import kotlin.collections.forEach
|
|
||||||
|
|
||||||
class DataRecord(val fields: Map<String, DataField>, operation: Operation = Operation.DO_NOTHING) {
|
class DataRecord(val fields: Map<String, DataField>, operation: Operation = Operation.DO_NOTHING) {
|
||||||
val operationProperty = operation.toProperty()
|
val operationProperty = operation.toProperty()
|
||||||
@@ -15,4 +13,11 @@ class DataRecord(val fields: Map<String, DataField>, operation: Operation = Oper
|
|||||||
init {
|
init {
|
||||||
fields.forEach { (_, field) -> field.record = this }
|
fields.forEach { (_, field) -> field.record = this }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun prepareStatement(table: String) = when (operation) {
|
||||||
|
Operation.INSERT -> "INSERT INTO `$table` SET $parameters;"
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
|
||||||
|
private val parameters = fields.map { (column, _) -> "`$column` = ?" }.joinToString(", ")
|
||||||
}
|
}
|
||||||
@@ -51,11 +51,7 @@ class QueryResultView : View() {
|
|||||||
override val root = borderpane {
|
override val root = borderpane {
|
||||||
top = toolbar {
|
top = toolbar {
|
||||||
button(graphic = FontIcon("fa-refresh")) {
|
button(graphic = FontIcon("fa-refresh")) {
|
||||||
action {
|
action { refresh() }
|
||||||
databaseController.execute(queryVM.query, queryVM.name, queryVM.table)?.let {
|
|
||||||
queryVM.item = it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
button(graphic = FontIcon("fa-plus")) {
|
button(graphic = FontIcon("fa-plus")) {
|
||||||
@@ -82,11 +78,20 @@ class QueryResultView : View() {
|
|||||||
enableWhen(queryVM.tableProperty.isNotNull)
|
enableWhen(queryVM.tableProperty.isNotNull)
|
||||||
|
|
||||||
action {
|
action {
|
||||||
println(queryVM.data.size)
|
val success = databaseController.submitBatch(queryVM.name, queryVM.data)
|
||||||
|
if (success) {
|
||||||
|
refresh()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
center = table
|
center = table
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun refresh() {
|
||||||
|
databaseController.execute(queryVM.query, queryVM.name, queryVM.table)?.let {
|
||||||
|
queryVM.item = it
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user