hey

package module
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2024 License: MIT Imports: 13 Imported by: 1

README

INSTALL

go get github.com/cd365/hey@latest

FOR EXAMPLE

Documentation

Overview

Package hey is a helper that quickly responds to the results of insert, delete, update, select sql statements. You can also use hey to quickly build sql statements.

Index

Constants

View Source
const (
	State0   = 0
	State1   = 1
	StateNo  = "NO"
	StateYes = "YES"
	StateOn  = "ON"
	StateOff = "OFF"
)
View Source
const (
	// DefaultTag Mapping of default database column name and struct tag.
	DefaultTag = "db"

	// EmptyString Empty string value.
	EmptyString = ""
)
View Source
const (
	SqlDollar = "$"
	SqlPoint  = "."
	SqlSpace  = " "
	SqlStar   = "*"

	SqlAs       = "AS"
	SqlAsc      = "ASC"
	SqlDesc     = "DESC"
	SqlUnion    = "UNION"
	SqlUnionAll = "UNION ALL"

	SqlJoinInner = "INNER JOIN"
	SqlJoinLeft  = "LEFT JOIN"
	SqlJoinRight = "RIGHT JOIN"
	SqlJoinFull  = "FULL JOIN"

	SqlAnd = "AND"
	SqlOr  = "OR"

	SqlAvg   = "AVG"
	SqlMax   = "MAX"
	SqlMin   = "MIN"
	SqlSum   = "SUM"
	SqlCount = "COUNT"

	SqlNull = "NULL"

	SqlPlaceholder   = "?"
	SqlEqual         = "="
	SqlNotEqual      = "<>"
	SqlGreater       = ">"
	SqlGreaterEqual  = ">="
	SqlLessThan      = "<"
	SqlLessThanEqual = "<="
)
View Source
const (
	AliasA = "a"
	AliasB = "b"
	AliasC = "c"
	AliasD = "d"
	AliasE = "e"
	AliasF = "f"
	AliasG = "g"
)
View Source
const (
	Id = "id"
)

Variables

View Source
var (
	DefaultConfig = &Config{
		DeleteMustUseWhere: true,
		UpdateMustUseWhere: true,
	}
)
View Source
var (
	ErrNoAffectedRows = errors.New("hey: there are no affected rows")
)

Functions

func ArgString added in v1.1.0

func ArgString(i interface{}) string

ArgString Convert SQL statement parameters into text strings.

func BasicTypeValue added in v1.1.0

func BasicTypeValue(value interface{}) interface{}

func ConcatString added in v1.1.0

func ConcatString(sss ...string) string

ConcatString concatenate string

func EvenSlice2Map added in v1.2.0

func EvenSlice2Map[K comparable](elems ...K) map[K]K

EvenSlice2Map even slice to map

func LastNotEmptyString added in v1.1.0

func LastNotEmptyString(sss []string) string

LastNotEmptyString get last not empty string, return empty string if it does not exist

func MakeCacheQueryMutexIndex added in v1.3.2

func MakeCacheQueryMutexIndex(key string, length int) int

MakeCacheQueryMutexIndex Calculate the mutex index for CacheQuery.

func MergeMap added in v1.2.0

func MergeMap[K comparable, V interface{}](elems ...map[K]V) map[K]V

MergeMap merge multiple maps

func MergeSlice added in v1.2.0

func MergeSlice[V interface{}](elems ...[]V) []V

MergeSlice merge multiple slices

func MustAffectedRows added in v1.2.0

func MustAffectedRows(affectedRows int64, err error) error

MustAffectedRows at least one row is affected

func PrepareString added in v1.3.0

func PrepareString(prepare string, args []interface{}) string

PrepareString Merge executed SQL statements and parameters.

func RemoveDuplicate

func RemoveDuplicate(dynamic ...interface{}) (result []interface{})

RemoveDuplicate remove duplicate element

func RemoveDuplicates added in v1.1.0

func RemoveDuplicates[T comparable](
	dynamic ...T,
) (result []T)

RemoveDuplicates remove duplicate element

func RemoveSliceMemberByIndex added in v1.1.1

func RemoveSliceMemberByIndex[T interface{}](indexList []int, elementList []T) []T

RemoveSliceMemberByIndex delete slice member by index

func ScanAll added in v1.3.0

func ScanAll(rows *sql.Rows, fc func(rows *sql.Rows) error) (err error)

ScanAll Iteratively scan from query results.

func ScanOne added in v1.3.0

func ScanOne(rows *sql.Rows, dest ...interface{}) error

ScanOne Scan at most once from the query results.

func ScanSliceStruct

func ScanSliceStruct(rows *sql.Rows, result interface{}, tag string) error

ScanSliceStruct Scan the query result set into the receiving object the receiving object type is *[]AnyStruct or *[]*AnyStruct.

func ScanViewMap added in v1.3.0

func ScanViewMap(rows *sql.Rows) ([]map[string]interface{}, error)

ScanViewMap Scan query result to []map[string]interface{}, view query result.

func Slice2MapNewKey added in v1.2.0

func Slice2MapNewKey[K comparable](elems []K, createKey func(v K) K) map[K]K

Slice2MapNewKey make map by slice, create key

func Slice2MapNewVal added in v1.2.0

func Slice2MapNewVal[K comparable, V interface{}](elems []K, createValue func(v K) V) map[K]V

Slice2MapNewVal make map by slice, create value

func SliceIter added in v1.2.0

func SliceIter[V interface{}](iter func(v V) V, elems []V) []V

SliceIter slice iteration

func SliceMatchMap added in v1.2.0

func SliceMatchMap[K comparable, X interface{}, Y interface{}](kx map[K]X, handle func(x X, y Y), key func(y Y) K, elems []Y)

SliceMatchMap use the `key` value of each element in `elems` to match in the map, and call `handle` if the match is successful

func SqlAlias added in v1.1.0

func SqlAlias(name string, alias string) string

SqlAlias sql alias name

func SqlPrefix added in v1.1.0

func SqlPrefix(prefix string, name string) string

SqlPrefix sql prefix name

func StructInsert added in v1.0.1

func StructInsert(object interface{}, tag string, except []string, allow []string) (fields []string, values [][]interface{})

StructInsert object should be one of struct{}, *struct{}, []struct, []*struct{}, *[]struct{}, *[]*struct{} get fields and values based on struct tag.

func StructModify

func StructModify(object interface{}, tag string, except ...string) (fields []string, values []interface{})

StructModify object should be one of struct{}, *struct{} get the fields and values that need to be modified

func StructObtain added in v1.1.0

func StructObtain(object interface{}, tag string, except ...string) (fields []string, values []interface{})

StructObtain object should be one of struct{}, *struct{} for get all fields and values

func StructUpdate added in v1.0.1

func StructUpdate(origin interface{}, latest interface{}, tag string, except ...string) (fields []string, values []interface{})

StructUpdate compare origin and latest for update

Types

type Add added in v1.0.1

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

Add for INSERT

func NewAdd added in v1.0.1

func NewAdd(way *Way) *Add

NewAdd for INSERT

func (*Add) Add added in v1.0.1

func (s *Add) Add() (int64, error)

Add execute the built SQL statement

func (*Add) Comment added in v1.0.1

func (s *Add) Comment(comment string) *Add

Comment set comment

func (*Add) Context added in v1.0.1

func (s *Add) Context(ctx context.Context) *Add

Context set context

func (*Add) Create added in v1.0.1

func (s *Add) Create(create interface{}) *Add

Create value of create should be one of struct{}, *struct{}, map[string]interface{}, []struct, []*struct{}, *[]struct{}, *[]*struct{}

func (*Add) DefaultFieldValue added in v1.1.0

func (s *Add) DefaultFieldValue(field string, value interface{}) *Add

DefaultFieldValue append default field-value for insert one or more rows

func (*Add) Except added in v1.0.1

func (s *Add) Except(except ...string) *Add

Except exclude some columns from insert one or more rows

func (*Add) FieldValue added in v1.1.0

func (s *Add) FieldValue(field string, value interface{}) *Add

FieldValue append field-value for insert one or more rows

func (*Add) Fields added in v1.1.0

func (s *Add) Fields(fields []string) *Add

Fields set fields, the current method resets the map field list and field index sequence

func (*Add) FieldsValues added in v1.1.0

func (s *Add) FieldsValues(fields []string, values [][]interface{}) *Add

FieldsValues set fields and values

func (*Add) OnConflict added in v1.1.0

func (s *Add) OnConflict(prepare string, args []interface{}) *Add

OnConflict on conflict do something

func (*Add) Returning added in v1.2.1

func (s *Add) Returning(fc func(row *sql.Row) error, returning ...string) error

Returning insert a row and return the data

func (*Add) SQL added in v1.0.1

func (s *Add) SQL() (prepare string, args []interface{})

SQL build SQL statement

func (*Add) Table added in v1.0.1

func (s *Add) Table(table string) *Add

Table set table name

func (*Add) UseResult added in v1.2.1

func (s *Add) UseResult(fc func(result sql.Result) error) *Add

UseResult provide sql.Result object

func (*Add) Values added in v1.0.1

func (s *Add) Values(values [][]interface{}) *Add

Values set values

func (*Add) ValuesSubQuery added in v1.0.1

func (s *Add) ValuesSubQuery(prepare string, args ...interface{}) *Add

ValuesSubQuery values is a query SQL statement

func (*Add) ValuesSubQueryGet added in v1.0.1

func (s *Add) ValuesSubQueryGet(get *Get) *Add

ValuesSubQueryGet values is a query SQL statement

func (*Add) Way added in v1.2.0

func (s *Add) Way() *Way

Way get current *Way

type CacheQuery added in v1.3.2

type CacheQuery interface {
	// Get Query cache data.
	Get(ctx context.Context, key string) (value interface{}, exists bool, err error)

	// Set Permanently cache data.
	Set(ctx context.Context, key string, value interface{}) error

	// SetTtl Set cache data, if the ttl(time ti live) is not a positive integer, the data should be stored for a long time or even permanently.
	SetTtl(ctx context.Context, key string, value interface{}, ttl time.Duration) error

	// Exist Cache data exist using key.
	Exist(ctx context.Context, key string) (exists bool, err error)

	// Remove Delete cache data using key.
	Remove(ctx context.Context, key string) error

	// Flush Empty the cache.
	Flush(ctx context.Context) error

	// Mutex Get a mutex lock based on the cache key.
	Mutex(key string) sync.Locker
}

CacheQuery Cache queried data.

type CacheValue added in v1.3.1

type CacheValue struct {
	// UnixMilli Cache validity deadline millisecond timestamp.
	UnixMilli int64

	// Value Cache any data.
	Value interface{}
}

CacheValue Cache data with validity deadline millisecond timestamp.

type Caller added in v1.1.0

type Caller interface {
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

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

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

Caller The implementation object is usually one of *sql.Conn, *sql.DB, *sql.Tx.

type Commander added in v1.3.0

type Commander interface {
	// SQL Get the SQL statement that can be executed and its corresponding parameter list.
	SQL() (prepare string, args []interface{})
}

type Config added in v1.1.0

type Config struct {
	// DeleteMustUseWhere Deletion of data must be filtered using conditions.
	DeleteMustUseWhere bool

	// UpdateMustUseWhere Updated data must be filtered using conditions.
	UpdateMustUseWhere bool
}

Config Configure of Way.

type Del added in v1.0.1

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

Del for DELETE

func NewDel added in v1.0.1

func NewDel(way *Way) *Del

NewDel for DELETE

func (*Del) Comment added in v1.0.1

func (s *Del) Comment(comment string) *Del

Comment set comment

func (*Del) Context added in v1.0.1

func (s *Del) Context(ctx context.Context) *Del

Context set context

func (*Del) Del added in v1.0.1

func (s *Del) Del() (int64, error)

Del execute the built SQL statement

func (*Del) F added in v1.2.0

func (s *Del) F(filter ...Filter) Filter

F make new Filter

func (*Del) SQL added in v1.0.1

func (s *Del) SQL() (prepare string, args []interface{})

SQL build SQL statement

func (*Del) Table added in v1.0.1

func (s *Del) Table(table string) *Del

Table set table name

func (*Del) Way added in v1.2.0

func (s *Del) Way() *Way

Way get current *Way

func (*Del) Where added in v1.0.1

func (s *Del) Where(where Filter) *Del

Where set where

type Filter

type Filter interface {
	Copy(filter ...Filter) Filter
	IsEmpty() bool
	And(prepare string, args ...interface{}) Filter
	Filter(filters ...Filter) Filter
	Group(group func(filter Filter)) Filter
	Equal(column string, value interface{}) Filter
	Greater(column string, value interface{}) Filter
	GreaterEqual(column string, value interface{}) Filter
	LessThan(column string, value interface{}) Filter
	LessThanEqual(column string, value interface{}) Filter
	Between(column string, start interface{}, end interface{}) Filter
	In(column string, values ...interface{}) Filter
	InQuery(column string, fc func() (prepare string, args []interface{})) Filter
	InGet(column string, cmd Commander) Filter
	InCols(columns []string, values ...[]interface{}) Filter
	InColsQuery(columns []string, fc func() (prepare string, args []interface{})) Filter
	InColsGet(columns []string, cmd Commander) Filter
	Exists(fc func() (prepare string, args []interface{})) Filter
	Like(column string, value interface{}) Filter
	IsNull(column string) Filter
	NotEqual(column string, value interface{}) Filter
	NotBetween(column string, start interface{}, end interface{}) Filter
	NotIn(column string, values ...interface{}) Filter
	NotInQuery(column string, fc func() (prepare string, args []interface{})) Filter
	NotInGet(column string, cmd Commander) Filter
	NotInCols(columns []string, values ...[]interface{}) Filter
	NotInColsQuery(columns []string, fc func() (prepare string, args []interface{})) Filter
	NotInColsGet(columns []string, cmd Commander) Filter
	NotExists(fc func() (prepare string, args []interface{})) Filter
	NotLike(column string, value interface{}) Filter
	IsNotNull(column string) Filter
	Or(prepare string, args ...interface{}) Filter
	OrFilter(filters ...Filter) Filter
	OrGroup(group func(filter Filter)) Filter
	OrEqual(column string, value interface{}) Filter
	OrGreater(column string, value interface{}) Filter
	OrGreaterEqual(column string, value interface{}) Filter
	OrLessThan(column string, value interface{}) Filter
	OrLessThanEqual(column string, value interface{}) Filter
	OrBetween(column string, start interface{}, end interface{}) Filter
	OrIn(column string, values ...interface{}) Filter
	OrInQuery(column string, fc func() (prepare string, args []interface{})) Filter
	OrInGet(column string, cmd Commander) Filter
	OrInCols(columns []string, values ...[]interface{}) Filter
	OrInColsQuery(columns []string, fc func() (prepare string, args []interface{})) Filter
	OrInColsGet(columns []string, cmd Commander) Filter
	OrExists(fc func() (prepare string, args []interface{})) Filter
	OrLike(column string, value interface{}) Filter
	OrIsNull(column string) Filter
	OrNotEqual(column string, value interface{}) Filter
	OrNotBetween(column string, start interface{}, end interface{}) Filter
	OrNotIn(column string, values ...interface{}) Filter
	OrNotInQuery(column string, fc func() (prepare string, args []interface{})) Filter
	OrNotInGet(column string, cmd Commander) Filter
	OrNotInCols(columns []string, values ...[]interface{}) Filter
	OrNotInColsQuery(columns []string, fc func() (prepare string, args []interface{})) Filter
	OrNotInColsGet(columns []string, cmd Commander) Filter
	OrNotExists(fc func() (prepare string, args []interface{})) Filter
	OrNotLike(column string, value interface{}) Filter
	OrIsNotNull(column string) Filter
	SQL() (prepare string, args []interface{})
}

func F added in v1.2.0

func F() Filter

type Get added in v1.0.1

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

Get for SELECT

func NewGet added in v1.0.1

func NewGet(way *Way) *Get

NewGet for SELECT

func (*Get) Alias added in v1.0.1

func (s *Get) Alias(alias string) *Get

Alias for table alias name, don't forget to call the current method when the table is a SQL statement

func (*Get) AppendColumn added in v1.1.0

func (s *Get) AppendColumn(column ...string) *Get

AppendColumn append the columns list of query

func (*Get) Asc added in v1.1.0

func (s *Get) Asc(column string) *Get

Asc set order by column ASC

func (*Get) Cache added in v1.2.2

func (s *Get) Cache(key string, duration time.Duration) *Get

Cache please make sure to use the same structure as the one receiving the query data

func (*Get) Column added in v1.0.1

func (s *Get) Column(column ...string) *Get

Column set the columns list of query

func (*Get) Comment added in v1.0.1

func (s *Get) Comment(comment string) *Get

Comment set comment

func (*Get) Context added in v1.0.1

func (s *Get) Context(ctx context.Context) *Get

Context set context

func (*Get) Count added in v1.0.1

func (s *Get) Count(column ...string) (int64, error)

Count execute the built SQL statement and scan query result for count

func (*Get) CountGet added in v1.1.0

func (s *Get) CountGet(result interface{}, countColumn ...string) (int64, error)

CountGet execute the built SQL statement and scan query result, count + get

func (*Get) CountQuery added in v1.1.0

func (s *Get) CountQuery(query func(rows *sql.Rows) (err error), countColumn ...string) (int64, error)

CountQuery execute the built SQL statement and scan query result, count + query

func (*Get) Desc added in v1.1.0

func (s *Get) Desc(column string) *Get

Desc set order by column Desc

func (*Get) Exists added in v1.3.1

func (s *Get) Exists() (exists bool, err error)

Exists Determine whether the query result exists.

func (*Get) F added in v1.2.0

func (s *Get) F(filter ...Filter) Filter

F make new Filter

func (*Get) FullJoin added in v1.0.1

func (s *Get) FullJoin(fs ...func(j *GetJoin)) *Get

FullJoin for full join

func (*Get) Get added in v1.0.1

func (s *Get) Get(result interface{}) error

Get execute the built SQL statement and scan query result

func (*Get) Group added in v1.0.1

func (s *Get) Group(group ...string) *Get

Group set group columns

func (*Get) Having added in v1.0.1

func (s *Get) Having(having Filter) *Get

Having set filter of group result

func (*Get) InnerJoin added in v1.0.1

func (s *Get) InnerJoin(fs ...func(j *GetJoin)) *Get

InnerJoin for inner join

func (*Get) Joins added in v1.1.0

func (s *Get) Joins(joins ...*GetJoin) *Get

Joins for join one or more tables

func (*Get) LeftJoin added in v1.0.1

func (s *Get) LeftJoin(fs ...func(j *GetJoin)) *Get

LeftJoin for left join

func (*Get) Limit added in v1.0.1

func (s *Get) Limit(limit int64) *Get

Limit set limit

func (*Get) Limiter added in v1.1.0

func (s *Get) Limiter(limiter Limiter) *Get

Limiter set limit and offset at the same time

func (*Get) Offset added in v1.0.1

func (s *Get) Offset(offset int64) *Get

Offset set offset

func (*Get) Order added in v1.0.1

func (s *Get) Order(order string, orderMap ...map[string]string) *Get

Order set the column sorting list in batches through regular expressions according to the request parameter value

func (*Get) Query added in v1.0.1

func (s *Get) Query(query func(rows *sql.Rows) (err error)) error

Query execute the built SQL statement and scan query result

func (*Get) RightJoin added in v1.0.1

func (s *Get) RightJoin(fs ...func(j *GetJoin)) *Get

RightJoin for right join

func (*Get) SQL added in v1.0.1

func (s *Get) SQL() (prepare string, args []interface{})

SQL build SQL statement

func (*Get) SQLCount added in v1.0.1

func (s *Get) SQLCount(columns ...string) (prepare string, args []interface{})

SQLCount build SQL statement for count

func (*Get) ScanAll added in v1.3.0

func (s *Get) ScanAll(fc func(rows *sql.Rows) error) error

ScanAll execute the built SQL statement and scan all from the query results.

func (*Get) ScanOne added in v1.3.0

func (s *Get) ScanOne(dest ...interface{}) error

ScanOne execute the built SQL statement and scan at most once from the query results.

func (*Get) SubQuery added in v1.1.0

func (s *Get) SubQuery(prepare string, args ...interface{}) *Get

SubQuery table is a query SQL statement

func (*Get) SubQueryGet added in v1.1.0

func (s *Get) SubQueryGet(get *Get, alias ...string) *Get

SubQueryGet table is a query SQL statement

func (*Get) Table added in v1.0.1

func (s *Get) Table(table string, alias ...string) *Get

Table set table name

func (*Get) Union added in v1.1.0

func (s *Get) Union(prepare string, args ...interface{}) *Get

Union union query

func (*Get) UnionAll added in v1.1.0

func (s *Get) UnionAll(prepare string, args ...interface{}) *Get

UnionAll union all query

func (*Get) UnionAllGet added in v1.1.0

func (s *Get) UnionAllGet(get ...*Get) *Get

UnionAllGet union all query

func (*Get) UnionGet added in v1.1.0

func (s *Get) UnionGet(get ...*Get) *Get

UnionGet union query

func (*Get) ViewMap added in v1.3.0

func (s *Get) ViewMap() (result []map[string]interface{}, err error)

ViewMap execute the built SQL statement and scan all from the query results.

func (*Get) Way added in v1.2.0

func (s *Get) Way() *Way

Way get current *Way

func (*Get) Where added in v1.0.1

func (s *Get) Where(where Filter) *Get

Where set where

func (*Get) With added in v1.1.0

func (s *Get) With(alias string, prepare string, args ...interface{}) *Get

With for with query

func (*Get) WithGet added in v1.1.0

func (s *Get) WithGet(alias string, get *Get) *Get

WithGet for with query using *Get

type GetJoin added in v1.0.1

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

GetJoin join SQL statement

func FullJoin

func FullJoin(table ...string) *GetJoin

func InnerJoin

func InnerJoin(table ...string) *GetJoin

func LeftJoin

func LeftJoin(table ...string) *GetJoin

func RightJoin

func RightJoin(table ...string) *GetJoin

func (*GetJoin) Alias added in v1.0.1

func (s *GetJoin) Alias(alias string) *GetJoin

Alias for table alias name, don't forget to call the current method when the table is a SQL statement

func (*GetJoin) Column added in v1.3.1

func (s *GetJoin) Column(column ...string) *GetJoin

Column Set the list of fields to be queried in the current table

func (*GetJoin) GetColumn added in v1.3.1

func (s *GetJoin) GetColumn() []string

GetColumn Get the list of fields to be queried in the current table

func (*GetJoin) On added in v1.0.1

func (s *GetJoin) On(on string) *GetJoin

On join query condition

func (*GetJoin) OnEqual added in v1.0.1

func (s *GetJoin) OnEqual(fields ...string) *GetJoin

OnEqual join query condition, support multiple fields

func (*GetJoin) SQL added in v1.0.1

func (s *GetJoin) SQL() (prepare string, args []interface{})

SQL build SQL statement

func (*GetJoin) SubQuery added in v1.1.0

func (s *GetJoin) SubQuery(prepare string, args ...interface{}) *GetJoin

SubQuery table is a query SQL statement

func (*GetJoin) SubQueryGet added in v1.1.0

func (s *GetJoin) SubQueryGet(get *Get, alias ...string) *GetJoin

SubQueryGet table is a query SQL statement

func (*GetJoin) Table added in v1.0.1

func (s *GetJoin) Table(table string) *GetJoin

Table set table name

func (*GetJoin) Using added in v1.1.0

func (s *GetJoin) Using(fields ...string) *GetJoin

Using join query condition

type Ident added in v1.2.0

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

Ident sql identifier

func (*Ident) AVG added in v1.2.0

func (s *Ident) AVG(field string) string

AVG AVG([prefix.]xxx) AS xxx

func (*Ident) Avg added in v1.2.0

func (s *Ident) Avg(field string, alias ...string) string

Avg AVG([prefix.]xxx)[ AS xxx|custom]

func (*Ident) COUNT added in v1.2.0

func (s *Ident) COUNT(field string, alias ...string) string

COUNT COUNT([prefix.]xxx) AS count|custom

func (*Ident) Field added in v1.3.1

func (s *Ident) Field(field ...string) []string

Field Batch set field prefix

func (*Ident) MAX added in v1.2.0

func (s *Ident) MAX(field string) string

MAX MAX([prefix.]xxx) AS xxx

func (*Ident) MIN added in v1.2.0

func (s *Ident) MIN(field string) string

MIN MIN([prefix.]xxx) AS xxx

func (*Ident) Max added in v1.2.0

func (s *Ident) Max(field string, alias ...string) string

Max MAX([prefix.]xxx)[ AS xxx|custom]

func (*Ident) Min added in v1.2.0

func (s *Ident) Min(field string, alias ...string) string

Min MIN([prefix.]xxx)[ AS xxx|custom]

func (*Ident) SUM added in v1.2.0

func (s *Ident) SUM(field string) string

SUM SUM([prefix.]xxx) AS xxx

func (*Ident) Sum added in v1.2.0

func (s *Ident) Sum(field string, alias ...string) string

Sum SUM([prefix.]xxx)[ AS xxx|custom]

func (*Ident) V added in v1.2.0

func (s *Ident) V(sss ...string) string

V returns expressions in different formats based on the length of the parameter length=0: prefix value length=1: prefix.name length>1: prefix.name AS alias_name (the alias value must not be empty, otherwise it will not be used)

type Limiter added in v1.1.0

type Limiter interface {
	GetLimit() int64
	GetOffset() int64
}

Limiter limit and offset

type LogArgs added in v1.1.0

type LogArgs struct {
	// Args SQL parameter list.
	Args []interface{}

	// StartAt The start time of the SQL statement.
	StartAt time.Time

	// EndAt The end time of the SQL statement.
	EndAt time.Time
}

LogArgs Record executed args of prepare.

type LogSQL added in v1.3.3

type LogSQL struct {
	// TxId Transaction ID.
	TxId string

	// TxMsg Transaction descriptors.
	TxMsg string

	// Prepare Preprocess the SQL statements that are executed.
	Prepare string

	// Error An error encountered when executing SQL.
	Error error

	// Args SQL parameter list.
	Args *LogArgs
}

LogSQL Record executed prepare args.

type LogTrans added in v1.3.3

type LogTrans struct {
	// Transaction id.
	TxId string

	// Transaction message.
	TxMsg string

	// Transaction start at.
	StartAt time.Time

	// Transaction end at.
	EndAt time.Time

	// Transaction result COMMIT || ROLLBACK.
	State string

	// Error.
	Error error
}

LogTrans Record executed transaction.

type Mod added in v1.0.1

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

Mod for UPDATE

func NewMod added in v1.0.1

func NewMod(way *Way) *Mod

NewMod for UPDATE

func (*Mod) Comment added in v1.0.1

func (s *Mod) Comment(comment string) *Mod

Comment set comment

func (*Mod) Context added in v1.0.1

func (s *Mod) Context(ctx context.Context) *Mod

Context set context

func (*Mod) Decr added in v1.0.1

func (s *Mod) Decr(field string, value interface{}) *Mod

Decr SET field = field - value

func (*Mod) DefaultDecr added in v1.1.0

func (s *Mod) DefaultDecr(field string, value interface{}) *Mod

DefaultDecr SET field = field - value

func (*Mod) DefaultExpr added in v1.1.0

func (s *Mod) DefaultExpr(field string, expr string, args ...interface{}) *Mod

DefaultExpr update field using custom expression

func (*Mod) DefaultIncr added in v1.1.0

func (s *Mod) DefaultIncr(field string, value interface{}) *Mod

DefaultIncr SET field = field + value

func (*Mod) DefaultSet added in v1.1.0

func (s *Mod) DefaultSet(field string, value interface{}) *Mod

DefaultSet SET field = value

func (*Mod) Except added in v1.0.1

func (s *Mod) Except(except ...string) *Mod

Except exclude some fields from update

func (*Mod) Expr added in v1.1.0

func (s *Mod) Expr(field string, expr string, args ...interface{}) *Mod

Expr update field using custom expr

func (*Mod) F added in v1.2.0

func (s *Mod) F(filter ...Filter) Filter

F make new Filter

func (*Mod) FieldsValues added in v1.1.0

func (s *Mod) FieldsValues(fields []string, values []interface{}) *Mod

FieldsValues SET field = value by slice, require len(fields) == len(values)

func (*Mod) Incr added in v1.0.1

func (s *Mod) Incr(field string, value interface{}) *Mod

Incr SET field = field + value

func (*Mod) Mod added in v1.0.1

func (s *Mod) Mod() (int64, error)

Mod execute the built SQL statement

func (*Mod) Modify added in v1.1.0

func (s *Mod) Modify(modify interface{}) *Mod

Modify value of modify should be one of struct{}, *struct{}, map[string]interface{}

func (*Mod) SQL added in v1.0.1

func (s *Mod) SQL() (prepare string, args []interface{})

SQL build SQL statement

func (*Mod) Set added in v1.0.1

func (s *Mod) Set(field string, value interface{}) *Mod

Set field = value

func (*Mod) SetSQL added in v1.1.0

func (s *Mod) SetSQL() (prepare string, args []interface{})

SetSQL prepare args of set

func (*Mod) Table added in v1.0.1

func (s *Mod) Table(table string) *Mod

Table set table name

func (*Mod) Update added in v1.1.0

func (s *Mod) Update(originObject interface{}, latestObject interface{}) *Mod

Update for compare origin and latest to automatically calculate need to update fields

func (*Mod) Way added in v1.2.0

func (s *Mod) Way() *Way

Way get current *Way

func (*Mod) Where added in v1.0.1

func (s *Mod) Where(where Filter) *Mod

Where set where

type Opts added in v1.1.0

type Opts func(s *Way)

Opts Custom attribute value of *Way.

func WithCache added in v1.2.2

func WithCache(cache CacheQuery) Opts

WithCache -> Uses cache for query data.

func WithConfig added in v1.1.0

func WithConfig(config *Config) Opts

WithConfig -> Uses custom configure.

func WithLogger added in v1.1.0

func WithLogger(log func(log *LogSQL)) Opts

WithLogger -> Uses custom logger.

func WithPrepare added in v1.1.0

func WithPrepare(fix func(prepare string) string) Opts

WithPrepare -> Uses custom fix prepare.

func WithReader added in v1.3.0

func WithReader(reader Reader) Opts

WithReader -> uses reader for query.

func WithScan added in v1.3.0

func WithScan(scan func(rows *sql.Rows, result interface{}, tag string) error) Opts

WithScan -> Uses scan for query data.

func WithTag added in v1.1.0

func WithTag(tag string) Opts

WithTag -> Uses custom tag.

func WithTxLogger added in v1.1.0

func WithTxLogger(txLog func(log *LogTrans)) Opts

WithTxLogger -> Uses custom transaction logger.

func WithTxOpts added in v1.1.0

func WithTxOpts(txOpts *sql.TxOptions) Opts

WithTxOpts -> Uses custom global default transaction isolation level.

type PrepareArgs added in v1.1.0

type PrepareArgs struct {
	Prepare string

	Args []interface{}
}

PrepareArgs Statements to be executed and corresponding parameter list.

type Reader added in v1.3.0

type Reader interface {
	// Read Get an object for read.
	Read() *Way
}

Reader Separate read and write, when you distinguish between reading and writing, please do not use the same object for both reading and writing.

func NewReader added in v1.3.0

func NewReader(choose func(n int) int, reads []*Way) Reader

NewReader It is recommended that objects used for writing should not appear in reads.

type Stmt added in v1.1.0

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

Stmt Prepare handle.

func (*Stmt) Close added in v1.1.0

func (s *Stmt) Close() (err error)

Close -> Close prepare handle.

func (*Stmt) Exec added in v1.1.0

func (s *Stmt) Exec(args ...interface{}) (int64, error)

Exec -> Execute prepared, that can be called repeatedly, return number of rows affected.

func (*Stmt) ExecContext added in v1.1.0

func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (int64, error)

ExecContext -> Execute prepared, that can be called repeatedly, return number of rows affected.

func (*Stmt) Execute added in v1.2.1

func (s *Stmt) Execute(args ...interface{}) (sql.Result, error)

Execute -> Execute prepared, that can be called repeatedly.

func (*Stmt) ExecuteContext added in v1.2.1

func (s *Stmt) ExecuteContext(ctx context.Context, args ...interface{}) (sql.Result, error)

ExecuteContext -> Execute prepared, that can be called repeatedly.

func (*Stmt) Query added in v1.1.0

func (s *Stmt) Query(query func(rows *sql.Rows) error, args ...interface{}) error

Query -> Query prepared, that can be called repeatedly.

func (*Stmt) QueryContext added in v1.1.0

func (s *Stmt) QueryContext(ctx context.Context, query func(rows *sql.Rows) error, args ...interface{}) error

QueryContext -> Query prepared, that can be called repeatedly.

func (*Stmt) QueryRow added in v1.2.1

func (s *Stmt) QueryRow(query func(rows *sql.Row) error, args ...interface{}) (err error)

QueryRow -> Query prepared, that can be called repeatedly.

func (*Stmt) QueryRowContext added in v1.2.1

func (s *Stmt) QueryRowContext(ctx context.Context, query func(rows *sql.Row) error, args ...interface{}) error

QueryRowContext -> Query prepared, that can be called repeatedly.

func (*Stmt) TakeAll added in v1.3.0

func (s *Stmt) TakeAll(result interface{}, args ...interface{}) error

TakeAll -> Query prepared and get all query results, that can be called repeatedly.

func (*Stmt) TakeAllContext added in v1.3.0

func (s *Stmt) TakeAllContext(ctx context.Context, result interface{}, args ...interface{}) error

TakeAllContext -> Query prepared and get all query results, that can be called repeatedly.

type SubQuery

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

func NewSubQuery added in v1.0.1

func NewSubQuery(prepare string, args ...interface{}) *SubQuery

func (*SubQuery) SQL added in v1.0.1

func (s *SubQuery) SQL() (prepare string, args []interface{})

type TxArgs added in v1.3.0

type TxArgs struct {
	// Use the specified database connection, default nil.
	Conn *sql.Conn

	// Use the specified transaction isolation level, default *Way.txOpts.
	Opts *sql.TxOptions

	// Try calling transaction multiple times, default 1.
	Times int
}

TxArgs Optional args for begin a transaction.

type Way

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

Way Quick insert, delete, update, select helper.

func NewWay

func NewWay(db *sql.DB, opts ...Opts) *Way

NewWay Instantiate a helper.

func (*Way) Add added in v1.0.1

func (s *Way) Add(table string) *Add

Add -> Create an instance that executes the INSERT sql statement.

func (*Way) AliasA added in v1.2.0

func (s *Way) AliasA() *Ident

AliasA SQL identifier prefix a.

func (*Way) AliasB added in v1.2.0

func (s *Way) AliasB() *Ident

AliasB SQL identifier prefix b.

func (*Way) AliasC added in v1.2.0

func (s *Way) AliasC() *Ident

AliasC SQL identifier prefix c.

func (*Way) AliasD added in v1.2.0

func (s *Way) AliasD() *Ident

AliasD SQL identifier prefix d.

func (*Way) AliasE added in v1.2.0

func (s *Way) AliasE() *Ident

AliasE SQL identifier prefix e.

func (*Way) AliasF added in v1.2.0

func (s *Way) AliasF() *Ident

AliasF SQL identifier prefix f.

func (*Way) AliasG added in v1.2.0

func (s *Way) AliasG() *Ident

AliasG SQL identifier prefix g.

func (*Way) Cache added in v1.2.2

func (s *Way) Cache() CacheQuery

Cache -> Get cache object.

func (*Way) DB

func (s *Way) DB() *sql.DB

DB -> Get the database connection pool object in the current instance.

func (*Way) Del added in v1.0.1

func (s *Way) Del(table string) *Del

Del -> Create an instance that executes the DELETE sql statement.

func (*Way) Exec

func (s *Way) Exec(prepare string, args ...interface{}) (int64, error)

Exec -> Execute the execute sql statement.

func (*Way) ExecContext

func (s *Way) ExecContext(ctx context.Context, prepare string, args ...interface{}) (int64, error)

ExecContext -> Execute the execute sql statement.

func (*Way) Execute added in v1.2.1

func (s *Way) Execute(prepare string, args ...interface{}) (sql.Result, error)

Execute -> Execute the execute sql statement.

func (*Way) ExecuteContext added in v1.2.1

func (s *Way) ExecuteContext(ctx context.Context, prepare string, args ...interface{}) (sql.Result, error)

ExecuteContext -> Execute the execute sql statement.

func (*Way) F added in v1.1.0

func (s *Way) F(filter ...Filter) Filter

F -> Quickly initialize a filter.

func (*Way) Get added in v1.0.1

func (s *Way) Get(table ...string) *Get

Get -> Create an instance that executes the SELECT sql statement.

func (*Way) Getter added in v1.1.0

func (s *Way) Getter(query func(rows *sql.Rows) error, prepare string, caller ...Caller) error

Getter -> Execute the query sql statement without args, no prepared is used.

func (*Way) GetterContext added in v1.1.0

func (s *Way) GetterContext(ctx context.Context, query func(rows *sql.Rows) error, prepare string, caller ...Caller) (err error)

GetterContext -> Execute the query sql statement without args, no prepared is used.

func (*Way) Ident added in v1.2.0

func (s *Way) Ident(prefix ...string) *Ident

Ident -> SQL identifier.

func (*Way) IsRead added in v1.3.0

func (s *Way) IsRead() bool

IsRead -> Is an object for read?

func (*Way) Mod added in v1.0.1

func (s *Way) Mod(table string) *Mod

Mod -> Create an instance that executes the UPDATE sql statement.

func (*Way) Now added in v1.1.0

func (s *Way) Now() time.Time

Now -> Get current time, the transaction open status will get the same time.

func (*Way) Prepare

func (s *Way) Prepare(prepare string, caller ...Caller) (*Stmt, error)

Prepare -> Prepare sql statement, don't forget to call *Stmt.Close().

func (*Way) PrepareContext added in v1.1.0

func (s *Way) PrepareContext(ctx context.Context, prepare string, caller ...Caller) (*Stmt, error)

PrepareContext -> Prepare sql statement, don't forget to call *Stmt.Close().

func (*Way) Query

func (s *Way) Query(query func(rows *sql.Rows) error, prepare string, args ...interface{}) error

Query -> Execute the query sql statement.

func (*Way) QueryContext

func (s *Way) QueryContext(ctx context.Context, query func(rows *sql.Rows) error, prepare string, args ...interface{}) error

QueryContext -> Execute the query sql statement.

func (*Way) QueryRow added in v1.2.1

func (s *Way) QueryRow(query func(row *sql.Row) error, prepare string, args ...interface{}) error

QueryRow -> Execute sql statement and return a row data, usually INSERT, UPDATE, DELETE.

func (*Way) QueryRowContext added in v1.2.1

func (s *Way) QueryRowContext(ctx context.Context, query func(row *sql.Row) error, prepare string, args ...interface{}) error

QueryRowContext -> Execute sql statement and return a row data, usually INSERT, UPDATE, DELETE.

func (*Way) Read added in v1.3.0

func (s *Way) Read() *Way

Read -> Get an object for read.

func (*Way) ScanAll added in v1.0.1

func (s *Way) ScanAll(rows *sql.Rows, fc func(rows *sql.Rows) error) error

ScanAll -> Iteratively scan from query results.

func (*Way) ScanOne added in v1.3.0

func (s *Way) ScanOne(rows *sql.Rows, dest ...interface{}) error

ScanOne -> Scan at most once from the query results.

func (*Way) Setter added in v1.1.0

func (s *Way) Setter(prepare string, caller ...Caller) (int64, error)

Setter -> Execute the execute sql statement without args, no prepared is used.

func (*Way) SetterContext added in v1.1.0

func (s *Way) SetterContext(ctx context.Context, prepare string, caller ...Caller) (int64, error)

SetterContext -> Execute the execute sql statement without args, no prepared is used.

func (*Way) Tag added in v1.0.1

func (s *Way) Tag() string

Tag -> Get tag value.

func (*Way) TakeAll added in v1.3.0

func (s *Way) TakeAll(result interface{}, prepare string, args ...interface{}) error

TakeAll -> Query prepared and get all query results.

func (*Way) TakeAllContext added in v1.3.0

func (s *Way) TakeAllContext(ctx context.Context, result interface{}, prepare string, args ...interface{}) error

TakeAllContext -> Query prepared and get all query results, through the mapping of column names and struct tags.

func (*Way) TxMsg added in v1.1.0

func (s *Way) TxMsg(msg string) *Way

TxMsg -> Set the prompt for the current transaction, can only be set once.

func (*Way) TxNil

func (s *Way) TxNil() bool

TxNil -> Whether the current instance has not opened a transaction.

func (*Way) TxTry added in v1.1.0

func (s *Way) TxTry(fc func(tx *Way) error, args ...*TxArgs) error

TxTry -> Batch execute SQL through transactions, In args, only the last transaction parameter is valid.

func (*Way) TxTryCtx added in v1.1.0

func (s *Way) TxTryCtx(ctx context.Context, fc func(tx *Way) error, args ...*TxArgs) (err error)

TxTryCtx -> Batch execute SQL through transactions, In args, only the last transaction parameter is valid.

type WindowFunc added in v1.3.1

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

WindowFunc SQL window function.

func NewWindowFunc added in v1.3.1

func NewWindowFunc(alias ...string) *WindowFunc

func (*WindowFunc) Alias added in v1.3.1

func (s *WindowFunc) Alias(alias string) *WindowFunc

Alias Set the alias of the field that uses the window function.

func (*WindowFunc) Asc added in v1.3.1

func (s *WindowFunc) Asc(column string) *WindowFunc

Asc Define the sorting within the partition so that the window function is calculated in order.

func (*WindowFunc) Avg added in v1.3.1

func (s *WindowFunc) Avg(column string) *WindowFunc

Avg AVG() Returns the average of all rows in the window.

func (*WindowFunc) Count added in v1.3.1

func (s *WindowFunc) Count(column string) *WindowFunc

Count COUNT() Returns the number of rows in the window.

func (*WindowFunc) DenseRank added in v1.3.1

func (s *WindowFunc) DenseRank() *WindowFunc

DenseRank DENSE_RANK() Similar to RANK(), but does not skip rankings.

func (*WindowFunc) Desc added in v1.3.1

func (s *WindowFunc) Desc(column string) *WindowFunc

Desc Define the sorting within the partition so that the window function is calculated in descending order.

func (*WindowFunc) FirstValue added in v1.3.1

func (s *WindowFunc) FirstValue(column string) *WindowFunc

FirstValue FIRST_VALUE() Returns the value of the first row in the window.

func (*WindowFunc) Lag added in v1.3.1

func (s *WindowFunc) Lag(column string, offset int64, defaultValue any) *WindowFunc

Lag LAG() Returns the value of the row before the current row.

func (*WindowFunc) LastValue added in v1.3.1

func (s *WindowFunc) LastValue(column string) *WindowFunc

LastValue LAST_VALUE() Returns the value of the last row in the window.

func (*WindowFunc) Lead added in v1.3.1

func (s *WindowFunc) Lead(column string, offset int64, defaultValue any) *WindowFunc

Lead LEAD() Returns the value of a row after the current row.

func (*WindowFunc) Max added in v1.3.1

func (s *WindowFunc) Max(column string) *WindowFunc

Max MAX() Returns the maximum value within the window.

func (*WindowFunc) Min added in v1.3.1

func (s *WindowFunc) Min(column string) *WindowFunc

Min MIN() Returns the minimum value within the window.

func (*WindowFunc) NthValue added in v1.3.1

func (s *WindowFunc) NthValue(column string, LineNumber int64) *WindowFunc

NthValue NTH_VALUE() The Nth value can be returned according to the specified order. This is very useful when you need to get data at a specific position.

func (*WindowFunc) Ntile added in v1.3.1

func (s *WindowFunc) Ntile(buckets int64) *WindowFunc

Ntile NTILE() Divide the rows in the window into n buckets and assign a bucket number to each row.

func (*WindowFunc) Partition added in v1.3.1

func (s *WindowFunc) Partition(column ...string) *WindowFunc

Partition The OVER clause defines window partitions so that the window function is calculated independently in each partition.

func (*WindowFunc) Rank added in v1.3.1

func (s *WindowFunc) Rank() *WindowFunc

Rank RANK() Assign a rank to each row, if there are duplicate values, the rank is skipped.

func (*WindowFunc) Result added in v1.3.1

func (s *WindowFunc) Result() string

Result Query column expressions.

func (*WindowFunc) RowNumber added in v1.3.1

func (s *WindowFunc) RowNumber() *WindowFunc

RowNumber ROW_NUMBER() Assign a unique serial number to each row, in the order specified, starting with 1.

func (*WindowFunc) Sum added in v1.3.1

func (s *WindowFunc) Sum(column string) *WindowFunc

Sum SUM() Returns the sum of all rows in the window.

func (*WindowFunc) WindowFrame added in v1.3.1

func (s *WindowFunc) WindowFrame(windowFrame string) *WindowFunc

WindowFrame Set custom window frame clause.

func (*WindowFunc) WithFunc added in v1.3.1

func (s *WindowFunc) WithFunc(withFunc string) *WindowFunc

WithFunc Using custom function. for example: CUME_DIST(), PERCENT_RANK(), PERCENTILE_CONT(), PERCENTILE_DISC()...

type WithQuery added in v1.1.0

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

func (*WithQuery) SQL added in v1.1.0

func (s *WithQuery) SQL() (prepare string, args []interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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