[Editor] Enable feeding Query object with query result data
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user