mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-05-15 20:37:52 +08:00
错误消息中文化: - 19 个驱动实现文件中 connection not open / table name required 等英文消息替换为中文 - methods_file.go / methods_db.go / methods_driver.go 英文消息中文化 - 前端 App/Sidebar/DataGrid/ConnectionModal/DriverManagerModal 同步替换 "Cancelled" → "已取消" 文档与测试: - database.go Database/BatchApplier 接口、types.go 12 个类型、driver_support.go 导出函数补充中文 godoc - 新增 logger_test.go(ErrorChain 5 个用例)和 methods_db_conn_test.go(连接管理 7 个用例) Bug 修复: - DataGrid: 将 liveTargets 空检查移至非虚拟路径,修复外部横向滚动条拖动时内容不跟随 - vite.config.ts: server.host 指定 127.0.0.1,修复 IPv6 回环被拦截导致 Wails 代理 502 基础设施: - .gitignore 新增 .gemini/ 规则,tmpclaude-* 改为 **/tmpclaude-* 覆盖子目录
108 lines
3.6 KiB
Go
108 lines
3.6 KiB
Go
package db
|
||
|
||
import (
|
||
"GoNavi-Wails/internal/connection"
|
||
"fmt"
|
||
"strings"
|
||
)
|
||
|
||
// Database 定义了统一的数据源访问接口。
|
||
// 所有数据库驱动(MySQL、PostgreSQL、Oracle 等)均需实现此接口。
|
||
// 方法调用方可通过 NewDatabase 工厂函数获取对应驱动的实例。
|
||
type Database interface {
|
||
// Connect 根据连接配置建立数据库连接。
|
||
Connect(config connection.ConnectionConfig) error
|
||
// Close 关闭数据库连接并释放底层资源。
|
||
Close() error
|
||
// Ping 测试连接是否仍然可用。
|
||
Ping() error
|
||
// Query 执行查询语句,返回结果行(列名→值映射)和列名列表。
|
||
Query(query string) ([]map[string]interface{}, []string, error)
|
||
// Exec 执行非查询语句(INSERT/UPDATE/DELETE 等),返回受影响行数。
|
||
Exec(query string) (int64, error)
|
||
// GetDatabases 返回当前连接可访问的数据库列表。
|
||
GetDatabases() ([]string, error)
|
||
// GetTables 返回指定数据库下的表列表。
|
||
GetTables(dbName string) ([]string, error)
|
||
// GetCreateStatement 返回指定表的建表 DDL 语句。
|
||
GetCreateStatement(dbName, tableName string) (string, error)
|
||
// GetColumns 返回指定表的列定义列表。
|
||
GetColumns(dbName, tableName string) ([]connection.ColumnDefinition, error)
|
||
// GetAllColumns 返回指定数据库下所有表的列定义(含表名标识)。
|
||
GetAllColumns(dbName string) ([]connection.ColumnDefinitionWithTable, error)
|
||
// GetIndexes 返回指定表的索引定义列表。
|
||
GetIndexes(dbName, tableName string) ([]connection.IndexDefinition, error)
|
||
// GetForeignKeys 返回指定表的外键定义列表。
|
||
GetForeignKeys(dbName, tableName string) ([]connection.ForeignKeyDefinition, error)
|
||
// GetTriggers 返回指定表的触发器定义列表。
|
||
GetTriggers(dbName, tableName string) ([]connection.TriggerDefinition, error)
|
||
}
|
||
|
||
// BatchApplier 定义了批量变更提交接口。
|
||
// 支持批量编辑的驱动实现此接口,用于一次性提交前端 DataGrid 中的增删改操作。
|
||
type BatchApplier interface {
|
||
// ApplyChanges 将一组变更(新增、修改、删除)批量提交到指定表。
|
||
ApplyChanges(tableName string, changes connection.ChangeSet) error
|
||
}
|
||
|
||
type databaseFactory func() Database
|
||
|
||
var databaseFactories = map[string]databaseFactory{
|
||
"mysql": func() Database {
|
||
return &MySQLDB{}
|
||
},
|
||
"postgres": func() Database {
|
||
return &PostgresDB{}
|
||
},
|
||
"oracle": func() Database {
|
||
return &OracleDB{}
|
||
},
|
||
"custom": func() Database {
|
||
return &CustomDB{}
|
||
},
|
||
}
|
||
|
||
func init() {
|
||
registerOptionalDatabaseFactories()
|
||
}
|
||
|
||
func registerDatabaseFactory(factory databaseFactory, dbTypes ...string) {
|
||
if factory == nil || len(dbTypes) == 0 {
|
||
return
|
||
}
|
||
for _, dbType := range dbTypes {
|
||
normalized := normalizeDatabaseType(dbType)
|
||
if normalized == "" {
|
||
continue
|
||
}
|
||
databaseFactories[normalized] = factory
|
||
}
|
||
}
|
||
|
||
func normalizeDatabaseType(dbType string) string {
|
||
normalized := strings.ToLower(strings.TrimSpace(dbType))
|
||
switch normalized {
|
||
case "doris":
|
||
return "diros"
|
||
case "postgresql":
|
||
return "postgres"
|
||
default:
|
||
return normalized
|
||
}
|
||
}
|
||
|
||
// NewDatabase 根据数据库类型创建对应的 Database 实例。
|
||
// dbType 为数据库类型标识(如 "mysql"、"postgres"、"oracle" 等),大小写不敏感。
|
||
// 如果指定类型未注册,返回错误。
|
||
func NewDatabase(dbType string) (Database, error) {
|
||
normalized := normalizeDatabaseType(dbType)
|
||
if normalized == "" {
|
||
normalized = "mysql"
|
||
}
|
||
factory, ok := databaseFactories[normalized]
|
||
if !ok {
|
||
return nil, fmt.Errorf("不支持的数据库类型:%s", dbType)
|
||
}
|
||
return factory(), nil
|
||
}
|