contracts/orm.go

148 lines
3.8 KiB
Go
Raw Permalink Normal View History

2023-04-12 16:56:55 +08:00
package contracts
import (
"reflect"
2023-04-20 16:50:48 +08:00
2024-05-09 13:15:24 +08:00
"git.fsdpf.net/go/condition"
2023-04-12 16:56:55 +08:00
"git.fsdpf.net/go/db"
2024-05-09 13:15:24 +08:00
"git.fsdpf.net/go/req"
2023-04-12 16:56:55 +08:00
)
type OrmExecute int
type RelationType string
2023-04-20 16:50:48 +08:00
type OrderByDirection string
2023-04-12 16:56:55 +08:00
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"
)
2023-04-20 16:50:48 +08:00
const (
OrderByDirection_ASC OrderByDirection = db.ORDER_ASC
OrderByDirection_DESC OrderByDirection = db.ORDER_DESC
)
2023-04-12 16:56:55 +08:00
type Orm interface {
2024-05-09 13:15:24 +08:00
SetGlobalParams(g req.GlobalParams) Orm
2023-04-12 16:56:55 +08:00
GetModel(params ...string) Model
SetRelationModel(m Model) Model
2024-05-09 13:15:24 +08:00
SetQueryField(qf req.QueryField) error
2023-04-12 16:56:55 +08:00
SetOrderBy(params []OrderBy) error
2024-05-09 13:15:24 +08:00
SetCondition(cond *condition.Condition) error
SetController(ctr OrmController) error
2023-04-12 16:56:55 +08:00
Execute(OrmExecute) (any, error)
Clone(tx *db.Transaction) Orm
Count() int
// Exists() bool
}
2023-04-12 16:56:55 +08:00
type Relation interface {
Parent() Model
AppendTo(Model)
Type() RelationType
SetType(RelationType)
2024-05-09 13:15:24 +08:00
SetCondition(*condition.Condition) *condition.Condition
SetPrependCondition(*condition.Condition) *condition.Condition
GetCondition() *condition.Condition
2023-04-12 16:56:55 +08:00
GetRelationResource() string
GetRelationField() string
GetRelationForeignKey() string
}
type Join interface {
Relation
GetCode() string
2024-05-09 13:15:24 +08:00
GetResource() req.Resource
2023-04-12 16:56:55 +08:00
// GetDependencies(items []ResRelation, dependencies ...string) []string
Inject(dbBuilder *db.Builder, m Model)
}
type Model interface {
Relation
2024-05-09 13:15:24 +08:00
condition.TokenValue
IsDuplicated() bool
2023-04-12 16:56:55 +08:00
GetCode() string
GetPrimaryKey() string
GetQueryFieldsStruct(extends ...reflect.StructField) any
GetQueryFieldsSliceStruct(fields ...reflect.StructField) any
GetAttribute() map[string]any
GetResult() reflect.Value
2024-05-09 13:15:24 +08:00
GetResource() req.Resource
2023-04-12 16:56:55 +08:00
QueryBuilder() *db.Builder
GetTransaction() *db.Transaction
2024-05-09 13:15:24 +08:00
SetQueryField(req.QueryField)
2023-04-12 16:56:55 +08:00
SetRelationResult(code string, vItems reflect.Value)
GetJoinsCode() []string
2024-05-09 13:15:24 +08:00
SetAuthDB(req.ResAuthDB)
2023-04-12 16:56:55 +08:00
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 {
2024-05-09 13:15:24 +08:00
AuthDB() req.ResAuthDB
2023-04-12 16:56:55 +08:00
}
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
}
2024-05-07 10:15:55 +08:00
// @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
2024-05-09 13:15:24 +08:00
type NewOrmJoin func(t RelationType, res req.Resource, alias, rResource, rField, rForeignKey string) Join
2024-05-07 10:15:55 +08:00
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
2024-05-09 13:15:24 +08:00
type NewOrmModel func(res req.Resource, code, name string) Model
2024-05-07 10:15:55 +08:00
2024-05-09 13:15:24 +08:00
type NewOrm func(res req.Resource, tx *db.Transaction) Orm
2024-05-07 10:15:55 +08:00
type GetOrmConditions func(categoryUuid string, opts ...condition.Option) *condition.Condition
2024-07-04 15:49:39 +08:00
type GetOrmOrderBy func(categoryUuid string) []OrderBy
type GetOrmGroupBy func(categoryUuid string) []GroupBy