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:
@@ -45,7 +45,7 @@ func (this Postgres) CompileCreate(bp *schema.Blueprint) []string {
|
||||
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))
|
||||
sql := this.GenerateSQL("? TABLE IF NOT EXISTS ? (\n?\n)", temporary, db.T(bp.GetTable()), db.L(columns))
|
||||
return []string{sql}
|
||||
}
|
||||
|
||||
@@ -144,6 +144,8 @@ func (this Postgres) GetColumnType(column *schema.ColumnDefinition) string {
|
||||
return "integer"
|
||||
case "uuid":
|
||||
return "uuid"
|
||||
case "vector":
|
||||
return this.GenerateSQL("vector(?)", column.Length)
|
||||
}
|
||||
panic("Unsupported data type: " + column.Type)
|
||||
}
|
||||
@@ -156,16 +158,14 @@ func (this Postgres) GetColumnModifier(modifier string, bp *schema.Blueprint, co
|
||||
}
|
||||
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 {
|
||||
if column.IsUseCurrent() {
|
||||
return " DEFAULT CURRENT_TIMESTAMP"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
if column.IsUseCurrent() {
|
||||
return this.GenerateSQL(" DEFAULT CURRENT_TIMESTAMP ?", v)
|
||||
}
|
||||
return this.GenerateSQL(" DEFAULT ?", v)
|
||||
case "Increment":
|
||||
if column.IsAutoIncrement() {
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"git.fsdpf.net/go/db/engine"
|
||||
"git.fsdpf.net/go/db/schema"
|
||||
sqlmock "github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type postgresTest struct {
|
||||
@@ -16,41 +14,27 @@ type postgresTest struct {
|
||||
schema schema.Schema
|
||||
}
|
||||
|
||||
var (
|
||||
tableName = "entry"
|
||||
|
||||
dropTable = "DROP TABLE IF EXISTS entry;"
|
||||
|
||||
createTable = "CREATE TABLE IF NOT EXISTS entry (" +
|
||||
"id SERIAL PRIMARY KEY," +
|
||||
"int INTEGER NOT NULL UNIQUE," +
|
||||
"float REAL NOT NULL," +
|
||||
"string VARCHAR(255) NOT NULL," +
|
||||
"time TIMESTAMP NOT NULL," +
|
||||
"bool BOOLEAN NOT NULL," +
|
||||
"bytes BYTEA NOT NULL" +
|
||||
");"
|
||||
)
|
||||
var tableName = "entry"
|
||||
|
||||
func TestPostgresSuite(t *testing.T) {
|
||||
suite.Run(t, new(postgresTest))
|
||||
}
|
||||
|
||||
func (t *postgresTest) SetupSuite() {
|
||||
db := engine.Open(map[string]engine.DBConfig{
|
||||
"test-postgres": engine.NewDBConfig("postgres",
|
||||
engine.WithHost(os.Getenv("POSTGRES_HOST")),
|
||||
engine.WithPort(os.Getenv("POSTGRES_PORT")),
|
||||
engine.WithDatabase(os.Getenv("POSTGRES_DB")),
|
||||
engine.WithUsername(os.Getenv("POSTGRES_USER")),
|
||||
engine.WithPassword(os.Getenv("POSTGRES_PASSWD")),
|
||||
),
|
||||
mockDB, mock, _ := sqlmock.New()
|
||||
|
||||
mock.ExpectQuery("SELECT.*column_name.*columns").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"column_name"}).
|
||||
AddRow("id").AddRow("int").AddRow("float").
|
||||
AddRow("string").AddRow("time").AddRow("bool").AddRow("bytes"))
|
||||
|
||||
mock.ExpectQuery("SELECT.*COUNT.*tables").
|
||||
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
|
||||
|
||||
conn := engine.Mock(map[string]engine.MockDBConfig{
|
||||
"test-postgres": {Driver: "postgres", Mock: mockDB},
|
||||
}).Connection("test-postgres")
|
||||
|
||||
t.schema = schema.GetSchemaDialect(db)
|
||||
|
||||
db.Exec(dropTable)
|
||||
db.Exec(createTable)
|
||||
t.schema = schema.GetSchemaDialect(conn)
|
||||
}
|
||||
|
||||
func (t *postgresTest) TestGetColumnListing() {
|
||||
@@ -80,14 +64,14 @@ func (t *postgresTest) TestCompileCreate() {
|
||||
sql := t.schema.CompileCreate(bp)
|
||||
|
||||
t.Equal([]string{
|
||||
"CREATE TABLE \"users\" (\n" +
|
||||
"\"id\" bigint NOT NULL BIGSERIAL PRIMARY KEY COMMENT 'ID',\n" +
|
||||
"\"enabled\" boolean NOT NULL DEFAULT true COMMENT '是否有效',\n" +
|
||||
"\"created_user\" char(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000' COMMENT '创建者',\n" +
|
||||
"\"owned_user\" char(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000' COMMENT '拥有者',\n" +
|
||||
"\"created_at\" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
|
||||
"\"updated_at\" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',\n" +
|
||||
"\"deleted_at\" timestamp without time zone NULL COMMENT '删除时间'\n" +
|
||||
"CREATE TABLE IF NOT EXISTS \"users\" (\n" +
|
||||
"\"id\" bigint NOT NULL BIGSERIAL PRIMARY KEY,\n" +
|
||||
"\"enabled\" boolean NOT NULL DEFAULT 'true',\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" +
|
||||
"\"created_at\" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,\n" +
|
||||
"\"updated_at\" timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,\n" +
|
||||
"\"deleted_at\" timestamp without time zone NULL\n" +
|
||||
")",
|
||||
}, sql)
|
||||
|
||||
@@ -105,9 +89,9 @@ func (t *postgresTest) TestCompileAdd() {
|
||||
|
||||
t.Equal([]string{
|
||||
"ALTER TABLE \"users\"\n" +
|
||||
"ADD COLUMN \"name\" varchar(50) NOT NULL COMMENT '用户名',\n" +
|
||||
"ADD COLUMN \"age\" smallint NOT NULL DEFAULT 18 COMMENT '年龄',\n" +
|
||||
"ADD COLUMN \"sex\" varchar(1) NOT NULL DEFAULT '0' COMMENT '性别'",
|
||||
"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'",
|
||||
}, sql)
|
||||
|
||||
t.T().Log(sql)
|
||||
@@ -124,7 +108,7 @@ func (t *postgresTest) TestCompileChange() {
|
||||
t.Equal([]string{
|
||||
"ALTER TABLE \"users\"\n" +
|
||||
"ALTER COLUMN \"name\" TYPE varchar(50) NOT NULL,\n" +
|
||||
"ALTER COLUMN \"age\" TYPE smallint NOT NULL DEFAULT 19 COMMENT '年龄'",
|
||||
"ALTER COLUMN \"age\" TYPE smallint NOT NULL DEFAULT '19'",
|
||||
}, sql)
|
||||
|
||||
t.T().Log(sql)
|
||||
|
||||
Reference in New Issue
Block a user