[Editor] Enable Query Result view dynamic table shape change on data refresh
This commit is contained in:
@@ -1,3 +1,10 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model.data
|
||||
|
||||
class DataField(val value: String)
|
||||
import javafx.beans.property.SimpleStringProperty
|
||||
import tornadofx.getValue
|
||||
import tornadofx.setValue
|
||||
|
||||
class DataField(value: String?) {
|
||||
val valueProperty = SimpleStringProperty(value)
|
||||
var value by valueProperty
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model.data
|
||||
|
||||
class DataRecord(val fields: Map<String, DataField>)
|
||||
class DataRecord(val fields: Map<String, DataField>, val operation: Operation = Operation.DO_NOTHING)
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model.data
|
||||
|
||||
enum class Operation {
|
||||
DO_NOTHING,
|
||||
INSERT,
|
||||
UPDATE,
|
||||
DELETE
|
||||
}
|
||||
@@ -1,12 +1,25 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model.data
|
||||
|
||||
import javafx.beans.property.SimpleListProperty
|
||||
import javafx.collections.ObservableList
|
||||
import tornadofx.getValue
|
||||
import tornadofx.toProperty
|
||||
|
||||
class Query(name: String, query: String, val columns: List<String>, val data: List<DataRecord>) {
|
||||
class Query(name: String, query: String, columns: ObservableList<String>, data: ObservableList<DataRecord>) {
|
||||
val nameProperty = name.toProperty()
|
||||
val name by nameProperty
|
||||
|
||||
val queryProperty = query.toProperty()
|
||||
val query by queryProperty
|
||||
|
||||
val columnsProperty = SimpleListProperty(columns)
|
||||
val columns by columnsProperty
|
||||
|
||||
val dataProperty = SimpleListProperty(data)
|
||||
val data by dataProperty
|
||||
|
||||
fun addEmptyRecord() {
|
||||
val fields = columns.map { it to DataField(null) }.toMap()
|
||||
data += DataRecord(fields, Operation.INSERT)
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import com.bartlomiejpluta.base.editor.database.model.schema.SchemaDatabase
|
||||
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.stereotype.Service
|
||||
import tornadofx.observableListOf
|
||||
import java.sql.Connection
|
||||
|
||||
@Service
|
||||
@@ -54,18 +55,18 @@ class H2DatabaseService : DatabaseService {
|
||||
val metadata = stmt.metaData
|
||||
|
||||
if (results != null && metadata != null) {
|
||||
val columns = mutableListOf<String>()
|
||||
val columns = observableListOf<String>()
|
||||
|
||||
for (i in 1..metadata.columnCount) {
|
||||
columns += metadata.getColumnLabel(i)
|
||||
}
|
||||
|
||||
val data = mutableListOf<DataRecord>()
|
||||
val data = observableListOf<DataRecord>()
|
||||
while (results.next()) {
|
||||
val record = mutableMapOf<String, DataField>()
|
||||
|
||||
for (i in 1..metadata.columnCount) {
|
||||
record[metadata.getColumnLabel(i)] = DataField(results.getObject(i).toString())
|
||||
record[metadata.getColumnLabel(i)] = DataField(results.getObject(i)?.toString())
|
||||
}
|
||||
|
||||
data += DataRecord(record)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.bartlomiejpluta.base.editor.database.view.query
|
||||
|
||||
import com.bartlomiejpluta.base.editor.database.component.QueryFieldCell
|
||||
import com.bartlomiejpluta.base.editor.database.controller.DatabaseController
|
||||
import com.bartlomiejpluta.base.editor.database.model.data.DataField
|
||||
import com.bartlomiejpluta.base.editor.database.model.data.DataRecord
|
||||
import com.bartlomiejpluta.base.editor.database.viewmodel.QueryVM
|
||||
import javafx.scene.control.TableColumn
|
||||
@@ -13,36 +11,40 @@ class QueryResultView : View() {
|
||||
private val databaseController: DatabaseController by di()
|
||||
private val queryVM = find<QueryVM>()
|
||||
|
||||
private val table = tableview<DataRecord>()
|
||||
private val table = tableview(queryVM.dataProperty)
|
||||
|
||||
init {
|
||||
queryVM.itemProperty.addListener { _, _, _ -> refreshData() }
|
||||
updateColumns()
|
||||
|
||||
refreshData()
|
||||
queryVM.itemProperty.addListener { _, _, query ->
|
||||
updateColumns()
|
||||
}
|
||||
}
|
||||
|
||||
private fun refreshData() {
|
||||
private fun updateColumns() {
|
||||
table.columns.clear()
|
||||
table.items.clear()
|
||||
queryVM.item?.let { query ->
|
||||
table.items.addAll(query.data)
|
||||
query.columns.map { column ->
|
||||
TableColumn<DataRecord, DataField>(column).apply {
|
||||
setCellValueFactory {
|
||||
it.value.fields[column].toProperty()
|
||||
}
|
||||
|
||||
setCellFactory { QueryFieldCell() }
|
||||
queryVM.columns.map { column ->
|
||||
TableColumn<DataRecord, String>(column).apply {
|
||||
setCellValueFactory {
|
||||
it.value.fields[column]!!.valueProperty
|
||||
}
|
||||
}.forEach { table.addColumnInternal(it) }
|
||||
}
|
||||
}
|
||||
}.let(table.columns::addAll)
|
||||
}
|
||||
|
||||
override val root = borderpane {
|
||||
top = toolbar {
|
||||
button(graphic = FontIcon("fa-refresh")) {
|
||||
action {
|
||||
databaseController.execute(queryVM.query, queryVM.name)?.let { queryVM.item = it }
|
||||
databaseController.execute(queryVM.query, queryVM.name)?.let {
|
||||
queryVM.item = it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
button(graphic = FontIcon("fa-plus")) {
|
||||
action {
|
||||
queryVM.item?.addEmptyRecord()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,4 +10,10 @@ class QueryVM(query: Query) : ItemViewModel<Query>(query) {
|
||||
|
||||
val queryProperty = bind(Query::queryProperty)
|
||||
val query by queryProperty
|
||||
|
||||
val columnsProperty = bind(Query::columnsProperty)
|
||||
val columns by columnsProperty
|
||||
|
||||
val dataProperty = bind(Query::dataProperty)
|
||||
val data by dataProperty
|
||||
}
|
||||
Reference in New Issue
Block a user