[Editor] Enable feeding Query object with query result data

This commit is contained in:
2021-03-25 12:47:59 +01:00
parent 39efbe6faa
commit c3c77edadd
5 changed files with 32 additions and 23 deletions

View File

@@ -1,4 +1,9 @@
package com.bartlomiejpluta.base.editor.database.model
class Query {
import tornadofx.getValue
import tornadofx.toProperty
class Query(name: String, val columns: List<String>, val data: List<Map<String, String>>) {
val nameProperty = name.toProperty()
val name by nameProperty
}

View File

@@ -42,9 +42,10 @@ class TablesListView : View() {
toolbar {
button("SQL Script", graphic = FontIcon("fa-code")) {
action {
val name = "Script ${++index}"
mainController.openScript(
fsNode = InMemoryStringFileNode("Script ${++index}", "sql", ""),
execute = { code -> onConnection { executeScript(code, this) } },
fsNode = InMemoryStringFileNode(name, "sql", ""),
execute = { code -> onConnection { executeScript(name, code, this) } },
saveable = false
)
}
@@ -81,32 +82,30 @@ class TablesListView : View() {
}
}
private fun executeScript(sql: String, conn: Connection) {
val stmt = conn.prepareStatement(sql)
stmt.execute()
val rs = stmt.resultSet
val meta = stmt.metaData
private fun executeScript(name: String, sql: String, conn: Connection) {
val stmt = conn.prepareStatement(sql).apply { execute() }
val results = stmt.resultSet
val metadata = stmt.metaData
if (meta != null) {
for (i in 1..meta.columnCount) {
print(meta.getColumnLabel(i))
print(" ")
if (results != null && metadata != null) {
val columns = mutableListOf<String>()
for (i in 1..metadata.columnCount) {
columns += metadata.getColumnLabel(i)
}
println()
}
val data = mutableListOf<Map<String, String>>()
while (results.next()) {
val record = mutableMapOf<String, String>()
if (rs != null) {
while (rs.next()) {
for (i in 1..meta.columnCount) {
print(rs.getObject(i))
print(" ")
for (i in 1..metadata.columnCount) {
record[metadata.getColumnLabel(i)] = results.getObject(i).toString()
}
println()
data += record
}
mainController.openQuery(Query())
mainController.openQuery(Query(name, columns, data))
}
refresh()

View File

@@ -2,6 +2,9 @@ package com.bartlomiejpluta.base.editor.database.viewmodel
import com.bartlomiejpluta.base.editor.database.model.Query
import tornadofx.ItemViewModel
import tornadofx.getValue
class QueryVM(query: Query) : ItemViewModel<Query>(query) {
val nameProperty = bind(Query::nameProperty)
val name by nameProperty
}

View File

@@ -121,7 +121,9 @@ class MainController : Controller() {
}
fun openQuery(query: Query) {
openItem<Query, Scope>({ false }, {}) {
val findQuery = { q: Query -> q.name == query.name }
openItem<Query, Scope>(findQuery, {}) {
val vm = QueryVM(query)
val scope = Scope()
setInScope(vm, scope)

View File

@@ -183,7 +183,7 @@ class MainView : View("BASE Game Editor") {
setInScope(vm, scope)
EditorTab(find<QueryResultFragment>(scope), FontIcon("fa-table")).apply {
text = "SQL Query"
textProperty().bind(item.nameProperty)
setOnClosed { mainController.openItems.remove(scope) }
}