feat: 升级 samber/do v1 → v2
This commit is contained in:
+62
-54
@@ -7,24 +7,24 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.fsdpf.net/go/contracts/res_type"
|
||||
"git.fsdpf.net/go/db"
|
||||
"git.fsdpf.net/go/db/schema"
|
||||
"git.fsdpf.net/go/req"
|
||||
"github.com/google/uuid"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// 资源字段
|
||||
// ResField 资源字段
|
||||
type ResField struct {
|
||||
Uuid string `db:"uuid"`
|
||||
Name string `db:"name"`
|
||||
Code string `db:"code"`
|
||||
CodeResource string `db:"codeResource"`
|
||||
DataType req.ResDataType `db:"table_type"`
|
||||
Length string `db:"length"`
|
||||
Comment string `db:"comment"`
|
||||
Default string `db:"default"`
|
||||
Uuid string `db:"uuid" json:"uuid"`
|
||||
Name string `db:"name" json:"name"`
|
||||
Code string `db:"code" json:"code"`
|
||||
CodeResource string `db:"codeResource" json:"codeResource"`
|
||||
DataType req.ResDataType `db:"data_type" json:"data_type"`
|
||||
Length string `db:"length" json:"length"`
|
||||
Comment string `db:"comment" json:"comment"`
|
||||
Default string `db:"default" json:"default"`
|
||||
Virtual bool `db:"virtual" json:"virtual"`
|
||||
VirtualExpr string `db:"virtual_expr" json:"virtual_expr"`
|
||||
}
|
||||
|
||||
func (this ResField) ToStructField(tags ...string) reflect.StructField {
|
||||
@@ -42,7 +42,7 @@ func (this ResField) ToStructField(tags ...string) reflect.StructField {
|
||||
req.ResTimestamp, req.ResDate, req.ResDatetime:
|
||||
typ = reflect.TypeOf(string(""))
|
||||
case req.ResInteger, req.ResSmallInteger:
|
||||
typ = reflect.TypeOf(res_type.ResFieldByInteger(0))
|
||||
typ = reflect.TypeOf(int64(0))
|
||||
case req.ResDecimal:
|
||||
typ = reflect.TypeOf(float64(0))
|
||||
case req.ResBoolean:
|
||||
@@ -62,6 +62,10 @@ func (this ResField) ToStructField(tags ...string) reflect.StructField {
|
||||
}
|
||||
}
|
||||
|
||||
func (this ResField) IsVirtual() bool {
|
||||
return this.Virtual
|
||||
}
|
||||
|
||||
func (this ResField) GetCode() string {
|
||||
return this.Code
|
||||
}
|
||||
@@ -101,30 +105,16 @@ func (this ResField) GetQueryDataType() req.RouteParamType {
|
||||
}
|
||||
|
||||
func (this ResField) ToValue(v any) any {
|
||||
switch this.DataType {
|
||||
case req.ResString, req.ResText, req.ResEnum,
|
||||
req.ResTimestamp, req.ResDate, req.ResDatetime:
|
||||
return strings.Trim(cast.ToString(v), " ")
|
||||
case req.ResInteger, req.ResSmallInteger:
|
||||
return cast.ToInt(v)
|
||||
case req.ResDecimal:
|
||||
return strings.Trim(cast.ToString(v), " ")
|
||||
case req.ResBoolean:
|
||||
if v, _ := strconv.ParseBool(fmt.Sprintf("%v", v)); v {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case req.ResJson:
|
||||
if this.DataType == req.ResJson {
|
||||
if v == nil {
|
||||
if this.Default != "" && this.Default[0:1] == "[" {
|
||||
return db.Raw("'[]'")
|
||||
return db.V("[]")
|
||||
} else if this.Default != "" && this.Default[0:1] == "{" {
|
||||
return db.Raw("'{}'")
|
||||
return db.V("{}")
|
||||
} else if this.Default == "" {
|
||||
return db.Raw("'{}'")
|
||||
return db.V("{}")
|
||||
}
|
||||
|
||||
return this.Default
|
||||
return this.GetRawDefault()
|
||||
}
|
||||
|
||||
if str, ok := v.(string); ok {
|
||||
@@ -137,52 +127,61 @@ func (this ResField) ToValue(v any) any {
|
||||
panic(fmt.Sprintf("%s, 类型转换错误, %s", this.Code, err))
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Trim(cast.ToString(v), " ")
|
||||
return v
|
||||
}
|
||||
|
||||
func (this ResField) GetRawDefault(driver string) db.Expression {
|
||||
func (this ResField) GetRawDefault() db.Expression {
|
||||
if this.DataType == req.ResJson {
|
||||
if this.Default != "" && this.Default[0:1] == "[" {
|
||||
return db.Raw("'[]'")
|
||||
return db.V("[]")
|
||||
} else if this.Default != "" && this.Default[0:1] == "{" {
|
||||
return db.Raw("'{}'")
|
||||
return db.V("{}")
|
||||
} else if this.Default == "" {
|
||||
return db.Raw("'{}'")
|
||||
return db.V("{}")
|
||||
}
|
||||
} else if this.DataType == req.ResBoolean {
|
||||
if v, _ := strconv.ParseBool(this.Default); v {
|
||||
return db.Raw("'1'")
|
||||
return db.V(true)
|
||||
}
|
||||
return db.Raw("'0'")
|
||||
return db.V(false)
|
||||
}
|
||||
|
||||
if len(this.Default) > 4 && strings.ToLower(this.Default[0:4]) == "sql:" {
|
||||
sql := strings.ToLower(this.Default[4:])
|
||||
if sql == "uuid()" {
|
||||
if driver == "sqlite" {
|
||||
return db.Raw("'" + uuid.NewString() + "'")
|
||||
}
|
||||
return db.Raw("uuid()")
|
||||
}
|
||||
return db.Raw(sql)
|
||||
return db.L(this.Default[4:])
|
||||
}
|
||||
|
||||
if this.Default == "" {
|
||||
if this.GetDataType() == req.ResDate || this.GetDataType() == req.ResDatetime {
|
||||
return db.Raw("NULL")
|
||||
return db.V(nil)
|
||||
}
|
||||
return db.Raw("''")
|
||||
return db.V("")
|
||||
}
|
||||
|
||||
if strings.ToUpper(this.Default) == "CURRENT_TIMESTAMP" {
|
||||
return db.Raw(this.Default)
|
||||
return db.L(this.Default)
|
||||
}
|
||||
|
||||
return db.Raw("'" + this.Default + "'")
|
||||
return db.V(this.Default)
|
||||
}
|
||||
|
||||
func (this ResField) ToBlueprint(table *schema.Blueprint) (temp *schema.ColumnDefinition) {
|
||||
switch this.Code {
|
||||
case "id":
|
||||
return table.BigIncrements("id").AutoIncrement().Comment("ID")
|
||||
case "enabled":
|
||||
return table.Boolean("enabled").Default("1").Comment("是否有效")
|
||||
case "created_user":
|
||||
return table.Char("created_user", 36).Default("00000000-0000-0000-0000-000000000000").Comment("创建者")
|
||||
case "owned_user":
|
||||
return table.Char("owned_user", 36).Default("00000000-0000-0000-0000-000000000000").Comment("拥有者")
|
||||
case "created_at":
|
||||
return table.Timestamp("created_at").UseCurrent().Comment("创建时间")
|
||||
case "updated_at":
|
||||
return table.Timestamp("updated_at").UseCurrent().Default(db.L("ON UPDATE CURRENT_TIMESTAMP")).Comment("更新时间")
|
||||
case "deleted_at":
|
||||
return table.DateTime("deleted_at").Nullable().Comment("删除时间")
|
||||
}
|
||||
|
||||
isNull := false
|
||||
comment := this.Name
|
||||
def := any(this.Default)
|
||||
@@ -207,15 +206,15 @@ func (this ResField) ToBlueprint(table *schema.Blueprint) (temp *schema.ColumnDe
|
||||
case "integer":
|
||||
// integer 默认长度 11
|
||||
temp = table.Integer(this.Code)
|
||||
case "date", "dateTime":
|
||||
case "date", "dateTime", "timestamp":
|
||||
if this.DataType == "date" {
|
||||
temp = table.Date(this.Code)
|
||||
} else {
|
||||
temp = table.DateTime(this.Code)
|
||||
}
|
||||
|
||||
if strings.ToUpper(this.Default) == "CURRENT_TIMESTAMP" {
|
||||
def = db.Raw(this.Default)
|
||||
if strings.ToUpper(this.Default) == "SQL:CURRENT_TIMESTAMP" {
|
||||
def = db.L("CURRENT_TIMESTAMP")
|
||||
} else if def == "" {
|
||||
isNull = true
|
||||
}
|
||||
@@ -242,6 +241,15 @@ func (this ResField) ToBlueprint(table *schema.Blueprint) (temp *schema.ColumnDe
|
||||
case "text":
|
||||
temp = table.Text(this.Code)
|
||||
isNull = true
|
||||
case "vector":
|
||||
temp = table.Vector(this.Code, cast.ToInt(this.Length))
|
||||
isNull = false
|
||||
default:
|
||||
panic(fmt.Sprintf("Unknown type: %s", this.DataType))
|
||||
}
|
||||
|
||||
if this.IsVirtual() {
|
||||
temp.VirtualAs(this.VirtualExpr)
|
||||
}
|
||||
|
||||
if isNull {
|
||||
|
||||
Reference in New Issue
Block a user