Files
db/exec/query_executor.go
T
what 21b80bdea4 feat: 完善扫描器、exec 及 schema 相关功能
- exec/scanner: 用 *interface{} 替换 **json.RawMessage 扫描目标,兼容 DuckDB 返回 map[string]interface{} 的场景;新增 toJSONRawMessage 转换函数
- exec/scanner: ScanVal 支持结构体指针,通过 JSON 中间层转换(DuckDB STRUCT 列)
- exec/scanner: 将 *sql.RawBytes 和 *[]byte 的处理从 ScanValContext 移入 scanner.ScanVal
- exec/query_executor: 简化 ScanValContext,移除私有 scan 方法
- exec: 补充 scanner 级别 ScanVal 测试用例
- internal/util/reflect: 重写 SafeSetVarValue,修复非指针 src 及 nil 指针字段的 panic
- internal/util/column_map: 恢复非匿名带标签结构体字段的展开逻辑
- schema: 新增 vector 列类型支持
- engine: 补充 DuckDB 相关配置
- dialect/sqlite3/vtab: 完善虚拟表适配器
- 各方言测试改用 sqlmock 虚拟连接
2026-05-20 17:52:28 +08:00

298 lines
7.9 KiB
Go

package exec
import (
"context"
gsql "database/sql"
"reflect"
"git.fsdpf.net/go/db/internal/errors"
"git.fsdpf.net/go/db/internal/util"
)
type (
QueryExecutor struct {
hook func(result interface{}) error
de DbExecutor
err error
query string
args []interface{}
}
)
var (
errUnsupportedScanMapType = errors.New("type must be a pointer to a map when scanning into a map")
errUnsupportedScanMapsType = errors.New("type must be a pointer to a slice when scanning into a map")
errUnsupportedScanStructType = errors.New("type must be a pointer to a struct when scanning into a struct")
errUnsupportedScanStructsType = errors.New("type must be a pointer to a slice when scanning into structs")
errUnsupportedScanValsType = errors.New("type must be a pointer to a slice when scanning into vals")
errScanValPointer = errors.New("type must be a pointer when scanning into val")
errScanValNonSlice = errors.New("type cannot be a pointer to a slice when scanning into val")
)
func newQueryExecutor(de DbExecutor, err error, query string, args ...interface{}) QueryExecutor {
return QueryExecutor{de: de, err: err, query: query, args: args}
}
func (q QueryExecutor) ToSQL() (sql string, args []interface{}, err error) {
return q.query, q.args, q.err
}
func (q QueryExecutor) Exec() (gsql.Result, error) {
return q.ExecContext(context.Background())
}
func (q QueryExecutor) ExecContext(ctx context.Context) (result gsql.Result, err error) {
if q.err != nil {
return nil, q.err
}
result, err = q.de.ExecContext(ctx, q.query, q.args...)
defer func() {
if err == nil && q.hook != nil {
err = q.hook(result)
}
}()
return result, err
}
func (q QueryExecutor) Query() (*gsql.Rows, error) {
return q.QueryContext(context.Background())
}
func (q QueryExecutor) QueryContext(ctx context.Context) (result *gsql.Rows, err error) {
if q.err != nil {
return nil, q.err
}
result, err = q.de.QueryContext(ctx, q.query, q.args...)
defer func() {
if err == nil && q.hook != nil {
err = q.hook(result)
}
}()
return result, err
}
// This will execute the SQL and append results to the slice
//
// var myStructs []MyStruct
// if err := db.From("test").ScanStructs(&myStructs); err != nil{
// panic(err.Error()
// }
// //use your structs
//
// i: A pointer to a slice of structs.
func (q QueryExecutor) ScanStructs(i interface{}) error {
return q.ScanStructsContext(context.Background(), i)
}
// This will execute the SQL and append results to the slice
//
// var myStructs []MyStruct
// if err := db.From("test").ScanStructsContext(ctx, &myStructs); err != nil{
// panic(err.Error()
// }
// //use your structs
//
// i: A pointer to a slice of structs.
func (q QueryExecutor) ScanStructsContext(ctx context.Context, i interface{}) error {
scanner, err := q.ScannerContext(ctx)
if err != nil {
return err
}
defer func() { _ = scanner.Close() }()
return scanner.ScanStructs(i)
}
// This will execute the SQL and fill out the struct with the fields returned.
// This method returns a boolean value that is false if no record was found
//
// var myStruct MyStruct
// found, err := db.From("test").Limit(1).ScanStruct(&myStruct)
// if err != nil{
// panic(err.Error()
// }
// if !found{
// fmt.Println("NOT FOUND")
// }
//
// i: A pointer to a struct
func (q QueryExecutor) ScanStruct(i interface{}) (bool, error) {
return q.ScanStructContext(context.Background(), i)
}
// This will execute the SQL and fill out the struct with the fields returned.
// This method returns a boolean value that is false if no record was found
//
// var myStruct MyStruct
// found, err := db.From("test").Limit(1).ScanStructContext(ctx, &myStruct)
// if err != nil{
// panic(err.Error()
// }
// if !found{
// fmt.Println("NOT FOUND")
// }
//
// i: A pointer to a struct
func (q QueryExecutor) ScanStructContext(ctx context.Context, i interface{}) (bool, error) {
val := reflect.ValueOf(i)
if !util.IsPointer(val.Kind()) {
return false, errUnsupportedScanStructType
}
val = reflect.Indirect(val)
if !util.IsStruct(val.Kind()) {
return false, errUnsupportedScanStructType
}
scanner, err := q.ScannerContext(ctx)
if err != nil {
return false, err
}
defer func() { _ = scanner.Close() }()
if scanner.Next() {
err = scanner.ScanStruct(i)
if err != nil {
return false, err
}
return true, scanner.Err()
}
return false, scanner.Err()
}
// This will execute the SQL and append results to the slice.
//
// var ids []uint32
// if err := db.From("test").Select("id").ScanVals(&ids); err != nil{
// panic(err.Error()
// }
//
// i: Takes a pointer to a slice of primitive values.
func (q QueryExecutor) ScanVals(i interface{}) error {
return q.ScanValsContext(context.Background(), i)
}
// This will execute the SQL and append results to the slice.
//
// var ids []uint32
// if err := db.From("test").Select("id").ScanValsContext(ctx, &ids); err != nil{
// panic(err.Error()
// }
//
// i: Takes a pointer to a slice of primitive values.
func (q QueryExecutor) ScanValsContext(ctx context.Context, i interface{}) error {
scanner, err := q.ScannerContext(ctx)
if err != nil {
return err
}
defer func() { _ = scanner.Close() }()
return scanner.ScanVals(i)
}
// This will execute the SQL and set the value of the primitive. This method will return false if no record is found.
//
// var id uint32
// found, err := db.From("test").Select("id").Limit(1).ScanVal(&id)
// if err != nil{
// panic(err.Error()
// }
// if !found{
// fmt.Println("NOT FOUND")
// }
//
// i: Takes a pointer to a primitive value.
func (q QueryExecutor) ScanVal(i interface{}) (bool, error) {
return q.ScanValContext(context.Background(), i)
}
// This will execute the SQL and set the value of the primitive. This method will return false if no record is found.
//
// var id uint32
// found, err := db.From("test").Select("id").Limit(1).ScanValContext(ctx, &id)
// if err != nil{
// panic(err.Error()
// }
// if !found{
// fmt.Println("NOT FOUND")
// }
//
// i: Takes a pointer to a primitive value.
func (q QueryExecutor) ScanValContext(ctx context.Context, i interface{}) (bool, error) {
val := reflect.ValueOf(i)
if !util.IsPointer(val.Kind()) {
return false, errScanValPointer
}
val = reflect.Indirect(val)
if util.IsSlice(val.Kind()) {
switch i.(type) {
case *gsql.RawBytes: // do nothing
case *[]byte: // do nothing
case gsql.Scanner: // do nothing
default:
return false, errScanValNonSlice
}
}
scanner, err := q.ScannerContext(ctx)
if err != nil {
return false, err
}
defer func() { _ = scanner.Close() }()
if scanner.Next() {
if err = scanner.ScanVal(i); err != nil {
return false, err
}
return true, scanner.Err()
}
return false, scanner.Err()
}
func (q QueryExecutor) GetRecord() (map[string]any, error) {
return q.GetRecordContext(context.Background())
}
func (q QueryExecutor) GetRecordContext(ctx context.Context) (map[string]any, error) {
scanner, err := q.ScannerContext(ctx)
if err != nil {
return nil, err
}
defer func() { _ = scanner.Close() }()
if scanner.Next() {
return scanner.GetRecord()
}
return nil, scanner.Err()
}
func (q QueryExecutor) GetRecords() ([]map[string]any, error) {
return q.GetRecordsContext(context.Background())
}
func (q QueryExecutor) GetRecordsContext(ctx context.Context) ([]map[string]any, error) {
scanner, err := q.ScannerContext(ctx)
if err != nil {
return nil, err
}
defer func() { _ = scanner.Close() }()
return scanner.GetRecords()
}
// Scanner will return a Scanner that can be used for manually scanning rows.
func (q QueryExecutor) Scanner() (Scanner, error) {
return q.ScannerContext(context.Background())
}
// ScannerContext will return a Scanner that can be used for manually scanning rows.
func (q QueryExecutor) ScannerContext(ctx context.Context) (Scanner, error) {
rows, err := q.QueryContext(ctx)
if err != nil {
return nil, err
}
return NewScanner(rows), nil
}
func (q *QueryExecutor) Hook(hook func(dataset interface{}) error) {
q.hook = hook
}