- 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 虚拟连接
105 lines
3.2 KiB
Go
105 lines
3.2 KiB
Go
package sqlite3
|
|
|
|
import (
|
|
"database/sql"
|
|
"regexp"
|
|
"time"
|
|
|
|
"git.fsdpf.net/go/db"
|
|
"git.fsdpf.net/go/db/exp"
|
|
gosqlite3 "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
// DriverWithIF 是注册了 IF() 函数的 SQLite3 驱动名称。
|
|
// 使用该驱动名打开连接时,可在 SQL 中直接使用 IF(condition, trueVal, falseVal)。
|
|
const DriverWithIF = "sqlite3_with_if"
|
|
|
|
func DialectOptions() *db.SQLDialectOptions {
|
|
opts := db.DefaultDialectOptions()
|
|
|
|
opts.SupportsReturn = true
|
|
opts.SupportsOrderByOnUpdate = true
|
|
opts.SupportsLimitOnUpdate = true
|
|
opts.SupportsOrderByOnDelete = true
|
|
opts.SupportsLimitOnDelete = true
|
|
opts.SupportsConflictUpdateWhere = false
|
|
opts.SupportsInsertIgnoreSyntax = true
|
|
opts.SupportsConflictTarget = true
|
|
opts.SupportsMultipleUpdateTables = false
|
|
opts.WrapCompoundsInParens = false
|
|
opts.SupportsDistinct = true // 设为 false 可全局禁止生成 DISTINCT 关键字
|
|
opts.SupportsDistinctOn = false
|
|
opts.SupportsWindowFunction = false
|
|
opts.SupportsLateral = false
|
|
|
|
opts.PlaceHolderFragment = []byte("?")
|
|
opts.IncludePlaceholderNum = false
|
|
opts.QuoteRune = '`'
|
|
opts.DefaultValuesFragment = []byte("")
|
|
opts.True = []byte("1")
|
|
opts.False = []byte("0")
|
|
opts.TimeFormat = time.RFC3339Nano
|
|
opts.BooleanOperatorLookup = map[exp.BooleanOperation][]byte{
|
|
exp.EqOp: []byte("="),
|
|
exp.NeqOp: []byte("!="),
|
|
exp.GtOp: []byte(">"),
|
|
exp.GteOp: []byte(">="),
|
|
exp.LtOp: []byte("<"),
|
|
exp.LteOp: []byte("<="),
|
|
exp.InOp: []byte("IN"),
|
|
exp.NotInOp: []byte("NOT IN"),
|
|
exp.IsOp: []byte("IS"),
|
|
exp.IsNotOp: []byte("IS NOT"),
|
|
exp.LikeOp: []byte("LIKE"),
|
|
exp.NotLikeOp: []byte("NOT LIKE"),
|
|
exp.ILikeOp: []byte("LIKE"),
|
|
exp.NotILikeOp: []byte("NOT LIKE"),
|
|
exp.RegexpLikeOp: []byte("REGEXP"),
|
|
exp.RegexpNotLikeOp: []byte("NOT REGEXP"),
|
|
exp.RegexpILikeOp: []byte("REGEXP"),
|
|
exp.RegexpNotILikeOp: []byte("NOT REGEXP"),
|
|
}
|
|
opts.UseLiteralIsBools = false
|
|
opts.BitwiseOperatorLookup = map[exp.BitwiseOperation][]byte{
|
|
exp.BitwiseOrOp: []byte("|"),
|
|
exp.BitwiseAndOp: []byte("&"),
|
|
exp.BitwiseLeftShiftOp: []byte("<<"),
|
|
exp.BitwiseRightShiftOp: []byte(">>"),
|
|
}
|
|
opts.EscapedRunes = map[rune][]byte{
|
|
'\'': []byte("''"),
|
|
}
|
|
opts.InsertIgnoreClause = []byte("INSERT OR IGNORE INTO ")
|
|
opts.ConflictFragment = []byte(" ON CONFLICT ")
|
|
opts.ConflictDoUpdateFragment = []byte(" DO UPDATE SET ")
|
|
opts.ConflictDoNothingFragment = []byte(" DO NOTHING ")
|
|
opts.ForUpdateFragment = []byte("")
|
|
opts.OfFragment = []byte("")
|
|
opts.NowaitFragment = []byte("")
|
|
return opts
|
|
}
|
|
|
|
func init() {
|
|
sql.Register(DriverWithIF, &gosqlite3.SQLiteDriver{
|
|
ConnectHook: func(conn *gosqlite3.SQLiteConn) error {
|
|
if err := conn.RegisterFunc("IF", func(cond int64, trueVal, falseVal interface{}) interface{} {
|
|
if cond != 0 {
|
|
return trueVal
|
|
}
|
|
return falseVal
|
|
}, true); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := conn.RegisterFunc("REGEXP", func(expr, item string) (bool, error) {
|
|
return regexp.MatchString(expr, item)
|
|
}, true); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
},
|
|
})
|
|
db.RegisterDialect("sqlite3", DialectOptions())
|
|
}
|