Documentation ¶
Overview ¶
Package wrapper is a thin wrapper of *xorm.Session. It aims at eliminating tedious if statements.
List can be replaced by List2:
type ListReq struct { CondA []int64 CondB []string CondC string CondD bool CondE int64 CondF *Ranger Limit int Offset int } type Model struct{} func List(ctx context.Context, req *ListReq) (int64, []*Model, error) { var sess *xorm.Session sess.Context(ctx) if len(req.CondA) != 0 { sess.Where("column_a IN ?", req.CondA) } if len(req.CondB) != 0 { sess.Where("column_b IN ?", req.CondB) } if condC := strings.TrimSpace(req.CondC); condC != "" { sess.Where("column_c LIKE ?", "%"+condC+"%") } sess.Where("column_d = ?", req.CondD) if req.CondE != 0 { sess.Where("column_e = ?", req.CondE) } if req.CondF != nil { sess.Where("column_f BETWEEN ? AND ?", req.CondF.Start, req.CondF.End) } var models []*Model count, err := sess. Desc("id"). Limit(req.Limit, req.Offset). FindAndCount(&models) if err != nil { return 0, nil, err } return count, models, nil } func List2(ctx context.Context, req *ListReq) (int64, []*Model, error) { sess := NewSession((*xorm.Session)(nil)) var models []*Model count, err := sess.Context(ctx). In("column_a", req.CondA). In("column_b", req.CondB). Like("column_c", req.CondC). Where("column_d = ?", req.CondD). Equal("column_e", req.CondE). Between("column_f", req.CondF). Desc("id"). Limit(req.Limit, req.Offset). FindAndCount(&models) if err != nil { return 0, nil, err } return count, models, nil }
Index ¶
- type Ranger
- type Session
- func (s Session) After(x0 func(any)) *Session
- func (s Session) Alias(x0 string) *Session
- func (s Session) AllCols() *Session
- func (s Session) And(x0 any, x1 ...any) *Session
- func (s Session) Asc(x0 ...string) *Session
- func (s Session) Before(x0 func(any)) *Session
- func (s *Session) Between(column string, ranger *Ranger) *Session
- func (s Session) BufferSize(x0 int) *Session
- func (s Session) Cascade(x0 ...bool) *Session
- func (s Session) Charset(x0 string) *Session
- func (s Session) Cols(x0 ...string) *Session
- func (s Session) Context(x0 context.Context) *Session
- func (s Session) ContextCache(x0 contexts.ContextCache) *Session
- func (s Session) Decr(x0 string, x1 ...any) *Session
- func (s Session) Desc(x0 ...string) *Session
- func (s Session) Distinct(x0 ...string) *Session
- func (s *Session) Equal(column string, val any) *Session
- func (s Session) ForUpdate() *Session
- func (s Session) GroupBy(x0 string) *Session
- func (s Session) Having(x0 string) *Session
- func (s Session) ID(x0 any) *Session
- func (s *Session) In(column string, values ...any) *Session
- func (s Session) Incr(x0 string, x1 ...any) *Session
- func (s Session) IndexHint(x0 string, x1 string, x2 string) *Session
- func (s Session) Join(x0 string, x1 any, x2 any, x3 ...any) *Session
- func (s *Session) Like(column string, val string) *Session
- func (s Session) Limit(x0 int, x1 ...int) *Session
- func (s Session) MustCols(x0 ...string) *Session
- func (s Session) MustLogSQL(x0 ...bool) *Session
- func (s Session) NoAutoCondition(x0 ...bool) *Session
- func (s Session) NoAutoTime() *Session
- func (s Session) NoCache() *Session
- func (s Session) NoCascade() *Session
- func (s Session) NoVersionCheck() *Session
- func (s Session) NotIn(x0 string, x1 ...any) *Session
- func (s Session) Nullable(x0 ...string) *Session
- func (s Session) Omit(x0 ...string) *Session
- func (s Session) Or(x0 any, x1 ...any) *Session
- func (s Session) OrderBy(x0 any, x1 ...any) *Session
- func (s Session) Prepare() *Session
- func (s Session) SQL(x0 any, x1 ...any) *Session
- func (s Session) Select(x0 string) *Session
- func (s Session) SetExpr(x0 string, x1 any) *Session
- func (s Session) StoreEngine(x0 string) *Session
- func (s Session) Table(x0 any) *Session
- func (s Session) Unscoped() *Session
- func (s Session) UseBool(x0 ...string) *Session
- func (s Session) Where(x0 any, x1 ...any) *Session
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is a thin wrapper of *xorm.Session. It overrides some methods of *xorm.Session and delegates others to the embedding *xorm.Session. It aims at eliminating tedious if statements.
func NewSession ¶
NewSession returns a new Session that wraps the embedded *xorm.Session.
func (Session) AllCols ¶
func (s Session) AllCols() *Session
AllCols overrides (*xorm.Session).AllCols method.
func (*Session) Between ¶
Between builds a `column BETWEEN ranger.Start AND ranger.End` condition if ranger is not nil.
func (Session) BufferSize ¶
BufferSize overrides (*xorm.Session).BufferSize method.
func (Session) ContextCache ¶
func (s Session) ContextCache(x0 contexts.ContextCache) *Session
ContextCache overrides (*xorm.Session).ContextCache method.
func (*Session) Equal ¶
Equal builds a `column = val` condition if val is not nil and is not the zero value.
func (Session) ForUpdate ¶
func (s Session) ForUpdate() *Session
ForUpdate overrides (*xorm.Session).ForUpdate method.
func (*Session) In ¶
In overrides (*xorm.Session).In method. If no values are given, or if the first value is nil, or if the first value is an empty slice, it does nothing. Otherwise, it delegates to (*xorm.Session).In.
func (*Session) Like ¶
Like builds a `column LIKE %val%` condition with val (strings.TrimSpace)ed. The condition is built only if the trimmed val is not an empty string.
func (Session) MustLogSQL ¶
MustLogSQL overrides (*xorm.Session).MustLogSQL method.
func (Session) NoAutoCondition ¶
NoAutoCondition overrides (*xorm.Session).NoAutoCondition method.
func (Session) NoAutoTime ¶
func (s Session) NoAutoTime() *Session
NoAutoTime overrides (*xorm.Session).NoAutoTime method.
func (Session) NoCache ¶
func (s Session) NoCache() *Session
NoCache overrides (*xorm.Session).NoCache method.
func (Session) NoCascade ¶
func (s Session) NoCascade() *Session
NoCascade overrides (*xorm.Session).NoCascade method.
func (Session) NoVersionCheck ¶
func (s Session) NoVersionCheck() *Session
NoVersionCheck overrides (*xorm.Session).NoVersionCheck method.
func (Session) Prepare ¶
func (s Session) Prepare() *Session
Prepare overrides (*xorm.Session).Prepare method.
func (Session) StoreEngine ¶
StoreEngine overrides (*xorm.Session).StoreEngine method.
func (Session) Unscoped ¶
func (s Session) Unscoped() *Session
Unscoped overrides (*xorm.Session).Unscoped method.