From 33f4208f390fd6a559563387a9bec64be494b6e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=A3=E8=A8=80=E5=B0=B1=E6=98=AFSiam?= <59419979@qq.com> Date: Wed, 11 Feb 2026 15:23:46 +0800 Subject: [PATCH] Support Sphinx DESCRIBE in GetColumns Update SphinxDB.GetColumns to use Sphinx's DESCRIBE output to build column definitions instead of delegating unconditionally to MySQL. The code issues `DESCRIBE ` and parses Field/Type/Properties (with case-insensitive lookup), sets sensible defaults (Nullable="YES", no primary key, Extra from Properties) and marks indexed fields as MUL. If DESCRIBE fails or returns no rows the implementation falls back to s.MySQLDB.GetColumns. Also add a logger import and a warning when DESCRIBE returns no columns. --- internal/db/sphinx_impl.go | 63 +++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/internal/db/sphinx_impl.go b/internal/db/sphinx_impl.go index 3be7e31..6f8e915 100644 --- a/internal/db/sphinx_impl.go +++ b/internal/db/sphinx_impl.go @@ -5,6 +5,7 @@ import ( "strings" "GoNavi-Wails/internal/connection" + "GoNavi-Wails/internal/logger" ) const sphinxDefaultDatabaseName = "default" @@ -108,7 +109,67 @@ func (s *SphinxDB) GetCreateStatement(dbName, tableName string) (string, error) } func (s *SphinxDB) GetColumns(dbName, tableName string) ([]connection.ColumnDefinition, error) { - return s.MySQLDB.GetColumns(s.resolveDatabaseName(dbName), tableName) + // Sphinx 使用 DESCRIBE 语法获取索引结构 + query := fmt.Sprintf("DESCRIBE %s", tableName) + data, _, err := s.MySQLDB.Query(query) + if err != nil { + // 如果 DESCRIBE 失败,尝试使用 MySQL 的方式作为降级 + return s.MySQLDB.GetColumns(s.resolveDatabaseName(dbName), tableName) + } + + var columns []connection.ColumnDefinition + for _, row := range data { + // Sphinx DESCRIBE 返回的字段:Field, Type, Properties + fieldName := "" + if val, ok := row["Field"]; ok { + fieldName = fmt.Sprintf("%v", val) + } else if val, ok := row["field"]; ok { + fieldName = fmt.Sprintf("%v", val) + } + + fieldType := "" + if val, ok := row["Type"]; ok { + fieldType = fmt.Sprintf("%v", val) + } else if val, ok := row["type"]; ok { + fieldType = fmt.Sprintf("%v", val) + } + + properties := "" + if val, ok := row["Properties"]; ok { + properties = fmt.Sprintf("%v", val) + } else if val, ok := row["properties"]; ok { + properties = fmt.Sprintf("%v", val) + } + + if fieldName == "" { + continue + } + + col := connection.ColumnDefinition{ + Name: fieldName, + Type: fieldType, + Nullable: "YES", // Sphinx 默认字段可为空 + Key: "", // Sphinx 没有主键概念 + Default: nil, // Sphinx DESCRIBE 不返回默认值 + Extra: properties, + Comment: "", + } + + // 根据 properties 判断是否为索引字段 + if strings.Contains(strings.ToLower(properties), "indexed") { + col.Key = "MUL" + } + + columns = append(columns, col) + } + + // 如果没有获取到任何列,尝试使用 MySQL 方式 + if len(columns) == 0 { + logger.Warnf("Sphinx DESCRIBE 未返回任何列,尝试使用 MySQL 方式获取:表=%s", tableName) + return s.MySQLDB.GetColumns(s.resolveDatabaseName(dbName), tableName) + } + + return columns, nil } func (s *SphinxDB) GetAllColumns(dbName string) ([]connection.ColumnDefinitionWithTable, error) {