[Editor] Refactor object model of database related code
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
package com.bartlomiejpluta.base.editor.database.component
|
||||
|
||||
import com.bartlomiejpluta.base.editor.database.model.Field
|
||||
import com.bartlomiejpluta.base.editor.database.model.Row
|
||||
import com.bartlomiejpluta.base.editor.database.model.data.DataField
|
||||
import com.bartlomiejpluta.base.editor.database.model.data.DataRecord
|
||||
import javafx.scene.control.TableCell
|
||||
|
||||
class QueryFieldCell : TableCell<Row, Field>() {
|
||||
class QueryFieldCell : TableCell<DataRecord, DataField>() {
|
||||
|
||||
override fun updateItem(item: Field?, empty: Boolean) {
|
||||
override fun updateItem(item: DataField?, empty: Boolean) {
|
||||
super.updateItem(item, empty)
|
||||
|
||||
when {
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
package com.bartlomiejpluta.base.editor.database.component
|
||||
|
||||
import com.bartlomiejpluta.base.editor.database.model.SQLColumn
|
||||
import com.bartlomiejpluta.base.editor.database.model.SQLElement
|
||||
import com.bartlomiejpluta.base.editor.database.model.SQLTable
|
||||
import com.bartlomiejpluta.base.editor.database.model.schema.Schema
|
||||
import com.bartlomiejpluta.base.editor.database.model.schema.SchemaColumn
|
||||
import com.bartlomiejpluta.base.editor.database.model.schema.SchemaTable
|
||||
import javafx.scene.control.ContextMenu
|
||||
import javafx.scene.control.cell.TextFieldTreeCell
|
||||
import tornadofx.action
|
||||
import tornadofx.item
|
||||
|
||||
class SQLElementCell(
|
||||
renameElement: (element: SQLElement, name: String) -> SQLElement,
|
||||
deleteElement: (element: SQLElement) -> Unit
|
||||
) : TextFieldTreeCell<SQLElement>() {
|
||||
renameElement: (element: Schema, name: String) -> Schema,
|
||||
deleteElement: (element: Schema) -> Unit
|
||||
) : TextFieldTreeCell<Schema>() {
|
||||
private val tableMenu = ContextMenu().apply {
|
||||
item("Rename") {
|
||||
action {
|
||||
@@ -48,7 +48,7 @@ class SQLElementCell(
|
||||
converter = SQLElementStringConverter(this, renameElement)
|
||||
}
|
||||
|
||||
override fun updateItem(item: SQLElement?, empty: Boolean) {
|
||||
override fun updateItem(item: Schema?, empty: Boolean) {
|
||||
super.updateItem(item, empty)
|
||||
|
||||
if (empty || item == null) {
|
||||
@@ -58,15 +58,15 @@ class SQLElementCell(
|
||||
}
|
||||
|
||||
text = when (item) {
|
||||
is SQLColumn -> "${item.name}${if (item.nullable) "?" else ""}"
|
||||
is SchemaColumn -> "${item.name}${if (item.nullable) "?" else ""}"
|
||||
else -> item.name
|
||||
}
|
||||
|
||||
graphic = item.icon
|
||||
|
||||
contextMenu = when (item) {
|
||||
is SQLTable -> tableMenu
|
||||
is SQLColumn -> columnMenu
|
||||
is SchemaTable -> tableMenu
|
||||
is SchemaColumn -> columnMenu
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package com.bartlomiejpluta.base.editor.database.component
|
||||
|
||||
import com.bartlomiejpluta.base.editor.database.model.SQLElement
|
||||
import com.bartlomiejpluta.base.editor.database.model.schema.Schema
|
||||
import javafx.scene.control.TreeCell
|
||||
import javafx.util.StringConverter
|
||||
|
||||
class SQLElementStringConverter(
|
||||
private val cell: TreeCell<SQLElement>,
|
||||
private val rename: (item: SQLElement, newName: String) -> SQLElement
|
||||
) : StringConverter<SQLElement>() {
|
||||
override fun toString(item: SQLElement?): String = item?.name ?: ""
|
||||
private val cell: TreeCell<Schema>,
|
||||
private val rename: (item: Schema, newName: String) -> Schema
|
||||
) : StringConverter<Schema>() {
|
||||
override fun toString(item: Schema?): String = item?.name ?: ""
|
||||
|
||||
// Disclaimer:
|
||||
// This is actually the only place where we have an access to both actual element
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model
|
||||
|
||||
class Field(val value: String)
|
||||
@@ -1,3 +0,0 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model
|
||||
|
||||
class Row(val fields: Map<String, Field>)
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model.data
|
||||
|
||||
class DataField(val value: String)
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model.data
|
||||
|
||||
class DataRecord(val fields: Map<String, DataField>)
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model
|
||||
package com.bartlomiejpluta.base.editor.database.model.data
|
||||
|
||||
import tornadofx.getValue
|
||||
import tornadofx.toProperty
|
||||
|
||||
class Query(name: String, val columns: List<String>, val data: List<Row>) {
|
||||
class Query(name: String, val columns: List<String>, val data: List<DataRecord>) {
|
||||
val nameProperty = name.toProperty()
|
||||
val name by nameProperty
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model
|
||||
package com.bartlomiejpluta.base.editor.database.model.schema
|
||||
|
||||
enum class ColumnType {
|
||||
INTEGER,
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model
|
||||
package com.bartlomiejpluta.base.editor.database.model.schema
|
||||
|
||||
import javafx.scene.Node
|
||||
import java.sql.Connection
|
||||
|
||||
interface SQLElement {
|
||||
interface Schema {
|
||||
val name: String
|
||||
|
||||
fun rename(connection: Connection, newName: String)
|
||||
@@ -1,15 +1,15 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model
|
||||
package com.bartlomiejpluta.base.editor.database.model.schema
|
||||
|
||||
import org.kordamp.ikonli.javafx.FontIcon
|
||||
import java.sql.Connection
|
||||
|
||||
class SQLColumn(
|
||||
val table: SQLTable,
|
||||
class SchemaColumn(
|
||||
val table: SchemaTable,
|
||||
name: String,
|
||||
val type: ColumnType,
|
||||
val nullable: Boolean,
|
||||
val primary: Boolean
|
||||
) : SQLElement {
|
||||
) : Schema {
|
||||
override var name: String = name
|
||||
private set(value) {
|
||||
field = value.toUpperCase()
|
||||
@@ -1,15 +1,15 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model
|
||||
package com.bartlomiejpluta.base.editor.database.model.schema
|
||||
|
||||
import javafx.scene.Node
|
||||
import tornadofx.observableListOf
|
||||
import java.sql.Connection
|
||||
|
||||
class SQLDatabase : SQLElement {
|
||||
class SchemaDatabase : Schema {
|
||||
override val name = "Database"
|
||||
val tables = observableListOf<SQLTable>()
|
||||
val tables = observableListOf<SchemaTable>()
|
||||
|
||||
fun addTable(name: String) {
|
||||
tables.add(SQLTable(this, name))
|
||||
tables.add(SchemaTable(this, name))
|
||||
}
|
||||
|
||||
override fun rename(connection: Connection, newName: String) {
|
||||
@@ -1,19 +1,19 @@
|
||||
package com.bartlomiejpluta.base.editor.database.model
|
||||
package com.bartlomiejpluta.base.editor.database.model.schema
|
||||
|
||||
import org.kordamp.ikonli.javafx.FontIcon
|
||||
import tornadofx.observableListOf
|
||||
import java.sql.Connection
|
||||
|
||||
class SQLTable(val database: SQLDatabase, name: String) : SQLElement {
|
||||
class SchemaTable(val database: SchemaDatabase, name: String) : Schema {
|
||||
override var name: String = name
|
||||
private set(value) {
|
||||
field = value.toUpperCase()
|
||||
}
|
||||
|
||||
val columns = observableListOf<SQLColumn>()
|
||||
val columns = observableListOf<SchemaColumn>()
|
||||
|
||||
fun addColumn(name: String, type: ColumnType, nullable: Boolean, primary: Boolean) {
|
||||
val column = SQLColumn(this, name, type, nullable, primary)
|
||||
val column = SchemaColumn(this, name, type, nullable, primary)
|
||||
columns += column
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.bartlomiejpluta.base.editor.database.service
|
||||
|
||||
import com.bartlomiejpluta.base.editor.database.model.SQLDatabase
|
||||
import com.bartlomiejpluta.base.editor.database.model.schema.SchemaDatabase
|
||||
import java.sql.Connection
|
||||
|
||||
interface DatabaseService {
|
||||
val database: SQLDatabase
|
||||
val database: SchemaDatabase
|
||||
fun run(op: Connection.() -> Unit)
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.bartlomiejpluta.base.editor.database.service
|
||||
|
||||
import com.bartlomiejpluta.base.editor.database.model.ColumnType
|
||||
import com.bartlomiejpluta.base.editor.database.model.SQLDatabase
|
||||
import com.bartlomiejpluta.base.editor.database.model.schema.ColumnType
|
||||
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
|
||||
@@ -9,9 +9,9 @@ import java.sql.Connection
|
||||
|
||||
@Service
|
||||
class H2DatabaseService : DatabaseService {
|
||||
override val database: SQLDatabase
|
||||
override val database: SchemaDatabase
|
||||
get() {
|
||||
val db = SQLDatabase()
|
||||
val db = SchemaDatabase()
|
||||
|
||||
run {
|
||||
val results = prepareStatement("SHOW TABLES").executeQuery()
|
||||
|
||||
@@ -2,27 +2,32 @@ package com.bartlomiejpluta.base.editor.database.view.list
|
||||
|
||||
import com.bartlomiejpluta.base.editor.database.component.SQLElementCell
|
||||
import com.bartlomiejpluta.base.editor.database.model.*
|
||||
import com.bartlomiejpluta.base.editor.database.model.Field
|
||||
import com.bartlomiejpluta.base.editor.database.model.data.DataField
|
||||
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.schema.Schema
|
||||
import com.bartlomiejpluta.base.editor.database.model.schema.SchemaDatabase
|
||||
import com.bartlomiejpluta.base.editor.database.model.schema.SchemaTable
|
||||
import com.bartlomiejpluta.base.editor.database.service.DatabaseService
|
||||
import com.bartlomiejpluta.base.editor.file.model.InMemoryStringFileNode
|
||||
import com.bartlomiejpluta.base.editor.main.controller.MainController
|
||||
import com.bartlomiejpluta.base.editor.project.context.ProjectContext
|
||||
import javafx.scene.control.TreeItem
|
||||
import org.kordamp.ikonli.javafx.FontIcon
|
||||
import tornadofx.*
|
||||
import java.sql.Connection
|
||||
import java.sql.SQLException
|
||||
import tornadofx.*
|
||||
|
||||
class TablesListView : View() {
|
||||
private val mainController: MainController by di()
|
||||
private val projectContext: ProjectContext by di()
|
||||
private val databaseService: DatabaseService by di()
|
||||
|
||||
private var database: SQLDatabase? = null
|
||||
private var database: SchemaDatabase? = null
|
||||
|
||||
private var index = 0
|
||||
|
||||
private val treeView = treeview<SQLElement> {
|
||||
private val treeView = treeview<Schema> {
|
||||
isShowRoot = false
|
||||
|
||||
setCellFactory {
|
||||
@@ -61,8 +66,8 @@ class TablesListView : View() {
|
||||
|
||||
treeView.populate {
|
||||
when (val value = it.value) {
|
||||
is SQLDatabase -> value.tables
|
||||
is SQLTable -> value.columns
|
||||
is SchemaDatabase -> value.tables
|
||||
is SchemaTable -> value.columns
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
@@ -92,15 +97,15 @@ class TablesListView : View() {
|
||||
columns += metadata.getColumnLabel(i)
|
||||
}
|
||||
|
||||
val data = mutableListOf<Row>()
|
||||
val data = mutableListOf<DataRecord>()
|
||||
while (results.next()) {
|
||||
val record = mutableMapOf<String, Field>()
|
||||
val record = mutableMapOf<String, DataField>()
|
||||
|
||||
for (i in 1..metadata.columnCount) {
|
||||
record[metadata.getColumnLabel(i)] = Field(results.getObject(i).toString())
|
||||
record[metadata.getColumnLabel(i)] = DataField(results.getObject(i).toString())
|
||||
}
|
||||
|
||||
data += Row(record)
|
||||
data += DataRecord(record)
|
||||
}
|
||||
|
||||
mainController.openQuery(Query(name, columns, data))
|
||||
@@ -109,12 +114,12 @@ class TablesListView : View() {
|
||||
refresh()
|
||||
}
|
||||
|
||||
private fun renameElement(element: SQLElement, newName: String): SQLElement {
|
||||
private fun renameElement(element: Schema, newName: String): Schema {
|
||||
onConnection { element.rename(this, newName) }
|
||||
return element
|
||||
}
|
||||
|
||||
private fun deleteElement(element: SQLElement) {
|
||||
private fun deleteElement(element: Schema) {
|
||||
onConnection(element::delete)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.bartlomiejpluta.base.editor.database.view.query
|
||||
|
||||
import com.bartlomiejpluta.base.editor.database.component.QueryFieldCell
|
||||
import com.bartlomiejpluta.base.editor.database.model.Field
|
||||
import com.bartlomiejpluta.base.editor.database.model.Row
|
||||
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
|
||||
import tornadofx.*
|
||||
@@ -10,7 +10,7 @@ import tornadofx.*
|
||||
class QueryResultView : View() {
|
||||
private val queryVM = find<QueryVM>()
|
||||
|
||||
private val table = tableview<Row> {
|
||||
private val table = tableview<DataRecord> {
|
||||
}
|
||||
|
||||
init {
|
||||
@@ -25,7 +25,7 @@ class QueryResultView : View() {
|
||||
queryVM.item?.let { query ->
|
||||
table.items.addAll(query.data)
|
||||
query.columns.map { column ->
|
||||
TableColumn<Row, Field>(column).apply {
|
||||
TableColumn<DataRecord, DataField>(column).apply {
|
||||
setCellValueFactory {
|
||||
it.value.fields[column].toProperty()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.bartlomiejpluta.base.editor.database.viewmodel
|
||||
|
||||
import com.bartlomiejpluta.base.editor.database.model.Query
|
||||
import com.bartlomiejpluta.base.editor.database.model.data.Query
|
||||
import tornadofx.ItemViewModel
|
||||
import tornadofx.getValue
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import com.bartlomiejpluta.base.editor.audio.viewmodel.SoundAssetDataVM
|
||||
import com.bartlomiejpluta.base.editor.code.model.CodeScope
|
||||
import com.bartlomiejpluta.base.editor.code.viewmodel.CodeVM
|
||||
import com.bartlomiejpluta.base.editor.command.context.UndoableScope
|
||||
import com.bartlomiejpluta.base.editor.database.model.Query
|
||||
import com.bartlomiejpluta.base.editor.database.model.data.Query
|
||||
import com.bartlomiejpluta.base.editor.database.viewmodel.QueryVM
|
||||
import com.bartlomiejpluta.base.editor.entityset.view.importing.ImportEntitySetFragment
|
||||
import com.bartlomiejpluta.base.editor.entityset.viewmodel.EntitySetAssetDataVM
|
||||
|
||||
Reference in New Issue
Block a user