- 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 虚拟连接
219 lines
6.2 KiB
Go
219 lines
6.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"git.fsdpf.net/go/db"
|
|
"git.fsdpf.net/go/db/dialect/postgres"
|
|
"git.fsdpf.net/go/db/internal/sb"
|
|
"git.fsdpf.net/go/db/schema"
|
|
"git.fsdpf.net/go/db/sqlgen"
|
|
)
|
|
|
|
type Postgres struct {
|
|
db *db.Database
|
|
esg sqlgen.ExpressionSQLGenerator
|
|
}
|
|
|
|
var pgDefaultModifiers = []string{
|
|
"Nullable", "Default", "Increment", "Comment",
|
|
}
|
|
var pgSerials = []string{
|
|
"bigInteger", "integer", "smallInteger",
|
|
}
|
|
|
|
func (this Postgres) GetColumnListing(table string) ([]string, error) {
|
|
cols := []string{}
|
|
err := this.db.From(db.S("information_schema").Table("columns")).Where(
|
|
db.C("table_name").Eq(table),
|
|
db.C("table_schema").Eq(db.L("current_database()")),
|
|
).Pluck(&cols, "column_name")
|
|
return cols, err
|
|
}
|
|
|
|
func (this Postgres) TableExists(table string) (bool, error) {
|
|
result, err := this.db.From(db.S("information_schema").Table("tables")).Where(db.Ex{
|
|
"table_name": table,
|
|
"table_schema": db.L("current_database()"),
|
|
}).Count()
|
|
return result > 0, err
|
|
}
|
|
|
|
func (this Postgres) CompileCreate(bp *schema.Blueprint) []string {
|
|
temporary := db.L("CREATE")
|
|
if bp.Temporary {
|
|
temporary = db.L("CREATE TEMPORARY")
|
|
}
|
|
columns := strings.Join(this.getAddedColumns(bp), ",\n")
|
|
sql := this.GenerateSQL("? TABLE IF NOT EXISTS ? (\n?\n)", temporary, db.T(bp.GetTable()), db.L(columns))
|
|
return []string{sql}
|
|
}
|
|
|
|
func (this Postgres) 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}
|
|
}
|
|
|
|
func (this Postgres) CompileChange(bp *schema.Blueprint) []string {
|
|
columns := strings.Join(schema.PrefixArray("ALTER COLUMN", this.getChangedColumns(bp)), ",\n")
|
|
sql := this.GenerateSQL("ALTER TABLE ?\n?", db.T(bp.GetTable()), db.L(columns))
|
|
return []string{sql}
|
|
}
|
|
|
|
func (this Postgres) CompileDropColumn(bp *schema.Blueprint) []string {
|
|
columns := []string{}
|
|
for _, item := range bp.GetCommands() {
|
|
if item.Type != "dropColumn" {
|
|
continue
|
|
}
|
|
for _, col := range item.Columns {
|
|
columns = append(columns, this.GenerateSQL("DROP COLUMN ?", db.C(col)))
|
|
}
|
|
}
|
|
sql := this.GenerateSQL("ALTER TABLE ?\n?", db.T(bp.GetTable()), db.L(strings.Join(columns, ",\n")))
|
|
return []string{sql}
|
|
}
|
|
|
|
func (this Postgres) CompileDrop(bp *schema.Blueprint) []string {
|
|
return []string{this.GenerateSQL("DROP TABLE ?", db.T(bp.GetTable()))}
|
|
}
|
|
|
|
func (this Postgres) CompileDropIfExists(bp *schema.Blueprint) []string {
|
|
return []string{this.GenerateSQL("DROP TABLE IF EXISTS ?", db.T(bp.GetTable()))}
|
|
}
|
|
|
|
func (this Postgres) CompileRename(bp *schema.Blueprint) []string {
|
|
toName := ""
|
|
commands := bp.GetCommands()
|
|
if len(commands) == 0 {
|
|
panic("new table undefined")
|
|
}
|
|
toName = bp.GetCommands()[0].To
|
|
if toName == "" {
|
|
panic("new table undefined")
|
|
}
|
|
return []string{this.GenerateSQL("ALTER TABLE ? RENAME TO ?", db.T(bp.GetTable()), db.T(toName))}
|
|
}
|
|
|
|
// 修改表备注
|
|
func (this Postgres) CompileModifyComment(bp *schema.Blueprint) []string {
|
|
return []string{this.GenerateSQL("COMMENT ON TABLE ? IS ?", db.T(bp.GetTable()), db.V(bp.Comment))}
|
|
}
|
|
|
|
func (this Postgres) addModifiers(sql string, bp *schema.Blueprint, column *schema.ColumnDefinition) string {
|
|
for _, modifier := range pgDefaultModifiers {
|
|
sql += this.GetColumnModifier(modifier, bp, column)
|
|
}
|
|
return sql
|
|
}
|
|
|
|
func (this Postgres) GetColumnType(column *schema.ColumnDefinition) string {
|
|
switch column.Type {
|
|
case "char":
|
|
return this.GenerateSQL("char(?)", column.Length)
|
|
case "string":
|
|
return this.GenerateSQL("varchar(?)", column.Length)
|
|
case "text":
|
|
return "text"
|
|
case "integer":
|
|
return "integer"
|
|
case "bigInteger":
|
|
return "bigint"
|
|
case "smallInteger":
|
|
return "smallint"
|
|
case "decimal":
|
|
return this.GenerateSQL("numeric(?,?)", column.Total, column.Places)
|
|
case "boolean":
|
|
return "boolean"
|
|
case "enum":
|
|
return this.GenerateSQL("varchar(?)", column.Length) // PostgreSQL uses custom types or check constraints
|
|
case "json":
|
|
return "json"
|
|
case "binary":
|
|
return "bytea"
|
|
case "datetime":
|
|
return "timestamp without time zone"
|
|
case "timestamp":
|
|
return "timestamp with time zone"
|
|
case "date":
|
|
return "date"
|
|
case "time":
|
|
return "time"
|
|
case "year":
|
|
return "integer"
|
|
case "uuid":
|
|
return "uuid"
|
|
case "vector":
|
|
return this.GenerateSQL("vector(?)", column.Length)
|
|
}
|
|
panic("Unsupported data type: " + column.Type)
|
|
}
|
|
|
|
func (this Postgres) GetColumnModifier(modifier string, bp *schema.Blueprint, column *schema.ColumnDefinition) string {
|
|
switch modifier {
|
|
case "Nullable":
|
|
if column.IsNullable() {
|
|
return " NULL"
|
|
}
|
|
return " NOT NULL"
|
|
case "Default":
|
|
if column.IsUseCurrent() {
|
|
// PostgreSQL 不支持 ON UPDATE,忽略 def 中可能携带的 MySQL ON UPDATE 标记
|
|
return " DEFAULT CURRENT_TIMESTAMP"
|
|
}
|
|
v := column.GetDefault()
|
|
if v == nil {
|
|
return ""
|
|
}
|
|
return this.GenerateSQL(" DEFAULT ?", v)
|
|
case "Increment":
|
|
if column.IsAutoIncrement() {
|
|
switch column.Type {
|
|
case "integer":
|
|
return " SERIAL PRIMARY KEY"
|
|
case "bigInteger":
|
|
return " BIGSERIAL PRIMARY KEY"
|
|
case "smallInteger":
|
|
return " SMALLSERIAL PRIMARY KEY"
|
|
}
|
|
}
|
|
case "Comment":
|
|
// PostgreSQL handles comments separately via COMMENT ON
|
|
return ""
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (this Postgres) getAddedColumns(bp *schema.Blueprint) (columns []string) {
|
|
for _, column := range bp.GetAddedColumns() {
|
|
sql := this.GenerateSQL("? ?", db.C(column.Name), db.L(this.GetColumnType(column)))
|
|
columns = append(columns, this.addModifiers(sql, bp, column))
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this Postgres) getChangedColumns(bp *schema.Blueprint) (columns []string) {
|
|
for _, column := range bp.GetChangedColumns() {
|
|
sql := this.GenerateSQL("? TYPE ?", db.C(column.Name), db.L(this.GetColumnType(column)))
|
|
columns = append(columns, this.addModifiers(sql, bp, column))
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this Postgres) GenerateSQL(sql string, args ...any) string {
|
|
sb := sb.NewSQLBuilder(false)
|
|
this.esg.Generate(sb, db.L(sql, args...))
|
|
result, _, _ := sb.ToSQL()
|
|
return result
|
|
}
|
|
|
|
func init() {
|
|
schema.RegisterDialect("postgres", func(db *db.Database) schema.Schema {
|
|
return &Postgres{
|
|
db: db,
|
|
esg: sqlgen.NewExpressionSQLGenerator("postgres", postgres.DialectOptions()),
|
|
}
|
|
})
|
|
}
|