[Editor] Improve database columns description

This commit is contained in:
2021-03-25 20:24:36 +01:00
parent d77d7d116f
commit eb40bbc1bb
6 changed files with 12 additions and 13 deletions

View File

@@ -8,7 +8,7 @@ import javafx.scene.control.cell.TextFieldTreeCell
import tornadofx.action
import tornadofx.item
class SQLElementCell(
class SchemaElementCell(
renameElement: (element: Schema, name: String) -> Schema,
deleteElement: (element: Schema) -> Unit
) : TextFieldTreeCell<Schema>() {
@@ -45,7 +45,7 @@ class SQLElementCell(
}
init {
converter = SQLElementStringConverter(this, renameElement)
converter = SchemaElementStringConverter(this, renameElement)
}
override fun updateItem(item: Schema?, empty: Boolean) {
@@ -58,7 +58,7 @@ class SQLElementCell(
}
text = when (item) {
is SchemaColumn -> "${item.name}${if (item.nullable) "?" else ""}"
is SchemaColumn -> "${item.name}: ${item.rawType}${if (item.nullable) "?" else ""}"
else -> item.name
}

View File

@@ -4,7 +4,7 @@ import com.bartlomiejpluta.base.editor.database.model.schema.Schema
import javafx.scene.control.TreeCell
import javafx.util.StringConverter
class SQLElementStringConverter(
class SchemaElementStringConverter(
private val cell: TreeCell<Schema>,
private val rename: (item: Schema, newName: String) -> Schema
) : StringConverter<Schema>() {

View File

@@ -6,10 +6,12 @@ import java.sql.Connection
class SchemaColumn(
val table: SchemaTable,
name: String,
val type: ColumnType,
val rawType: String,
val nullable: Boolean,
val primary: Boolean
) : Schema {
val type = ColumnType.valueOf(rawType.replace(" ", "_").substringBefore("("))
override var name: String = name
private set(value) {
field = value.toUpperCase()
@@ -25,7 +27,7 @@ class SchemaColumn(
table.columns.remove(this)
}
override val icon = when (type) {
override val icon = if (primary) FontIcon("fa-key") else when (type) {
ColumnType.INTEGER -> FontIcon("fa-hashtag")
ColumnType.BOOLEAN -> FontIcon("fa-toggle-on")
ColumnType.TINYINT -> FontIcon("fa-hashtag")

View File

@@ -12,7 +12,7 @@ class SchemaTable(val database: SchemaDatabase, name: String) : Schema {
val columns = observableListOf<SchemaColumn>()
fun addColumn(name: String, type: ColumnType, nullable: Boolean, primary: Boolean) {
fun addColumn(name: String, type: String, nullable: Boolean, primary: Boolean) {
val column = SchemaColumn(this, name, type, nullable, primary)
columns += column
}

View File

@@ -3,7 +3,6 @@ package com.bartlomiejpluta.base.editor.database.service
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.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
@@ -31,7 +30,7 @@ class H2DatabaseService : DatabaseService {
while (results.next()) {
table.addColumn(
results.getString("FIELD"),
parseType(results.getString("TYPE")),
results.getString("TYPE"),
results.getBoolean("NULL"),
results.getString("KEY") == "PRI"
)
@@ -49,8 +48,6 @@ class H2DatabaseService : DatabaseService {
return projectContext.project?.database?.connection?.use(op)
}
private fun parseType(type: String) = ColumnType.valueOf(type.replace(" ", "_").substringBefore("("))
override fun execute(statement: String, name: String): Query? = run {
val stmt = prepareStatement(statement).apply { execute() }
val results = stmt.resultSet

View File

@@ -1,6 +1,6 @@
package com.bartlomiejpluta.base.editor.database.view.list
import com.bartlomiejpluta.base.editor.database.component.SQLElementCell
import com.bartlomiejpluta.base.editor.database.component.SchemaElementCell
import com.bartlomiejpluta.base.editor.database.controller.DatabaseController
import com.bartlomiejpluta.base.editor.database.model.*
import com.bartlomiejpluta.base.editor.database.model.schema.Schema
@@ -28,7 +28,7 @@ class TablesListView : View() {
isShowRoot = false
setCellFactory {
SQLElementCell(this@TablesListView::renameElement, this@TablesListView::deleteElement)
SchemaElementCell(this@TablesListView::renameElement, this@TablesListView::deleteElement)
}
setOnMouseClicked { event ->