sqlutil

package
v2.11.2 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: MIT Imports: 17 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BatchInsert

func BatchInsert(conn Executor, rows any, 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 any, opts ...InsertOpt) (sql string, args []any)

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

type Bitmap[T constraints.Integer] struct {
	// contains filtered or unexported fields
}

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 NewBitmap

func NewBitmap[T constraints.Integer](val *T) Bitmap[T]

NewBitmap returns a new bitmap value.

func (*Bitmap[T]) Clear

func (b *Bitmap[T]) Clear(mask int)

Clear clears mask from the bitmap.

func (Bitmap[T]) Get

func (b Bitmap[T]) Get(mask int) bool

Get returns whether mask is set in the bitmap.

func (*Bitmap[T]) Scan

func (b *Bitmap[T]) Scan(src any) error

Scan implements sql.Scanner interface.

func (*Bitmap[T]) Set

func (b *Bitmap[T]) Set(mask int)

Set sets mask to the bitmap.

func (Bitmap[T]) Underlying

func (b Bitmap[T]) Underlying() T

Underlying returns the underlying integer value.

func (Bitmap[T]) Value

func (b Bitmap[T]) 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 ...any) *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 ...any) *Condition

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

func (*Condition) AndCond

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

AndCond combines the given Condition using "AND" operator.

func (*Condition) Build

func (p *Condition) Build() (string, []any)

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

func (*Condition) IfAnd

func (p *Condition) IfAnd(cond bool, clause string, args ...any) *Condition

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

func (*Condition) IfAndCond

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 ...any) *Condition

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

func (*Condition) IfOrCond

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 ...any) *Condition

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

func (*Condition) OrCond

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 ...any) (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 ...any) (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

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

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

type JSON struct {
	ezmap.Map
}

JSON holds a map[string]any 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

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

func (*JSON) Scan

func (p *JSON) Scan(src any) error

Scan implements sql.Scanner interface.

func (*JSON) UnmarshalJSON

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

func (JSON) Value

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

Value implements driver.Valuer interface.

type LazyBinary

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

func NewLazyBinary(raw []byte) LazyBinary

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

func (*LazyBinary) Get

func (p *LazyBinary) Get(unmarshalFunc Unmarshaler) (any, 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

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

GetBytes returns the underlying byte slice.

func (*LazyBinary) Scan

func (p *LazyBinary) Scan(src any) error

Scan implements sql.Scanner interface.

func (*LazyBinary) Set

func (p *LazyBinary) Set(b []byte, data any)

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

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

Value implements driver.Valuer interface.

type Unmarshaler

type Unmarshaler func([]byte) (any, 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