Documentation ¶
Index ¶
- Variables
- func Load(tx database.DB, v any, relation string) error
- func LoadContext(ctx context.Context, tx database.DB, v any, relation string) error
- func LoadMissing(tx database.DB, v any, relation string) error
- func LoadMissingContext(ctx context.Context, tx database.DB, v any, relation string) error
- func NewSelects() *selects
- type BelongsTo
- func (r *BelongsTo[T]) ForeignKeys() []*ForeignKey
- func (r *BelongsTo[T]) Initialize(parent any, field reflect.StructField) error
- func (r *BelongsTo[T]) Load(ctx context.Context, tx database.DB, relations []Relationship) error
- func (v *BelongsTo) Loaded() bool
- func (v *BelongsTo) MarshalJSON() ([]byte, error)
- func (r BelongsTo) Query() *ModelBuilder[T]
- func (r BelongsTo) Subquery() *Builder
- func (v *BelongsTo) Value() (T, bool)
- type Builder
- func (b *Builder) AddGroupBy(columns ...string) *Builder
- func (b *Builder) AddSelect(columns ...string) *Builder
- func (b *Builder) AddSelectFunction(function, column string) *Builder
- func (b *Builder) AddSelectSubquery(sb QueryBuilder, as string) *Builder
- func (b *Builder) And(cb func(q *Conditions)) *Builder
- func (b *Builder) Clone() *Builder
- func (b *Builder) Context() context.Context
- func (b *Builder) Count(tx database.DB) (int, error)
- func (b *Builder) CrossJoin(table, localColumn, operator, foreignColumn string) *Builder
- func (b *Builder) CrossJoinOn(table string, cb func(q *Conditions)) *Builder
- func (b *Builder) Delete(tx database.DB) error
- func (b *Builder) Deleter() *Deleter
- func (b *Builder) Distinct() *Builder
- func (b *Builder) Dump() *Builder
- func (b *Builder) From(table string) *Builder
- func (b *Builder) GetTable() string
- func (b *Builder) GroupBy(columns ...string) *Builder
- func (b *Builder) Having(column, operator string, value any) *Builder
- func (b *Builder) HavingAnd(cb func(q *Conditions)) *Builder
- func (b *Builder) HavingColumn(column, operator string, valueColumn string) *Builder
- func (b *Builder) HavingExists(query QueryBuilder) *Builder
- func (b *Builder) HavingHas(relation string, cb func(q *Builder) *Builder) *Builder
- func (b *Builder) HavingIn(column string, values []any) *Builder
- func (b *Builder) HavingOr(cb func(q *Conditions)) *Builder
- func (b *Builder) HavingRaw(rawSql string, bindings ...any) *Builder
- func (b *Builder) HavingSubquery(subquery QueryBuilder, operator string, value any) *Builder
- func (b *Builder) InnerJoin(table, localColumn, operator, foreignColumn string) *Builder
- func (b *Builder) InnerJoinOn(table string, cb func(q *Conditions)) *Builder
- func (b *Builder) Join(table, localColumn, operator, foreignColumn string) *Builder
- func (b *Builder) JoinOn(table string, cb func(q *Conditions)) *Builder
- func (b *Builder) LeftJoin(table, localColumn, operator, foreignColumn string) *Builder
- func (b *Builder) LeftJoinOn(table string, cb func(q *Conditions)) *Builder
- func (b *Builder) Limit(limit int) *Builder
- func (b *Builder) Load(tx database.DB, v any) (err error)
- func (b *Builder) LoadOne(tx database.DB, v any) errordeprecated
- func (b *Builder) Offset(offset int) *Builder
- func (b *Builder) Or(cb func(q *Conditions)) *Builder
- func (b *Builder) OrHaving(column, operator string, value any) *Builder
- func (b *Builder) OrHavingColumn(column, operator string, valueColumn string) *Builder
- func (b *Builder) OrHavingExists(query QueryBuilder) *Builder
- func (b *Builder) OrHavingHas(relation string, cb func(q *Builder) *Builder) *Builder
- func (b *Builder) OrHavingIn(column string, values []any) *Builder
- func (b *Builder) OrHavingRaw(rawSql string, bindings ...any) *Builder
- func (b *Builder) OrHavingSubquery(subquery QueryBuilder, operator string, value any) *Builder
- func (b *Builder) OrWhere(column, operator string, value any) *Builder
- func (b *Builder) OrWhereColumn(column, operator string, valueColumn string) *Builder
- func (b *Builder) OrWhereExists(query QueryBuilder) *Builder
- func (b *Builder) OrWhereHas(relation string, cb func(q *Builder) *Builder) *Builder
- func (b *Builder) OrWhereIn(column string, values []any) *Builder
- func (b *Builder) OrWhereRaw(rawSql string, bindings ...any) *Builder
- func (b *Builder) OrWhereSubquery(subquery QueryBuilder, operator string, value any) *Builder
- func (b *Builder) OrderBy(column string) *Builder
- func (b *Builder) OrderByDesc(column string) *Builder
- func (b *Builder) RightJoin(table, localColumn, operator, foreignColumn string) *Builder
- func (b *Builder) RightJoinOn(table string, cb func(q *Conditions)) *Builder
- func (b *Builder) SQLString(d dialects.Dialect) (string, []any, error)
- func (b *Builder) Select(columns ...string) *Builder
- func (b *Builder) SelectFunction(function, column string) *Builder
- func (b *Builder) SelectSubquery(sb QueryBuilder, as string) *Builder
- func (b *Builder) Unordered() *Builder
- func (b *Builder) Where(column, operator string, value any) *Builder
- func (b *Builder) WhereColumn(column, operator string, valueColumn string) *Builder
- func (b *Builder) WhereExists(query QueryBuilder) *Builder
- func (b *Builder) WhereHas(relation string, cb func(q *Builder) *Builder) *Builder
- func (b *Builder) WhereIn(column string, values []any) *Builder
- func (b *Builder) WhereRaw(rawSql string, bindings ...any) *Builder
- func (b *Builder) WhereSubquery(subquery QueryBuilder, operator string, value any) *Builder
- func (b *Builder) WithContext(ctx context.Context) *Builder
- func (b *Builder) WithScope(scope *Scope) *Builder
- func (b *Builder) WithoutGlobalScope(scope *Scope) *Builder
- func (b *Builder) WithoutScope(scope *Scope) *Builder
- type Conditions
- func (c *Conditions) And(cb func(q *Conditions)) *Conditions
- func (c *Conditions) Clone() *Conditions
- func (c *Conditions) Or(cb func(q *Conditions)) *Conditions
- func (c *Conditions) OrWhere(column, operator string, value any) *Conditions
- func (c *Conditions) OrWhereColumn(column, operator string, valueColumn string) *Conditions
- func (c *Conditions) OrWhereExists(query QueryBuilder) *Conditions
- func (c *Conditions) OrWhereHas(relation string, cb func(q *Builder) *Builder) *Conditions
- func (c *Conditions) OrWhereIn(column string, values []any) *Conditions
- func (c *Conditions) OrWhereRaw(rawSql string, bindings ...any) *Conditions
- func (c *Conditions) OrWhereSubquery(subquery QueryBuilder, operator string, value any) *Conditions
- func (c *Conditions) SQLString(d dialects.Dialect) (string, []any, error)
- func (c *Conditions) Where(column, operator string, value any) *Conditions
- func (c *Conditions) WhereColumn(column, operator string, valueColumn string) *Conditions
- func (c *Conditions) WhereExists(query QueryBuilder) *Conditions
- func (c *Conditions) WhereHas(relation string, cb func(q *Builder) *Builder) *Conditions
- func (c *Conditions) WhereIn(column string, values []any) *Conditions
- func (c *Conditions) WhereRaw(rawSql string, bindings ...any) *Conditions
- func (c *Conditions) WhereSubquery(subquery QueryBuilder, operator string, value any) *Conditions
- type Deleter
- type ForeignKey
- type HasMany
- func (r *HasMany[T]) ForeignKeys() []*ForeignKey
- func (r *HasMany[T]) Initialize(parent any, field reflect.StructField) error
- func (r *HasMany[T]) Load(ctx context.Context, tx database.DB, relations []Relationship) error
- func (v *HasMany) Loaded() bool
- func (v *HasMany) MarshalJSON() ([]byte, error)
- func (r HasMany) Query() *ModelBuilder[T]
- func (r HasMany) Subquery() *Builder
- func (v *HasMany) Value() (T, bool)
- type HasOne
- func (r *HasOne[T]) ForeignKeys() []*ForeignKey
- func (r *HasOne[T]) Initialize(parent any, field reflect.StructField) error
- func (r *HasOne[T]) Load(ctx context.Context, tx database.DB, relations []Relationship) error
- func (v *HasOne) Loaded() bool
- func (v *HasOne) MarshalJSON() ([]byte, error)
- func (r HasOne) Query() *ModelBuilder[T]
- func (r HasOne) Subquery() *Builder
- func (v *HasOne) Value() (T, bool)
- type ModelBuilder
- func (b *ModelBuilder[T]) AddGroupBy(columns ...string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) AddSelect(columns ...string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) AddSelectFunction(function, column string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) AddSelectSubquery(sb QueryBuilder, as string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) And(cb func(q *Conditions)) *ModelBuilder[T]
- func (b *ModelBuilder[T]) Clone() *ModelBuilder[T]
- func (b *ModelBuilder[T]) Context() context.Context
- func (b *ModelBuilder[T]) Count(tx database.DB) (int, error)
- func (b *ModelBuilder[T]) CrossJoin(table, localColumn, operator, foreignColumn string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) CrossJoinOn(table string, cb func(q *Conditions)) *ModelBuilder[T]
- func (b *ModelBuilder[T]) Delete(tx database.DB) error
- func (b *ModelBuilder[T]) Deleter() *Deleter
- func (b *ModelBuilder[T]) Distinct() *ModelBuilder[T]
- func (b *ModelBuilder[T]) Dump() *ModelBuilder[T]
- func (b *ModelBuilder[T]) Each(tx database.DB, cb func(v T) error) error
- func (b *ModelBuilder[T]) Find(tx database.DB, primaryKeyValue any) (T, error)
- func (b *ModelBuilder[T]) First(tx database.DB) (T, error)
- func (b *ModelBuilder[T]) From(table string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) Get(tx database.DB) ([]T, error)
- func (b *ModelBuilder[T]) GetTable() string
- func (b *ModelBuilder[T]) GroupBy(columns ...string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) Having(column, operator string, value any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) HavingAnd(cb func(q *Conditions)) *ModelBuilder[T]
- func (b *ModelBuilder[T]) HavingColumn(column, operator string, valueColumn string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) HavingExists(query QueryBuilder) *ModelBuilder[T]
- func (b *ModelBuilder[T]) HavingHas(relation string, cb func(q *Builder) *Builder) *ModelBuilder[T]
- func (b *ModelBuilder[T]) HavingIn(column string, values []any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) HavingOr(cb func(q *Conditions)) *ModelBuilder[T]
- func (b *ModelBuilder[T]) HavingRaw(rawSql string, bindings ...any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) HavingSubquery(subquery QueryBuilder, operator string, value any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) InnerJoin(table, localColumn, operator, foreignColumn string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) InnerJoinOn(table string, cb func(q *Conditions)) *ModelBuilder[T]
- func (b *ModelBuilder[T]) Join(table, localColumn, operator, foreignColumn string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) JoinOn(table string, cb func(q *Conditions)) *ModelBuilder[T]
- func (b *ModelBuilder[T]) LeftJoin(table, localColumn, operator, foreignColumn string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) LeftJoinOn(table string, cb func(q *Conditions)) *ModelBuilder[T]
- func (b *ModelBuilder[T]) Limit(limit int) *ModelBuilder[T]
- func (b *ModelBuilder[T]) Load(tx database.DB, v any) error
- func (b *ModelBuilder[T]) LoadOne(tx database.DB, v any) errordeprecated
- func (b *ModelBuilder[T]) Offset(offset int) *ModelBuilder[T]
- func (b *ModelBuilder[T]) Or(cb func(q *Conditions)) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrHaving(column, operator string, value any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrHavingColumn(column, operator string, valueColumn string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrHavingExists(query QueryBuilder) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrHavingHas(relation string, cb func(q *Builder) *Builder) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrHavingIn(column string, values []any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrHavingRaw(rawSql string, bindings ...any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrHavingSubquery(subquery QueryBuilder, operator string, value any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrWhere(column, operator string, value any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrWhereColumn(column, operator string, valueColumn string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrWhereExists(query QueryBuilder) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrWhereHas(relation string, cb func(q *Builder) *Builder) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrWhereIn(column string, values []any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrWhereRaw(rawSql string, bindings ...any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrWhereSubquery(subquery QueryBuilder, operator string, value any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrderBy(column string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) OrderByDesc(column string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) RightJoin(table, localColumn, operator, foreignColumn string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) RightJoinOn(table string, cb func(q *Conditions)) *ModelBuilder[T]
- func (b *ModelBuilder[T]) SQLString(d dialects.Dialect) (string, []any, error)
- func (b *ModelBuilder[T]) Select(columns ...string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) SelectFunction(function, column string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) SelectSubquery(sb QueryBuilder, as string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) Unordered() *ModelBuilder[T]
- func (b *ModelBuilder[T]) Where(column, operator string, value any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) WhereColumn(column, operator string, valueColumn string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) WhereExists(query QueryBuilder) *ModelBuilder[T]
- func (b *ModelBuilder[T]) WhereHas(relation string, cb func(q *Builder) *Builder) *ModelBuilder[T]
- func (b *ModelBuilder[T]) WhereIn(column string, values []any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) WhereRaw(rawSql string, bindings ...any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) WhereSubquery(subquery QueryBuilder, operator string, value any) *ModelBuilder[T]
- func (b *ModelBuilder[T]) With(withs ...string) *ModelBuilder[T]
- func (b *ModelBuilder[T]) WithContext(ctx context.Context) *ModelBuilder[T]
- func (b *ModelBuilder[T]) WithScope(scope *Scope) *ModelBuilder[T]
- func (b *ModelBuilder[T]) WithoutGlobalScope(scope *Scope) *ModelBuilder[T]
- func (b *ModelBuilder[T]) WithoutScope(scope *Scope) *ModelBuilder[T]
- type QueryBuilder
- type QueryError
- type Relationship
- type Scope
- type ScopeFunc
- type Scoper
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrMissingRelationship = fmt.Errorf("missing relationship") ErrMissingField = fmt.Errorf("missing related field") )
var SoftDeletes = &Scope{ Name: "soft-deletes", Apply: func(b *Builder) *Builder { return b.Where(b.GetTable()+".deleted_at", "=", nil) }, }
Functions ¶
func LoadContext ¶
func LoadMissingContext ¶
func NewSelects ¶
func NewSelects() *selects
Types ¶
type BelongsTo ¶
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) 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 (BelongsTo) Query ¶ added in v0.15.0
func (r BelongsTo) Query() *ModelBuilder[T]
Query returns a ModelBuilder scoped to the relationship.
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 ¶
GroupBy adds a "group by" clause to the query.
func (*Builder) AddSelectFunction ¶
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) 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) HavingAnd ¶
func (b *Builder) HavingAnd(cb func(q *Conditions)) *Builder
HavingAnd adds a group of conditions to the query
func (*Builder) HavingColumn ¶
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 ¶
HavingHas adds a relationship exists condition to the query with having clauses.
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) 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) 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) 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) 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) Or ¶
func (b *Builder) Or(cb func(q *Conditions)) *Builder
Or adds a group of conditions to the query with an or
func (*Builder) OrHavingColumn ¶
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 ¶
OrHavingHas adds a relationship exists condition to the query with having clauses and an or.
func (*Builder) OrHavingIn ¶
OrHavingIn adds an or having in clause to the query.
func (*Builder) OrHavingRaw ¶
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) OrWhereColumn ¶
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 ¶
OrWhereHas adds a relationship exists condition to the query with where clauses and an or.
func (*Builder) OrWhereRaw ¶
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) OrderByDesc ¶
OrderByDesc adds a descending order by 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) SelectFunction ¶
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) WhereColumn ¶
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 ¶
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) 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 ¶
WithContext adds a context to the query that will be used when fetching results.
func (*Builder) WithoutGlobalScope ¶
WithoutGlobalScope removes a global scope from the query.
func (*Builder) WithoutScope ¶
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) 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 ForeignKey ¶
func (*ForeignKey) Equal ¶
func (f *ForeignKey) Equal(v *ForeignKey) bool
type HasMany ¶
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) 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 (HasMany) Query ¶ added in v0.15.0
func (r HasMany) Query() *ModelBuilder[T]
Query returns a ModelBuilder scoped to the relationship.
type HasOne ¶
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) 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 (HasOne) Query ¶ added in v0.15.0
func (r HasOne) Query() *ModelBuilder[T]
Query returns a ModelBuilder scoped to the relationship.
type ModelBuilder ¶ added in v0.10.0
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 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]) 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]) 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 }
Source Files ¶
- belongs-to-one.go
- builder.go
- clone.go
- context.go
- default-scopes.go
- delete.go
- from.go
- generated_Builder.go
- generated_ModelBuilder.go
- get.go
- group-by.go
- has-many.go
- has-one-or-many.go
- has-one.go
- join.go
- limit.go
- order-by.go
- relationship-load.go
- relationship.go
- scope.go
- select.go
- to-sql.go
- util.go
- where-list.go
- with.go