Documentation
¶
Overview ¶
Package qb provides SQL query builder utilities for dynamic WHERE/ORDER/GROUP/LIMIT style queries and bulk insert/update/delete statements.
By default package-level helpers keep MySQL-compatible behavior. Use New(...) or *WithDialect helpers to generate SQL for sqlite and postgres. You can also use FromDB(...) with a datasource that exposes Dialect().
JSON helper functions (JsonContains/JsonSet/JsonArrayAppend/JsonArrayInsert/JsonRemove) generate MySQL JSON function syntax.
Index ¶
- Variables
- func AggregateAvg(col string) agBuilder
- func AggregateCount(col string) agBuilder
- func AggregateMax(col string) agBuilder
- func AggregateMin(col string) agBuilder
- func AggregateSum(col string) agBuilder
- func BuildDelete(table string, where map[string]interface{}) (string, []interface{}, error)
- func BuildDeleteWithDialect(dialect, table string, where map[string]interface{}) (string, []interface{}, error)
- func BuildInsert(table string, data []map[string]interface{}) (string, []interface{}, error)
- func BuildInsertIgnore(table string, data []map[string]interface{}) (string, []interface{}, error)
- func BuildInsertIgnoreWithDialect(dialect, table string, data []map[string]interface{}) (string, []interface{}, error)
- func BuildInsertOnDuplicate(table string, data []map[string]interface{}, update map[string]interface{}) (string, []interface{}, error)
- func BuildInsertOnDuplicateWithDialect(dialect, table string, data []map[string]interface{}, ...) (string, []interface{}, error)
- func BuildInsertWithDialect(dialect, table string, data []map[string]interface{}) (string, []interface{}, error)
- func BuildReplaceInsert(table string, data []map[string]interface{}) (string, []interface{}, error)
- func BuildReplaceInsertWithDialect(dialect, table string, data []map[string]interface{}) (string, []interface{}, error)
- func BuildSelect(table string, where map[string]interface{}, selectField []string) (string, []interface{}, error)
- func BuildSelectWithDialect(dialect, table string, where map[string]interface{}, selectField []string) (string, []interface{}, error)
- func BuildUpdate(table string, where map[string]interface{}, update map[string]interface{}) (string, []interface{}, error)
- func BuildUpdateWithDialect(dialect, table string, where map[string]interface{}, ...) (string, []interface{}, error)
- func BuildUpsert(table string, data []map[string]interface{}, conflictColumns []string, ...) (string, []interface{}, error)
- func BuildUpsertWithDialect(dialect, table string, data []map[string]interface{}, conflictColumns []string, ...) (string, []interface{}, error)
- func NamedQuery(sql string, data map[string]interface{}) (string, []interface{}, error)
- func NamedQueryWithDialect(dialect, sql string, data map[string]interface{}) (string, []interface{}, error)
- func OmitEmpty(where map[string]interface{}, omitKey []string) map[string]interface{}
- type AggregateSymbleBuilder
- type AggregateSymbolBuilder
- type Between
- type Builder
- func (b Builder) AggregateQuery(ctx context.Context, db *sql.DB, table string, where map[string]interface{}, ...) (ResultResolver, error)
- func (b Builder) BuildDelete(table string, where map[string]interface{}) (string, []interface{}, error)
- func (b Builder) BuildInsert(table string, data []map[string]interface{}) (string, []interface{}, error)
- func (b Builder) BuildInsertIgnore(table string, data []map[string]interface{}) (string, []interface{}, error)
- func (b Builder) BuildInsertOnDuplicate(table string, data []map[string]interface{}, update map[string]interface{}) (string, []interface{}, error)
- func (b Builder) BuildReplaceInsert(table string, data []map[string]interface{}) (string, []interface{}, error)
- func (b Builder) BuildSelect(table string, where map[string]interface{}, selectField []string) (cond string, vals []interface{}, err error)
- func (b Builder) BuildUpdate(table string, where map[string]interface{}, update map[string]interface{}) (string, []interface{}, error)
- func (b Builder) BuildUpsert(table string, data []map[string]interface{}, conflictColumns []string, ...) (string, []interface{}, error)
- func (b Builder) NamedQuery(sql string, data map[string]interface{}) (string, []interface{}, error)
- type Comparable
- func Custom(query string, args ...interface{}) Comparable
- func JsonArrayAppend(field string, pathAndValuePair ...interface{}) Comparable
- func JsonArrayInsert(field string, pathAndValuePair ...interface{}) Comparable
- func JsonContains(fullJsonPath string, jsonLike interface{}) Comparable
- func JsonRemove(field string, path ...string) Comparable
- func JsonSet(field string, pathAndValuePair ...interface{}) Comparable
- type Dialect
- type DialectProvider
- type Eq
- type Gt
- type Gte
- type In
- type IsZeroer
- type Like
- type Lt
- type Lte
- type Ne
- type NestWhere
- type NotBetween
- type NotIn
- type NotLike
- type NullType
- type OrWhere
- type Raw
- type ResultResolver
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupportedOperator reports there's unsupported operators in where-condition ErrUnsupportedOperator = errors.New("[builder] unsupported operator") )
var IsZeroType = reflect.TypeOf((*IsZeroer)(nil)).Elem()
Functions ¶
func BuildDelete ¶
BuildDelete work as its name says and uses MySQL dialect for backward compatibility.
func BuildDeleteWithDialect ¶
func BuildDeleteWithDialect(dialect, table string, where map[string]interface{}) (string, []interface{}, error)
BuildDeleteWithDialect builds a DELETE query for the given dialect.
func BuildInsert ¶
BuildInsert work as its name says and uses MySQL dialect for backward compatibility.
func BuildInsertIgnore ¶
BuildInsertIgnore work as its name says and uses MySQL dialect for backward compatibility.
func BuildInsertIgnoreWithDialect ¶
func BuildInsertIgnoreWithDialect(dialect, table string, data []map[string]interface{}) (string, []interface{}, error)
BuildInsertIgnoreWithDialect builds an INSERT IGNORE-style query for the given dialect.
func BuildInsertOnDuplicate ¶
func BuildInsertOnDuplicate(table string, data []map[string]interface{}, update map[string]interface{}) (string, []interface{}, error)
BuildInsertOnDuplicate builds an INSERT ... ON DUPLICATE KEY UPDATE clause. This function uses MySQL dialect for backward compatibility.
func BuildInsertOnDuplicateWithDialect ¶
func BuildInsertOnDuplicateWithDialect(dialect, table string, data []map[string]interface{}, update map[string]interface{}) (string, []interface{}, error)
BuildInsertOnDuplicateWithDialect builds an INSERT ... ON DUPLICATE KEY UPDATE query for the given dialect.
func BuildInsertWithDialect ¶
func BuildInsertWithDialect(dialect, table string, data []map[string]interface{}) (string, []interface{}, error)
BuildInsertWithDialect builds an INSERT query for the given dialect.
func BuildReplaceInsert ¶
BuildReplaceInsert work as its name says and uses MySQL dialect for backward compatibility.
func BuildReplaceInsertWithDialect ¶
func BuildReplaceInsertWithDialect(dialect, table string, data []map[string]interface{}) (string, []interface{}, error)
BuildReplaceInsertWithDialect builds a REPLACE-style query for the given dialect.
func BuildSelect ¶
func BuildSelect(table string, where map[string]interface{}, selectField []string) (string, []interface{}, error)
BuildSelect works as its name says and uses MySQL dialect for backward compatibility.
func BuildSelectWithDialect ¶
func BuildSelectWithDialect(dialect, table string, where map[string]interface{}, selectField []string) (string, []interface{}, error)
BuildSelectWithDialect builds a SELECT query for the given dialect.
func BuildUpdate ¶
func BuildUpdate(table string, where map[string]interface{}, update map[string]interface{}) (string, []interface{}, error)
BuildUpdate work as its name says and uses MySQL dialect for backward compatibility.
func BuildUpdateWithDialect ¶
func BuildUpdateWithDialect(dialect, table string, where map[string]interface{}, update map[string]interface{}) (string, []interface{}, error)
BuildUpdateWithDialect builds an UPDATE query for the given dialect.
func BuildUpsert ¶
func BuildUpsert(table string, data []map[string]interface{}, conflictColumns []string, update map[string]interface{}) (string, []interface{}, error)
BuildUpsert builds an upsert query using MySQL dialect for backward-compatible defaults.
For MySQL, conflictColumns is ignored and ON DUPLICATE KEY semantics are used. For PostgreSQL and SQLite, conflictColumns is required when update is non-empty.
func BuildUpsertWithDialect ¶
func BuildUpsertWithDialect(dialect, table string, data []map[string]interface{}, conflictColumns []string, update map[string]interface{}) (string, []interface{}, error)
BuildUpsertWithDialect builds an upsert query for the provided dialect.
func NamedQuery ¶
NamedQuery is used for expressing complex query and uses MySQL placeholders for backward compatibility.
Types ¶
type AggregateSymbleBuilder ¶
type AggregateSymbleBuilder interface {
Symble() string
}
AggregateSymbleBuilder is deprecated, use AggregateSymbolBuilder instead.
type AggregateSymbolBuilder ¶
type AggregateSymbolBuilder interface {
Symbol() string
}
AggregateSymbolBuilder needs to be implemented so executor can get what should be put into `select Symbol() from xxx where yyy`.
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder builds SQL for a specific dialect.
func FromDB ¶
func FromDB(db DialectProvider) (*Builder, error)
FromDB creates a Builder from a provider that exposes Dialect().
func New ¶
New returns a Builder for the provided dialect.
Supported values include:
- mysql, mariadb
- postgres, postgresql, supabase, cockroachdb
- sqlite, sqlite3
func (Builder) AggregateQuery ¶
func (b Builder) AggregateQuery(ctx context.Context, db *sql.DB, table string, where map[string]interface{}, aggregate interface{}) (ResultResolver, error)
AggregateQuery is a helper function to execute the aggregate query and return the result.
func (Builder) BuildDelete ¶
func (b Builder) BuildDelete(table string, where map[string]interface{}) (string, []interface{}, error)
BuildDelete work as its name says.
func (Builder) BuildInsert ¶
func (b Builder) BuildInsert(table string, data []map[string]interface{}) (string, []interface{}, error)
BuildInsert work as its name says.
func (Builder) BuildInsertIgnore ¶
func (b Builder) BuildInsertIgnore(table string, data []map[string]interface{}) (string, []interface{}, error)
BuildInsertIgnore work as its name says.
func (Builder) BuildInsertOnDuplicate ¶
func (b Builder) BuildInsertOnDuplicate(table string, data []map[string]interface{}, update map[string]interface{}) (string, []interface{}, error)
BuildInsertOnDuplicate builds an INSERT ... ON DUPLICATE KEY UPDATE clause.
func (Builder) BuildReplaceInsert ¶
func (b Builder) BuildReplaceInsert(table string, data []map[string]interface{}) (string, []interface{}, error)
BuildReplaceInsert work as its name says.
func (Builder) BuildSelect ¶
func (b Builder) BuildSelect(table string, where map[string]interface{}, selectField []string) (cond string, vals []interface{}, err error)
BuildSelect work as its name says. supported operators including: =,in,>,>=,<,<=,<>,!=. key without operator will be regarded as =. special key begin with _: _orderby,_groupby,_limit,_having. the value of _limit supports int/uint/int64/uint64 and integer slices with one or two elements (ie: []uint{0, 100}). the value of _having must be a map just like where but only support =,in,>,>=,<,<=,<>,!= for more examples,see README.md or open a issue.
func (Builder) BuildUpdate ¶
func (b Builder) BuildUpdate(table string, where map[string]interface{}, update map[string]interface{}) (string, []interface{}, error)
BuildUpdate work as its name says.
type Comparable ¶
type Comparable interface {
Build() ([]string, []interface{})
}
Comparable requires type implements the Build method
func Custom ¶
func Custom(query string, args ...interface{}) Comparable
func JsonArrayAppend ¶
func JsonArrayAppend(field string, pathAndValuePair ...interface{}) Comparable
JsonArrayAppend generates JSON object args and calls JSON_ARRAY_APPEND.
MySQL only.
usage update := map[string]interface{}{"_custom_xxx": builder.JsonArrayAppend(field, "$", 1, "$[last]", []string{"2","3"}}
func JsonArrayInsert ¶
func JsonArrayInsert(field string, pathAndValuePair ...interface{}) Comparable
JsonArrayInsert generates JSON object args and calls JSON_ARRAY_INSERT to insert at index.
MySQL only.
usage update := map[string]interface{}{"_custom_xxx": builder.JsonArrayInsert(field, "$[0]", 1, "$[0]", []string{"2","3"}}
func JsonContains ¶
func JsonContains(fullJsonPath string, jsonLike interface{}) Comparable
JsonContains checks whether target JSON contains all items in jsonLike.
MySQL only.
notice: fullJsonPath should hard code, never from user input.
func JsonRemove ¶
func JsonRemove(field string, path ...string) Comparable
JsonRemove call MySQL JSON_REMOVE function; remove element from Array or Map path removed in order, prev remove affect the later operation, maybe the array shrink
MySQL only.
remove last array element; update := map[string]interface{}{"_custom_xxx":builder.JsonRemove(field,'$.list[last]')} remove element; update := map[string]interface{}{"_custom_xxx":builder.JsonRemove(field,'$.key0')}
func JsonSet ¶
func JsonSet(field string, pathAndValuePair ...interface{}) Comparable
JsonSet aims to set/update json field values.
MySQL only.
notice: jsonPath should hard code, never from user input;
usage update := map[string]interface{}{"_custom_xxx": builder.JsonSet(field, "$.code", 1, "$.user_info", map[string]any{"name": "", "age": 18})}
type Dialect ¶
type Dialect string
Dialect represents a SQL dialect that qb can generate queries for.
type DialectProvider ¶
type DialectProvider interface {
Dialect() string
}
DialectProvider describes a type that can expose SQL dialect.
type NestWhere ¶
type NestWhere []Comparable
type NotBetween ¶
type NotBetween map[string][]interface{}
func (NotBetween) Build ¶
func (nbt NotBetween) Build() ([]string, []interface{})
type NullType ¶
type NullType byte
NullType is the NULL type in mysql
const ( // IsNull the same as `is null` IsNull NullType // IsNotNull the same as `is not null` IsNotNull )
type OrWhere ¶
type OrWhere []Comparable
type ResultResolver ¶
ResultResolver is a helper for retrieving data caller should know the type and call the responding method
func AggregateQuery ¶
func AggregateQuery(ctx context.Context, db *sql.DB, table string, where map[string]interface{}, aggregate interface{}) (ResultResolver, error)
AggregateQuery is a helper function to execute the aggregate query and return the result
func AggregateQueryWithDialect ¶
func AggregateQueryWithDialect(ctx context.Context, db *sql.DB, dialect, table string, where map[string]interface{}, aggregate interface{}) (ResultResolver, error)
AggregateQueryWithDialect executes aggregate query with the provided dialect.