sqlutil

package
v0.17.3 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BatchInsert

func BatchInsert(conn Executor, rows interface{}, opts ...InsertOpt) (result sql.Result, err error)

BatchInsert generates SQL and executes it on the provided Executor. The provided param `rows` must be a slice of struct or pointer to struct, and the slice must have at least one element, or it returns error.

func MakeBatchInsertSQL

func MakeBatchInsertSQL(rows interface{}, opts ...InsertOpt) (sql string, args []interface{})

MakeBatchInsertSQL generates SQL and returns the arguments to execute on database connection. The provided param `rows` must be a slice of struct or pointer to struct, and the slice must have at least one element, or it panics.

The returned query uses `?` as parameter placeholder, if you are using this function with database which don't use `?` as placeholder, you may check the `Rebind` function from package `github.com/jmoiron/sqlx` to replace placeholders.

Types

type Bitmap added in v0.5.0

type Bitmap int

Bitmap represents a bitmap value, it implements sql/driver.Valuer and sql.Scanner. Bitmap provides Get, Set and Clear methods to manipulate the bitmap value.

func (*Bitmap) Clear added in v0.5.0

func (b *Bitmap) Clear(mask int)

func (Bitmap) Get added in v0.5.0

func (b Bitmap) Get(mask int) bool

func (*Bitmap) Scan added in v0.5.0

func (b *Bitmap) Scan(src interface{}) error

Scan implements sql.Scanner interface.

func (*Bitmap) Set added in v0.5.0

func (b *Bitmap) Set(mask int)

func (Bitmap) Value added in v0.5.0

func (b Bitmap) Value() (driver.Value, error)

Value implements driver.Valuer interface.

type Condition

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

Condition represents a query filter to work with SQL query.

func And

func And(conds ...*Condition) *Condition

And returns a new *Condition which is combination of given conditions using the "AND" operator.

func Cond

func Cond(clause string, args ...interface{}) *Condition

Cond creates a new *Condition from the given params.

func Or

func Or(conds ...*Condition) *Condition

Or returns a new *Condition which is combination of given conditions using the "OR" operator.

func (*Condition) And

func (p *Condition) And(clause string, args ...interface{}) *Condition

And combines the given query filter to Condition using "AND" operator.

func (*Condition) AndCond added in v0.15.3

func (p *Condition) AndCond(c *Condition) *Condition

AndCond combines the given Condition using "AND" operator.

func (*Condition) Build

func (p *Condition) Build() (string, []interface{})

Build returns the query filter clause and parameters of the Condition.

func (*Condition) IfAnd

func (p *Condition) IfAnd(cond bool, clause string, args ...interface{}) *Condition

IfAnd checks cond, if cond is true, it combines the query filter to Condition using "AND" operator.

func (*Condition) IfAndCond added in v0.15.3

func (p *Condition) IfAndCond(cond bool, c *Condition) *Condition

IfAndCond checks cond, if cond is true, it combines the given Condition using "AND" operator.

func (*Condition) IfOr

func (p *Condition) IfOr(cond bool, clause string, args ...interface{}) *Condition

IfOr checks cond, it cond is true, it combines the query filter to Condition using "OR" operator.

func (*Condition) IfOrCond added in v0.15.3

func (p *Condition) IfOrCond(cond bool, c *Condition) *Condition

IfOrCond checks cond, if cond is true, it combines the given Condition using "OR" operator.

func (*Condition) Or

func (p *Condition) Or(clause string, args ...interface{}) *Condition

Or combines the given query filter to Condition using "OR" operator.

func (*Condition) OrCond added in v0.15.3

func (p *Condition) OrCond(c *Condition) *Condition

OrCond combines the given Condition using "OR" operator.

func (*Condition) String

func (p *Condition) String() string

String returns the string representation of the Condition.

type ContextExecutor

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

ContextExecutor is an optional interface to support context execution. If `BatchInsert` function is called with `WithContext` option, and the provided Executor implements this interface, then the method `ExecContext` will be called instead of the method `Exec`.

type Executor

type Executor interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
}

Executor is the minimal interface for batch inserting requires. The interface is implemented by *sql.DB, *sql.Tx, *sqlx.DB, *sqlx.Tx.

type InsertOpt

type InsertOpt func(*InsertOptions)

InsertOpt represents an inserting option to use with batch inserting operation.

func OmitColumns added in v0.5.2

func OmitColumns(cols ...string) InsertOpt

OmitColumns exclude given columns from the generated query.

func OnConflict

func OnConflict(clause string) InsertOpt

OnConflict appends the postgresql "ON CONFLICT" clause to the generated query.

func OnDuplicateKey

func OnDuplicateKey(clause string) InsertOpt

OnDuplicateKey appends the mysql "ON DUPLICATE KEY" clause to the generated query.

func WithContext

func WithContext(ctx context.Context) InsertOpt

WithContext makes the query executed with `ExecContext` if available.

func WithIgnore

func WithIgnore() InsertOpt

WithIgnore adds the mysql "IGNORE" adverb to the the generated query.

func WithQuote added in v0.2.11

func WithQuote(quote string) InsertOpt

WithQuote quotes the table name and column names with the given string.

func WithTable

func WithTable(tableName string) InsertOpt

WithTable makes the generated query to use provided table name.

type InsertOptions

type InsertOptions struct {
	Context   context.Context
	TableName string
	Quote     string
	OmitCols  []string

	Ignore         bool
	OnDuplicateKey string
	OnConflict     string
}

InsertOptions holds options to use with batch inserting operation.

type JSON added in v0.9.0

type JSON struct {
	gemap.Map
}

JSON holds a map[string]interface{} value, it implements sql/driver.Valuer and sql.Scanner. It uses JSON to do serialization.

JSON embeds a gemap.Map, thus all methods defined on gemap.Map is also available from a JSON instance.

func (JSON) MarshalJSON added in v0.16.3

func (p JSON) MarshalJSON() ([]byte, error)

func (*JSON) Scan added in v0.9.0

func (p *JSON) Scan(src interface{}) error

Scan implements sql.Scanner interface.

func (*JSON) UnmarshalJSON added in v0.16.3

func (p *JSON) UnmarshalJSON(data []byte) error

func (JSON) Value added in v0.9.0

func (p JSON) Value() (driver.Value, error)

Value implements driver.Valuer interface.

type LazyBinary added in v0.5.0

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

LazyBinary is a lazy wrapper around a binary value, where the underlying object will be unmarshalled the first time it is needed and cached. It implements sql/driver.Valuer and sql.Scanner.

LazyBinary provides same concurrency safety as []byte, it's safe for concurrent read, but not safe for concurrent write or read/write.

See types_test.go for example usage.

func NewLazyBinary added in v0.5.0

func NewLazyBinary(raw []byte) LazyBinary

NewLazyBinary creates a new lazy binary wrapper, delaying the unmarshalling work until it is first needed.

func (*LazyBinary) Get added in v0.5.0

func (p *LazyBinary) Get(unmarshalFunc Unmarshaler) (interface{}, error)

Get returns the underlying data wrapped by the LazyBinary wrapper, if the data has not been unmarshalled, it will be unmarshalled using the provided unmarshalFunc. The unmarshalling work will do only once, the result data and error will be cached and reused for further calling.

func (*LazyBinary) GetBytes added in v0.5.0

func (p *LazyBinary) GetBytes() []byte

GetBytes returns the underlying byte slice.

func (*LazyBinary) Scan added in v0.5.0

func (p *LazyBinary) Scan(src interface{}) error

Scan implements sql.Scanner interface.

func (*LazyBinary) Set added in v0.5.0

func (p *LazyBinary) Set(b []byte, data interface{})

Set sets the data and marshaled bytes to the LazyBinary wrapper. If the param data is nil, the underlying cache will be removed.

func (LazyBinary) Value added in v0.5.0

func (p LazyBinary) Value() (driver.Value, error)

Value implements driver.Valuer interface.

type Unmarshaler added in v0.5.0

type Unmarshaler func([]byte) (interface{}, error)

Unmarshaler is a function which unmarshalls data from a byte slice.

Jump to

Keyboard shortcuts

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