contracts/orm.go

147 lines
3.8 KiB
Go

package contracts
import (
"reflect"
"git.fsdpf.net/go/condition"
"git.fsdpf.net/go/db"
"git.fsdpf.net/go/req"
)
type OrmExecute int
type RelationType string
type OrderByDirection string
const (
OrmExecute_Query OrmExecute = iota
OrmExecute_Show
OrmExecute_Store
OrmExecute_Destroy
)
const (
RelationType_HasOne RelationType = "hasOne"
RelationType_HasMany RelationType = "hasMany"
RelationType_Inner RelationType = "inner"
RelationType_Left RelationType = "left"
RelationType_Right RelationType = "right"
)
const (
OrderByDirection_ASC OrderByDirection = db.ORDER_ASC
OrderByDirection_DESC OrderByDirection = db.ORDER_DESC
)
type Orm interface {
SetGlobalParams(g req.GlobalParams) Orm
GetModel(params ...string) Model
SetRelationModel(m Model) Model
SetQueryField(qf req.QueryField) error
SetOrderBy(params []OrderBy) error
SetCondition(cond *condition.Condition) error
SetController(ctr OrmController) error
Execute(OrmExecute) (any, error)
Clone(tx *db.Transaction) Orm
Count() int
// Exists() bool
}
type Relation interface {
Parent() Model
AppendTo(Model)
Type() RelationType
SetType(RelationType)
SetCondition(*condition.Condition) *condition.Condition
SetPrependCondition(*condition.Condition) *condition.Condition
GetCondition() *condition.Condition
GetRelationResource() string
GetRelationField() string
GetRelationForeignKey() string
}
type Join interface {
Relation
GetCode() string
GetResource() req.Resource
// GetDependencies(items []ResRelation, dependencies ...string) []string
Inject(dbBuilder *db.Builder, m Model)
}
type Model interface {
Relation
condition.TokenValue
GetCode() string
GetPrimaryKey() string
GetQueryFieldsStruct(extends ...reflect.StructField) any
GetQueryFieldsSliceStruct(fields ...reflect.StructField) any
GetAttribute() map[string]any
GetResult() reflect.Value
GetResource() req.Resource
QueryBuilder() *db.Builder
GetTransaction() *db.Transaction
SetQueryField(req.QueryField)
SetRelationResult(code string, vItems reflect.Value)
GetJoinsCode() []string
SetAuthDB(req.ResAuthDB)
SetOrderBy(params ...OrderBy) Model
SetGroupBy(params ...GroupBy) Model
SetJoin(Join) Model
SetLimit(int) Model
SetOffset(int) Model
SetAttribute(data any) Model
SetPrimaryKey(string) Model
SetWithRecursive(QueryWithRecursive) Model
DelQueryField(string)
Query() error
Store() error
Destroy() error
Count() (int, error)
// Exists() (bool, error)
}
type OrmController interface {
AuthDB() req.ResAuthDB
}
type OrderBy interface {
ToSql() db.Expression
Inject(dbBuilder *db.Builder, m Model)
}
type GroupBy interface {
ToSql() db.Expression
Inject(dbBuilder *db.Builder, m Model)
}
type QueryWithRecursive interface {
ToQueryBuilder(*db.Builder, Model) (*db.Builder, any)
ToTreeData(any) any
}
// @title 创建关联模型
// @param t 关联类型
// @param m 关联模型
// @param rResource 链接父模型
// @param rField 连接父模型字段, 如 xx_id
// @param rForeignKey 关联外键, 如 id
type NewOrmRelation func(t RelationType, m Model, rResource, rField, rForeignKey string) Model
type NewOrmJoin func(t RelationType, res req.Resource, alias, rResource, rField, rForeignKey string) Join
type NewOrmQueryWithRecursive func(pField, cField string, root any, isWithoutCondition bool, depth int) QueryWithRecursive
type NewOrmOrderBy func(sql string, direction OrderByDirection) OrderBy
type NewOrmGroupBy func(sql string) GroupBy
type NewOrmModel func(res req.Resource, code, name string) Model
type NewOrm func(res req.Resource, tx *db.Transaction) Orm
type GetOrmConditions func(categoryUuid string, opts ...condition.Option) *condition.Condition
type GetOrmOrderBy func(categoryUuid string) []OrderBy
type GetOrmGroupBy func(categoryUuid string) []GroupBy