- 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
2.0 KiB
Go
Executable File
105 lines
2.0 KiB
Go
Executable File
package schema
|
|
|
|
import (
|
|
"git.fsdpf.net/go/db"
|
|
)
|
|
|
|
type Builder struct {
|
|
db *db.Database // The database connection instance
|
|
schema Schema // The schema grammar instance
|
|
}
|
|
|
|
func New(db *db.Database) *Builder {
|
|
return &Builder{
|
|
db: db,
|
|
schema: GetSchemaDialect(db),
|
|
}
|
|
}
|
|
|
|
// 判断数据表是否存在
|
|
func (this Builder) HasTable(table string) (bool, error) {
|
|
return this.schema.TableExists(table)
|
|
}
|
|
|
|
// 判断数据库列是否存在
|
|
func (this Builder) GetColumnListing(table string) (columns []string, err error) {
|
|
return this.schema.GetColumnListing(table)
|
|
}
|
|
|
|
// 修改表
|
|
func (this Builder) Table(table string, cb func(*Blueprint)) error {
|
|
bp := NewBlueprint(table)
|
|
cb(bp)
|
|
return this.Build(bp)
|
|
}
|
|
|
|
// 创建表
|
|
func (this Builder) Create(table string, cb func(*Blueprint)) error {
|
|
bp := NewBlueprint(table)
|
|
bp.Create()
|
|
cb(bp)
|
|
return this.Build(bp)
|
|
}
|
|
|
|
// 修改表名
|
|
func (this Builder) Rename(from, to string) error {
|
|
bp := NewBlueprint(from)
|
|
bp.Rename(to)
|
|
return this.Build(bp)
|
|
}
|
|
|
|
// 修改表备注
|
|
func (this Builder) ModifyComment(table, comment string) error {
|
|
bp := NewBlueprint(table)
|
|
bp.ModifyComment(comment)
|
|
return this.Build(bp)
|
|
}
|
|
|
|
// 删除表
|
|
func (this Builder) Drop(table string) error {
|
|
bp := NewBlueprint(table)
|
|
bp.Drop()
|
|
return this.Build(bp)
|
|
}
|
|
|
|
// 删除表, 先判断再删除
|
|
func (this Builder) DropIfExists(table string) error {
|
|
bp := NewBlueprint(table)
|
|
bp.DropIfExists()
|
|
return this.Build(bp)
|
|
}
|
|
|
|
// toSql
|
|
func (this Builder) ToSQL(bp *Blueprint) []string {
|
|
return bp.ToSql(this.schema)
|
|
}
|
|
|
|
// Build execute the blueprint to build / modify the table
|
|
func (this Builder) Build(bp *Blueprint) error {
|
|
sqls := this.ToSQL(bp)
|
|
|
|
if len(sqls) == 0 {
|
|
return nil
|
|
}
|
|
|
|
tx, err := this.db.Begin()
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// fmt.Println(strings.Join(sqls, "\n---\n"))
|
|
|
|
err = tx.Wrap(func() error {
|
|
for _, sql := range sqls {
|
|
if _, err := tx.Exec(sql); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
return err
|
|
}
|