hey

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: MIT Imports: 11 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 (
	AliasA = "a"
	AliasB = "b"
	AliasC = "c"
	AliasD = "d"
	AliasE = "e"
	AliasF = "f"
	AliasG = "g"
)
View Source
const (
	State0   = 0
	State1   = 1
	StateNo  = "NO"
	StateYes = "YES"
	StateOn  = "ON"
	StateOff = "OFF"
)
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 (
	// DefaultTag mapping of default database column name and struct tag
	DefaultTag = "db"
)
View Source
const (
	EmptyString = ""
)
View Source
const (
	Id = "id"
)

Variables

View Source
var (
	DefaultConfig = &Config{
		DeleteMustUseWhere: true,
		UpdateMustUseWhere: true,
	}
)
View Source
var (
	DefaultPgsql = &Pgsql{}
)
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

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 MapKey](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 MergeMap added in v1.2.0

func MergeMap[K MapKey, 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 PrepareArgs added in v1.1.0

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

func RemoveDuplicate

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

RemoveDuplicate remove duplicate element

func RemoveDuplicates added in v1.1.0

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

RemoveDuplicates remove duplicate element

func RemoveSliceMemberByIndex added in v1.1.1

func RemoveSliceMemberByIndex[T MapKey | ~bool | ~float32 | ~float64 | interface{}](indexList []int, elementList []T) []T

RemoveSliceMemberByIndex delete slice member by index

func RowsNext added in v1.1.0

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

RowsNext traversing and processing query results

func RowsNextRow added in v1.1.0

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

RowsNextRow scan one line of 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 Slice2MapNewKey added in v1.2.0

func Slice2MapNewKey[K MapKey](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 MapKey, 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 MapKey, 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) (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 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 Config added in v1.1.0

type Config struct {
	DeleteMustUseWhere bool
	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
	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, preparer Preparer) Filter
	InCols(columns []string, values ...[]interface{}) Filter
	InColsQuery(columns []string, fc func() (prepare string, args []interface{})) Filter
	InColsGet(columns []string, preparer Preparer) 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, preparer Preparer) Filter
	NotInCols(columns []string, values ...[]interface{}) Filter
	NotInColsQuery(columns []string, fc func() (prepare string, args []interface{})) Filter
	NotInColsGet(columns []string, preparer Preparer) 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, preparer Preparer) Filter
	OrInCols(columns []string, values ...[]interface{}) Filter
	OrInColsQuery(columns []string, fc func() (prepare string, args []interface{})) Filter
	OrInColsGet(columns []string, preparer Preparer) 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, preparer Preparer) Filter
	OrNotInCols(columns []string, values ...[]interface{}) Filter
	OrNotInColsQuery(columns []string, fc func() (prepare string, args []interface{})) Filter
	OrNotInColsGet(columns []string, preparer Preparer) 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) 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) (count int64, err 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) 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) RowsNextRow added in v1.1.0

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

RowsNextRow execute the built SQL statement and scan query result, only scan one row

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) 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) 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) 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) 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    []interface{}
	StartAt time.Time
	EndAt   time.Time
	Error   error
}

LogArgs record executed args of prepare

type LogPrepareArgs added in v1.1.0

type LogPrepareArgs struct {
	TxId    string
	TxMsg   string
	Prepare string
	Args    *LogArgs
}

LogPrepareArgs record executed prepare args

type LogTransaction added in v1.1.0

type LogTransaction struct {
	TxId    string    // transaction id
	TxMsg   string    // transaction message
	StartAt time.Time // transaction start at
	EndAt   time.Time // transaction end at
	State   string    // transaction result COMMIT || ROLLBACK
	Error   error     // error
}

LogTransaction record executed transaction

type MapKey added in v1.2.0

type MapKey interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~uintptr | ~string
}

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 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 *LogPrepareArgs)) 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 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 *LogTransaction)) 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 Pgsql added in v1.1.0

type Pgsql struct{}

func (*Pgsql) CloneTableStruct added in v1.1.0

func (s *Pgsql) CloneTableStruct(
	dst string,
	src string,
	seq string,
) (
	create []string,
	drop []string,
)

CloneTableStruct clone table structure

func (*Pgsql) InsertOnConflict added in v1.1.0

func (s *Pgsql) InsertOnConflict(
	onConflictColumns []string,
	updateColumns []string,
) (prepare string, args []interface{})

InsertOnConflict rely on the unique index of the table

func (*Pgsql) Prepare added in v1.1.0

func (s *Pgsql) Prepare(str string) string

Prepare fix pgsql SQL statement, ?, ?, ?... => $1, $2, $3...

type Preparer added in v1.1.0

type Preparer interface {
	// SQL get the prepare sql statement and parameter list
	SQL() (prepare string, args []interface{})
}

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{}) (result sql.Result, err 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{}) (err error)

QueryContext -> query prepared that can be called repeatedly

func (*Stmt) QueryRow added in v1.2.1

func (s *Stmt) QueryRow(ctx context.Context, 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{}) (err error)

QueryRowContext -> query prepared that can be called repeatedly

func (*Stmt) ScanAll added in v1.1.0

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

ScanAll -> query prepared and scan results that can be called repeatedly

func (*Stmt) ScanAllContext added in v1.1.0

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

ScanAllContext -> query prepared and scan 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 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) 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) 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) RowsNext added in v1.1.0

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

RowsNext -> traversing and processing query results

func (*Way) RowsNextRow added in v1.1.0

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

RowsNextRow -> scan one line of query results

func (*Way) ScanAll added in v1.0.1

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

ScanAll -> query and scan results

func (*Way) ScanAllContext added in v1.1.0

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

ScanAllContext -> query and scan results through the mapping of column names and struct tags

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) Tx added in v1.1.3

func (s *Way) Tx(fc func(tx *Way) error, conn ...*sql.Conn) error

Tx -> call transaction once

func (*Way) TxCtx added in v1.1.3

func (s *Way) TxCtx(ctx context.Context, fc func(tx *Way) error, conn ...*sql.Conn) error

TxCtx -> call transaction once

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, times int, conn ...*sql.Conn) error

TxTry -> call transaction multiple times

func (*Way) TxTryCtx added in v1.1.0

func (s *Way) TxTryCtx(ctx context.Context, fc func(tx *Way) error, times int, conn ...*sql.Conn) (err error)

TxTryCtx -> call transaction multiple times

type WayWriterReader added in v1.1.0

type WayWriterReader interface {
	// W get an object for write
	W() *Way

	// R get an object for read
	R() *Way
}

WayWriterReader -> read and write separation

func NewWayWriterReader added in v1.1.0

func NewWayWriterReader(
	choose func(n int) int,
	writer []*Way,
	reader []*Way,
) (WayWriterReader, error)

NewWayWriterReader -> read and write separated calls

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