queries

package
v0.0.0-...-fc4761b Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2020 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	JoinInner joinKind = iota
	JoinOuterLeft
	JoinOuterRight
	JoinNatural
	JoinOuterFull
)

Join type constants

View Source
const (
	EQ  operator = "="
	NEQ operator = "!="
	LT  operator = "<"
	LTE operator = "<="
	GT  operator = ">"
	GTE operator = ">="
)

Supported operations

Variables

This section is empty.

Functions

func AppendFrom

func AppendFrom(q *Query, from ...string)

AppendFrom on the query.

func AppendFullOuterJoin

func AppendFullOuterJoin(q *Query, clause string, args ...interface{})

AppendFullOuterJoin on the query.

func AppendGroupBy

func AppendGroupBy(q *Query, clause string)

AppendGroupBy on the query.

func AppendHaving

func AppendHaving(q *Query, clause string, args ...interface{})

AppendHaving on the query.

func AppendIn

func AppendIn(q *Query, clause string, args ...interface{})

AppendIn on the query.

func AppendInnerJoin

func AppendInnerJoin(q *Query, clause string, args ...interface{})

AppendInnerJoin on the query.

func AppendLeftOuterJoin

func AppendLeftOuterJoin(q *Query, clause string, args ...interface{})

AppendLeftOuterJoin on the query.

func AppendLoad

func AppendLoad(q *Query, relationships string)

AppendLoad on the query.

func AppendNotIn

func AppendNotIn(q *Query, clause string, args ...interface{})

AppendNotIn on the query.

func AppendOrderBy

func AppendOrderBy(q *Query, clause string, args ...interface{})

AppendOrderBy on the query.

func AppendRightOuterJoin

func AppendRightOuterJoin(q *Query, clause string, args ...interface{})

AppendRightOuterJoin on the query.

func AppendSelect

func AppendSelect(q *Query, columns ...string)

AppendSelect on the query.

func AppendWhere

func AppendWhere(q *Query, clause string, args ...interface{})

AppendWhere on the query.

func AppendWhereLeftParen

func AppendWhereLeftParen(q *Query)

AppendWhereLeftParen creates a right left in the where expression

func AppendWhereRightParen

func AppendWhereRightParen(q *Query)

AppendWhereRightParen creates a right paren in the where expression

func AppendWith

func AppendWith(q *Query, clause string, args ...interface{})

AppendWith on the query.

func Apply

func Apply(q *Query, mods ...QueryMod)

Apply the query mods to the Query object

func Assign

func Assign(dst, src interface{})

Assign assigns a value to another using reflection. Dst must be a pointer.

func Bind

func Bind(rows *sql.Rows, obj interface{}) error

Bind inserts the rows into the passed in object pointer, because the caller owns the rows it is imperative to note that the caller MUST both close the rows and check for errors on the rows.

If you neglect closing the rows your application may have a memory leak if the rows are not implicitly closed by iteration alone. If you neglect checking the rows.Err silent failures may occur in your application.

Valid types to bind to are: *Struct, []*Struct, and []Struct. Keep in mind if you use []Struct that Bind will be doing copy-by-value as a method of keeping heap memory usage low which means if your Struct contains reference types/pointers you will see incorrect results, do not use []Struct with a Struct with reference types.

Bind rules:

  • Struct tags control bind, in the form of: `boil:"name,bind"`
  • If "name" is omitted the sql column names that come back are TitleCased and matched against the field name.
  • If the "name" part of the struct tag is specified, the given name will be used instead of the struct field name for binding.
  • If the "name" of the struct tag is "-", this field will not be bound to.
  • If the ",bind" option is specified on a struct field and that field is a struct itself, it will be recursed into to look for fields for binding.

Example usage:

type JoinStruct struct {
  // User1 can have it's struct fields bound to since it specifies
  // ,bind in the struct tag, it will look specifically for
  // fields that are prefixed with "user." returning from the query.
  // For example "user.id" column name will bind to User1.ID
  User1      *models.User `boil:"user,bind"`
  // User2 will follow the same rules as noted above except it will use
  // "friend." as the prefix it's looking for.
  User2      *models.User `boil:"friend,bind"`
  // RandomData will not be recursed into to look for fields to
  // bind and will not be bound to because of the - for the name.
  RandomData myStruct     `boil:"-"`
  // Date will not be recursed into to look for fields to bind because
  // it does not specify ,bind in the struct tag. But it can be bound to
  // as it does not specify a - for the name.
  Date       time.Time
}

models.Users(
  queries.InnerJoin("users as friend on users.friend_id = friend.id")
).Bind(&joinStruct)

For custom objects that want to use eager loading, please see the loadRelationships function.

func BindMapping

func BindMapping(typ reflect.Type, mapping map[string]uint64, cols []string) ([]uint64, error)

BindMapping creates a mapping that helps look up the pointer for the column given.

func BuildQuery

func BuildQuery(q *Query) (string, []interface{})

BuildQuery builds a query object into the query string and it's accompanying arguments. Using this method allows query building without immediate execution.

func Equal

func Equal(a, b interface{}) bool

Equal is different to reflect.DeepEqual in that it's both less efficient less magical, and dosen't concern itself with a wide variety of types that could be present but it does use the driver.Valuer interface since many types that will go through database things will use these.

We're focused on basic types + []byte. Since we're really only interested in things that are typically used for primary keys in a database.

Choosing not to use the DefaultParameterConverter here because sqlboiler doesn't generate pointer columns.

func GetSelect

func GetSelect(q *Query) []string

GetSelect from the query

func IsNil

func IsNil(val interface{}) bool

IsNil is a more generic version of IsValuerNil, will check to make sure it's not a valuer first.

func IsValuerNil

func IsValuerNil(val driver.Valuer) bool

IsValuerNil returns true if the valuer's value is null.

func MakeStructMapping

func MakeStructMapping(typ reflect.Type) map[string]uint64

MakeStructMapping creates a map of the struct to be able to quickly look up its pointers and values by name.

func MustTime

func MustTime(val driver.Valuer) time.Time

MustTime retrieves a time value from a valuer.

func NonZeroDefaultSet

func NonZeroDefaultSet(defaults []string, obj interface{}) []string

NonZeroDefaultSet returns the fields included in the defaults slice that are non zero values

func PtrsFromMapping

func PtrsFromMapping(val reflect.Value, mapping []uint64) []interface{}

PtrsFromMapping expects to be passed an addressable struct and a mapping of where to find things. It pulls the pointers out referred to by the mapping.

func Rels

func Rels(r ...string) string

Rels is an alias for strings.Join to make it easier to use relationship name constants in Load.

func SetArgs

func SetArgs(q *Query, args ...interface{})

SetArgs is primarily for re-use of a query so that the query text does not need to be re-generated, useful if you're performing the same query with different arguments over and over.

func SetCount

func SetCount(q *Query)

SetCount on the query.

func SetDelete

func SetDelete(q *Query)

SetDelete on the query.

func SetDialect

func SetDialect(q *Query, dialect *database.Dialect)

SetDialect on the query.

func SetDistinct

func SetDistinct(q *Query, distinct string)

SetDistinct on the query.

func SetFor

func SetFor(q *Query, clause string)

SetFor on the query.

func SetFrom

func SetFrom(q *Query, from ...string)

SetFrom replaces the current from statements.

func SetLastInAsOr

func SetLastInAsOr(q *Query)

SetLastInAsOr is an alias for SetLastWhereAsOr

func SetLastWhereAsOr

func SetLastWhereAsOr(q *Query)

SetLastWhereAsOr sets the or separator for the tail "WHERE" in the slice

func SetLimit

func SetLimit(q *Query, limit int)

SetLimit on the query.

func SetLoad

func SetLoad(q *Query, relationships ...string)

SetLoad on the query.

func SetLoadMods

func SetLoadMods(q *Query, rel string, appl Applicator)

SetLoadMods on the query.

func SetOffset

func SetOffset(q *Query, offset int)

SetOffset on the query.

func SetSQL

func SetSQL(q *Query, sql string, args ...interface{})

SetSQL on the query.

func SetScanner

func SetScanner(scanner sql.Scanner, val driver.Value)

SetScanner attempts to set a scannable value on a scanner.

func SetSelect

func SetSelect(q *Query, sel []string)

SetSelect on the query.

func SetUpdate

func SetUpdate(q *Query, cols map[string]interface{})

SetUpdate on the query.

func ValuesFromMapping

func ValuesFromMapping(val reflect.Value, mapping []uint64) []interface{}

ValuesFromMapping expects to be passed an addressable struct and a mapping of where to find things. It pulls the pointers out referred to by the mapping.

Types

type Applicator

type Applicator interface {
	Apply(*Query)
}

Applicator exists only to allow query mods into the query struct around eager loaded relationships.

type Nullable

type Nullable interface {
	IsZero() bool
}

Nullable object

type Query

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

Query holds the state for the built up query

func Raw

func Raw(query string, args ...interface{}) *Query

Raw makes a raw query, usually for use with bind

func RawG

func RawG(query string, args ...interface{}) *Query

RawG makes a raw query using the global simmer.Executor, usually for use with bind

func (*Query) Bind

func (q *Query) Bind(ctx context.Context, exec simmer.Executor, obj interface{}) error

Bind executes the query and inserts the result into the passed in object pointer.

If Context is non-nil it will upgrade the Executor to a ContextExecutor and query with the passed context. If Context is non-nil, any eager loading that's done must also be using load* methods that support context as the first parameter.

Also see documentation for Bind()

func (*Query) BindG

func (q *Query) BindG(ctx context.Context, obj interface{}) error

BindG executes the query and inserts the result into the passed in object pointer. It uses the global executor. Also see documentation for Bind() and Query.Bind()

func (*Query) BindP

func (q *Query) BindP(ctx context.Context, exec simmer.Executor, obj interface{})

BindP executes the query and inserts the result into the passed in object pointer. It panics on error. Also see documentation for Bind() and Query.Bind()

func (*Query) Exec

func (q *Query) Exec(exec simmer.Executor) (sql.Result, error)

Exec executes a query that does not need a row returned

func (*Query) ExecContext

func (q *Query) ExecContext(ctx context.Context, exec simmer.ContextExecutor) (sql.Result, error)

ExecContext executes a query that does not need a row returned

func (*Query) ExecP

func (q *Query) ExecP(exec simmer.Executor) sql.Result

ExecP executes a query that does not need a row returned It will panic on error

func (*Query) Query

func (q *Query) Query(exec simmer.Executor) (*sql.Rows, error)

Query executes the query for the All finisher and returns multiple rows

func (*Query) QueryContext

func (q *Query) QueryContext(ctx context.Context, exec simmer.ContextExecutor) (*sql.Rows, error)

QueryContext executes the query for the All finisher and returns multiple rows

func (*Query) QueryP

func (q *Query) QueryP(exec simmer.Executor) *sql.Rows

QueryP executes the query for the All finisher and returns multiple rows It will panic on error

func (*Query) QueryRow

func (q *Query) QueryRow(exec simmer.Executor) *sql.Row

QueryRow executes the query for the One finisher and returns a row

func (*Query) QueryRowContext

func (q *Query) QueryRowContext(ctx context.Context, exec simmer.ContextExecutor) *sql.Row

QueryRowContext executes the query for the One finisher and returns a row

type QueryMod

type QueryMod interface {
	Apply(q *Query)
}

QueryMod modifies a query object.

func And

func And(clause string, args ...interface{}) QueryMod

And allows you to specify a where clause separated by an AND for your statement And is a duplicate of the Where function, but allows for more natural looking query mod chains, for example: (Where("a=?"), And("b=?"), Or("c=?")))

Because Where statements are by default combined with and, there's no reason to call this method as it behaves the same as "Where"

func AndIn

func AndIn(clause string, args ...interface{}) QueryMod

AndIn allows you to specify a "x IN (set)" clause separated by an AndIn for your where statement. AndIn is a duplicate of the WhereIn function, but allows for more natural looking query mod chains, for example: (WhereIn("column1 in ?"), AndIn("column2 in ?"), OrIn("column3 in ?"))

func AndNotIn

func AndNotIn(clause string, args ...interface{}) QueryMod

AndNotIn allows you to specify a "x NOT IN (set)" clause separated by an AndNotIn for your where statement. AndNotIn is a duplicate of the WhereNotIn function, but allows for more natural looking query mod chains, for example: (WhereNotIn("column1 not in ?"), AndIn("column2 not in ?"), OrIn("column3 not in ?"))

func Distinct

func Distinct(clause string) QueryMod

Distinct allows you to filter duplicates

func Expr

func Expr(wheremods ...QueryMod) QueryMod

Expr groups where query mods. It's detrimental to use this with any other type of Query Mod because the effects will always only affect where clauses.

When Expr is used, the entire query will stop doing automatic paretheses for the where statement and you must use Expr anywhere you would like them.

Do NOT use with anything except where.

func For

func For(clause string) QueryMod

For inserts a concurrency locking clause at the end of your statement

func From

func From(from string) QueryMod

From allows to specify the table for your statement

func FullOuterJoin

func FullOuterJoin(clause string, args ...interface{}) QueryMod

FullOuterJoin on another table

func GroupBy

func GroupBy(clause string) QueryMod

GroupBy allows you to specify a group by clause for your statement

func Having

func Having(clause string, args ...interface{}) QueryMod

Having allows you to specify a having clause for your statement

func InnerJoin

func InnerJoin(clause string, args ...interface{}) QueryMod

InnerJoin on another table

func LeftOuterJoin

func LeftOuterJoin(clause string, args ...interface{}) QueryMod

LeftOuterJoin on another table

func Limit

func Limit(limit int) QueryMod

Limit the number of returned rows

func Load

func Load(relationship string, mods ...QueryMod) QueryMod

Load allows you to specify foreign key relationships to eager load for your query. Passed in relationships need to be in the format MyThing or MyThings. Relationship name plurality is important, if your relationship is singular, you need to specify the singular form and vice versa.

In the following example we see how to eager load a users's videos and the video's tags comments, and publisher during a query to find users.

models.Users(qm.Load("Videos.Tags"))

In order to filter better on the query for the relationships you can additionally supply query mods.

models.Users(qm.Load("Videos.Tags", Where("deleted = ?", isDeleted)))

Keep in mind the above only sets the query mods for the query on the last specified relationship. In this case, only Tags will get the query mod. If you want to do intermediate relationships with query mods you must specify them separately:

models.Users(
  qm.Load("Videos", Where("deleted = false"))
  qm.Load("Videos.Tags", Where("deleted = ?", isDeleted))
)

func Offset

func Offset(offset int) QueryMod

Offset into the results

func Or

func Or(clause string, args ...interface{}) QueryMod

Or allows you to specify a where clause separated by an OR for your statement

func Or2

func Or2(q QueryMod) QueryMod

Or2 takes a Where query mod and turns it into an Or. It can be detrimental if used on things that are not Where query mods as it will still modify the last Where statement into an Or.

func OrIn

func OrIn(clause string, args ...interface{}) QueryMod

OrIn allows you to specify an IN clause separated by an OR for your where statement

func OrNotIn

func OrNotIn(clause string, args ...interface{}) QueryMod

OrNotIn allows you to specify a NOT IN clause separated by an OR for your where statement

func OrderBy

func OrderBy(clause string) QueryMod

OrderBy allows you to specify a order by clause for your statement

func RightOuterJoin

func RightOuterJoin(clause string, args ...interface{}) QueryMod

RightOuterJoin on another table

func SQL

func SQL(sql string, args ...interface{}) QueryMod

SQL allows you to execute a plain SQL statement

func Select

func Select(columns ...string) QueryMod

Select specific columns opposed to all columns

func Where

func Where(clause string, args ...interface{}) QueryMod

Where allows you to specify a where clause for your statement. If multiple Where statements are used they are combined with 'and'

func WhereIn

func WhereIn(clause string, args ...interface{}) QueryMod

WhereIn allows you to specify a "x IN (set)" clause for your where statement Example clauses: "column in ?", "(column1,column2) in ?"

func WhereNotIn

func WhereNotIn(clause string, args ...interface{}) QueryMod

WhereNotIn allows you to specify a "x NOT IN (set)" clause for your where statement. Example clauses: "column not in ?", "(column1,column2) not in ?"

func With

func With(clause string, args ...interface{}) QueryMod

With allows you to pass in a Common Table Expression clause (and args)

type QueryModFunc

type QueryModFunc func(q *Query)

The QueryModFunc type is an adapter to allow the use of ordinary functions for query modifying. If f is a function with the appropriate signature, QueryModFunc(f) is a QueryMod that calls f.

func (QueryModFunc) Apply

func (f QueryModFunc) Apply(q *Query)

Apply calls f(q).

type WhereQueryMod

type WhereQueryMod struct {
	Clause string
	Args   []interface{}
}

WhereQueryMod allows construction of where clauses

func WhereIsNotNull

func WhereIsNotNull(name string) WhereQueryMod

WhereIsNotNull is a helper that just returns "name is not null"

func WhereIsNull

func WhereIsNull(name string) WhereQueryMod

WhereIsNull is a helper that just returns "name is null"

func WhereNullEQ

func WhereNullEQ(name string, negated bool, value interface{}) WhereQueryMod

WhereNullEQ is a helper for doing equality with null types

func WhereOp

func WhereOp(name string, operator operator, value interface{}) WhereQueryMod

Where is a helper for doing operations on primitive types

func (WhereQueryMod) Apply

func (qm WhereQueryMod) Apply(q *Query)

Apply implements QueryMod.Apply.

Jump to

Keyboard shortcuts

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