Documentation
¶
Index ¶
- Variables
- func Column(s SQLTableNamer, property string, flags ...Flag) string
- func ColumnValues(s SQLTableNamer) []interface{}
- func Placeholders(num int) string
- func Table(t SQLTableNamer) string
- func Unmarshal(s Scannable, dst interface{}, additional ...interface{}) error
- type ColumnList
- type ErrWrongNumberArgs
- type Flag
- type Query
- func (q *Query) AppendToParent() *Query
- func (q *Query) Args() []any
- func (q *Query) Assign(obj SQLTableNamer, property string, value any) *Query
- func (q *Query) Comparison(obj SQLTableNamer, property, operator string, value any) *Query
- func (q *Query) ComplexExpression(query string) *Query
- func (q *Query) Expression(key string, values ...any) *Query
- func (q *Query) Flush(join string) *Query
- func (q *Query) In(obj SQLTableNamer, property string, values ...any) *Query
- func (q *Query) Limit(limit int64) *Query
- func (q *Query) MySQLString() (string, error)
- func (q *Query) Offset(offset int64) *Query
- func (q *Query) OrderBy(column string) *Query
- func (q *Query) OrderByDesc(column string) *Query
- func (q *Query) PostgreSQLString() (string, error)
- func (q *Query) SQLiteString() (string, error)
- func (q *Query) String() string
- func (q *Query) Where() *Query
- type SQLTableNamer
- type Scannable
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNeedsFlush is returned when a Query is used while it has expressions left in its buffer // that haven’t been flushed using the Query’s Flush method. ErrNeedsFlush = errors.New("Query has dangling buffer, its Flush method needs to be called") )
Functions ¶
func Column ¶
func Column(s SQLTableNamer, property string, flags ...Flag) string
Column returns the name of the column that `property` maps to for `s`. `property` must be the exact name of a property on `s`, or Column will panic.
func ColumnValues ¶
func ColumnValues(s SQLTableNamer) []interface{}
ColumnValues returns the values in `s` for each column in `s`, in the same order `Columns` returns the names.
func Placeholders ¶
Placeholders returns a formatted string containing `num` placeholders. The placeholders will be comma-separated.
func Table ¶
func Table(t SQLTableNamer) string
Table is a convenient shorthand wrapper for the GetSQLTableName method on `t`.
func Unmarshal ¶
Unmarshal reads the Scannable `s` into the variable at `d`, and returns an error if it is unable to. If there are more values than `d` has properties associated with columns, `additional` can be supplied to catch the extra values. The variables in `additional` must be a compatible type with and be in the same order as the columns of `s`.
Types ¶
type ColumnList ¶
type ColumnList []string
ColumnList represents a set of columns.
func Columns ¶
func Columns(s SQLTableNamer, flags ...Flag) ColumnList
Columns returns a ColumnList containing the names of the columns in `s`.
func (ColumnList) String ¶
func (c ColumnList) String() string
String returns the columns in the ColumnList, joined by ", ", often used to create an SQL-formatted list of column names.
type ErrWrongNumberArgs ¶
ErrWrongNumberArgs is returned when you’ve generated a Query with a certain number of placeholders, but supplied a different number of arguments. The NumExpected property holds the number of placeholders in the Query, and the NumFound property holds the number of arguments supplied.
func (ErrWrongNumberArgs) Error ¶
func (e ErrWrongNumberArgs) Error() string
Error fills the error interface.
type Flag ¶
type Flag int
Flag represents a modification to the returned values from our Column or Columns functions. See the constants defined in this package for valid values.
const ( // FlagFull returns columns in their absolute table.column format. FlagFull Flag = iota // FlagTicked returns columns using ticks to quote the column name, like `column`. FlagTicked // FlagDoubleQuoted returns columns using double quotes to quote the column name, like "column". FlagDoubleQuoted )
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query represents an SQL query that is being built. It can be used from its empty value, or it can be instantiated with the New method.
Query instances are used to build SQL query string and argument lists, and consist of an SQL string and a buffer. The Flush method must be called before the Query is used, or you may leave expressions dangling in the buffer.
The Query type is not meant to be concurrency-safe; if you need to modify it from multiple goroutines, you need to coordinate that access yourself.
func Insert ¶
func Insert[Type SQLTableNamer](values ...Type) *Query
Insert returns a Query instance containing SQL that will insert the passed `values` into the database.
func (*Query) AppendToParent ¶ added in v0.4.0
AppendToParent sets the entire Query as a single expression on its parent Query. It should only be called on Querys created by calling ComplexExpression; calling it on a Query that isn't a ComplexExpression will panic.
It returns the parent of the Query.
func (*Query) Args ¶
Args returns a slice of the arguments attached to the Query, which should be used when executing your SQL to fill the placeholders.
Note that Args returns its internal slice; you should copy the returned slice over before modifying it.
func (*Query) Assign ¶
func (q *Query) Assign(obj SQLTableNamer, property string, value any) *Query
Assign adds an expression to the Query’s buffer in the form of "column = ?", and adds `value` to the arguments for this query. `obj` and `property` are used to determine the column. `property` must exactly match the name of a property on `obj`, or the call will panic.
func (*Query) Comparison ¶
func (q *Query) Comparison(obj SQLTableNamer, property, operator string, value any) *Query
Comparison adds a comparison expression to the Query’s buffer. A comparison takes the form of `column operator ?`, with `value` added as an argument to the Query. Column is determined by finding the column name for the passed property on the passed SQLTableNamer. The passed property must be a string that matches, identically, the property name; if it does not, it will panic.
func (*Query) ComplexExpression ¶ added in v0.4.0
ComplexExpression starts a Query with a new buffer, so it can be flushed without affecting the outer Query's buffer of expressions.
Once a ComplexExpression has been flushed, it should have its AppendToParent method called, which sets the entire ComplexExpression as a single expression on its parent Query.
func (*Query) Expression ¶
Expression adds a raw string and optional values to the Query’s buffer.
func (*Query) Flush ¶
Flush flushes the expressions in the Query’s buffer, adding them to the SQL string being built. It must be called before a Query can be used. Any pending expressions (anything since the last Flush or since the Query was instantiated) are joined using `join`, then added onto the Query’s SQL string, with a space between the SQL string and the expressions.
func (*Query) In ¶
func (q *Query) In(obj SQLTableNamer, property string, values ...any) *Query
In adds an expression to the Query’s buffer in the form of "column IN (value, value, value)". `values` are the variables to match against, and `obj` and `property` are used to determine the column. `property` must exactly match the name of a property on `obj`, or the call will panic.
func (*Query) Limit ¶
Limit adds an expression to the Query’s buffer in the form of "LIMIT ?", and adds `limit` as an argument to the Query.
func (*Query) MySQLString ¶
MySQLString returns a SQL string that can be passed to MySQL to execute your query. If the number of placeholders do not match the number of arguments provided to your Query, an ErrWrongNumberArgs error will be returned. If there are still expressions left in the buffer (meaning the Flush method wasn't called) an ErrNeedsFlush error will be returned.
func (*Query) Offset ¶
Offset adds an expression to the Query’s buffer in the form of "OFFSET ?", and adds `offset` as an argument to the Query.
func (*Query) OrderBy ¶
OrderBy adds an expression to the Query’s buffer in the form of "ORDER BY column".
func (*Query) OrderByDesc ¶
OrderByDesc adds an expression to the Query’s buffer in the form of "ORDER BY column DESC".
func (*Query) PostgreSQLString ¶
PostgreSQLString returns an SQL string that can be passed to PostgreSQL to execute your query. If the number of placeholders do not match the number of arguments provided to your Query, an ErrWrongNumberArgs error will be returned. If there are still expressions left in the buffer (meaning the Flush method wasn't called) an ErrNeedsFlush error will be returned.
func (*Query) SQLiteString ¶ added in v0.3.0
SQLiteString returns a SQL string that can be passed to SQLite to execute your query. If the number of placeholders do not match the number of arguments provided to your Query, an ErrWrongNumberArgs error will be returned. If there are still expressions left in the buffer (meaning the Flush method wasn't called) an ErrNeedsFlush error will be returned.
type SQLTableNamer ¶
type SQLTableNamer interface {
GetSQLTableName() string
}
SQLTableNamer is used to represent a type that corresponds to an SQL table. It must define the GetSQLTableName method, returning the name of the SQL table to store data for that type in.