[feat] 新增 NewOrderBy 方法
This commit is contained in:
parent
1deaa426d2
commit
a02c41c650
36
support/orm/orderBy.go
Normal file
36
support/orm/orderBy.go
Normal file
@ -0,0 +1,36 @@
|
||||
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())
|
||||
}
|
Loading…
Reference in New Issue
Block a user