query

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2023 License: MIT Imports: 8 Imported by: 7

Documentation

Index

Constants

View Source
const (
	EQ   = "="
	NEQ  = "!="
	GT   = ">"
	GTE  = ">="
	LT   = "<"
	LTE  = "<="
	LIKE = "LIKE"
)

Variables

View Source
var (
	// ErrModelNotSet means you are not set the model for the domain object
	ErrModelNotSet = errors.New("model not set")
	// ErrNoResult means there is no result for current query.
	ErrNoResult = errors.New("no result")
)

Functions

func ToAnys

func ToAnys[T any](items []T) []any

ToAnys convert []T to []any ([]any)

func Transaction

func Transaction(db *sql.DB, cb func(tx Database) error) (err error)

Transaction create a transaction with auto commit support

Types

type Condition

type Condition interface {
	WhereColumn(field, operator string, value string) Condition
	OrWhereColumn(field, operator string, value string) Condition
	OrWhereNotExist(subQuery SubQuery) Condition
	OrWhereExist(subQuery SubQuery) Condition
	WhereNotExist(subQuery SubQuery) Condition
	WhereExist(subQuery SubQuery) Condition
	OrWhereNotNull(field string) Condition
	OrWhereNull(field string) Condition
	WhereNotNull(field string) Condition
	WhereNull(field string) Condition
	OrWhereRaw(raw string, items ...any) Condition
	WhereRaw(raw string, items ...any) Condition
	OrWhereNotIn(field string, items ...any) Condition
	OrWhereIn(field string, items ...any) Condition
	WhereNotIn(field string, items ...any) Condition
	WhereIn(field string, items ...any) Condition
	WhereGroup(wc ConditionGroup) Condition
	OrWhereGroup(wc ConditionGroup) Condition
	Where(field string, value ...any) Condition
	OrWhere(field string, value ...any) Condition
	WhereBetween(field string, min, max any) Condition
	WhereNotBetween(field string, min, max any) Condition
	OrWhereBetween(field string, min, max any) Condition
	OrWhereNotBetween(field string, min, max any) Condition

	WhereCondition(cond sqlCondition) Condition

	When(when When, cg ConditionGroup) Condition
	OrWhen(when When, cg ConditionGroup) Condition

	Get() []sqlCondition
	Append(cond Condition) Condition

	Clone() Condition
	Empty() bool
	Resolve(tableAlias string) (string, []any)
}

func ConditionBuilder

func ConditionBuilder() Condition

type ConditionGroup

type ConditionGroup func(builder Condition)

type Database

type Database interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
}

type DatabaseWrap

type DatabaseWrap struct {
	// contains filtered or unexported fields
}

func NewDatabaseWrap

func NewDatabaseWrap(db Database) *DatabaseWrap

func (*DatabaseWrap) ExecContext

func (d *DatabaseWrap) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*DatabaseWrap) GetDB

func (d *DatabaseWrap) GetDB() Database

func (*DatabaseWrap) QueryContext

func (d *DatabaseWrap) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

type Expr

type Expr struct {
	Value    string
	Type     exprType
	Bindings []interface{}
}

func Raw

func Raw(rawStr string, bindings ...interface{}) Expr

type KV

type KV map[string]interface{}

type PaginateMeta

type PaginateMeta struct {
	Page     int64 `json:"page"`
	PerPage  int64 `json:"per_page"`
	Total    int64 `json:"total"`
	LastPage int64 `json:"last_page"`
}

type SQLBuilder

type SQLBuilder struct {
	// contains filtered or unexported fields
}

func Builder

func Builder() SQLBuilder

func (SQLBuilder) AppendCondition

func (builder SQLBuilder) AppendCondition(cond Condition) SQLBuilder

func (SQLBuilder) Clone

func (builder SQLBuilder) Clone() SQLBuilder

func (SQLBuilder) Condition

func (builder SQLBuilder) Condition(where Condition) SQLBuilder

func (SQLBuilder) CrossJoin

func (builder SQLBuilder) CrossJoin(tableName string, on ConditionGroup) SQLBuilder

func (SQLBuilder) Fields

func (builder SQLBuilder) Fields(fields ...Expr) SQLBuilder

Fields set fields for query

func (SQLBuilder) GetFields

func (builder SQLBuilder) GetFields() []Expr

GetFields return all fields for query

func (SQLBuilder) GroupBy

func (builder SQLBuilder) GroupBy(fields ...string) SQLBuilder

func (SQLBuilder) Having

func (builder SQLBuilder) Having(closure ConditionGroup) SQLBuilder

func (SQLBuilder) InnerJoin

func (builder SQLBuilder) InnerJoin(tableName string, on ConditionGroup) SQLBuilder

func (SQLBuilder) LeftJoin

func (builder SQLBuilder) LeftJoin(tableName string, on ConditionGroup) SQLBuilder

func (SQLBuilder) Limit

func (builder SQLBuilder) Limit(limit int64) SQLBuilder

func (SQLBuilder) Merge

func (builder SQLBuilder) Merge(b2s ...SQLBuilder) SQLBuilder

func (SQLBuilder) Offset

func (builder SQLBuilder) Offset(offset int64) SQLBuilder

func (SQLBuilder) OrWhen

func (builder SQLBuilder) OrWhen(when When, cg ConditionGroup) SQLBuilder

func (SQLBuilder) OrWhere

func (builder SQLBuilder) OrWhere(field string, value ...interface{}) SQLBuilder

func (SQLBuilder) OrWhereBetween

func (builder SQLBuilder) OrWhereBetween(field string, min interface{}, max interface{}) SQLBuilder

func (SQLBuilder) OrWhereColumn

func (builder SQLBuilder) OrWhereColumn(field, operator string, value string) SQLBuilder

func (SQLBuilder) OrWhereExist

func (builder SQLBuilder) OrWhereExist(subQuery SubQuery) SQLBuilder

func (SQLBuilder) OrWhereGroup

func (builder SQLBuilder) OrWhereGroup(wc ConditionGroup) SQLBuilder

func (SQLBuilder) OrWhereIn

func (builder SQLBuilder) OrWhereIn(field string, items ...interface{}) SQLBuilder

func (SQLBuilder) OrWhereNotBetween

func (builder SQLBuilder) OrWhereNotBetween(field string, min interface{}, max interface{}) SQLBuilder

func (SQLBuilder) OrWhereNotExist

func (builder SQLBuilder) OrWhereNotExist(subQuery SubQuery) SQLBuilder

func (SQLBuilder) OrWhereNotIn

func (builder SQLBuilder) OrWhereNotIn(field string, items ...interface{}) SQLBuilder

func (SQLBuilder) OrWhereNotNull

func (builder SQLBuilder) OrWhereNotNull(field string) SQLBuilder

func (SQLBuilder) OrWhereNull

func (builder SQLBuilder) OrWhereNull(field string) SQLBuilder

func (SQLBuilder) OrWhereRaw

func (builder SQLBuilder) OrWhereRaw(raw string, items ...interface{}) SQLBuilder

func (SQLBuilder) OrderBy

func (builder SQLBuilder) OrderBy(field string, direction string) SQLBuilder

func (SQLBuilder) OrderByRaw

func (builder SQLBuilder) OrderByRaw(raw string) SQLBuilder

func (SQLBuilder) ResolveAvg

func (builder SQLBuilder) ResolveAvg(field string) (string, []interface{})

func (SQLBuilder) ResolveCount

func (builder SQLBuilder) ResolveCount() (string, []interface{})

func (SQLBuilder) ResolveDelete

func (builder SQLBuilder) ResolveDelete() (string, []interface{})

func (SQLBuilder) ResolveInsert

func (builder SQLBuilder) ResolveInsert(kvPairs KV) (string, []interface{})

func (SQLBuilder) ResolveMax

func (builder SQLBuilder) ResolveMax(field string) (string, []interface{})

func (SQLBuilder) ResolveMin

func (builder SQLBuilder) ResolveMin(field string) (string, []interface{})

func (SQLBuilder) ResolveQuery

func (builder SQLBuilder) ResolveQuery() (string, []interface{})

func (SQLBuilder) ResolveSum

func (builder SQLBuilder) ResolveSum(field string) (string, []interface{})

func (SQLBuilder) ResolveUpdate

func (builder SQLBuilder) ResolveUpdate(kvPairs KV) (string, []interface{})

func (SQLBuilder) RightJoin

func (builder SQLBuilder) RightJoin(tableName string, on ConditionGroup) SQLBuilder

func (SQLBuilder) Select

func (builder SQLBuilder) Select(fields ...interface{}) SQLBuilder

func (SQLBuilder) Table

func (builder SQLBuilder) Table(name string) SQLBuilder

func (SQLBuilder) Union

func (builder SQLBuilder) Union(b2 SubQuery, distinct bool) SQLBuilder

func (SQLBuilder) When

func (builder SQLBuilder) When(when When, cg ConditionGroup) SQLBuilder

func (SQLBuilder) Where

func (builder SQLBuilder) Where(field string, value ...interface{}) SQLBuilder

func (SQLBuilder) WhereBetween

func (builder SQLBuilder) WhereBetween(field string, min interface{}, max interface{}) SQLBuilder

func (SQLBuilder) WhereColumn

func (builder SQLBuilder) WhereColumn(field, operator string, value string) SQLBuilder

func (SQLBuilder) WhereExist

func (builder SQLBuilder) WhereExist(subQuery SubQuery) SQLBuilder

func (SQLBuilder) WhereGroup

func (builder SQLBuilder) WhereGroup(wc ConditionGroup) SQLBuilder

func (SQLBuilder) WhereIn

func (builder SQLBuilder) WhereIn(field string, items ...interface{}) SQLBuilder

func (SQLBuilder) WhereNotBetween

func (builder SQLBuilder) WhereNotBetween(field string, min interface{}, max interface{}) SQLBuilder

func (SQLBuilder) WhereNotExist

func (builder SQLBuilder) WhereNotExist(subQuery SubQuery) SQLBuilder

func (SQLBuilder) WhereNotIn

func (builder SQLBuilder) WhereNotIn(field string, items ...interface{}) SQLBuilder

func (SQLBuilder) WhereNotNull

func (builder SQLBuilder) WhereNotNull(field string) SQLBuilder

func (SQLBuilder) WhereNull

func (builder SQLBuilder) WhereNull(field string) SQLBuilder

func (SQLBuilder) WhereRaw

func (builder SQLBuilder) WhereRaw(raw string, items ...interface{}) SQLBuilder

type SubQuery

type SubQuery interface {
	ResolveQuery() (string, []any)
}

type When

type When func() bool

Jump to

Keyboard shortcuts

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