Skip to content

Commit

Permalink
为 MySQL 和 PGSQL 插件添加 ValueAsBytes 字段,增强数据处理能力,修正 PGSQL 配置解析中的键名
Browse files Browse the repository at this point in the history
  • Loading branch information
zgxkbtl committed Dec 1, 2024
1 parent 926ba84 commit 1e972c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
30 changes: 26 additions & 4 deletions pkg/plugins/mysql_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (

// MYSQLPlugin 结构体定义 MySQL 插件
type MySQLPlugin struct {
Name string
AllowRemote bool
Configs []Body
Name string
AllowRemote bool
ValueAsBytes bool
Configs []Body
}

// getConnection 创建数据库连接
Expand Down Expand Up @@ -72,6 +73,12 @@ func (p *MySQLPlugin) DoSQLExecute(body *Body) *QueryResult {
}
}

// 获取列类型
columnTypes, errCT := rows.ColumnTypes()
if errCT != nil {
logger.Log1.Warningf("获取列类型失败: %v", err)
}

// 准备结果集
var result []map[string]interface{}

Expand All @@ -92,7 +99,21 @@ func (p *MySQLPlugin) DoSQLExecute(body *Body) *QueryResult {
row := make(map[string]interface{})
for i, col := range columns {
val := values[i].(*interface{})
row[col] = *val
switch v := (*val).(type) {
// 对于[]byte类型的数据,特殊处理
case []byte:
if p.ValueAsBytes {
row[col] = v
continue
}
if columnTypes[i].DatabaseTypeName() == "DATE" {
row[col] = string(v)
} else {
row[col] = string(v)
}
default:
row[col] = v
}
}
result = append(result, row)
}
Expand Down Expand Up @@ -137,6 +158,7 @@ func (p *MySQLPlugin) Init() error {
WithField("插件名", p.Name).
WithField("配置列表", p.Configs).
WithField("允许远程配置", p.AllowRemote).
WithField("以二进制作为结果", p.ValueAsBytes).
Info("插件已初始化")
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/pgsql_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ func (p *PGSQLPlugin) Init() error {
var sqlConfigs []Body

// 解析 SQL 配置
if err := viper.UnmarshalKey("plugins.mssql", &sqlConfigs); err != nil {
if err := viper.UnmarshalKey("plugins.pgsql", &sqlConfigs); err != nil {
logger.Log1.Fatalf("解析 MSSQL 配置出错: %v", err)
}

p.Configs = sqlConfigs

p.AllowRemote = viper.GetBool("auth.mssql.allow_remote")
p.AllowRemote = viper.GetBool("auth.pgsql.allow_remote")

logger.Log1.
WithField("插件名", p.Name).
Expand Down

0 comments on commit 1e972c9

Please sign in to comment.