query

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Placeholders

func Placeholders(repeat int) string

Placeholders makes n-th repeats of Go's SQL placeholder(?), joining them by comma(,).

Example:

Placeholder(2) // ?,?

func QuoteColumn

func QuoteColumn(col string) string

QuoteColumn surrounds SQL identifiers with backquote, keeping some meta-characters "*", ".", "`" intact.

Example:

QuoteColumn("users.id") // `users`.`id`
QuoteColumn("users.*") // `users`.*

func QuoteColumns

func QuoteColumns(str ...string) string

QuoteColumns quotes each string and joins them by comma(,).

Types

type Builder

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

Builder is a dynamic SQL query builder.

func NewBuilder

func NewBuilder(base ...Query) *Builder

func (*Builder) Add

func (b *Builder) Add(q ...Query) *Builder

Add appends given Queries components.

func (*Builder) Build

func (b *Builder) Build() Query

Build constructs final SQL statement, joining by single space(" ").

func (*Builder) Clone

func (b *Builder) Clone() *Builder

Clone makes a shallow copy of the builder.

func (*Builder) Join

func (b *Builder) Join(sep string) Query

Join joins accumulative query components by given separator.

func (*Builder) Query

func (b *Builder) Query(str string, args ...any) *Builder

Query appends the given query component and arguments into the buffer.

Example:

b.Query(":?", query.V(1,2))

is the same as:

b.Add(query.Q(":?", query.V(1,2)))

func (*Builder) Sprintf

func (b *Builder) Sprintf(str string, args ...any) *Builder

Sprintf is short-hand for fmt.Sprintf.

Example:

b.Sprintf("%s", "go")

is the same as:

b.Query(fmt.Sprintf("%s", "go"))

type Condition

type Condition interface {
	Query
	And(str string, args ...any)
	Or(str string, args ...any)
	AndCond(other Condition)
	OrCond(other Condition)
}

func Cond

func Cond(str string, args ...any) Condition

func CondFrom

func CondFrom(q ...Query) Condition

type KeyIterator

type KeyIterator[T any] interface {
	Get(i int) (string, T)
	Keys() []string
	Values() []T
	Size() int
	Map() map[string]T
}

func NewKeyIterator

func NewKeyIterator[T any](data map[string]T) KeyIterator[T]

type Query

type Query interface {
	Query() (string, []any, error)
}

func Cols

func Cols(cols ...string) Query

Cols wraps given identifiers like column, and table with backquote as possible. It is used for embedding table names or columns into queries dynamically. If multiple values are given, they will be joined by a comma(,).

Example:

Cols("aaa","bbb") // `aaa`,`bbb`
Cols("users.*") // `users`.*

func New

func New(q string, args ...any) Query

New returns Query based on given query and arguments. First argument query can contain exql placeholder format (:?) with the corresponding Query in rest arguments. Given query component will be interpolated internally and embedded into the final SQL statement. Except (:?) placeholders, all static statements will be embedded barely with no assertions. You must pay attention to the input query if it is variable.

func Q

func Q(q string, args ...any) Query

Q is a short-hand version of New.

func Set

func Set(m map[string]any) Query

Set transforms map into "key = value" assignment expression in SQL. Example:

values := map[string]any{ "name": "go", "age": 20}
db.Exec("update users set :? where id = ?", query.Set(values, 1))

is the same as:

db.DB().Exec("update users set age = ?, name = ? where id = ?", 20, "go", 1)

func V

func V(a ...any) Query

V wraps one or more values for the prepared statement. It counts number of values and interpolates Go's SQL placeholder(?), holding values for later. Multiple values will be joined by comma(,).

Example:

V(1,"a") // ?,? -> query | [1,"a"] -> arguments

The code below

db.Query(query.New("select * from users where id in (:?)", query.V(1,2)))

is the same as:

db.DB().Query("select * from users where id in (?,?)", 1, 2)

func Vals

func Vals[T any](vals []T) Query

Vals is another form of V that accepts a slice in generic type.

Jump to

Keyboard shortcuts

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