pan: darlinggo.co/pan Index | Files

package pan

import "darlinggo.co/pan"

Index

Package Files

query.go reflect.go

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")
)

func Column Uses

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 Uses

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 Uses

func Placeholders(num int) string

Placeholders returns a formatted string containing `num` placeholders. The placeholders will be comma-separated.

func Table Uses

func Table(t SQLTableNamer) string

Table is a convenient shorthand wrapper for the GetSQLTableName method on `t`.

func Unmarshal Uses

func Unmarshal(s Scannable, dst interface{}, additional ...interface{}) error

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`.

type ColumnList Uses

type ColumnList []string

ColumnList represents a set of columns.

func Columns Uses

func Columns(s SQLTableNamer, flags ...Flag) ColumnList

Columns returns a ColumnList containing the names of the columns in `s`.

func (ColumnList) String Uses

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 Uses

type ErrWrongNumberArgs struct {
    NumExpected int
    NumFound    int
}

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 Uses

func (e ErrWrongNumberArgs) Error() string

Error fills the error interface.

type Flag Uses

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 Uses

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 Uses

func Insert(values ...SQLTableNamer) *Query

Insert returns a Query instance containing SQL that will insert the passed `values` into the database. All `values` will be inserted into the same table, so invalid SQL will be generated if all `values` are not the same type.

func New Uses

func New(query string) *Query

New returns a new Query instance, primed for use.

func (*Query) Args Uses

func (q *Query) Args() []interface{}

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 Uses

func (q *Query) Assign(obj SQLTableNamer, property string, value interface{}) *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 Uses

func (q *Query) Comparison(obj SQLTableNamer, property, operator string, value interface{}) *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) Expression Uses

func (q *Query) Expression(key string, values ...interface{}) *Query

Expression adds a raw string and optional values to the Query’s buffer.

func (*Query) Flush Uses

func (q *Query) Flush(join string) *Query

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 Uses

func (q *Query) In(obj SQLTableNamer, property string, values ...interface{}) *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 Uses

func (q *Query) Limit(limit int64) *Query

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 Uses

func (q *Query) MySQLString() (string, error)

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 Uses

func (q *Query) Offset(offset int64) *Query

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 Uses

func (q *Query) OrderBy(column string) *Query

OrderBy adds an expression to the Query’s buffer in the form of "ORDER BY column".

func (*Query) OrderByDesc Uses

func (q *Query) OrderByDesc(column string) *Query

OrderByDesc adds an expression to the Query’s buffer in the form of "ORDER BY column DESC".

func (*Query) PostgreSQLString Uses

func (q *Query) PostgreSQLString() (string, error)

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) String Uses

func (q *Query) String() string

String returns a version of your Query with all the arguments in the place of their placeholders. It does not do any sanitization, and is vulnerable to SQL injection. It is meant as a debugging aid, not to be executed. The string will almost certainly not be valid SQL.

func (*Query) Where Uses

func (q *Query) Where() *Query

Where adds a WHERE keyword to the Query’s buffer, then calls Flush on the Query, using a space as the join parameter.

Where can only be called once per Query; calling it multiple times on the same Query will be no-ops after the first.

type SQLTableNamer Uses

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.

type Scannable Uses

type Scannable interface {
    Scan(dst ...interface{}) error
    Columns() ([]string, error)
}

Scannable defines a type that can insert the results of a Query into the SQLTableNamer a Query was built from, and can list off the column names, in order, that those results represent.

Package pan imports 9 packages (graph). Updated 2018-09-15. Refresh now. Tools for package owners.