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