contracts/base/query_field.go

126 lines
2.6 KiB
Go
Raw Normal View History

2023-04-22 01:49:03 +08:00
package base
import (
"reflect"
"strings"
"unicode"
"git.fsdpf.net/go/contracts/res_type"
"git.fsdpf.net/go/db"
2024-05-09 13:15:24 +08:00
"git.fsdpf.net/go/req"
2023-04-22 01:49:03 +08:00
)
type QueryField struct {
ResField
2024-05-09 13:15:24 +08:00
typ req.RouteParamType `db:"dataType"`
alias string `db:"alias"`
isExpr bool `db:"isExpr"`
2023-04-22 01:49:03 +08:00
isOmitempty bool
ignored bool
}
func (this QueryField) Alias() string {
return this.alias
}
func (this QueryField) Ignored() bool {
return this.ignored
}
func (this QueryField) IsExpr() bool {
return this.isExpr
}
func (this QueryField) IsOmitempty() bool {
return this.isOmitempty
}
2024-05-09 13:15:24 +08:00
func (this QueryField) Type() req.RouteParamType {
2023-04-22 01:49:03 +08:00
return this.typ
}
2024-05-09 13:15:24 +08:00
func (this *QueryField) SetOptions(options byte) req.QueryField {
if options&req.IsExpr != 0 {
2023-04-22 01:49:03 +08:00
this.isExpr = true
}
2024-05-09 13:15:24 +08:00
if options&req.IsOmitempty != 0 {
2023-04-22 01:49:03 +08:00
this.isOmitempty = true
}
2024-05-09 13:15:24 +08:00
if options&req.Ignored != 0 {
2023-04-22 01:49:03 +08:00
this.ignored = true
}
return this
}
func (this QueryField) GetCodeOrAlias() string {
if this.Alias() != "" {
return this.Alias()
}
return this.Code
}
func (this *QueryField) ToSql() db.Expression {
if this.Alias() != "" {
if this.IsExpr() {
if this.GetCode() == "" {
return db.Raw("null as `" + this.Alias() + "`")
}
return db.Raw("(" + this.GetCode() + ") as `" + this.Alias() + "`")
}
return db.Raw("`" + this.GetCodeResource() + "`.`" + this.GetCode() + "` as `" + this.Alias() + "`")
}
return db.Raw("`" + this.GetCodeResource() + "`.`" + this.GetCode() + "`")
}
func (this *QueryField) ToStructField(tags ...string) reflect.StructField {
var typ reflect.Type
fCode := this.GetCodeOrAlias()
// 字段规则
if !unicode.IsLetter(rune(fCode[0])) {
panic("struct field name invalid. " + fCode)
}
fTag := `db:"` + fCode
if this.ignored {
fTag = fTag + `" json:"-"`
} else if this.isOmitempty {
fTag = fTag + `" json:"` + fCode + `,omitempty"`
} else {
fTag = fTag + `" json:"` + fCode + `"`
}
if len(tags) > 0 {
fTag = strings.Join(tags, " ") + " " + fTag
}
switch this.typ {
2024-05-09 13:15:24 +08:00
case req.ReqString:
typ = reflect.TypeOf(string(""))
2024-05-09 13:15:24 +08:00
case req.ReqNumber:
2023-04-22 01:49:03 +08:00
typ = reflect.TypeOf(res_type.ResFieldByNumber(0))
2024-05-09 13:15:24 +08:00
case req.ReqInteger:
2023-04-22 01:49:03 +08:00
typ = reflect.TypeOf(res_type.ResFieldByInteger(0))
2024-05-09 13:15:24 +08:00
case req.ReqFloat:
2023-04-22 01:49:03 +08:00
typ = reflect.TypeOf(res_type.ResFieldByFloat(0))
2024-05-09 13:15:24 +08:00
case req.ReqBool:
2023-04-22 01:49:03 +08:00
typ = reflect.TypeOf(true)
2024-05-09 13:15:24 +08:00
case req.ReqArray:
typ = reflect.TypeOf([]any{})
2024-05-09 13:15:24 +08:00
case req.ReqJson:
typ = reflect.TypeOf(map[string]any{})
2023-04-22 01:49:03 +08:00
}
return reflect.StructField{
Name: strings.ToUpper(fCode[:1]) + fCode[1:],
Tag: reflect.StructTag(fTag),
Type: typ,
}
}