Skip to content

Commit 5918bda

Browse files
committed
增强 MSSQL 插件,支持不常用参数配置,改进 SQL 执行逻辑和初始化过程
1 parent 575c476 commit 5918bda

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

pkg/plugins/mssql_plugin.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"database/sql"
66
"fmt"
77
"reflect"
8+
"sync"
89
"time"
910

1011
_ "github.com/microsoft/go-mssqldb"
@@ -18,15 +19,16 @@ import (
1819

1920
// MYSQLPlugin 结构体定义 MySQL 插件
2021
type MSSQLPlugin struct {
21-
Name string
22-
AllowRemote bool
23-
Configs []Body
22+
Name string
23+
AllowRemote bool
24+
LessCommonParameters string // 不常用参数 (如: encrypt=disable;trustServerCertificate=true)
25+
Configs []Body
2426
}
2527

2628
// getConnection 创建数据库连接
2729
func (p *MSSQLPlugin) GetConnection(body *Body) (*sql.DB, error) {
28-
connString := fmt.Sprintf("server=%s;port=%d;user id=%s;password=%s;database=%s",
29-
body.Host, body.Port, body.User, body.Password, body.Database)
30+
connString := fmt.Sprintf("server=%s;port=%d;user id=%s;password=%s;database=%s;%s",
31+
body.Host, body.Port, body.User, body.Password, body.Database, p.LessCommonParameters)
3032

3133
return sql.Open("sqlserver", connString)
3234
}
@@ -98,8 +100,17 @@ func (p *MSSQLPlugin) DoSQLExecute(body *Body) (qr *QueryResult) {
98100
values := make([]interface{}, len(columns))
99101
for i, colType := range columnTypes {
100102
// 根据列的扫描类型创建对应的变量
101-
values[i] = reflect.New(colType.ScanType()).Interface()
102103
dbType := colType.DatabaseTypeName()
104+
if colType.ScanType() == nil {
105+
logger.Log1.WithFields(map[string]interface{}{
106+
"column": columns[i],
107+
"type": dbType,
108+
}).Warn("列的扫描类型为空")
109+
var v interface{}
110+
values[i] = &v
111+
continue
112+
}
113+
values[i] = reflect.New(colType.ScanType()).Interface()
103114
// 根据数据库类型创建对应的变量
104115
switch dbType {
105116
case "DECIMAL", "NUMERIC", "FLOAT", "REAL":
@@ -168,7 +179,14 @@ func NewMSSQLPlugin() *MSSQLPlugin {
168179
}
169180
}
170181

182+
var initOnce sync.Once
183+
171184
func (p *MSSQLPlugin) Init() error {
185+
initOnce.Do(func() {
186+
// 设置默认值
187+
viper.SetDefault("auth.mssql.allow_remote", false)
188+
})
189+
172190
// 定义一个变量来存储 SQL 配置
173191
var sqlConfigs []Body
174192

@@ -180,11 +198,13 @@ func (p *MSSQLPlugin) Init() error {
180198
p.Configs = sqlConfigs
181199

182200
p.AllowRemote = viper.GetBool("auth.mssql.allow_remote")
201+
p.LessCommonParameters = viper.GetString("auth.mssql.less_common_parameters")
183202

184203
logger.Log1.
185204
WithField("插件名", p.Name).
186205
WithField("配置列表", p.Configs).
187206
WithField("允许远程配置", p.AllowRemote).
207+
WithField("不常用参数", p.LessCommonParameters).
188208
Info("插件已初始化")
189209
return nil
190210
}

pkg/plugins/sql_executor_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ func TestORACLEDBPlugin_doSQLExecute(t *testing.T) {
119119
func TestMSSQLPlugin_doSQLExecute(t *testing.T) {
120120
// 创建一个 MSSQL 插件
121121
p := &plugin.MSSQLPlugin{
122-
Name: "",
123-
AllowRemote: true,
122+
Name: "",
123+
AllowRemote: true,
124+
LessCommonParameters: "encrypt=disable;trustServerCertificate=true",
124125
}
125126
// 创建一个 Body
126127
body := &plugin.Body{
@@ -145,3 +146,34 @@ func TestMSSQLPlugin_doSQLExecute(t *testing.T) {
145146

146147
require.Equal(t, "success", qr.Message)
147148
}
149+
150+
func TestMSSQLPlugin_doSQLExecute2(t *testing.T) {
151+
// 创建一个 MSSQL 插件
152+
p := &plugin.MSSQLPlugin{
153+
Name: "",
154+
AllowRemote: true,
155+
LessCommonParameters: "encrypt=disable;trustServerCertificate=true",
156+
}
157+
// 创建一个 Body
158+
body := &plugin.Body{
159+
Host: "localhost",
160+
Port: 1433,
161+
User: "sa",
162+
Password: "sa123456A",
163+
Database: "master",
164+
SQL: "SELECT * FROM AllDataTypesTest",
165+
}
166+
// 执行 SQL 查询
167+
qr := p.DoSQLExecute(body)
168+
// 断言结果
169+
require.NotNil(t, qr)
170+
require.NotNil(t, qr.Result)
171+
require.NotNil(t, qr.Columns)
172+
jsonData, err := json.Marshal(qr.Result)
173+
if err != nil {
174+
t.Error(err)
175+
}
176+
t.Log(string(jsonData))
177+
178+
require.Equal(t, "success", qr.Message)
179+
}

release_notes.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
“钉钉连接平台自动化官方互助交流群”群的钉钉群号: 109135000489
66

7+
### 2024-12-13
8+
9+
- MSSQL 插件支持 `auth.mssql.less_common_parameters` 配置, 用于配置不常用的参数。可用于兼容TLS1.0、TLS1.1等特殊情况
10+
11+
712
### 2024-12-10
813

914
- 增强数据库插件的日志输出

0 commit comments

Comments
 (0)