wrapper

package module
v0.0.0-...-dd5a89a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2024 License: MIT Imports: 6 Imported by: 0

README

xorm-session-wrapper

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
}

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Ranger

type Ranger struct {
	Start any
	End   any
}

Ranger defines a range with a Start and an End.

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

func NewSession(embedded *xorm.Session) *Session

NewSession returns a new Session that wraps the embedded *xorm.Session.

func (Session) After

func (s Session) After(x0 func(any)) *Session

After overrides (*xorm.Session).After method.

func (Session) Alias

func (s Session) Alias(x0 string) *Session

Alias overrides (*xorm.Session).Alias method.

func (Session) AllCols

func (s Session) AllCols() *Session

AllCols overrides (*xorm.Session).AllCols method.

func (Session) And

func (s Session) And(x0 any, x1 ...any) *Session

And overrides (*xorm.Session).And method.

func (Session) Asc

func (s Session) Asc(x0 ...string) *Session

Asc overrides (*xorm.Session).Asc method.

func (Session) Before

func (s Session) Before(x0 func(any)) *Session

Before overrides (*xorm.Session).Before method.

func (*Session) Between

func (s *Session) Between(column string, ranger *Ranger) *Session

Between builds a `column BETWEEN ranger.Start AND ranger.End` condition if ranger is not nil.

func (Session) BufferSize

func (s Session) BufferSize(x0 int) *Session

BufferSize overrides (*xorm.Session).BufferSize method.

func (Session) Cascade

func (s Session) Cascade(x0 ...bool) *Session

Cascade overrides (*xorm.Session).Cascade method.

func (Session) Charset

func (s Session) Charset(x0 string) *Session

Charset overrides (*xorm.Session).Charset method.

func (Session) Cols

func (s Session) Cols(x0 ...string) *Session

Cols overrides (*xorm.Session).Cols method.

func (Session) Context

func (s Session) Context(x0 context.Context) *Session

Context overrides (*xorm.Session).Context method.

func (Session) ContextCache

func (s Session) ContextCache(x0 contexts.ContextCache) *Session

ContextCache overrides (*xorm.Session).ContextCache method.

func (Session) Decr

func (s Session) Decr(x0 string, x1 ...any) *Session

Decr overrides (*xorm.Session).Decr method.

func (Session) Desc

func (s Session) Desc(x0 ...string) *Session

Desc overrides (*xorm.Session).Desc method.

func (Session) Distinct

func (s Session) Distinct(x0 ...string) *Session

Distinct overrides (*xorm.Session).Distinct method.

func (*Session) Equal

func (s *Session) Equal(column string, val any) *Session

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) GroupBy

func (s Session) GroupBy(x0 string) *Session

GroupBy overrides (*xorm.Session).GroupBy method.

func (Session) Having

func (s Session) Having(x0 string) *Session

Having overrides (*xorm.Session).Having method.

func (Session) ID

func (s Session) ID(x0 any) *Session

ID overrides (*xorm.Session).ID method.

func (*Session) In

func (s *Session) In(column string, values ...any) *Session

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) Incr

func (s Session) Incr(x0 string, x1 ...any) *Session

Incr overrides (*xorm.Session).Incr method.

func (Session) IndexHint

func (s Session) IndexHint(x0 string, x1 string, x2 string) *Session

IndexHint overrides (*xorm.Session).IndexHint method.

func (Session) Join

func (s Session) Join(x0 string, x1 any, x2 any, x3 ...any) *Session

Join overrides (*xorm.Session).Join method.

func (*Session) Like

func (s *Session) Like(column string, val string) *Session

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) Limit

func (s Session) Limit(x0 int, x1 ...int) *Session

Limit overrides (*xorm.Session).Limit method.

func (Session) MustCols

func (s Session) MustCols(x0 ...string) *Session

MustCols overrides (*xorm.Session).MustCols method.

func (Session) MustLogSQL

func (s Session) MustLogSQL(x0 ...bool) *Session

MustLogSQL overrides (*xorm.Session).MustLogSQL method.

func (Session) NoAutoCondition

func (s Session) NoAutoCondition(x0 ...bool) *Session

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) NotIn

func (s Session) NotIn(x0 string, x1 ...any) *Session

NotIn overrides (*xorm.Session).NotIn method.

func (Session) Nullable

func (s Session) Nullable(x0 ...string) *Session

Nullable overrides (*xorm.Session).Nullable method.

func (Session) Omit

func (s Session) Omit(x0 ...string) *Session

Omit overrides (*xorm.Session).Omit method.

func (Session) Or

func (s Session) Or(x0 any, x1 ...any) *Session

Or overrides (*xorm.Session).Or method.

func (Session) OrderBy

func (s Session) OrderBy(x0 any, x1 ...any) *Session

OrderBy overrides (*xorm.Session).OrderBy method.

func (Session) Prepare

func (s Session) Prepare() *Session

Prepare overrides (*xorm.Session).Prepare method.

func (Session) SQL

func (s Session) SQL(x0 any, x1 ...any) *Session

SQL overrides (*xorm.Session).SQL method.

func (Session) Select

func (s Session) Select(x0 string) *Session

Select overrides (*xorm.Session).Select method.

func (Session) SetExpr

func (s Session) SetExpr(x0 string, x1 any) *Session

SetExpr overrides (*xorm.Session).SetExpr method.

func (Session) StoreEngine

func (s Session) StoreEngine(x0 string) *Session

StoreEngine overrides (*xorm.Session).StoreEngine method.

func (Session) Table

func (s Session) Table(x0 any) *Session

Table overrides (*xorm.Session).Table method.

func (Session) Unscoped

func (s Session) Unscoped() *Session

Unscoped overrides (*xorm.Session).Unscoped method.

func (Session) UseBool

func (s Session) UseBool(x0 ...string) *Session

UseBool overrides (*xorm.Session).UseBool method.

func (Session) Where

func (s Session) Where(x0 any, x1 ...any) *Session

Where overrides (*xorm.Session).Where method.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL