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 虚拟连接
This commit is contained in:
2026-05-20 17:52:28 +08:00
parent ac81f1ff0b
commit 21b80bdea4
36 changed files with 1131 additions and 318 deletions
+76 -23
View File
@@ -40,20 +40,33 @@ func (this DuckDB) CompileCreate(bp *schema.Blueprint) []string {
if bp.Temporary {
temporary = db.L("CREATE TEMPORARY")
}
columns := strings.Join(this.getAddedColumns(bp), ",\n")
sql := this.GenerateSQL("? TABLE ? (\n?\n)", temporary, db.T(bp.GetTable()), db.L(columns))
if comment := bp.Comment; comment != "" {
sql = sql[:len(sql)-1] + ";" + this.GenerateSQL("\nCOMMENT ON TABLE ? IS ?", db.T(bp.GetTable()), db.V(comment))
// 自增列需要先建序列
var sqls []string
for _, column := range bp.GetAddedColumns() {
if column.IsAutoIncrement() {
sqls = append(sqls, this.GenerateSQL("CREATE SEQUENCE IF NOT EXISTS ?",
db.T(this.seqName(bp.GetTable(), column.Name))))
}
}
return []string{sql}
columns := strings.Join(this.getAddedColumns(bp), ",\n")
sqls = append(sqls, this.GenerateSQL("? TABLE IF NOT EXISTS ? (\n?\n)", temporary, db.T(bp.GetTable()), db.L(columns)))
if comment := bp.Comment; comment != "" {
sqls = append(sqls, this.GenerateSQL("COMMENT ON TABLE ? IS ?", db.T(bp.GetTable()), db.V(comment)))
}
sqls = append(sqls, this.columnComments(bp.GetTable(), bp.GetAddedColumns())...)
return sqls
}
func (this DuckDB) CompileAdd(bp *schema.Blueprint) []string {
columns := strings.Join(schema.PrefixArray("ADD COLUMN", this.getAddedColumns(bp)), ",\n")
sql := this.GenerateSQL("ALTER TABLE ?\n?", db.T(bp.GetTable()), db.L(columns))
return []string{sql}
sqls := []string{sql}
sqls = append(sqls, this.columnComments(bp.GetTable(), bp.GetAddedColumns())...)
return sqls
}
func (this DuckDB) CompileChange(bp *schema.Blueprint) []string {
@@ -121,16 +134,37 @@ func (this DuckDB) CompileDropIfExists(bp *schema.Blueprint) []string {
}
func (this DuckDB) CompileRename(bp *schema.Blueprint) []string {
toName := ""
commands := bp.GetCommands()
if len(commands) == 0 {
panic("new table undefined")
}
toName = commands[0].To
toName := commands[0].To
if toName == "" {
panic("new table undefined")
}
return []string{this.GenerateSQL("ALTER TABLE ? RENAME TO ?", db.T(bp.GetTable()), db.T(toName))}
fromName := bp.GetTable()
sqls := []string{this.GenerateSQL("ALTER TABLE ? RENAME TO ?", db.T(fromName), db.T(toName))}
// 查找该表关联的序列(命名规则: {table}_{column}_seq),一并重命名并更新列 DEFAULT
var seqNames []string
_ = this.db.From(db.L("duckdb_sequences()")).
Where(db.C("sequence_name").Like(fromName+"_%")).
Pluck(&seqNames, "sequence_name")
prefix := fromName + "_"
for _, oldSeq := range seqNames {
if !strings.HasSuffix(oldSeq, "_seq") {
continue
}
colName := oldSeq[len(prefix) : len(oldSeq)-len("_seq")]
newSeq := this.seqName(toName, colName)
sqls = append(sqls, this.GenerateSQL("ALTER SEQUENCE ? RENAME TO ?", db.T(oldSeq), db.T(newSeq)))
sqls = append(sqls, this.GenerateSQL("ALTER TABLE ? ALTER COLUMN ? SET DEFAULT nextval(?)",
db.T(toName), db.C(colName), db.V(newSeq)))
}
return sqls
}
func (this DuckDB) CompileModifyComment(bp *schema.Blueprint) []string {
@@ -185,6 +219,8 @@ func (this DuckDB) GetColumnType(column *schema.ColumnDefinition) string {
return "SMALLINT"
case "uuid":
return "UUID"
case "vector":
return this.GenerateSQL("FLOAT[?]", column.Length)
}
panic("Unsupported data type: " + column.Type)
}
@@ -197,19 +233,17 @@ func (this DuckDB) GetColumnModifier(modifier string, bp *schema.Blueprint, colu
}
return " NOT NULL"
case "Default":
if column.IsUseCurrent() {
// DuckDB 不支持 ON UPDATE,忽略 def 中可能携带的 MySQL ON UPDATE 标记
return " DEFAULT CURRENT_TIMESTAMP"
}
v := column.GetDefault()
if v == nil {
if column.IsUseCurrent() {
return " DEFAULT CURRENT_TIMESTAMP"
}
return ""
}
return this.GenerateSQL(" DEFAULT ?", v)
case "Increment":
if column.IsAutoIncrement() {
// DuckDB 使用 SERIAL 或者 SEQUENCE
return " PRIMARY KEY"
}
// 自增列在 getAddedColumns 中单独处理(GENERATED ALWAYS AS IDENTITY),此处无需输出
case "Comment":
// DuckDB 支持列注释,但需要在 CREATE TABLE 后使用 COMMENT ON
return ""
@@ -217,22 +251,41 @@ func (this DuckDB) GetColumnModifier(modifier string, bp *schema.Blueprint, colu
return ""
}
func (this DuckDB) columnComments(table string, columns []*schema.ColumnDefinition) (sqls []string) {
for _, column := range columns {
if v := column.GetComment(); v != "" {
sqls = append(sqls, this.GenerateSQL("COMMENT ON COLUMN ?.? IS ?",
db.T(table), db.C(column.Name), db.V(v)))
}
}
return
}
func (this DuckDB) seqName(table, column string) string {
return table + "_" + column + "_seq"
}
func (this DuckDB) getAddedColumns(bp *schema.Blueprint) (columns []string) {
for _, column := range bp.GetAddedColumns() {
colType := this.GetColumnType(column)
// 对于自增列,使用 SERIAL 类型
var sql string
if column.IsAutoIncrement() {
var baseType string
switch column.Type {
case "bigInteger":
colType = "BIGSERIAL"
baseType = "BIGINT"
case "smallInteger":
colType = "SMALLSERIAL"
baseType = "SMALLINT"
default:
colType = "SERIAL"
baseType = "INTEGER"
}
seqName := this.seqName(bp.GetTable(), column.Name)
sql = this.GenerateSQL("? ? DEFAULT nextval(?) PRIMARY KEY",
db.C(column.Name), db.L(baseType), db.V(seqName))
} else {
sql = this.GenerateSQL("? ?", db.C(column.Name), db.L(this.GetColumnType(column)))
sql = this.addModifiers(sql, bp, column)
}
sql := this.GenerateSQL("? ?", db.C(column.Name), db.L(colType))
columns = append(columns, this.addModifiers(sql, bp, column))
columns = append(columns, sql)
}
return
}
+13 -2
View File
@@ -81,8 +81,9 @@ func (t *duckDBTest) TestCompileCreate() {
sql := t.schema.CompileCreate(bp)
t.Equal([]string{
"CREATE TABLE \"users\" (\n" +
"\"id\" BIGSERIAL NOT NULL PRIMARY KEY,\n" +
"CREATE SEQUENCE IF NOT EXISTS \"users_id_seq\"",
"CREATE TABLE IF NOT EXISTS \"users\" (\n" +
"\"id\" BIGINT DEFAULT nextval('users_id_seq') PRIMARY KEY,\n" +
"\"enabled\" BOOLEAN NOT NULL DEFAULT '1',\n" +
"\"created_user\" CHAR(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',\n" +
"\"owned_user\" CHAR(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',\n" +
@@ -90,6 +91,13 @@ func (t *duckDBTest) TestCompileCreate() {
"\"updated_at\" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n" +
"\"deleted_at\" TIMESTAMP NULL\n" +
")",
"COMMENT ON COLUMN \"users\".\"id\" IS 'ID'",
"COMMENT ON COLUMN \"users\".\"enabled\" IS '是否有效'",
"COMMENT ON COLUMN \"users\".\"created_user\" IS '创建者'",
"COMMENT ON COLUMN \"users\".\"owned_user\" IS '拥有者'",
"COMMENT ON COLUMN \"users\".\"created_at\" IS '创建时间'",
"COMMENT ON COLUMN \"users\".\"updated_at\" IS '更新时间'",
"COMMENT ON COLUMN \"users\".\"deleted_at\" IS '删除时间'",
}, sql)
t.T().Log(sql)
@@ -109,6 +117,9 @@ func (t *duckDBTest) TestCompileAdd() {
"ADD COLUMN \"name\" VARCHAR(50) NOT NULL,\n" +
"ADD COLUMN \"age\" SMALLINT NOT NULL DEFAULT '18',\n" +
"ADD COLUMN \"sex\" VARCHAR(1) NOT NULL DEFAULT '0'",
"COMMENT ON COLUMN \"users\".\"name\" IS '用户名'",
"COMMENT ON COLUMN \"users\".\"age\" IS '年龄'",
"COMMENT ON COLUMN \"users\".\"sex\" IS '性别'",
}, sql)
t.T().Log(sql)
+12 -2
View File
@@ -41,8 +41,9 @@ func Example_createTable() {
}
// Output:
// CREATE TABLE "users" (
// "id" BIGSERIAL NOT NULL PRIMARY KEY,
// CREATE SEQUENCE IF NOT EXISTS "users_id_seq"
// CREATE TABLE IF NOT EXISTS "users" (
// "id" BIGINT DEFAULT nextval('users_id_seq') PRIMARY KEY,
// "name" VARCHAR(50) NOT NULL,
// "email" VARCHAR(100) NOT NULL,
// "age" INTEGER NULL,
@@ -50,6 +51,13 @@ func Example_createTable() {
// "created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
// "updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
// )
// COMMENT ON COLUMN "users"."id" IS '用户ID'
// COMMENT ON COLUMN "users"."name" IS '用户名'
// COMMENT ON COLUMN "users"."email" IS '邮箱'
// COMMENT ON COLUMN "users"."age" IS '年龄'
// COMMENT ON COLUMN "users"."active" IS '是否激活'
// COMMENT ON COLUMN "users"."created_at" IS '创建时间'
// COMMENT ON COLUMN "users"."updated_at" IS '更新时间'
}
func Example_addColumn() {
@@ -75,6 +83,8 @@ func Example_addColumn() {
// ALTER TABLE "users"
// ADD COLUMN "phone" VARCHAR(20) NULL,
// ADD COLUMN "address" TEXT NULL
// COMMENT ON COLUMN "users"."phone" IS '电话号码'
// COMMENT ON COLUMN "users"."address" IS '地址'
}
func Example_dropColumn() {