mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-06-21 22:14:02 +08:00
- 补齐 SQL 分类逻辑,识别 SQL Server 裸存储过程调用、RETURNING/OUTPUT、SELECT INTO 及消息块场景 - 调整多语句执行与批量写入分支,避免返回行或服务端消息被 Exec 路径吞掉 - 为 PostgreSQL、OpenGauss、Kingbase、HighGo 补充 notice 回传能力并增加回归测试
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type sqlTextNoticeHandlerSetter func(driver.Conn, func(string))
|
|
|
|
func querySQLConnWithTextNotices(ctx context.Context, conn *sql.Conn, query string, setHandler sqlTextNoticeHandlerSetter) ([]map[string]interface{}, []string, []string, error) {
|
|
if conn == nil {
|
|
return nil, nil, nil, fmt.Errorf("连接未打开")
|
|
}
|
|
if setHandler == nil {
|
|
return nil, nil, nil, fmt.Errorf("未配置消息捕获处理器")
|
|
}
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
|
|
notices := make([]string, 0, 2)
|
|
addNotice := func(text string) {
|
|
text = strings.TrimSpace(text)
|
|
if text != "" {
|
|
notices = append(notices, text)
|
|
}
|
|
}
|
|
|
|
if err := conn.Raw(func(rawConn interface{}) error {
|
|
driverConn, ok := rawConn.(driver.Conn)
|
|
if !ok {
|
|
return fmt.Errorf("底层连接类型不支持消息捕获")
|
|
}
|
|
setHandler(driverConn, addNotice)
|
|
return nil
|
|
}); err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
defer func() {
|
|
_ = conn.Raw(func(rawConn interface{}) error {
|
|
driverConn, ok := rawConn.(driver.Conn)
|
|
if ok {
|
|
setHandler(driverConn, nil)
|
|
}
|
|
return nil
|
|
})
|
|
}()
|
|
|
|
rows, err := conn.QueryContext(ctx, query)
|
|
if err != nil {
|
|
return nil, nil, append([]string(nil), notices...), err
|
|
}
|
|
defer rows.Close()
|
|
|
|
data, columns, err := scanRows(rows)
|
|
return data, columns, append([]string(nil), notices...), err
|
|
}
|