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
- Variables
- func ArgString(i interface{}) string
- func BasicTypeValue(value interface{}) interface{}
- func BuildCount(s *Get, countColumns ...string) (prepare string, args []interface{})
- func BuildGet(s *Get) (prepare string, args []interface{})
- func BuildOrderByLimitOffset(s *Get) (prepare string)
- func BuildTable(s *Get) (prepare string, args []interface{})
- func BuildUnion(withs []*GetWith, unionType string, gets []*Get) (prepare string, args []interface{})
- func BuildWith(withs []*GetWith) (prepare string, args []interface{})
- func ConcatString(sss ...string) string
- func EqualAll(f Filter, column string, subquery *Get)
- func EqualAny(f Filter, column string, subquery *Get)
- func EvenSlice2Map[K comparable](elems ...K) map[K]K
- func ExpectGet(gets ...*Get) (prepare string, args []interface{})
- func GetCount(get *Get, countColumns ...string) (count int64, err error)
- func GetCountGet(get *Get, result interface{}, countColumn ...string) (int64, error)
- func GetCountQuery(get *Get, query func(rows *sql.Rows) (err error), countColumn ...string) (int64, error)
- func GetExists(get *Get) (exists bool, err error)
- func GetGet(get *Get, result interface{}) error
- func GetQuery(get *Get, query func(rows *sql.Rows) (err error)) error
- func GetScanAll(get *Get, fc func(rows *sql.Rows) error) error
- func GetScanOne(get *Get, dest ...interface{}) error
- func GetViewMap(get *Get) (result []map[string]interface{}, err error)
- func GreaterThanAll(f Filter, column string, subquery *Get)
- func GreaterThanAny(f Filter, column string, subquery *Get)
- func GreaterThanEqualAll(f Filter, column string, subquery *Get)
- func GreaterThanEqualAny(f Filter, column string, subquery *Get)
- func IntersectGet(gets ...*Get) (prepare string, args []interface{})
- func LastNotEmptyString(sss []string) string
- func LessThanAll(f Filter, column string, subquery *Get)
- func LessThanAny(f Filter, column string, subquery *Get)
- func LessThanEqualAll(f Filter, column string, subquery *Get)
- func LessThanEqualAny(f Filter, column string, subquery *Get)
- func MergeMap[K comparable, V interface{}](elems ...map[K]V) map[K]V
- func MergeSlice[V interface{}](elems ...[]V) []V
- func MustAffectedRows(affectedRows int64, err error) error
- func NotEqualAll(f Filter, column string, subquery *Get)
- func NotEqualAny(f Filter, column string, subquery *Get)
- func PrefixColumn(table string, column ...string) []string
- func PrepareString(prepare string, args []interface{}) string
- func PutCase(i *Case)
- func PutFilter(f Filter)
- func PutIdent(i *Ident)
- func RemoveDuplicate(dynamic ...interface{}) (result []interface{})
- func RemoveDuplicates[T comparable](dynamic ...T) (result []T)
- func RemoveSliceMemberByIndex[T interface{}](indexList []int, elementList []T) []T
- func ScanAll(rows *sql.Rows, fc func(rows *sql.Rows) error) (err error)
- func ScanOne(rows *sql.Rows, dest ...interface{}) error
- func ScanSliceStruct(rows *sql.Rows, result interface{}, tag string) error
- func ScanViewMap(rows *sql.Rows) ([]map[string]interface{}, error)
- func Slice2MapNewKey[K comparable](elems []K, createKey func(v K) K) map[K]K
- func Slice2MapNewVal[K comparable, V interface{}](elems []K, createValue func(v K) V) map[K]V
- func SliceIter[V interface{}](iter func(v V) V, elems []V) []V
- 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)
- func SqlAlias(name string, alias string) string
- func SqlPrefix(prefix string, name string) string
- func StructInsert(object interface{}, tag string, except []string, allow []string) (fields []string, values [][]interface{})
- func StructModify(object interface{}, tag string, except ...string) (fields []string, values []interface{})
- func StructObtain(object interface{}, tag string, except ...string) (fields []string, values []interface{})
- func StructUpdate(origin interface{}, latest interface{}, tag string, except ...string) (fields []string, values []interface{})
- type Add
- func (s *Add) Add() (int64, error)
- func (s *Add) Comment(comment string) *Add
- func (s *Add) Context(ctx context.Context) *Add
- func (s *Add) Create(create interface{}) *Add
- func (s *Add) Default(fc func(o *Add)) *Add
- func (s *Add) Except(except ...string) *Add
- func (s *Add) FieldValue(field string, value interface{}) *Add
- func (s *Add) FieldsValues(fields []string, values [][]interface{}) *Add
- func (s *Add) OnConflict(prepare string, args []interface{}) *Add
- func (s *Add) Permit(permit ...string) *Add
- func (s *Add) Returning(fc func(row *sql.Row) error, returning ...string) error
- func (s *Add) SQL() (prepare string, args []interface{})
- func (s *Add) Table(table string) *Add
- func (s *Add) UseResult(fc func(result sql.Result) error) *Add
- func (s *Add) ValuesSubQuery(prepare string, args []interface{}) *Add
- func (s *Add) ValuesSubQueryGet(get *Get, fields ...string) *Add
- func (s *Add) Way() *Way
- type Caller
- type Case
- type Config
- type Del
- func (s *Del) Comment(comment string) *Del
- func (s *Del) Context(ctx context.Context) *Del
- func (s *Del) Del() (int64, error)
- func (s *Del) F(fs ...Filter) Filter
- func (s *Del) SQL() (prepare string, args []interface{})
- func (s *Del) Table(table string) *Del
- func (s *Del) Way() *Way
- func (s *Del) Where(where Filter) *Del
- type Filter
- type Get
- func (s *Get) AddCol(column ...string) *Get
- func (s *Get) AddColCase(caseList ...func(c *Case)) *Get
- func (s *Get) Alias(alias string) *Get
- func (s *Get) Asc(column string) *Get
- func (s *Get) Column(column ...string) *Get
- func (s *Get) Comment(comment string) *Get
- func (s *Get) Context(ctx context.Context) *Get
- func (s *Get) Count(column ...string) (int64, error)
- func (s *Get) CountGet(result interface{}, countColumn ...string) (int64, error)
- func (s *Get) CountQuery(query func(rows *sql.Rows) (err error), countColumn ...string) (int64, error)
- func (s *Get) CrossJoin(fs ...func(j *GetJoin)) *Get
- func (s *Get) Desc(column string) *Get
- func (s *Get) Distinct(distinct bool) *Get
- func (s *Get) Exists() (bool, error)
- func (s *Get) F(fs ...Filter) Filter
- func (s *Get) FullJoin(fs ...func(j *GetJoin)) *Get
- func (s *Get) Get(result interface{}) error
- func (s *Get) Group(group ...string) *Get
- func (s *Get) Having(having Filter) *Get
- func (s *Get) InnerJoin(fs ...func(j *GetJoin)) *Get
- func (s *Get) Joins(joins ...*GetJoin) *Get
- func (s *Get) LeftJoin(fs ...func(j *GetJoin)) *Get
- func (s *Get) Limit(limit int64) *Get
- func (s *Get) Limiter(limiter Limiter) *Get
- func (s *Get) Offset(offset int64) *Get
- func (s *Get) Order(order string, orderMap ...map[string]string) *Get
- func (s *Get) Query(query func(rows *sql.Rows) (err error)) error
- func (s *Get) Raw(prepare string, args []interface{}) *Get
- func (s *Get) RightJoin(fs ...func(j *GetJoin)) *Get
- func (s *Get) SQL() (prepare string, args []interface{})
- func (s *Get) SQLCount(columns ...string) (string, []interface{})
- func (s *Get) ScanAll(fc func(rows *sql.Rows) error) error
- func (s *Get) ScanOne(dest ...interface{}) error
- func (s *Get) SubQuery(prepare string, args []interface{}) *Get
- func (s *Get) SubQueryGet(get *Get, alias ...string) *Get
- func (s *Get) Table(table string, alias ...string) *Get
- func (s *Get) Union(unions ...*Get) *Get
- func (s *Get) UnionAll(unions ...*Get) *Get
- func (s *Get) ViewMap() ([]map[string]interface{}, error)
- func (s *Get) Way() *Way
- func (s *Get) Where(where Filter) *Get
- func (s *Get) With(with ...*GetWith) *Get
- type GetJoin
- func (s *GetJoin) Alias(alias string) *GetJoin
- func (s *GetJoin) On(on string) *GetJoin
- func (s *GetJoin) OnEqual(fields ...string) *GetJoin
- func (s *GetJoin) SQL() (prepare string, args []interface{})
- func (s *GetJoin) SubQuery(prepare string, args []interface{}) *GetJoin
- func (s *GetJoin) SubQueryGet(get *Get, alias ...string) *GetJoin
- func (s *GetJoin) Table(table string) *GetJoin
- func (s *GetJoin) Using(fields ...string) *GetJoin
- type GetWith
- type Ident
- func (s *Ident) AVG(field string) string
- func (s *Ident) Avg(field string, alias ...string) string
- func (s *Ident) COUNT(field string, alias ...string) string
- func (s *Ident) Field(field ...string) []string
- func (s *Ident) MAX(field string) string
- func (s *Ident) MIN(field string) string
- func (s *Ident) Max(field string, alias ...string) string
- func (s *Ident) Min(field string, alias ...string) string
- func (s *Ident) SUM(field string) string
- func (s *Ident) Sum(field string, alias ...string) string
- func (s *Ident) V(sss ...string) string
- type Limiter
- type LogArgs
- type LogSQL
- type LogTrans
- type Mod
- func (s *Mod) Comment(comment string) *Mod
- func (s *Mod) Context(ctx context.Context) *Mod
- func (s *Mod) Decr(field string, value interface{}) *Mod
- func (s *Mod) Default(fc func(o *Mod)) *Mod
- func (s *Mod) Except(except ...string) *Mod
- func (s *Mod) Expr(field string, expr string, args ...interface{}) *Mod
- func (s *Mod) F(fs ...Filter) Filter
- func (s *Mod) FieldsValues(fields []string, values []interface{}) *Mod
- func (s *Mod) Incr(field string, value interface{}) *Mod
- func (s *Mod) Mod() (int64, error)
- func (s *Mod) Modify(modify interface{}) *Mod
- func (s *Mod) Permit(permit ...string) *Mod
- func (s *Mod) SQL() (prepare string, args []interface{})
- func (s *Mod) Set(field string, value interface{}) *Mod
- func (s *Mod) SetCase(field string, value func(c *Case)) *Mod
- func (s *Mod) SetSQL() (prepare string, args []interface{})
- func (s *Mod) Table(table string) *Mod
- func (s *Mod) Update(originObject interface{}, latestObject interface{}) *Mod
- func (s *Mod) Way() *Way
- func (s *Mod) Where(where Filter) *Mod
- type Opts
- func WithConfig(config *Config) Opts
- func WithLogger(log func(log *LogSQL)) Opts
- func WithPrepare(fix func(prepare string) string) Opts
- func WithReader(reader Reader) Opts
- func WithScan(scan func(rows *sql.Rows, result interface{}, tag string) error) Opts
- func WithTag(tag string) Opts
- func WithTxLogger(txLog func(log *LogTrans)) Opts
- func WithTxOpts(txOpts *sql.TxOptions) Opts
- type PrepareArgs
- type Reader
- type Stmt
- func (s *Stmt) Close() (err error)
- func (s *Stmt) Exec(args ...interface{}) (int64, error)
- func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (int64, error)
- func (s *Stmt) Execute(args ...interface{}) (sql.Result, error)
- func (s *Stmt) ExecuteContext(ctx context.Context, args ...interface{}) (sql.Result, error)
- func (s *Stmt) Query(query func(rows *sql.Rows) error, args ...interface{}) error
- func (s *Stmt) QueryContext(ctx context.Context, query func(rows *sql.Rows) error, args ...interface{}) error
- func (s *Stmt) QueryRow(query func(rows *sql.Row) error, args ...interface{}) (err error)
- func (s *Stmt) QueryRowContext(ctx context.Context, query func(rows *sql.Row) error, args ...interface{}) error
- func (s *Stmt) TakeAll(result interface{}, args ...interface{}) error
- func (s *Stmt) TakeAllContext(ctx context.Context, result interface{}, args ...interface{}) error
- type SubQuery
- type TxArgs
- type Way
- func (s *Way) Add(table string) *Add
- func (s *Way) AliasA() *Ident
- func (s *Way) AliasB() *Ident
- func (s *Way) AliasC() *Ident
- func (s *Way) AliasD() *Ident
- func (s *Way) AliasE() *Ident
- func (s *Way) AliasF() *Ident
- func (s *Way) AliasG() *Ident
- func (s *Way) Begin(ctx context.Context, opts ...*sql.TxOptions) error
- func (s *Way) BeginWithConn(ctx context.Context, conn *sql.Conn, opts ...*sql.TxOptions) error
- func (s *Way) Commit() error
- func (s *Way) DB() *sql.DB
- func (s *Way) Del(table string) *Del
- func (s *Way) Exec(prepare string, args ...interface{}) (int64, error)
- func (s *Way) ExecContext(ctx context.Context, prepare string, args ...interface{}) (int64, error)
- func (s *Way) Execute(prepare string, args ...interface{}) (sql.Result, error)
- func (s *Way) ExecuteContext(ctx context.Context, prepare string, args ...interface{}) (sql.Result, error)
- func (s *Way) F(fs ...Filter) Filter
- func (s *Way) Get(table ...string) *Get
- func (s *Way) Getter(query func(rows *sql.Rows) error, prepare string, caller ...Caller) error
- func (s *Way) GetterContext(ctx context.Context, query func(rows *sql.Rows) error, prepare string, ...) (err error)
- func (s *Way) Ident(prefix ...string) *Ident
- func (s *Way) IsRead() bool
- func (s *Way) Mod(table string) *Mod
- func (s *Way) Now() time.Time
- func (s *Way) Prepare(prepare string, caller ...Caller) (*Stmt, error)
- func (s *Way) PrepareContext(ctx context.Context, prepare string, caller ...Caller) (*Stmt, error)
- func (s *Way) Query(query func(rows *sql.Rows) error, prepare string, args ...interface{}) error
- func (s *Way) QueryContext(ctx context.Context, query func(rows *sql.Rows) error, prepare string, ...) error
- func (s *Way) QueryRow(query func(row *sql.Row) error, prepare string, args ...interface{}) error
- func (s *Way) QueryRowContext(ctx context.Context, query func(row *sql.Row) error, prepare string, ...) error
- func (s *Way) Read() *Way
- func (s *Way) Rollback() error
- func (s *Way) ScanAll(rows *sql.Rows, fc func(rows *sql.Rows) error) error
- func (s *Way) ScanOne(rows *sql.Rows, dest ...interface{}) error
- func (s *Way) Setter(prepare string, caller ...Caller) (int64, error)
- func (s *Way) SetterContext(ctx context.Context, prepare string, caller ...Caller) (int64, error)
- func (s *Way) Tag() string
- func (s *Way) TakeAll(result interface{}, prepare string, args ...interface{}) error
- func (s *Way) TakeAllContext(ctx context.Context, result interface{}, prepare string, args ...interface{}) error
- func (s *Way) TxMsg(msg string) *Way
- func (s *Way) TxNil() bool
- func (s *Way) TxTry(fc func(tx *Way) error, args ...*TxArgs) error
- func (s *Way) TxTryCtx(ctx context.Context, fc func(tx *Way) error, args ...*TxArgs) (err error)
- type WindowFunc
- func (s *WindowFunc) Alias(alias string) *WindowFunc
- func (s *WindowFunc) Asc(column string) *WindowFunc
- func (s *WindowFunc) Avg(column string) *WindowFunc
- func (s *WindowFunc) Count(column string) *WindowFunc
- func (s *WindowFunc) DenseRank() *WindowFunc
- func (s *WindowFunc) Desc(column string) *WindowFunc
- func (s *WindowFunc) FirstValue(column string) *WindowFunc
- func (s *WindowFunc) Lag(column string, offset int64, defaultValue any) *WindowFunc
- func (s *WindowFunc) LastValue(column string) *WindowFunc
- func (s *WindowFunc) Lead(column string, offset int64, defaultValue any) *WindowFunc
- func (s *WindowFunc) Max(column string) *WindowFunc
- func (s *WindowFunc) Min(column string) *WindowFunc
- func (s *WindowFunc) NthValue(column string, LineNumber int64) *WindowFunc
- func (s *WindowFunc) Ntile(buckets int64) *WindowFunc
- func (s *WindowFunc) Partition(column ...string) *WindowFunc
- func (s *WindowFunc) Rank() *WindowFunc
- func (s *WindowFunc) Result() string
- func (s *WindowFunc) RowNumber() *WindowFunc
- func (s *WindowFunc) Sum(column string) *WindowFunc
- func (s *WindowFunc) WindowFrame(windowFrame string) *WindowFunc
- func (s *WindowFunc) WithFunc(withFunc string) *WindowFunc
Constants ¶
const ( State0 = 0 State1 = 1 StateNo = "NO" StateYes = "YES" StateOn = "ON" StateOff = "OFF" )
const ( // DefaultTag Mapping of default database column name and struct tag. DefaultTag = "db" // EmptyString Empty string value. EmptyString = "" )
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" SqlJoinCross = "CROSS JOIN" SqlAnd = "AND" SqlOr = "OR" SqlAvg = "AVG" SqlMax = "MAX" SqlMin = "MIN" SqlSum = "SUM" SqlCount = "COUNT" SqlNot = "NOT" SqlNull = "NULL" SqlPlaceholder = "?" SqlEqual = "=" SqlNotEqual = "<>" SqlGreaterThan = ">" SqlGreaterThanEqual = ">=" SqlLessThan = "<" SqlLessThanEqual = "<=" SqlAll = "ALL" SqlAny = "ANY" SqlLeftSmallBracket = "(" SqlRightSmallBracket = ")" SqlExpect = "EXCEPT" SqlIntersect = "INTERSECT" SqlDistinct = "DISTINCT" )
const ( AliasA = "a" AliasB = "b" AliasC = "c" AliasD = "d" AliasE = "e" AliasF = "f" AliasG = "g" )
const (
Id = "id"
)
Variables ¶
var ( DefaultConfig = &Config{ DeleteMustUseWhere: true, UpdateMustUseWhere: true, } )
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 BuildCount ¶ added in v1.4.0
BuildCount Build count query. SELECT COUNT(*) AS count FROM ( [WITH xxx] SELECT xxx FROM xxx [INNER JOIN xxx ON xxx] [WHERE xxx] [GROUP BY xxx [HAVING xxx]] ) AS a SELECT COUNT(*) AS count FROM ( query1 UNION [ALL] query2 [UNION [ALL] ...] ) AS a
func BuildGet ¶ added in v1.4.0
BuildGet Build a complete query. [WITH xxx] SELECT xxx FROM xxx [INNER JOIN xxx ON xxx] [WHERE xxx] [GROUP BY xxx [HAVING xxx]] [ORDER BY xxx] [LIMIT xxx [OFFSET xxx]]
func BuildOrderByLimitOffset ¶ added in v1.4.0
BuildOrderByLimitOffset Build query table of ORDER BY, LIMIT, OFFSET. [ORDER BY xxx] [LIMIT xxx [OFFSET xxx]]
func BuildTable ¶ added in v1.4.0
BuildTable Build query table (without ORDER BY, LIMIT, OFFSET). [WITH xxx] SELECT xxx FROM xxx [INNER JOIN xxx ON xxx] [WHERE xxx] [GROUP BY xxx [HAVING xxx]]
func BuildUnion ¶ added in v1.4.0
func BuildUnion(withs []*GetWith, unionType string, gets []*Get) (prepare string, args []interface{})
BuildUnion Combines the results of two or more SELECT queries into a single result set. [WITH xxx] /*query1*/( [WITH xxx] SELECT xxx FROM xxx [INNER JOIN xxx ON xxx] [WHERE xxx] [GROUP BY xxx [HAVING xxx]] [ORDER BY xxx] [LIMIT xxx [OFFSET xxx]]) UNION [ALL] /*query2*/( [WITH xxx] SELECT xxx FROM xxx [INNER JOIN xxx ON xxx] [WHERE xxx] [GROUP BY xxx [HAVING xxx]] [ORDER BY xxx] [LIMIT xxx [OFFSET xxx]]) [ORDER BY xxx] [LIMIT xxx [OFFSET xxx]]
func ConcatString ¶ added in v1.1.0
ConcatString concatenate string.
func EqualAll ¶ added in v1.4.0
EqualAll There are few practical application scenarios because all values are required to be equal.
func EqualAny ¶ added in v1.4.0
EqualAny Implement the filter condition: column = ANY ( subquery ) .
func EvenSlice2Map ¶ added in v1.2.0
func EvenSlice2Map[K comparable](elems ...K) map[K]K
EvenSlice2Map even slice to map.
func GetCount ¶ added in v1.4.0
GetCount execute the built SQL statement and scan query result for count.
func GetCountGet ¶ added in v1.4.0
GetCountGet execute the built SQL statement and scan query result, count + get.
func GetCountQuery ¶ added in v1.4.0
func GetCountQuery(get *Get, query func(rows *sql.Rows) (err error), countColumn ...string) (int64, error)
GetCountQuery execute the built SQL statement and scan query result, count + query.
func GetScanAll ¶ added in v1.4.0
GetScanAll execute the built SQL statement and scan all from the query results.
func GetScanOne ¶ added in v1.4.0
GetScanOne execute the built SQL statement and scan at most once from the query results.
func GetViewMap ¶ added in v1.4.0
GetViewMap execute the built SQL statement and scan all from the query results.
func GreaterThanAll ¶ added in v1.4.0
GreaterThanAll Implement the filter condition: column > ALL ( subquery ) .
func GreaterThanAny ¶ added in v1.4.0
GreaterThanAny Implement the filter condition: column > ANY ( subquery ) .
func GreaterThanEqualAll ¶ added in v1.4.0
GreaterThanEqualAll Implement the filter condition: column >= ALL ( subquery ) .
func GreaterThanEqualAny ¶ added in v1.4.0
GreaterThanEqualAny Implement the filter condition: column >= ANY ( subquery ) .
func IntersectGet ¶ added in v1.4.0
IntersectGet (query1) INTERSECT (query2) INTERSECT (query3)...
func LastNotEmptyString ¶ added in v1.1.0
LastNotEmptyString get last not empty string, return empty string if it does not exist.
func LessThanAll ¶ added in v1.4.0
LessThanAll Implement the filter condition: column < ALL ( subquery ) .
func LessThanAny ¶ added in v1.4.0
LessThanAny Implement the filter condition: column < ANY ( subquery ) .
func LessThanEqualAll ¶ added in v1.4.0
LessThanEqualAll Implement the filter condition: column <= ALL ( subquery ) .
func LessThanEqualAny ¶ added in v1.4.0
LessThanEqualAny Implement the filter condition: column <= ANY ( subquery ) .
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
MustAffectedRows at least one row is affected.
func NotEqualAll ¶ added in v1.4.0
NotEqualAll Implement the filter condition: column <> ALL ( subquery ) .
func NotEqualAny ¶ added in v1.4.0
NotEqualAny Implement the filter condition: column <> ANY ( subquery ) .
func PrefixColumn ¶ added in v1.4.0
PrefixColumn Prefix the field list with the table name or table alias.
func PrepareString ¶ added in v1.3.0
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 ScanSliceStruct ¶
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
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 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 (*Add) Create ¶ added in v1.0.1
Create value of create should be one of struct{}, *struct{}, map[string]interface{}, []struct, []*struct{}, *[]struct{}, *[]*struct{}.
func (*Add) FieldValue ¶ added in v1.1.0
FieldValue append field-value for insert one or more rows.
func (*Add) FieldsValues ¶ added in v1.1.0
FieldsValues set fields and values.
func (*Add) OnConflict ¶ added in v1.1.0
OnConflict on conflict do something.
func (*Add) ValuesSubQuery ¶ added in v1.0.1
ValuesSubQuery values is a query SQL statement.
func (*Add) ValuesSubQueryGet ¶ added in v1.0.1
ValuesSubQueryGet values is a query SQL statement.
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 Case ¶ added in v1.4.0
type Case struct {
// contains filtered or unexported fields
}
Case Implementing SQL CASE.
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.
type Filter ¶
type Filter interface {
// SQL Generate conditional filtering SQL statements and their parameters.
SQL() (string, []interface{})
// Clean Clear the existing conditional filtering of the current object.
Clean() Filter
// IsEmpty Is the current object an empty object?
IsEmpty() bool
// Not Negate the result of the current conditional filter object. Multiple negations are allowed.
Not() Filter
// And Use logical operator `AND` to combine custom conditions.
And(expr string, args ...interface{}) Filter
// Or Use logical operator `OR` to combine custom conditions.
Or(expr string, args ...interface{}) Filter
// Group Add a new condition group, which is connected by the `AND` logical operator by default.
Group(group func(g Filter)) Filter
// OrGroup Add a new condition group, which is connected by the `OR` logical operator by default.
OrGroup(group func(g Filter)) Filter
// Use Implement import a set of conditional filter objects into the current object.
Use(fs ...Filter) Filter
// New Create a new conditional filter object based on a set of conditional filter objects.
New(fs ...Filter) Filter
// GreaterThan Implement conditional filtering: column > value .
GreaterThan(column string, value interface{}) Filter
// GreaterThanEqual Implement conditional filtering: column >= value .
GreaterThanEqual(column string, value interface{}) Filter
// LessThan Implement conditional filtering: column < value .
LessThan(column string, value interface{}) Filter
// LessThanEqual Implement conditional filtering: column <= value .
LessThanEqual(column string, value interface{}) Filter
// Equal Implement conditional filtering: column = value .
Equal(column string, value interface{}) Filter
// Between Implement conditional filtering: column BETWEEN value1 AND value2 .
Between(column string, start interface{}, end interface{}) Filter
// In Implement conditional filtering: column IN ( value1, value2, value3... ) .
In(column string, values ...interface{}) Filter
// InSql Implement conditional filtering: column IN ( subquery ) .
InSql(column string, prepare string, args ...interface{}) Filter
// InCols Implement conditional filtering: ( column1, column2, column3... ) IN ( ( value1, value2, value3... ), ( value21, value22, value23... )... ) .
InCols(columns []string, values ...[]interface{}) Filter
// InColsSql Implement conditional filtering: ( column1, column2, column3... ) IN ( subquery ) .
InColsSql(columns []string, prepare string, args ...interface{}) Filter
// Exists Implement conditional filtering: EXISTS ( subquery ) .
Exists(prepare string, args ...interface{}) Filter
// Like Implement conditional filtering: column LIKE value .
Like(column string, value interface{}) Filter
// IsNull Implement conditional filtering: column IS NULL .
IsNull(column string) Filter
// InGet Implement conditional filtering: column IN ( subquery ) .
InGet(column string, get *Get) Filter
// InColsGet Implement conditional filtering: ( column1, column2, column3... ) IN ( subquery ) .
InColsGet(columns []string, get *Get) Filter
// ExistsGet Implement conditional filtering: EXISTS ( subquery ) .
ExistsGet(get *Get) Filter
// NotEqual Implement conditional filtering: column <> value .
NotEqual(column string, value interface{}) Filter
// NotBetween Implement conditional filtering: column NOT BETWEEN value1 AND value2 .
NotBetween(column string, start interface{}, end interface{}) Filter
// NotIn Implement conditional filtering: column NOT IN ( value1, value2, value3... ) .
NotIn(column string, values ...interface{}) Filter
// NotInCols Implement conditional filtering: ( column1, column2, column3... ) NOT IN ( ( value1, value2, value3... ), ( value21, value22, value23... )... ) .
NotInCols(columns []string, values ...[]interface{}) Filter
// NotLike Implement conditional filtering: column NOT LIKE value .
NotLike(column string, value interface{}) Filter
// IsNotNull Implement conditional filtering: column IS NOT NULL .
IsNotNull(column string) Filter
}
Filter Implement SQL statement condition filtering.
type Get ¶ added in v1.0.1
type Get struct {
// contains filtered or unexported fields
}
Get for SELECT.
func (*Get) AddColCase ¶ added in v1.4.0
AddColCase append the columns list of query.
func (*Get) Alias ¶ added in v1.0.1
Alias for table alias name, don't forget to call the current method when the table is a SQL statement.
func (*Get) Count ¶ added in v1.0.1
Count execute the built SQL statement and scan query result for count.
func (*Get) CountGet ¶ added in v1.1.0
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) Distinct ¶ added in v1.4.0
Distinct Remove duplicate records: one field value or a combination of multiple fields.
func (*Get) Order ¶ added in v1.0.1
Order set the column sorting list in batches through regular expressions according to the request parameter value.
func (*Get) Raw ¶ added in v1.4.0
Raw Directly set the native SQL statement and the corresponding parameter list.
func (*Get) ScanAll ¶ added in v1.3.0
ScanAll execute the built SQL statement and scan all from the query results.
func (*Get) ScanOne ¶ added in v1.3.0
ScanOne execute the built SQL statement and scan at most once from the query results.
func (*Get) SubQueryGet ¶ added in v1.1.0
SubQueryGet table is a query SQL statement.
func (*Get) Union ¶ added in v1.1.0
Union for union(After calling the current method, only WITH, ORDER BY, LIMIT, and OFFSET are valid for the current query attributes.).
func (*Get) UnionAll ¶ added in v1.1.0
UnionAll for union all(After calling the current method, only WITH, ORDER BY, LIMIT, and OFFSET are valid for the current query attributes.).
type GetJoin ¶ added in v1.0.1
type GetJoin struct {
// contains filtered or unexported fields
}
GetJoin join SQL statement.
func (*GetJoin) Alias ¶ added in v1.0.1
Alias for table alias name, don't forget to call the current method when the table is a SQL statement.
func (*GetJoin) SubQueryGet ¶ added in v1.1.0
SubQueryGet table is a query SQL statement.
type GetWith ¶ added in v1.4.0
type GetWith struct {
// contains filtered or unexported fields
}
GetWith CTE: Common Table Expression.
type Ident ¶ added in v1.2.0
type Ident struct {
// contains filtered or unexported fields
}
Ident sql identifier.
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 (*Mod) FieldsValues ¶ added in v1.1.0
FieldsValues SET field = value by slice, require len(fields) == len(values).
func (*Mod) Modify ¶ added in v1.1.0
Modify value of modify should be one of struct{}, *struct{}, map[string]interface{}.
func (*Mod) SetCase ¶ added in v1.4.0
SetCase SET salary = CASE WHEN department_id = 1 THEN salary * 1.1 WHEN department_id = 2 THEN salary * 1.05 ELSE salary.
type Opts ¶ added in v1.1.0
type Opts func(s *Way)
Opts Custom attribute value of *Way.
func WithConfig ¶ added in v1.1.0
WithConfig -> Uses custom configure.
func WithLogger ¶ added in v1.1.0
WithLogger -> Uses custom logger.
func WithPrepare ¶ added in v1.1.0
WithPrepare -> Uses custom fix prepare.
func WithReader ¶ added in v1.3.0
WithReader -> uses reader for query.
func WithTxLogger ¶ added in v1.1.0
WithTxLogger -> Uses custom transaction logger.
func WithTxOpts ¶ added in v1.1.0
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.
type Stmt ¶ added in v1.1.0
type Stmt struct {
// contains filtered or unexported fields
}
Stmt Prepare handle.
func (*Stmt) Exec ¶ added in v1.1.0
Exec -> Execute prepared, that can be called repeatedly, return number of rows affected.
func (*Stmt) ExecContext ¶ added in v1.1.0
ExecContext -> Execute prepared, that can be called repeatedly, return number of rows affected.
func (*Stmt) ExecuteContext ¶ added in v1.2.1
ExecuteContext -> Execute 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) 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.
type SubQuery ¶
type SubQuery struct {
// contains filtered or unexported fields
}
func NewSubQuery ¶ added in v1.0.1
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 (*Way) BeginWithConn ¶ added in v1.4.0
BeginWithConn -> Open transaction(no transaction logging).
func (*Way) ExecContext ¶
ExecContext -> 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) Getter ¶ added in v1.1.0
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) Now ¶ added in v1.1.0
Now -> Get current time, the transaction open status will get the same time.
func (*Way) PrepareContext ¶ added in v1.1.0
PrepareContext -> Prepare sql statement, don't forget to call *Stmt.Close().
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
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) Setter ¶ added in v1.1.0
Setter -> Execute the execute sql statement without args, no prepared is used.
func (*Way) SetterContext ¶ added in v1.1.0
SetterContext -> Execute the execute sql statement without args, no prepared is used.
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
TxMsg -> Set the prompt for the current transaction, can only be set once.
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()...