From 62b4905e5422c7b4ecf882a57c0b19722deb1b63 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Sun, 24 May 2026 21:43:03 +0800 Subject: [PATCH] feat: split data type into type+length columns with SearchableSelect, add Shiki SQL highlighting - Separate data type input into type dropdown (SearchableSelect with allowCustom) and length input, Navicat-style - Add comprehensive DATA_TYPE_OPTIONS for all 6 database engines, validated against official docs - Add Shiki SQL syntax highlighting to the SQL preview panel - Add i18n keys for new UI elements (typePlaceholder, length, noMatchingType) --- .../structure/TableStructureEditor.vue | 54 +++++++++-- apps/desktop/src/i18n/locales/en.ts | 3 + apps/desktop/src/i18n/locales/es.ts | 3 + apps/desktop/src/i18n/locales/zh-CN.ts | 3 + apps/desktop/src/lib/sqlHighlighter.ts | 55 ++++++++++++ .../src/lib/tableStructureEditorState.ts | 90 +++++++++++++++++++ 6 files changed, 203 insertions(+), 5 deletions(-) create mode 100644 apps/desktop/src/lib/sqlHighlighter.ts diff --git a/apps/desktop/src/components/structure/TableStructureEditor.vue b/apps/desktop/src/components/structure/TableStructureEditor.vue index 9bd99f65ff..5f9d5ba557 100644 --- a/apps/desktop/src/components/structure/TableStructureEditor.vue +++ b/apps/desktop/src/components/structure/TableStructureEditor.vue @@ -28,24 +28,44 @@ import { DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; +import { SearchableSelect } from "@/components/ui/searchable-select"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { useConnectionStore } from "@/stores/connectionStore"; +import { useTheme } from "@/composables/useTheme"; import { useToast } from "@/composables/useToast"; +import { type SqlHighlighter, createShikiSqlHighlighter } from "@/lib/sqlHighlighter"; import { type EditableStructureColumn, type EditableStructureIndex } from "@/lib/tableStructureEditorSql"; import { getTableStructureCapabilities } from "@/lib/tableStructureCapabilities"; import { buildStructureTargetLabel, + combineDataType, createColumnDrafts, createIndexDrafts, + DATA_TYPE_OPTIONS, + splitDataType, toColumnNames, } from "@/lib/tableStructureEditorState"; import type { ForeignKeyInfo, TriggerInfo } from "@/types/database"; import * as api from "@/lib/api"; const { t } = useI18n(); +const { isDark } = useTheme(); const store = useConnectionStore(); const { toast } = useToast(); +const sqlHighlighter = ref(); +onMounted(async () => { + sqlHighlighter.value = await createShikiSqlHighlighter({ + appearance: () => (isDark.value ? "dark" : "light"), + }); +}); + +const highlightedSql = computed(() => { + if (!pendingStatements.value.length) return ""; + const sql = pendingStatements.value.join("\n"); + return sqlHighlighter.value?.(sql) ?? sql; +}); + const props = defineProps<{ connectionId: string; database: string; @@ -93,6 +113,7 @@ function onIndexColResize(e: MouseEvent, col: number) { const connection = computed(() => (props.connectionId ? store.getConfig(props.connectionId) : undefined)); const databaseType = computed(() => connection.value?.db_type); const structureCapabilities = computed(() => getTableStructureCapabilities(databaseType.value)); +const dataTypeOptions = computed(() => DATA_TYPE_OPTIONS[databaseType.value ?? ""] ?? []); const indexTypesByDb: Record = { postgres: ["BTREE", "HASH", "GIST", "SPGIST", "GIN", "BRIN"], @@ -476,9 +497,12 @@ watch( {{ t("structureEditor.columnName") }} - + {{ t("structureEditor.dataType") }} + + {{ t("structureEditor.length") }} + {{ t("structureEditor.nullable") }} @@ -516,10 +540,31 @@ watch( /> + + + + @@ -891,8 +936,7 @@ watch(
{{ pendingStatements.join("\n") }}
+ v-html="highlightedSql" />
{{ t("structureEditor.noChanges") }}
diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts index 752a7aa9c5..1e7f216c48 100644 --- a/apps/desktop/src/i18n/locales/en.ts +++ b/apps/desktop/src/i18n/locales/en.ts @@ -888,6 +888,9 @@ export default { addIndex: "Add Index", columnName: "Column", dataType: "Type", + typePlaceholder: "Select or type...", + noMatchingType: "No matching type", + length: "Length", nullable: "Nullable", primaryKey: "Primary Key", defaultValue: "Default", diff --git a/apps/desktop/src/i18n/locales/es.ts b/apps/desktop/src/i18n/locales/es.ts index a24df3dd29..ba8fd02224 100644 --- a/apps/desktop/src/i18n/locales/es.ts +++ b/apps/desktop/src/i18n/locales/es.ts @@ -785,6 +785,9 @@ export default { addIndex: "Agregar índice", columnName: "Columna", dataType: "Tipo", + typePlaceholder: "Seleccionar o escribir...", + noMatchingType: "Sin coincidencias", + length: "Longitud", nullable: "Admite nulos", primaryKey: "Clave primaria", defaultValue: "Valor por defecto", diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts index 4a448540ed..6b9e060c7d 100644 --- a/apps/desktop/src/i18n/locales/zh-CN.ts +++ b/apps/desktop/src/i18n/locales/zh-CN.ts @@ -869,6 +869,9 @@ export default { addIndex: "新增索引", columnName: "字段名", dataType: "类型", + length: "长度", + typePlaceholder: "选择或输入...", + noMatchingType: "无匹配类型", nullable: "可为空", primaryKey: "主键", defaultValue: "默认值", diff --git a/apps/desktop/src/lib/sqlHighlighter.ts b/apps/desktop/src/lib/sqlHighlighter.ts new file mode 100644 index 0000000000..3a9b32171f --- /dev/null +++ b/apps/desktop/src/lib/sqlHighlighter.ts @@ -0,0 +1,55 @@ +import type { AppThemeAppearance } from "@/lib/appTheme"; + +export type SqlHighlighter = (content: string, appearance?: AppThemeAppearance) => string; + +interface ShikiSqlHighlighterOptions { + appearance: () => AppThemeAppearance; +} + +const SHIKI_THEMES = { + dark: "github-dark", + light: "github-light", +} as const; + +type ShikiHighlighter = Awaited>; + +let highlighterPromise: Promise | undefined; + +export async function createShikiSqlHighlighter( + options: ShikiSqlHighlighterOptions, +): Promise { + const highlighter = await getShikiSqlHighlighter(); + return (content, appearance = options.appearance()) => + highlighter.codeToHtml(content, { + lang: "sql", + structure: "inline", + theme: SHIKI_THEMES[appearance], + }); +} + +function getShikiSqlHighlighter(): Promise { + highlighterPromise ??= loadShikiSqlHighlighter(); + return highlighterPromise; +} + +async function loadShikiSqlHighlighter(): Promise { + const [ + { createHighlighterCore }, + { createJavaScriptRegexEngine }, + githubDark, + githubLight, + sql, + ] = await Promise.all([ + import("shiki/core"), + import("shiki/engine/javascript"), + import("shiki/themes/github-dark.mjs"), + import("shiki/themes/github-light.mjs"), + import("shiki/langs/sql.mjs"), + ]); + + return createHighlighterCore({ + engine: createJavaScriptRegexEngine(), + langs: [sql.default], + themes: [githubDark.default, githubLight.default], + }); +} diff --git a/apps/desktop/src/lib/tableStructureEditorState.ts b/apps/desktop/src/lib/tableStructureEditorState.ts index 011b9707dc..0b538decaf 100644 --- a/apps/desktop/src/lib/tableStructureEditorState.ts +++ b/apps/desktop/src/lib/tableStructureEditorState.ts @@ -1,6 +1,79 @@ import type { ColumnInfo, IndexInfo } from "../types/database.ts"; import type { EditableStructureColumn, EditableStructureIndex } from "./tableStructureEditorSql.ts"; +export const DATA_TYPE_OPTIONS: Record = { + mysql: [ + "tinyint", "smallint", "mediumint", "int", "integer", "bigint", + "float", "double", "double precision", "real", + "decimal", "numeric", + "bit", "boolean", "bool", "serial", + "char", "varchar", "tinytext", "text", "mediumtext", "longtext", + "binary", "varbinary", "tinyblob", "blob", "mediumblob", "longblob", + "enum", "set", + "date", "datetime", "timestamp", "time", "year", + "json", + "geometry", "point", "linestring", "polygon", "multipoint", "multilinestring", "multipolygon", "geometrycollection", + ], + postgres: [ + "smallint", "int2", "integer", "int", "int4", "bigint", "int8", + "smallserial", "serial", "bigserial", + "decimal", "numeric", "real", "float", "float4", "double precision", "float8", "money", + "boolean", "bool", + "char", "character", "varchar", "character varying", "text", + "bytea", + "date", "time", "time without time zone", "time with time zone", "timetz", "timestamp", "timestamp without time zone", "timestamp with time zone", "timestamptz", "interval", + "uuid", + "json", "jsonb", "xml", + "bit", "bit varying", "varbit", + "tsvector", "tsquery", + "cidr", "inet", "macaddr", "macaddr8", + "point", "line", "lseg", "box", "path", "polygon", "circle", + "int4range", "int8range", "numrange", "tsrange", "tstzrange", "daterange", + "oid", + ], + sqlite: [ + "integer", "real", "text", "blob", "numeric", + ], + sqlserver: [ + "bit", + "tinyint", "smallint", "int", "integer", "bigint", + "decimal", "numeric", "float", "real", "money", "smallmoney", + "char", "nchar", "varchar", "nvarchar", "text", "ntext", + "date", "time", "datetime", "datetime2", "smalldatetime", "datetimeoffset", "timestamp", + "binary", "varbinary", "image", + "uniqueidentifier", "xml", "sql_variant", "hierarchyid", + "geography", "geometry", + ], + oracle: [ + "number", "integer", "float", "binary_float", "binary_double", + "char", "nchar", "varchar2", "nvarchar2", + "clob", "nclob", "long", + "date", "timestamp", "timestamp with time zone", "timestamp with local time zone", + "interval year to month", "interval day to second", + "raw", "long raw", "blob", "bfile", + "boolean", "json", "vector", + "rowid", "urowid", + "xmltype", "sdo_geometry", + ], + clickhouse: [ + "Int8", "Int16", "Int32", "Int64", "Int128", "Int256", + "UInt8", "UInt16", "UInt32", "UInt64", "UInt128", "UInt256", + "Float16", "Float32", "Float64", + "Decimal", "Decimal32", "Decimal64", "Decimal128", "Decimal256", + "Bool", + "String", "FixedString", + "Date", "Date32", "DateTime", "DateTime64", + "UUID", + "IPv4", "IPv6", + "Enum8", "Enum16", + "Array", "Map", "Tuple", "Nested", + "Nullable", "LowCardinality", + "SimpleAggregateFunction", "AggregateFunction", + "Point", "Ring", "Polygon", "MultiPolygon", + "JSON", + ], +}; + export function createColumnDrafts(columns: ColumnInfo[]): EditableStructureColumn[] { return columns.map((column, index) => ({ id: `existing:${column.name}`, @@ -36,6 +109,23 @@ export function toColumnNames(columns: string[]): string { return columns.join(", "); } +export function splitDataType(raw: string): { baseType: string; params: string } { + const trimmed = raw.trim(); + const parenIdx = trimmed.indexOf("("); + if (parenIdx === -1) return { baseType: trimmed, params: "" }; + const baseType = trimmed.slice(0, parenIdx).trim(); + const params = trimmed.slice(parenIdx + 1, trimmed.lastIndexOf(")")).trim(); + return { baseType, params }; +} + +export function combineDataType(baseType: string, params: string): string { + const type = baseType.trim(); + const p = params.trim(); + if (!type) return ""; + if (!p) return type; + return `${type}(${p})`; +} + export function buildStructureTargetLabel( connectionName: string | undefined, database: string | undefined,