[feat] 新增 orm helper
This commit is contained in:
parent
a8d2320945
commit
1ce61a8377
30
helper/orm.go
Normal file
30
helper/orm.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package helper
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.fsdpf.net/go/contracts"
|
||||||
|
"git.fsdpf.net/go/contracts/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
// @title 创建关联模型
|
||||||
|
// @param t 关联类型
|
||||||
|
// @param m 关联模型
|
||||||
|
// @param rResource 链接父模型
|
||||||
|
// @param rField 连接父模型字段, 如 xx_id
|
||||||
|
// @param rForeignKey 关联外键, 如 id
|
||||||
|
type NewOrmRelationModel func(t contracts.RelationType, m contracts.Model, rResource, rField, rForeignKey string) contracts.Model
|
||||||
|
|
||||||
|
type NewOrmJoin func(t contracts.RelationType, res contracts.Resource, alias, rResource, rField, rForeignKey string) contracts.Join
|
||||||
|
|
||||||
|
type NewOrmCondition func(typ contracts.ConditionType, describe string) contracts.Condition
|
||||||
|
|
||||||
|
type NewOrmConditionExpr func(rResource, rField, token string, operator contracts.ConditionOperator, tType contracts.ConditionTokenType, ignoreEmptyParma bool) contracts.ConditionExpr
|
||||||
|
|
||||||
|
type NewOrmConditionFuncExpr func(rResource, rField, token, fn, fnParam, string, operator contracts.ConditionOperator, tType contracts.ConditionTokenType, ignoreEmptyParma bool) contracts.ConditionExpr
|
||||||
|
|
||||||
|
type NewOrmConditionByRes func(items []base.ResCondition) contracts.Condition
|
||||||
|
|
||||||
|
type NewOrmQueryField func(rField base.ResField, t contracts.QueryDataType, alias string, options int) contracts.QueryField
|
||||||
|
|
||||||
|
type NewOrderBy func(sql string, direction contracts.OrderByDirection) contracts.OrderBy
|
||||||
|
|
||||||
|
type NewGroupBy func(sql string) contracts.GroupBy
|
6
orm.go
6
orm.go
@ -14,6 +14,7 @@ type QueryDataType string
|
|||||||
type ConditionType string
|
type ConditionType string
|
||||||
type ConditionOperator string
|
type ConditionOperator string
|
||||||
type ConditionTokenType string
|
type ConditionTokenType string
|
||||||
|
type OrderByDirection string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
OrmExecute_Query OrmExecute = iota
|
OrmExecute_Query OrmExecute = iota
|
||||||
@ -69,6 +70,11 @@ const (
|
|||||||
QueryDataType_Json QueryDataType = "json"
|
QueryDataType_Json QueryDataType = "json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
OrderByDirection_ASC OrderByDirection = db.ORDER_ASC
|
||||||
|
OrderByDirection_DESC OrderByDirection = db.ORDER_DESC
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
QueryField_IsExpr = 1 << iota
|
QueryField_IsExpr = 1 << iota
|
||||||
QueryField_IsOmitempty
|
QueryField_IsOmitempty
|
||||||
|
@ -1 +0,0 @@
|
|||||||
package orm
|
|
@ -1,36 +0,0 @@
|
|||||||
package orm
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.fsdpf.net/go/contracts"
|
|
||||||
"git.fsdpf.net/go/db"
|
|
||||||
)
|
|
||||||
|
|
||||||
type OrderByDirection string
|
|
||||||
type OrderByType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
OrderByDirection_ASC OrderByDirection = db.ORDER_ASC
|
|
||||||
OrderByDirection_DESC OrderByDirection = db.ORDER_DESC
|
|
||||||
)
|
|
||||||
|
|
||||||
type OrderBy struct {
|
|
||||||
sql string // SQL 或 字段
|
|
||||||
direction OrderByDirection // 排序方向
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewOrderBy(sql string, params ...OrderByDirection) contracts.OrderBy {
|
|
||||||
direction := OrderByDirection_ASC
|
|
||||||
if len(params) > 0 {
|
|
||||||
direction = params[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
return OrderBy{sql, direction}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this OrderBy) ToSql() db.Expression {
|
|
||||||
return db.Raw(this.sql + " " + string(this.direction))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this OrderBy) Inject(dbBuilder *db.Builder, m contracts.Model) {
|
|
||||||
dbBuilder.OrderBy(this.ToSql())
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
package orm
|
|
Loading…
Reference in New Issue
Block a user