builder

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2024 License: MIT Imports: 14 Imported by: 6

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingRelationship = fmt.Errorf("missing relationship")
	ErrMissingField        = fmt.Errorf("missing related field")
)
View Source
var SoftDeletes = &Scope{
	Name: "soft-deletes",
	Apply: func(b *Builder) *Builder {
		return b.Where(b.GetTable()+".deleted_at", "=", nil)
	},
}

Functions

func Load

func Load(tx database.DB, v any, relation string) error

func LoadContext

func LoadContext(ctx context.Context, tx database.DB, v any, relation string) error

func LoadMissing

func LoadMissing(tx database.DB, v any, relation string) error

func LoadMissingContext

func LoadMissingContext(ctx context.Context, tx database.DB, v any, relation string) error

func NewSelects

func NewSelects() *selects

Types

type BelongsTo

type BelongsTo[T model.Model] struct {
	// contains filtered or unexported fields
}

BelongsTo represents a belongs to relationship on a model. The parent model with a BelongsTo property will have a column referencing another tables primary key. For example if model Foo had a BelongsTo[*Bar] property the foos table would have a foos.bar_id column related to the bars.id column. Struct tags can be used to change the column names if they don't follow the default naming convention. The column on the parent model can be set with a foreign tag and the column on the related model can be set with an owner tag.

Tags:

  • owner: parent model
  • foreign: related model
Example
sqlite.UseSQLite()
type Bar struct {
	model.BaseModel
	ID   int    `db:"id,autoincrement,primary"`
	Name string `db:"name"`
}

type Foo struct {
	model.BaseModel
	ID    int `db:"id,autoincrement,primary"`
	BarID int `db:"bar_id"`
	Bar   *builder.BelongsTo[*Bar]
}

db := sqlx.MustOpen("sqlite3", ":memory:")
defer db.Close()

createFoo, err := migrate.CreateFromModel(&Foo{})
check(err)
err = createFoo.Run(context.Background(), db)
check(err)
createBar, err := migrate.CreateFromModel(&Bar{})
check(err)
err = createBar.Run(context.Background(), db)
check(err)

foo := &Foo{BarID: 1}
err = model.Save(db, foo)
check(err)
bar := &Bar{ID: 1, Name: "bar name"}
err = model.Save(db, bar)
check(err)

err = builder.Load(db, foo, "Bar")
check(err)
relatedBar, _ := foo.Bar.Value()

fmt.Println(relatedBar.Name)
Output:

bar name

func (*BelongsTo[T]) ForeignKeys

func (r *BelongsTo[T]) ForeignKeys() []*ForeignKey

ForeignKeys returns a list of related tables and what columns they are related on.

func (*BelongsTo[T]) Initialize

func (r *BelongsTo[T]) Initialize(parent any, field reflect.StructField) error

func (*BelongsTo[T]) Load

func (r *BelongsTo[T]) Load(ctx context.Context, tx database.DB, relations []Relationship) error

func (*BelongsTo) Loaded

func (v *BelongsTo) Loaded() bool

Loaded returns true if the relationship has been fetched and false if it has not.

func (*BelongsTo) MarshalJSON

func (v *BelongsTo) MarshalJSON() ([]byte, error)

func (BelongsTo) Query added in v0.15.0

func (r BelongsTo) Query() *ModelBuilder[T]

Query returns a ModelBuilder scoped to the relationship.

func (BelongsTo) Subquery

func (r BelongsTo) Subquery() *Builder

Subquery returns a Builder scoped to the relationship.

func (*BelongsTo) Value

func (v *BelongsTo) Value() (T, bool)

Value will return the related value and if it has been fetched.

type Builder

type Builder struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"fmt"

	"github.com/abibby/salusa/database/builder"
	"github.com/abibby/salusa/database/dialects"
	"github.com/abibby/salusa/internal/test"
)

func main() {
	query, bindings, err := builder.
		From[*test.Foo]().
		Where("column", "=", "value").
		SQLString(dialects.New())
	if err != nil {
		panic(err)
	}

	fmt.Println(bindings)
	fmt.Println(query)
}
Output:

[value]
SELECT "foos".* FROM "foos" WHERE "column" = ?

func NewBuilder added in v0.10.0

func NewBuilder() *Builder

NewBuilder creates a new SubBuilder without anything selected

func (*Builder) AddGroupBy

func (b *Builder) AddGroupBy(columns ...string) *Builder

GroupBy adds a "group by" clause to the query.

func (*Builder) AddSelect

func (b *Builder) AddSelect(columns ...string) *Builder

AddSelect adds new columns to be selected.

func (*Builder) AddSelectFunction

func (b *Builder) AddSelectFunction(function, column string) *Builder

SelectFunction adds a column to be selected with a function applied.

func (*Builder) AddSelectSubquery

func (b *Builder) AddSelectSubquery(sb QueryBuilder, as string) *Builder

AddSelectSubquery adds a subquery to be selected.

func (*Builder) And

func (b *Builder) And(cb func(q *Conditions)) *Builder

And adds a group of conditions to the query

func (*Builder) Clone

func (b *Builder) Clone() *Builder

func (*Builder) Context

func (b *Builder) Context() context.Context

Context returns the context value from the query.

func (*Builder) Count added in v0.14.0

func (b *Builder) Count(tx database.DB) (int, error)

Count executes select and returns the number of records.

func (*Builder) CrossJoin

func (b *Builder) CrossJoin(table, localColumn, operator, foreignColumn string) *Builder

CrossJoin adds a cross join clause to the query.

func (*Builder) CrossJoinOn

func (b *Builder) CrossJoinOn(table string, cb func(q *Conditions)) *Builder

CrossJoinOn adds a cross join clause to the query with a complex on statement.

func (*Builder) Delete added in v0.7.0

func (b *Builder) Delete(tx database.DB) error

func (*Builder) Deleter added in v0.7.0

func (b *Builder) Deleter() *Deleter

func (*Builder) Distinct

func (b *Builder) Distinct() *Builder

Distinct forces the query to only return distinct results.

func (*Builder) Dump

func (b *Builder) Dump() *Builder

func (*Builder) From

func (b *Builder) From(table string) *Builder

From sets the table which the query is targeting.

func (*Builder) GetTable

func (b *Builder) GetTable() string

GetTable returns the table the query is targeting

func (*Builder) GroupBy

func (b *Builder) GroupBy(columns ...string) *Builder

GroupBy sets the "group by" clause to the query.

func (*Builder) Having

func (b *Builder) Having(column, operator string, value any) *Builder

Having adds a basic having clause to the query.

func (*Builder) HavingAnd

func (b *Builder) HavingAnd(cb func(q *Conditions)) *Builder

HavingAnd adds a group of conditions to the query

func (*Builder) HavingColumn

func (b *Builder) HavingColumn(column, operator string, valueColumn string) *Builder

HavingColumn adds a having clause to the query comparing two columns.

func (*Builder) HavingExists

func (b *Builder) HavingExists(query QueryBuilder) *Builder

HavingExists add an exists clause to the query.

func (*Builder) HavingHas

func (b *Builder) HavingHas(relation string, cb func(q *Builder) *Builder) *Builder

HavingHas adds a relationship exists condition to the query with having clauses.

func (*Builder) HavingIn

func (b *Builder) HavingIn(column string, values []any) *Builder

HavingIn adds a having in clause to the query.

func (*Builder) HavingOr

func (b *Builder) HavingOr(cb func(q *Conditions)) *Builder

HavingOr adds a group of conditions to the query with an or

func (*Builder) HavingRaw

func (b *Builder) HavingRaw(rawSql string, bindings ...any) *Builder

HavingRaw adds a raw having clause to the query.

func (*Builder) HavingSubquery

func (b *Builder) HavingSubquery(subquery QueryBuilder, operator string, value any) *Builder

HavingSubquery adds a having clause to the query comparing a column and a subquery.

func (*Builder) InnerJoin

func (b *Builder) InnerJoin(table, localColumn, operator, foreignColumn string) *Builder

InnerJoin adds an inner join clause to the query.

func (*Builder) InnerJoinOn

func (b *Builder) InnerJoinOn(table string, cb func(q *Conditions)) *Builder

InnerJoinOn adds an inner join clause to the query with a complex on statement.

func (*Builder) Join

func (b *Builder) Join(table, localColumn, operator, foreignColumn string) *Builder

Join adds a join clause to the query.

func (*Builder) JoinOn

func (b *Builder) JoinOn(table string, cb func(q *Conditions)) *Builder

JoinOn adds a join clause to the query with a complex on statement.

func (*Builder) LeftJoin

func (b *Builder) LeftJoin(table, localColumn, operator, foreignColumn string) *Builder

LeftJoin adds a left join clause to the query.

func (*Builder) LeftJoinOn

func (b *Builder) LeftJoinOn(table string, cb func(q *Conditions)) *Builder

LeftJoinOn adds a left join clause to the query with a complex on statement.

func (*Builder) Limit

func (b *Builder) Limit(limit int) *Builder

Limit set the maximum number of rows to return.

func (*Builder) Load

func (b *Builder) Load(tx database.DB, v any) (err error)

Load executes the query as a select statement and sets v to the result.

func (*Builder) LoadOne deprecated

func (b *Builder) LoadOne(tx database.DB, v any) error

Load executes the query as a select statement and sets v to the first record.

Deprecated: Use Builder.Load

func (*Builder) Offset

func (b *Builder) Offset(offset int) *Builder

Offset sets the number of rows to skip before returning the result.

func (*Builder) Or

func (b *Builder) Or(cb func(q *Conditions)) *Builder

Or adds a group of conditions to the query with an or

func (*Builder) OrHaving

func (b *Builder) OrHaving(column, operator string, value any) *Builder

OrHaving adds an or having clause to the query

func (*Builder) OrHavingColumn

func (b *Builder) OrHavingColumn(column, operator string, valueColumn string) *Builder

OrHavingColumn adds an or having clause to the query comparing two columns.

func (*Builder) OrHavingExists

func (b *Builder) OrHavingExists(query QueryBuilder) *Builder

WhereExists add an exists clause to the query.

func (*Builder) OrHavingHas

func (b *Builder) OrHavingHas(relation string, cb func(q *Builder) *Builder) *Builder

OrHavingHas adds a relationship exists condition to the query with having clauses and an or.

func (*Builder) OrHavingIn

func (b *Builder) OrHavingIn(column string, values []any) *Builder

OrHavingIn adds an or having in clause to the query.

func (*Builder) OrHavingRaw

func (b *Builder) OrHavingRaw(rawSql string, bindings ...any) *Builder

OrHavingRaw adds a raw or having clause to the query.

func (*Builder) OrHavingSubquery

func (b *Builder) OrHavingSubquery(subquery QueryBuilder, operator string, value any) *Builder

OrHavingSubquery adds an or having clause to the query comparing a column and a subquery.

func (*Builder) OrWhere

func (b *Builder) OrWhere(column, operator string, value any) *Builder

OrWhere adds an or where clause to the query

func (*Builder) OrWhereColumn

func (b *Builder) OrWhereColumn(column, operator string, valueColumn string) *Builder

OrWhereColumn adds an or where clause to the query comparing two columns.

func (*Builder) OrWhereExists

func (b *Builder) OrWhereExists(query QueryBuilder) *Builder

WhereExists add an exists clause to the query.

func (*Builder) OrWhereHas

func (b *Builder) OrWhereHas(relation string, cb func(q *Builder) *Builder) *Builder

OrWhereHas adds a relationship exists condition to the query with where clauses and an or.

func (*Builder) OrWhereIn

func (b *Builder) OrWhereIn(column string, values []any) *Builder

OrWhereIn adds an or where in clause to the query.

func (*Builder) OrWhereRaw

func (b *Builder) OrWhereRaw(rawSql string, bindings ...any) *Builder

OrWhereRaw adds a raw or where clause to the query.

func (*Builder) OrWhereSubquery

func (b *Builder) OrWhereSubquery(subquery QueryBuilder, operator string, value any) *Builder

OrWhereSubquery adds an or where clause to the query comparing a column and a subquery.

func (*Builder) OrderBy

func (b *Builder) OrderBy(column string) *Builder

OrderBy adds an order by clause to the query.

func (*Builder) OrderByDesc

func (b *Builder) OrderByDesc(column string) *Builder

OrderByDesc adds a descending order by clause to the query.

func (*Builder) RightJoin

func (b *Builder) RightJoin(table, localColumn, operator, foreignColumn string) *Builder

RightJoin adds a right join clause to the query.

func (*Builder) RightJoinOn

func (b *Builder) RightJoinOn(table string, cb func(q *Conditions)) *Builder

RightJoinOn adds a right join clause to the query with a complex on statement.

func (*Builder) SQLString added in v0.10.0

func (b *Builder) SQLString(d dialects.Dialect) (string, []any, error)

func (*Builder) Select

func (b *Builder) Select(columns ...string) *Builder

Select sets the columns to be selected.

func (*Builder) SelectFunction

func (b *Builder) SelectFunction(function, column string) *Builder

SelectFunction sets a column to be selected with a function applied.

func (*Builder) SelectSubquery

func (b *Builder) SelectSubquery(sb QueryBuilder, as string) *Builder

SelectSubquery sets a subquery to be selected.

func (*Builder) Unordered

func (b *Builder) Unordered() *Builder

Unordered removes all order by clauses from the query.

func (*Builder) Where

func (b *Builder) Where(column, operator string, value any) *Builder

Where adds a basic where clause to the query.

func (*Builder) WhereColumn

func (b *Builder) WhereColumn(column, operator string, valueColumn string) *Builder

WhereColumn adds a where clause to the query comparing two columns.

func (*Builder) WhereExists

func (b *Builder) WhereExists(query QueryBuilder) *Builder

WhereExists add an exists clause to the query.

func (*Builder) WhereHas

func (b *Builder) WhereHas(relation string, cb func(q *Builder) *Builder) *Builder

WhereHas adds a relationship exists condition to the query with where clauses.

Example
package main

import (
	"fmt"

	"github.com/abibby/salusa/database/builder"
	"github.com/abibby/salusa/database/dialects"
	"github.com/abibby/salusa/internal/test"
)

func main() {
	query, bindings, err := builder.
		From[*test.Foo]().
		WhereHas("Bar", func(q *builder.Builder) *builder.Builder {
			return q.Where("id", "=", 7)
		}).
		SQLString(dialects.New())
	if err != nil {
		panic(err)
	}

	fmt.Println(bindings)
	fmt.Println(query)
}
Output:

[7]
SELECT "foos".* FROM "foos" WHERE EXISTS (SELECT "bars".* FROM "bars" WHERE "foo_id" = "foos"."id" AND "id" = ?)

func (*Builder) WhereIn

func (b *Builder) WhereIn(column string, values []any) *Builder

WhereIn adds a where in clause to the query.

func (*Builder) WhereRaw

func (b *Builder) WhereRaw(rawSql string, bindings ...any) *Builder

WhereRaw adds a raw where clause to the query.

func (*Builder) WhereSubquery

func (b *Builder) WhereSubquery(subquery QueryBuilder, operator string, value any) *Builder

WhereSubquery adds a where clause to the query comparing a column and a subquery.

func (*Builder) WithContext

func (b *Builder) WithContext(ctx context.Context) *Builder

WithContext adds a context to the query that will be used when fetching results.

func (*Builder) WithScope

func (b *Builder) WithScope(scope *Scope) *Builder

WithScope adds a local scope to a query.

func (*Builder) WithoutGlobalScope

func (b *Builder) WithoutGlobalScope(scope *Scope) *Builder

WithoutGlobalScope removes a global scope from the query.

func (*Builder) WithoutScope

func (b *Builder) WithoutScope(scope *Scope) *Builder

WithoutScope removes the given scope from the local scopes.

type Conditions

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

func (*Conditions) And

func (c *Conditions) And(cb func(q *Conditions)) *Conditions

And adds a group of conditions to the query

func (*Conditions) Clone

func (c *Conditions) Clone() *Conditions

func (*Conditions) Or

func (c *Conditions) Or(cb func(q *Conditions)) *Conditions

Or adds a group of conditions to the query with an or

func (*Conditions) OrWhere

func (c *Conditions) OrWhere(column, operator string, value any) *Conditions

OrWhere adds an or where clause to the query

func (*Conditions) OrWhereColumn

func (c *Conditions) OrWhereColumn(column, operator string, valueColumn string) *Conditions

OrWhereColumn adds an or where clause to the query comparing two columns.

func (*Conditions) OrWhereExists

func (c *Conditions) OrWhereExists(query QueryBuilder) *Conditions

WhereExists add an exists clause to the query.

func (*Conditions) OrWhereHas

func (c *Conditions) OrWhereHas(relation string, cb func(q *Builder) *Builder) *Conditions

OrWhereHas adds a relationship exists condition to the query with where clauses and an or.

func (*Conditions) OrWhereIn

func (c *Conditions) OrWhereIn(column string, values []any) *Conditions

OrWhereIn adds an or where in clause to the query.

func (*Conditions) OrWhereRaw

func (c *Conditions) OrWhereRaw(rawSql string, bindings ...any) *Conditions

OrWhereRaw adds a raw or where clause to the query.

func (*Conditions) OrWhereSubquery

func (c *Conditions) OrWhereSubquery(subquery QueryBuilder, operator string, value any) *Conditions

OrWhereSubquery adds an or where clause to the query comparing a column and a subquery.

func (*Conditions) SQLString added in v0.10.0

func (c *Conditions) SQLString(d dialects.Dialect) (string, []any, error)

func (*Conditions) Where

func (c *Conditions) Where(column, operator string, value any) *Conditions

Where adds a basic where clause to the query.

func (*Conditions) WhereColumn

func (c *Conditions) WhereColumn(column, operator string, valueColumn string) *Conditions

WhereColumn adds a where clause to the query comparing two columns.

func (*Conditions) WhereExists

func (c *Conditions) WhereExists(query QueryBuilder) *Conditions

WhereExists add an exists clause to the query.

func (*Conditions) WhereHas

func (c *Conditions) WhereHas(relation string, cb func(q *Builder) *Builder) *Conditions

WhereHas adds a relationship exists condition to the query with where clauses.

func (*Conditions) WhereIn

func (c *Conditions) WhereIn(column string, values []any) *Conditions

WhereIn adds a where in clause to the query.

func (*Conditions) WhereRaw

func (c *Conditions) WhereRaw(rawSql string, bindings ...any) *Conditions

WhereRaw adds a raw where clause to the query.

func (*Conditions) WhereSubquery

func (c *Conditions) WhereSubquery(subquery QueryBuilder, operator string, value any) *Conditions

WhereSubquery adds a where clause to the query comparing a column and a subquery.

type Deleter added in v0.7.0

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

func (*Deleter) SQLString added in v0.10.0

func (d *Deleter) SQLString(dialect dialects.Dialect) (string, []any, error)

type ForeignKey

type ForeignKey struct {
	LocalKey     string
	RelatedTable string
	RelatedKey   string
}

func (*ForeignKey) Equal

func (f *ForeignKey) Equal(v *ForeignKey) bool

type HasMany

type HasMany[T model.Model] struct {
	// contains filtered or unexported fields
}

Tags:

  • local: parent model
  • foreign: related model

func (*HasMany[T]) ForeignKeys

func (r *HasMany[T]) ForeignKeys() []*ForeignKey

ForeignKeys returns a list of related tables and what columns they are related on.

func (*HasMany[T]) Initialize

func (r *HasMany[T]) Initialize(parent any, field reflect.StructField) error

func (*HasMany[T]) Load

func (r *HasMany[T]) Load(ctx context.Context, tx database.DB, relations []Relationship) error

func (*HasMany) Loaded

func (v *HasMany) Loaded() bool

Loaded returns true if the relationship has been fetched and false if it has not.

func (*HasMany) MarshalJSON

func (v *HasMany) MarshalJSON() ([]byte, error)

func (HasMany) Query added in v0.15.0

func (r HasMany) Query() *ModelBuilder[T]

Query returns a ModelBuilder scoped to the relationship.

func (HasMany) Subquery

func (r HasMany) Subquery() *Builder

Subquery returns a Builder scoped to the relationship.

func (*HasMany) Value

func (v *HasMany) Value() (T, bool)

Value will return the related value and if it has been fetched.

type HasOne

type HasOne[T model.Model] struct {
	// contains filtered or unexported fields
}

Tags:

  • local: parent model
  • foreign: related model

func (*HasOne[T]) ForeignKeys

func (r *HasOne[T]) ForeignKeys() []*ForeignKey

ForeignKeys returns a list of related tables and what columns they are related on.

func (*HasOne[T]) Initialize

func (r *HasOne[T]) Initialize(parent any, field reflect.StructField) error

func (*HasOne[T]) Load

func (r *HasOne[T]) Load(ctx context.Context, tx database.DB, relations []Relationship) error

func (*HasOne) Loaded

func (v *HasOne) Loaded() bool

Loaded returns true if the relationship has been fetched and false if it has not.

func (*HasOne) MarshalJSON

func (v *HasOne) MarshalJSON() ([]byte, error)

func (HasOne) Query added in v0.15.0

func (r HasOne) Query() *ModelBuilder[T]

Query returns a ModelBuilder scoped to the relationship.

func (HasOne) Subquery

func (r HasOne) Subquery() *Builder

Subquery returns a Builder scoped to the relationship.

func (*HasOne) Value

func (v *HasOne) Value() (T, bool)

Value will return the related value and if it has been fetched.

type ModelBuilder added in v0.10.0

type ModelBuilder[T model.Model] struct {
	// contains filtered or unexported fields
}

ModelBuilder represents an sql query and any bindings needed to run it.

func From

func From[T model.Model]() *ModelBuilder[T]

From creates a new query from the models table and with table.* selected

func New

func New[T model.Model]() *ModelBuilder[T]

New creates a new Builder with * selected

func NewEmpty

func NewEmpty[T model.Model]() *ModelBuilder[T]

NewEmpty creates a new helpers without anything selected

func (*ModelBuilder[T]) AddGroupBy added in v0.10.0

func (b *ModelBuilder[T]) AddGroupBy(columns ...string) *ModelBuilder[T]

GroupBy adds a "group by" clause to the query.

func (*ModelBuilder[T]) AddSelect added in v0.10.0

func (b *ModelBuilder[T]) AddSelect(columns ...string) *ModelBuilder[T]

AddSelect adds new columns to be selected.

func (*ModelBuilder[T]) AddSelectFunction added in v0.10.0

func (b *ModelBuilder[T]) AddSelectFunction(function, column string) *ModelBuilder[T]

SelectFunction adds a column to be selected with a function applied.

func (*ModelBuilder[T]) AddSelectSubquery added in v0.10.0

func (b *ModelBuilder[T]) AddSelectSubquery(sb QueryBuilder, as string) *ModelBuilder[T]

AddSelectSubquery adds a subquery to be selected.

func (*ModelBuilder[T]) And added in v0.10.0

func (b *ModelBuilder[T]) And(cb func(q *Conditions)) *ModelBuilder[T]

And adds a group of conditions to the query

func (*ModelBuilder[T]) Clone added in v0.10.0

func (b *ModelBuilder[T]) Clone() *ModelBuilder[T]

func (*ModelBuilder[T]) Context added in v0.10.0

func (b *ModelBuilder[T]) Context() context.Context

Context returns the context value from the query.

func (*ModelBuilder[T]) Count added in v0.14.0

func (b *ModelBuilder[T]) Count(tx database.DB) (int, error)

Count executes select and returns the number of records.

func (*ModelBuilder[T]) CrossJoin added in v0.10.0

func (b *ModelBuilder[T]) CrossJoin(table, localColumn, operator, foreignColumn string) *ModelBuilder[T]

CrossJoin adds a cross join clause to the query.

func (*ModelBuilder[T]) CrossJoinOn added in v0.10.0

func (b *ModelBuilder[T]) CrossJoinOn(table string, cb func(q *Conditions)) *ModelBuilder[T]

CrossJoinOn adds a cross join clause to the query with a complex on statement.

func (*ModelBuilder[T]) Delete added in v0.10.0

func (b *ModelBuilder[T]) Delete(tx database.DB) error

func (*ModelBuilder[T]) Deleter added in v0.10.0

func (b *ModelBuilder[T]) Deleter() *Deleter

func (*ModelBuilder[T]) Distinct added in v0.10.0

func (b *ModelBuilder[T]) Distinct() *ModelBuilder[T]

Distinct forces the query to only return distinct results.

func (*ModelBuilder[T]) Dump added in v0.10.0

func (b *ModelBuilder[T]) Dump() *ModelBuilder[T]

func (*ModelBuilder[T]) Each added in v0.10.0

func (b *ModelBuilder[T]) Each(tx database.DB, cb func(v T) error) error

func (*ModelBuilder[T]) Find added in v0.10.0

func (b *ModelBuilder[T]) Find(tx database.DB, primaryKeyValue any) (T, error)

Find returns the record with a matching primary key. It will fail on tables with multiple primary keys.

func (*ModelBuilder[T]) First added in v0.10.0

func (b *ModelBuilder[T]) First(tx database.DB) (T, error)

Get executes the query as a select statement and returns the first record.

func (*ModelBuilder[T]) From added in v0.10.0

func (b *ModelBuilder[T]) From(table string) *ModelBuilder[T]

From sets the table which the query is targeting.

func (*ModelBuilder[T]) Get added in v0.10.0

func (b *ModelBuilder[T]) Get(tx database.DB) ([]T, error)

Get executes the query as a select statement and returns the result.

func (*ModelBuilder[T]) GetTable added in v0.10.0

func (b *ModelBuilder[T]) GetTable() string

GetTable returns the table the query is targeting

func (*ModelBuilder[T]) GroupBy added in v0.10.0

func (b *ModelBuilder[T]) GroupBy(columns ...string) *ModelBuilder[T]

GroupBy sets the "group by" clause to the query.

func (*ModelBuilder[T]) Having added in v0.10.0

func (b *ModelBuilder[T]) Having(column, operator string, value any) *ModelBuilder[T]

Having adds a basic having clause to the query.

func (*ModelBuilder[T]) HavingAnd added in v0.10.0

func (b *ModelBuilder[T]) HavingAnd(cb func(q *Conditions)) *ModelBuilder[T]

HavingAnd adds a group of conditions to the query

func (*ModelBuilder[T]) HavingColumn added in v0.10.0

func (b *ModelBuilder[T]) HavingColumn(column, operator string, valueColumn string) *ModelBuilder[T]

HavingColumn adds a having clause to the query comparing two columns.

func (*ModelBuilder[T]) HavingExists added in v0.10.0

func (b *ModelBuilder[T]) HavingExists(query QueryBuilder) *ModelBuilder[T]

HavingExists add an exists clause to the query.

func (*ModelBuilder[T]) HavingHas added in v0.10.0

func (b *ModelBuilder[T]) HavingHas(relation string, cb func(q *Builder) *Builder) *ModelBuilder[T]

HavingHas adds a relationship exists condition to the query with having clauses.

func (*ModelBuilder[T]) HavingIn added in v0.10.0

func (b *ModelBuilder[T]) HavingIn(column string, values []any) *ModelBuilder[T]

HavingIn adds a having in clause to the query.

func (*ModelBuilder[T]) HavingOr added in v0.10.0

func (b *ModelBuilder[T]) HavingOr(cb func(q *Conditions)) *ModelBuilder[T]

HavingOr adds a group of conditions to the query with an or

func (*ModelBuilder[T]) HavingRaw added in v0.10.0

func (b *ModelBuilder[T]) HavingRaw(rawSql string, bindings ...any) *ModelBuilder[T]

HavingRaw adds a raw having clause to the query.

func (*ModelBuilder[T]) HavingSubquery added in v0.10.0

func (b *ModelBuilder[T]) HavingSubquery(subquery QueryBuilder, operator string, value any) *ModelBuilder[T]

HavingSubquery adds a having clause to the query comparing a column and a subquery.

func (*ModelBuilder[T]) InnerJoin added in v0.10.0

func (b *ModelBuilder[T]) InnerJoin(table, localColumn, operator, foreignColumn string) *ModelBuilder[T]

InnerJoin adds an inner join clause to the query.

func (*ModelBuilder[T]) InnerJoinOn added in v0.10.0

func (b *ModelBuilder[T]) InnerJoinOn(table string, cb func(q *Conditions)) *ModelBuilder[T]

InnerJoinOn adds an inner join clause to the query with a complex on statement.

func (*ModelBuilder[T]) Join added in v0.10.0

func (b *ModelBuilder[T]) Join(table, localColumn, operator, foreignColumn string) *ModelBuilder[T]

Join adds a join clause to the query.

func (*ModelBuilder[T]) JoinOn added in v0.10.0

func (b *ModelBuilder[T]) JoinOn(table string, cb func(q *Conditions)) *ModelBuilder[T]

JoinOn adds a join clause to the query with a complex on statement.

func (*ModelBuilder[T]) LeftJoin added in v0.10.0

func (b *ModelBuilder[T]) LeftJoin(table, localColumn, operator, foreignColumn string) *ModelBuilder[T]

LeftJoin adds a left join clause to the query.

func (*ModelBuilder[T]) LeftJoinOn added in v0.10.0

func (b *ModelBuilder[T]) LeftJoinOn(table string, cb func(q *Conditions)) *ModelBuilder[T]

LeftJoinOn adds a left join clause to the query with a complex on statement.

func (*ModelBuilder[T]) Limit added in v0.10.0

func (b *ModelBuilder[T]) Limit(limit int) *ModelBuilder[T]

Limit set the maximum number of rows to return.

func (*ModelBuilder[T]) Load added in v0.10.0

func (b *ModelBuilder[T]) Load(tx database.DB, v any) error

Load executes the query as a select statement and sets v to the result.

func (*ModelBuilder[T]) LoadOne deprecated added in v0.10.0

func (b *ModelBuilder[T]) LoadOne(tx database.DB, v any) error

Load executes the query as a select statement and sets v to the result.

Deprecated: Use ModelBuilder.Load

func (*ModelBuilder[T]) Offset added in v0.10.0

func (b *ModelBuilder[T]) Offset(offset int) *ModelBuilder[T]

Offset sets the number of rows to skip before returning the result.

func (*ModelBuilder[T]) Or added in v0.10.0

func (b *ModelBuilder[T]) Or(cb func(q *Conditions)) *ModelBuilder[T]

Or adds a group of conditions to the query with an or

func (*ModelBuilder[T]) OrHaving added in v0.10.0

func (b *ModelBuilder[T]) OrHaving(column, operator string, value any) *ModelBuilder[T]

OrHaving adds an or having clause to the query

func (*ModelBuilder[T]) OrHavingColumn added in v0.10.0

func (b *ModelBuilder[T]) OrHavingColumn(column, operator string, valueColumn string) *ModelBuilder[T]

OrHavingColumn adds an or having clause to the query comparing two columns.

func (*ModelBuilder[T]) OrHavingExists added in v0.10.0

func (b *ModelBuilder[T]) OrHavingExists(query QueryBuilder) *ModelBuilder[T]

WhereExists add an exists clause to the query.

func (*ModelBuilder[T]) OrHavingHas added in v0.10.0

func (b *ModelBuilder[T]) OrHavingHas(relation string, cb func(q *Builder) *Builder) *ModelBuilder[T]

OrHavingHas adds a relationship exists condition to the query with having clauses and an or.

func (*ModelBuilder[T]) OrHavingIn added in v0.10.0

func (b *ModelBuilder[T]) OrHavingIn(column string, values []any) *ModelBuilder[T]

OrHavingIn adds an or having in clause to the query.

func (*ModelBuilder[T]) OrHavingRaw added in v0.10.0

func (b *ModelBuilder[T]) OrHavingRaw(rawSql string, bindings ...any) *ModelBuilder[T]

OrHavingRaw adds a raw or having clause to the query.

func (*ModelBuilder[T]) OrHavingSubquery added in v0.10.0

func (b *ModelBuilder[T]) OrHavingSubquery(subquery QueryBuilder, operator string, value any) *ModelBuilder[T]

OrHavingSubquery adds an or having clause to the query comparing a column and a subquery.

func (*ModelBuilder[T]) OrWhere added in v0.10.0

func (b *ModelBuilder[T]) OrWhere(column, operator string, value any) *ModelBuilder[T]

OrWhere adds an or where clause to the query

func (*ModelBuilder[T]) OrWhereColumn added in v0.10.0

func (b *ModelBuilder[T]) OrWhereColumn(column, operator string, valueColumn string) *ModelBuilder[T]

OrWhereColumn adds an or where clause to the query comparing two columns.

func (*ModelBuilder[T]) OrWhereExists added in v0.10.0

func (b *ModelBuilder[T]) OrWhereExists(query QueryBuilder) *ModelBuilder[T]

WhereExists add an exists clause to the query.

func (*ModelBuilder[T]) OrWhereHas added in v0.10.0

func (b *ModelBuilder[T]) OrWhereHas(relation string, cb func(q *Builder) *Builder) *ModelBuilder[T]

OrWhereHas adds a relationship exists condition to the query with where clauses and an or.

func (*ModelBuilder[T]) OrWhereIn added in v0.10.0

func (b *ModelBuilder[T]) OrWhereIn(column string, values []any) *ModelBuilder[T]

OrWhereIn adds an or where in clause to the query.

func (*ModelBuilder[T]) OrWhereRaw added in v0.10.0

func (b *ModelBuilder[T]) OrWhereRaw(rawSql string, bindings ...any) *ModelBuilder[T]

OrWhereRaw adds a raw or where clause to the query.

func (*ModelBuilder[T]) OrWhereSubquery added in v0.10.0

func (b *ModelBuilder[T]) OrWhereSubquery(subquery QueryBuilder, operator string, value any) *ModelBuilder[T]

OrWhereSubquery adds an or where clause to the query comparing a column and a subquery.

func (*ModelBuilder[T]) OrderBy added in v0.10.0

func (b *ModelBuilder[T]) OrderBy(column string) *ModelBuilder[T]

OrderBy adds an order by clause to the query.

func (*ModelBuilder[T]) OrderByDesc added in v0.10.0

func (b *ModelBuilder[T]) OrderByDesc(column string) *ModelBuilder[T]

OrderByDesc adds a descending order by clause to the query.

func (*ModelBuilder[T]) RightJoin added in v0.10.0

func (b *ModelBuilder[T]) RightJoin(table, localColumn, operator, foreignColumn string) *ModelBuilder[T]

RightJoin adds a right join clause to the query.

func (*ModelBuilder[T]) RightJoinOn added in v0.10.0

func (b *ModelBuilder[T]) RightJoinOn(table string, cb func(q *Conditions)) *ModelBuilder[T]

RightJoinOn adds a right join clause to the query with a complex on statement.

func (*ModelBuilder[T]) SQLString added in v0.10.0

func (b *ModelBuilder[T]) SQLString(d dialects.Dialect) (string, []any, error)

func (*ModelBuilder[T]) Select added in v0.10.0

func (b *ModelBuilder[T]) Select(columns ...string) *ModelBuilder[T]

Select sets the columns to be selected.

func (*ModelBuilder[T]) SelectFunction added in v0.10.0

func (b *ModelBuilder[T]) SelectFunction(function, column string) *ModelBuilder[T]

SelectFunction sets a column to be selected with a function applied.

func (*ModelBuilder[T]) SelectSubquery added in v0.10.0

func (b *ModelBuilder[T]) SelectSubquery(sb QueryBuilder, as string) *ModelBuilder[T]

SelectSubquery sets a subquery to be selected.

func (*ModelBuilder[T]) Unordered added in v0.10.0

func (b *ModelBuilder[T]) Unordered() *ModelBuilder[T]

Unordered removes all order by clauses from the query.

func (*ModelBuilder[T]) Where added in v0.10.0

func (b *ModelBuilder[T]) Where(column, operator string, value any) *ModelBuilder[T]

Where adds a basic where clause to the query.

func (*ModelBuilder[T]) WhereColumn added in v0.10.0

func (b *ModelBuilder[T]) WhereColumn(column, operator string, valueColumn string) *ModelBuilder[T]

WhereColumn adds a where clause to the query comparing two columns.

func (*ModelBuilder[T]) WhereExists added in v0.10.0

func (b *ModelBuilder[T]) WhereExists(query QueryBuilder) *ModelBuilder[T]

WhereExists add an exists clause to the query.

func (*ModelBuilder[T]) WhereHas added in v0.10.0

func (b *ModelBuilder[T]) WhereHas(relation string, cb func(q *Builder) *Builder) *ModelBuilder[T]

WhereHas adds a relationship exists condition to the query with where clauses.

func (*ModelBuilder[T]) WhereIn added in v0.10.0

func (b *ModelBuilder[T]) WhereIn(column string, values []any) *ModelBuilder[T]

WhereIn adds a where in clause to the query.

func (*ModelBuilder[T]) WhereRaw added in v0.10.0

func (b *ModelBuilder[T]) WhereRaw(rawSql string, bindings ...any) *ModelBuilder[T]

WhereRaw adds a raw where clause to the query.

func (*ModelBuilder[T]) WhereSubquery added in v0.10.0

func (b *ModelBuilder[T]) WhereSubquery(subquery QueryBuilder, operator string, value any) *ModelBuilder[T]

WhereSubquery adds a where clause to the query comparing a column and a subquery.

func (*ModelBuilder[T]) With added in v0.10.0

func (b *ModelBuilder[T]) With(withs ...string) *ModelBuilder[T]

func (*ModelBuilder[T]) WithContext added in v0.10.0

func (b *ModelBuilder[T]) WithContext(ctx context.Context) *ModelBuilder[T]

WithContext adds a context to the query that will be used when fetching results.

func (*ModelBuilder[T]) WithScope added in v0.10.0

func (b *ModelBuilder[T]) WithScope(scope *Scope) *ModelBuilder[T]

WithScope adds a local scope to a query.

func (*ModelBuilder[T]) WithoutGlobalScope added in v0.10.0

func (b *ModelBuilder[T]) WithoutGlobalScope(scope *Scope) *ModelBuilder[T]

WithoutGlobalScope removes a global scope from the query.

func (*ModelBuilder[T]) WithoutScope added in v0.10.0

func (b *ModelBuilder[T]) WithoutScope(scope *Scope) *ModelBuilder[T]

WithoutScope removes the given scope from the local scopes.

type QueryBuilder

type QueryBuilder interface {
	helpers.SQLStringer
	// contains filtered or unexported methods
}

QueryBuilder is implemented by *ModelBuilder and *Builder

type QueryError added in v0.15.0

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

func (*QueryError) Error added in v0.15.0

func (e *QueryError) Error() string

func (*QueryError) Unwrap added in v0.15.0

func (e *QueryError) Unwrap() error

type Relationship

type Relationship interface {
	relationship.Relationship
	Subquery() *Builder
	Load(ctx context.Context, tx database.DB, relations []Relationship) error
	ForeignKeys() []*ForeignKey
}

type Scope

type Scope struct {
	Name  string
	Apply ScopeFunc
}

Scope is a modifier for a query that can be easily applied.

type ScopeFunc

type ScopeFunc func(b *Builder) *Builder

type Scoper

type Scoper interface {
	Scopes() []*Scope
}

Jump to

Keyboard shortcuts

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