contracts/orm.go

214 lines
5.9 KiB
Go

package contracts
import (
"reflect"
"git.fsdpf.net/go/db"
)
type OrmExecute int
type RelationType string
type ConditionType string
type ConditionOperator string
type ConditionTokenType 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 (
ConditionType_OR ConditionType = "OR"
ConditionType_AND ConditionType = "AND"
)
const (
ConditionTokenType_SQL ConditionTokenType = "sql"
ConditionTokenType_FUNC ConditionTokenType = "func"
ConditionTokenType_PARAM ConditionTokenType = "param"
ConditionTokenType_STRING ConditionTokenType = "string"
)
const (
ConditionOperator_IS_NULL ConditionOperator = "IS NULL"
ConditionOperator_IS_NOT_NULL ConditionOperator = "IS NOT NULL"
ConditionOperator_EQ ConditionOperator = "="
ConditionOperator_NE ConditionOperator = "!="
ConditionOperator_GT ConditionOperator = ">"
ConditionOperator_GE ConditionOperator = ">="
ConditionOperator_LT ConditionOperator = "<"
ConditionOperator_LE ConditionOperator = "<="
ConditionOperator_LIKE ConditionOperator = "LIKE"
ConditionOperator_NOT_LIKE ConditionOperator = "NOT LIKE"
ConditionOperator_IN ConditionOperator = "IN"
ConditionOperator_NOT_IN ConditionOperator = "NOT IN"
ConditionOperator_REGEXP ConditionOperator = "REGEXP"
ConditionOperator_NOT_REGEXP ConditionOperator = "NOT REGEXP"
)
const (
OrderByDirection_ASC OrderByDirection = db.ORDER_ASC
OrderByDirection_DESC OrderByDirection = db.ORDER_DESC
)
type Orm interface {
SetGlobalParams(g GlobalParams) Orm
GetModel(params ...string) Model
SetRelationModel(m Model) Model
SetQueryField(qf QueryField) error
SetOrderBy(params []OrderBy) error
SetCondition(cond 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
SetPrependCondition(Condition) Condition
GetCondition() Condition
GetRelationResource() string
GetRelationField() string
GetRelationForeignKey() string
}
type Join interface {
Relation
GetCode() string
GetResource() Resource
// GetDependencies(items []ResRelation, dependencies ...string) []string
Inject(dbBuilder *db.Builder, m Model)
}
type Model interface {
Relation
ModelParam
GetCode() string
GetPrimaryKey() string
GetQueryFieldsStruct(extends ...reflect.StructField) any
GetQueryFieldsSliceStruct(fields ...reflect.StructField) any
GetAttribute() map[string]any
GetResult() reflect.Value
GetResource() Resource
QueryBuilder() *db.Builder
GetTransaction() *db.Transaction
SetQueryField(QueryField)
SetRelationResult(code string, vItems reflect.Value)
GetJoinsCode() []string
SetAuthDB(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 ModelParam interface {
GetParam(k string) GlobalParams
GetGlobalParamsUser() User
}
type OrmController interface {
AuthDB() ResAuthDB
}
type Condition interface {
Type() ConditionType
IsEmpty() bool
IsNotEmpty() bool
IsAlwaysRight() bool
ToSql(ModelParam) db.Expression
AppendTo(Condition)
SetExpr(ConditionExpr) Condition
SetCondition(Condition) Condition
SetMatchPrefix(string) Condition
GetFields(operator ConditionOperator, types ...ConditionTokenType) map[string]string
GetFieldsValue(m ModelParam, isWithResource bool) map[string]any
}
type ConditionExpr interface {
GetField() string
GetFieldResource() string
GetOperator() ConditionOperator
SetMatchPrefix(string) ConditionExpr
AppendTo(Condition)
ToSql(m ModelParam) db.Expression
GetTokenName() string
GetTokenType() ConditionTokenType
GetTokenValue(ModelParam) any
GetTokenSqlValue(ModelParam) string
IsIgnoreEmptyParma(ModelParam) bool
}
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 Resource, alias, rResource, rField, rForeignKey string) Join
type NewOrmCondition func(typ ConditionType, describe string) Condition
type NewOrmConditionExpr func(rResource, rField, token string, operator ConditionOperator, tType ConditionTokenType, ignoreEmptyParma bool) ConditionExpr
type NewOrmConditionFuncExpr func(rResource, rField, token string, operator ConditionOperator, tType ConditionTokenType, ignoreEmptyParma bool, fn, fnParam string) ConditionExpr
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 Resource, code, name string) Model
type NewOrm func(res Resource, tx *db.Transaction) Orm
type GetOrmConditionByRes func(categoryUuid string, describe string) Condition