Files
contracts/orm.go
T
what c3618adb02 重构: 接口层同步 ResFlags 重命名,Model.SetAuthDB 改名 SetPermission
HttpController.AuthDB()、OrmController.AuthDB() 类型从 req.ResScope 同步成 req.ResFlags。Model.SetAuthDB 改名 SetPermission(req.ResFlags),跟 req-v2 新的位标志权限体系对齐;OrmController.AuthDB() 暂不改名,范围只限 orm 这块。
2026-07-22 14:04:49 +08:00

156 lines
4.1 KiB
Go

package contracts
import (
"context"
"reflect"
"git.fsdpf.net/go/condition"
"git.fsdpf.net/go/db"
"git.fsdpf.net/go/db/exp"
"git.fsdpf.net/go/reflux"
"git.fsdpf.net/go/req"
)
type OrmExecute int
type RelationType string
const (
OrmQuery OrmExecute = iota
OrmShow
OrmStore
OrmDestroy
)
const (
OrmHasOne RelationType = "hasOne"
OrmHasMany RelationType = "hasMany"
OrmInnerJoin RelationType = "inner"
OrmLeftJoin RelationType = "left"
OrmRightJoin RelationType = "right"
)
type ModelDecorator = func(Model) Model
type Orm interface {
SetGlobalParams(g req.UserAccessor) 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
Controller() reflect.Value
Execute(OrmExecute) (reflux.R, error)
Clone(tx *db.TxDatabase) Orm
Count() int64
// Exists() bool
}
type Relation interface {
Parent() Model
AppendTo(Model)
Type() RelationType
SetType(RelationType)
SetCondition(*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.SelectDataset, m Model)
}
type Model interface {
Relation
condition.TokenValue
IsDuplicated() bool
GetCode() string
GetPrimaryKey() string
GetQueryFieldsStruct(extends ...reflect.StructField) any
GetQueryFieldsSliceStruct(fields ...reflect.StructField) any
GetAttribute() reflux.R
SetRelationAttribute(string, reflux.R) error
GetResult() reflux.R
GetResource() req.Resource
GetTransaction() *db.TxDatabase
SetQueryField(...req.QueryField)
// SetRelationResult(code string, items []reflux.R)
GetJoinsCode() []string
SetPermission(req.ResFlags)
SetOrderBy(params ...OrderBy) Model
SetGroupBy(params ...GroupBy) Model
SetJoin(Join) Model
SetLimit(uint) Model
SetOffset(uint) Model
// SetAttribute(data any) Model
SetPrimaryKey(string) Model
SetWithRecursive(QueryWithRecursive) Model
DelQueryField(string)
SelectDataset() *db.SelectDataset
SelectDatasetWithUser(u req.User) *db.SelectDataset
Query() error
Store() error
Destroy() error
Count() (int64, error)
// Exists() (bool, error)
}
type OrmController interface {
AuthDB() req.ResFlags
}
type OrderBy interface {
ToSql() exp.OrderedExpression
Inject(sd *db.SelectDataset, m Model)
}
type GroupBy interface {
ToSql() exp.ColumnListExpression
Inject(sd *db.SelectDataset, m Model)
}
type QueryWithRecursive interface {
ToQueryBuilder(*db.SelectDataset, Model) (*db.SelectDataset, any)
ToTreeData([]reflux.R) reflux.R
// RootCondition 返回锚点条件(pField = root),供外部如 Count() 在不展开递归查询的情况下复用。
RootCondition() exp.Expression
}
// @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 exp.IdentifierExpression, root any, isWithoutCondition bool, depth uint) QueryWithRecursive
type NewOrmOrderBy func(sql db.Expression, direction exp.SortDirection) OrderBy
type NewOrmGroupBy func(db.Expression) GroupBy
type NewOrmModel func(res req.Resource, code, name string) Model
type NewOrm func(res req.Resource, opts ...OrmOption) Orm
type GetOrmConditions func(categoryUuid string, opts ...condition.Option) *condition.Condition
type GetOrmOrderBy func(categoryUuid string) []OrderBy
type GetOrmGroupBy func(categoryUuid string) []GroupBy
type OrmOption func(Orm)
type WithTx func(tx *db.TxDatabase) OrmOption
type WithContext func(ctx context.Context) OrmOption
type WithUserAccessor func(ua req.UserAccessor) OrmOption