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) Subquery() *SubBuilder
- func (v *BelongsTo) Value() (T, bool)
- type Builder
- func (b *Builder[T]) AddGroupBy(columns ...string) *Builder[T]
- func (b *Builder[T]) AddSelect(columns ...string) *Builder[T]
- func (b *Builder[T]) AddSelectFunction(function, column string) *Builder[T]
- func (b *Builder[T]) AddSelectSubquery(sb QueryBuilder, as string) *Builder[T]
- func (b *Builder[T]) And(cb func(q *Conditions)) *Builder[T]
- func (b *Builder[T]) Clone() *Builder[T]
- func (b *Builder[T]) Context() context.Context
- func (b *Builder[T]) CrossJoin(table, localColumn, operator, foreignColumn string) *Builder[T]
- func (b *Builder[T]) CrossJoinOn(table string, cb func(q *Conditions)) *Builder[T]
- func (b *Builder[T]) Delete(tx database.DB) error
- func (b *Builder[T]) Deleter() *Deleter
- func (b *Builder[T]) Distinct() *Builder[T]
- func (b *Builder[T]) Dump() *Builder[T]
- func (b *Builder[T]) Each(tx database.DB, cb func(v T) error) error
- func (b *Builder[T]) Find(tx database.DB, primaryKeyValue any) (T, error)
- func (b *Builder[T]) First(tx database.DB) (T, error)
- func (b *Builder[T]) From(table string) *Builder[T]
- func (b *Builder[T]) Get(tx database.DB) ([]T, error)
- func (b *Builder[T]) GetTable() string
- func (b *Builder[T]) GroupBy(columns ...string) *Builder[T]
- func (b *Builder[T]) Having(column, operator string, value any) *Builder[T]
- func (b *Builder[T]) HavingAnd(cb func(q *Conditions)) *Builder[T]
- func (b *Builder[T]) HavingColumn(column, operator string, valueColumn string) *Builder[T]
- func (b *Builder[T]) HavingExists(query QueryBuilder) *Builder[T]
- func (b *Builder[T]) HavingHas(relation string, cb func(q *SubBuilder) *SubBuilder) *Builder[T]
- func (b *Builder[T]) HavingIn(column string, values []any) *Builder[T]
- func (b *Builder[T]) HavingOr(cb func(q *Conditions)) *Builder[T]
- func (b *Builder[T]) HavingRaw(rawSql string, bindings ...any) *Builder[T]
- func (b *Builder[T]) HavingSubquery(subquery QueryBuilder, operator string, value any) *Builder[T]
- func (b *Builder[T]) InnerJoin(table, localColumn, operator, foreignColumn string) *Builder[T]
- func (b *Builder[T]) InnerJoinOn(table string, cb func(q *Conditions)) *Builder[T]
- func (b *Builder[T]) Join(table, localColumn, operator, foreignColumn string) *Builder[T]
- func (b *Builder[T]) JoinOn(table string, cb func(q *Conditions)) *Builder[T]
- func (b *Builder[T]) LeftJoin(table, localColumn, operator, foreignColumn string) *Builder[T]
- func (b *Builder[T]) LeftJoinOn(table string, cb func(q *Conditions)) *Builder[T]
- func (b *Builder[T]) Limit(limit int) *Builder[T]
- func (b *Builder[T]) Load(tx database.DB, v any) error
- func (b *Builder[T]) LoadOne(tx database.DB, v any) error
- func (b *Builder[T]) Offset(offset int) *Builder[T]
- func (b *Builder[T]) Or(cb func(q *Conditions)) *Builder[T]
- func (b *Builder[T]) OrHaving(column, operator string, value any) *Builder[T]
- func (b *Builder[T]) OrHavingColumn(column, operator string, valueColumn string) *Builder[T]
- func (b *Builder[T]) OrHavingExists(query QueryBuilder) *Builder[T]
- func (b *Builder[T]) OrHavingHas(relation string, cb func(q *SubBuilder) *SubBuilder) *Builder[T]
- func (b *Builder[T]) OrHavingIn(column string, values []any) *Builder[T]
- func (b *Builder[T]) OrHavingRaw(rawSql string, bindings ...any) *Builder[T]
- func (b *Builder[T]) OrHavingSubquery(subquery QueryBuilder, operator string, value any) *Builder[T]
- func (b *Builder[T]) OrWhere(column, operator string, value any) *Builder[T]
- func (b *Builder[T]) OrWhereColumn(column, operator string, valueColumn string) *Builder[T]
- func (b *Builder[T]) OrWhereExists(query QueryBuilder) *Builder[T]
- func (b *Builder[T]) OrWhereHas(relation string, cb func(q *SubBuilder) *SubBuilder) *Builder[T]
- func (b *Builder[T]) OrWhereIn(column string, values []any) *Builder[T]
- func (b *Builder[T]) OrWhereRaw(rawSql string, bindings ...any) *Builder[T]
- func (b *Builder[T]) OrWhereSubquery(subquery QueryBuilder, operator string, value any) *Builder[T]
- func (b *Builder[T]) OrderBy(column string) *Builder[T]
- func (b *Builder[T]) OrderByDesc(column string) *Builder[T]
- func (b *Builder[T]) RightJoin(table, localColumn, operator, foreignColumn string) *Builder[T]
- func (b *Builder[T]) RightJoinOn(table string, cb func(q *Conditions)) *Builder[T]
- func (b *Builder[T]) Select(columns ...string) *Builder[T]
- func (b *Builder[T]) SelectFunction(function, column string) *Builder[T]
- func (b *Builder[T]) SelectSubquery(sb QueryBuilder, as string) *Builder[T]
- func (b *Builder[T]) ToSQL(d dialects.Dialect) (string, []any, error)
- func (b *Builder[T]) Unordered() *Builder[T]
- func (b *Builder[T]) Where(column, operator string, value any) *Builder[T]
- func (b *Builder[T]) WhereColumn(column, operator string, valueColumn string) *Builder[T]
- func (b *Builder[T]) WhereExists(query QueryBuilder) *Builder[T]
- func (b *Builder[T]) WhereHas(relation string, cb func(q *SubBuilder) *SubBuilder) *Builder[T]
- func (b *Builder[T]) WhereIn(column string, values []any) *Builder[T]
- func (b *Builder[T]) WhereRaw(rawSql string, bindings ...any) *Builder[T]
- func (b *Builder[T]) WhereSubquery(subquery QueryBuilder, operator string, value any) *Builder[T]
- func (b *Builder[T]) With(withs ...string) *Builder[T]
- func (b *Builder[T]) WithContext(ctx context.Context) *Builder[T]
- func (b *Builder[T]) WithScope(scope *Scope) *Builder[T]
- func (b *Builder[T]) WithoutGlobalScope(scope *Scope) *Builder[T]
- func (b *Builder[T]) WithoutScope(scope *Scope) *Builder[T]
- 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 *SubBuilder) *SubBuilder) *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) ToSQL(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 *SubBuilder) *SubBuilder) *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) Subquery() *SubBuilder
- 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) Subquery() *SubBuilder
- func (v *HasOne) Value() (T, bool)
- type QueryBuilder
- type Relationship
- type Scope
- type ScopeFunc
- type Scoper
- type SubBuilder
- func (b *SubBuilder) AddGroupBy(columns ...string) *SubBuilder
- func (b *SubBuilder) AddSelect(columns ...string) *SubBuilder
- func (b *SubBuilder) AddSelectFunction(function, column string) *SubBuilder
- func (b *SubBuilder) AddSelectSubquery(sb QueryBuilder, as string) *SubBuilder
- func (b *SubBuilder) And(cb func(q *Conditions)) *SubBuilder
- func (b *SubBuilder) Clone() *SubBuilder
- func (b *SubBuilder) Context() context.Context
- func (b *SubBuilder) CrossJoin(table, localColumn, operator, foreignColumn string) *SubBuilder
- func (b *SubBuilder) CrossJoinOn(table string, cb func(q *Conditions)) *SubBuilder
- func (b *SubBuilder) Delete(tx database.DB) error
- func (b *SubBuilder) Deleter() *Deleter
- func (b *SubBuilder) Distinct() *SubBuilder
- func (b *SubBuilder) Dump() *SubBuilder
- func (b *SubBuilder) From(table string) *SubBuilder
- func (b *SubBuilder) GetTable() string
- func (b *SubBuilder) GroupBy(columns ...string) *SubBuilder
- func (b *SubBuilder) Having(column, operator string, value any) *SubBuilder
- func (b *SubBuilder) HavingAnd(cb func(q *Conditions)) *SubBuilder
- func (b *SubBuilder) HavingColumn(column, operator string, valueColumn string) *SubBuilder
- func (b *SubBuilder) HavingExists(query QueryBuilder) *SubBuilder
- func (b *SubBuilder) HavingHas(relation string, cb func(q *SubBuilder) *SubBuilder) *SubBuilder
- func (b *SubBuilder) HavingIn(column string, values []any) *SubBuilder
- func (b *SubBuilder) HavingOr(cb func(q *Conditions)) *SubBuilder
- func (b *SubBuilder) HavingRaw(rawSql string, bindings ...any) *SubBuilder
- func (b *SubBuilder) HavingSubquery(subquery QueryBuilder, operator string, value any) *SubBuilder
- func (b *SubBuilder) InnerJoin(table, localColumn, operator, foreignColumn string) *SubBuilder
- func (b *SubBuilder) InnerJoinOn(table string, cb func(q *Conditions)) *SubBuilder
- func (b *SubBuilder) Join(table, localColumn, operator, foreignColumn string) *SubBuilder
- func (b *SubBuilder) JoinOn(table string, cb func(q *Conditions)) *SubBuilder
- func (b *SubBuilder) LeftJoin(table, localColumn, operator, foreignColumn string) *SubBuilder
- func (b *SubBuilder) LeftJoinOn(table string, cb func(q *Conditions)) *SubBuilder
- func (b *SubBuilder) Limit(limit int) *SubBuilder
- func (b *SubBuilder) Load(tx database.DB, v any) error
- func (b *SubBuilder) LoadOne(tx database.DB, v any) error
- func (b *SubBuilder) Offset(offset int) *SubBuilder
- func (b *SubBuilder) Or(cb func(q *Conditions)) *SubBuilder
- func (b *SubBuilder) OrHaving(column, operator string, value any) *SubBuilder
- func (b *SubBuilder) OrHavingColumn(column, operator string, valueColumn string) *SubBuilder
- func (b *SubBuilder) OrHavingExists(query QueryBuilder) *SubBuilder
- func (b *SubBuilder) OrHavingHas(relation string, cb func(q *SubBuilder) *SubBuilder) *SubBuilder
- func (b *SubBuilder) OrHavingIn(column string, values []any) *SubBuilder
- func (b *SubBuilder) OrHavingRaw(rawSql string, bindings ...any) *SubBuilder
- func (b *SubBuilder) OrHavingSubquery(subquery QueryBuilder, operator string, value any) *SubBuilder
- func (b *SubBuilder) OrWhere(column, operator string, value any) *SubBuilder
- func (b *SubBuilder) OrWhereColumn(column, operator string, valueColumn string) *SubBuilder
- func (b *SubBuilder) OrWhereExists(query QueryBuilder) *SubBuilder
- func (b *SubBuilder) OrWhereHas(relation string, cb func(q *SubBuilder) *SubBuilder) *SubBuilder
- func (b *SubBuilder) OrWhereIn(column string, values []any) *SubBuilder
- func (b *SubBuilder) OrWhereRaw(rawSql string, bindings ...any) *SubBuilder
- func (b *SubBuilder) OrWhereSubquery(subquery QueryBuilder, operator string, value any) *SubBuilder
- func (b *SubBuilder) OrderBy(column string) *SubBuilder
- func (b *SubBuilder) OrderByDesc(column string) *SubBuilder
- func (b *SubBuilder) RightJoin(table, localColumn, operator, foreignColumn string) *SubBuilder
- func (b *SubBuilder) RightJoinOn(table string, cb func(q *Conditions)) *SubBuilder
- func (b *SubBuilder) Select(columns ...string) *SubBuilder
- func (b *SubBuilder) SelectFunction(function, column string) *SubBuilder
- func (b *SubBuilder) SelectSubquery(sb QueryBuilder, as string) *SubBuilder
- func (b *SubBuilder) ToSQL(d dialects.Dialect) (string, []any, error)
- func (b *SubBuilder) Unordered() *SubBuilder
- func (b *SubBuilder) Where(column, operator string, value any) *SubBuilder
- func (b *SubBuilder) WhereColumn(column, operator string, valueColumn string) *SubBuilder
- func (b *SubBuilder) WhereExists(query QueryBuilder) *SubBuilder
- func (b *SubBuilder) WhereHas(relation string, cb func(q *SubBuilder) *SubBuilder) *SubBuilder
- func (b *SubBuilder) WhereIn(column string, values []any) *SubBuilder
- func (b *SubBuilder) WhereRaw(rawSql string, bindings ...any) *SubBuilder
- func (b *SubBuilder) WhereSubquery(subquery QueryBuilder, operator string, value any) *SubBuilder
- func (b *SubBuilder) WithContext(ctx context.Context) *SubBuilder
- func (b *SubBuilder) WithScope(scope *Scope) *SubBuilder
- func (b *SubBuilder) WithoutGlobalScope(scope *Scope) *SubBuilder
- func (b *SubBuilder) WithoutScope(scope *Scope) *SubBuilder
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 *SubBuilder) *SubBuilder { 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) Subquery ¶
func (r BelongsTo) Subquery() *SubBuilder
Subquery returns a SubBuilder scoped to the relationship.
type Builder ¶
Builder represents an sql query and any bindings needed to run it.
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"). ToSQL(dialects.New()) if err != nil { panic(err) } fmt.Println(bindings) fmt.Println(query) }
Output: [value] SELECT "foos".* FROM "foos" WHERE "column" = ?
func (*Builder[T]) AddGroupBy ¶
GroupBy adds a "group by" clause to the query.
func (*Builder[T]) AddSelectFunction ¶
SelectFunction adds a column to be selected with a function applied.
func (*Builder[T]) AddSelectSubquery ¶
func (b *Builder[T]) AddSelectSubquery(sb QueryBuilder, as string) *Builder[T]
AddSelectSubquery adds a subquery to be selected.
func (*Builder[T]) And ¶
func (b *Builder[T]) And(cb func(q *Conditions)) *Builder[T]
And adds a group of conditions to the query
func (*Builder[T]) CrossJoinOn ¶
func (b *Builder[T]) CrossJoinOn(table string, cb func(q *Conditions)) *Builder[T]
CrossJoinOn adds a cross join clause to the query with a complex on statement.
func (*Builder[T]) Find ¶
Find returns the record with a matching primary key. It will fail on tables with multiple primary keys.
func (*Builder[T]) First ¶
Get executes the query as a select statement and returns the first record.
func (*Builder[T]) HavingAnd ¶
func (b *Builder[T]) HavingAnd(cb func(q *Conditions)) *Builder[T]
HavingAnd adds a group of conditions to the query
func (*Builder[T]) HavingColumn ¶
HavingColumn adds a having clause to the query comparing two columns.
func (*Builder[T]) HavingExists ¶
func (b *Builder[T]) HavingExists(query QueryBuilder) *Builder[T]
HavingExists add an exists clause to the query.
func (*Builder[T]) HavingHas ¶
func (b *Builder[T]) HavingHas(relation string, cb func(q *SubBuilder) *SubBuilder) *Builder[T]
HavingHas adds a relationship exists condition to the query with having clauses.
func (*Builder[T]) HavingOr ¶
func (b *Builder[T]) HavingOr(cb func(q *Conditions)) *Builder[T]
HavingOr adds a group of conditions to the query with an or
func (*Builder[T]) HavingSubquery ¶
func (b *Builder[T]) HavingSubquery(subquery QueryBuilder, operator string, value any) *Builder[T]
HavingSubquery adds a having clause to the query comparing a column and a subquery.
func (*Builder[T]) InnerJoinOn ¶
func (b *Builder[T]) InnerJoinOn(table string, cb func(q *Conditions)) *Builder[T]
InnerJoinOn adds an inner join clause to the query with a complex on statement.
func (*Builder[T]) JoinOn ¶
func (b *Builder[T]) JoinOn(table string, cb func(q *Conditions)) *Builder[T]
JoinOn adds a join clause to the query with a complex on statement.
func (*Builder[T]) LeftJoinOn ¶
func (b *Builder[T]) LeftJoinOn(table string, cb func(q *Conditions)) *Builder[T]
LeftJoinOn adds a left join clause to the query with a complex on statement.
func (*Builder[T]) LoadOne ¶
Load executes the query as a select statement and sets v to the result.
func (*Builder[T]) Or ¶
func (b *Builder[T]) Or(cb func(q *Conditions)) *Builder[T]
Or adds a group of conditions to the query with an or
func (*Builder[T]) OrHavingColumn ¶
OrHavingColumn adds an or having clause to the query comparing two columns.
func (*Builder[T]) OrHavingExists ¶
func (b *Builder[T]) OrHavingExists(query QueryBuilder) *Builder[T]
WhereExists add an exists clause to the query.
func (*Builder[T]) OrHavingHas ¶
func (b *Builder[T]) OrHavingHas(relation string, cb func(q *SubBuilder) *SubBuilder) *Builder[T]
OrHavingHas adds a relationship exists condition to the query with having clauses and an or.
func (*Builder[T]) OrHavingIn ¶
OrHavingIn adds an or having in clause to the query.
func (*Builder[T]) OrHavingRaw ¶
OrHavingRaw adds a raw or having clause to the query.
func (*Builder[T]) OrHavingSubquery ¶
func (b *Builder[T]) OrHavingSubquery(subquery QueryBuilder, operator string, value any) *Builder[T]
OrHavingSubquery adds an or having clause to the query comparing a column and a subquery.
func (*Builder[T]) OrWhereColumn ¶
OrWhereColumn adds an or where clause to the query comparing two columns.
func (*Builder[T]) OrWhereExists ¶
func (b *Builder[T]) OrWhereExists(query QueryBuilder) *Builder[T]
WhereExists add an exists clause to the query.
func (*Builder[T]) OrWhereHas ¶
func (b *Builder[T]) OrWhereHas(relation string, cb func(q *SubBuilder) *SubBuilder) *Builder[T]
OrWhereHas adds a relationship exists condition to the query with where clauses and an or.
func (*Builder[T]) OrWhereRaw ¶
OrWhereRaw adds a raw or where clause to the query.
func (*Builder[T]) OrWhereSubquery ¶
func (b *Builder[T]) OrWhereSubquery(subquery QueryBuilder, operator string, value any) *Builder[T]
OrWhereSubquery adds an or where clause to the query comparing a column and a subquery.
func (*Builder[T]) OrderByDesc ¶
OrderByDesc adds a descending order by clause to the query.
func (*Builder[T]) RightJoinOn ¶
func (b *Builder[T]) RightJoinOn(table string, cb func(q *Conditions)) *Builder[T]
RightJoinOn adds a right join clause to the query with a complex on statement.
func (*Builder[T]) SelectFunction ¶
SelectFunction sets a column to be selected with a function applied.
func (*Builder[T]) SelectSubquery ¶
func (b *Builder[T]) SelectSubquery(sb QueryBuilder, as string) *Builder[T]
SelectSubquery sets a subquery to be selected.
func (*Builder[T]) WhereColumn ¶
WhereColumn adds a where clause to the query comparing two columns.
func (*Builder[T]) WhereExists ¶
func (b *Builder[T]) WhereExists(query QueryBuilder) *Builder[T]
WhereExists add an exists clause to the query.
func (*Builder[T]) WhereHas ¶
func (b *Builder[T]) WhereHas(relation string, cb func(q *SubBuilder) *SubBuilder) *Builder[T]
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.SubBuilder) *builder.SubBuilder { return q.Where("id", "=", 7) }). ToSQL(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[T]) WhereSubquery ¶
func (b *Builder[T]) WhereSubquery(subquery QueryBuilder, operator string, value any) *Builder[T]
WhereSubquery adds a where clause to the query comparing a column and a subquery.
func (*Builder[T]) WithContext ¶
WithContext adds a context to the query that will be used when fetching results.
func (*Builder[T]) WithoutGlobalScope ¶
WithoutGlobalScope removes a global scope from the query.
func (*Builder[T]) 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 *SubBuilder) *SubBuilder) *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 *SubBuilder) *SubBuilder) *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) Subquery ¶
func (r HasMany) Subquery() *SubBuilder
Subquery returns a SubBuilder 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) Subquery ¶
func (r HasOne) Subquery() *SubBuilder
Subquery returns a SubBuilder scoped to the relationship.
type QueryBuilder ¶
QueryBuilder is implemented by *Builder and *SubBuilder
type Relationship ¶
type Relationship interface { relationship.Relationship Subquery() *SubBuilder Load(ctx context.Context, tx database.DB, relations []Relationship) error ForeignKeys() []*ForeignKey }
type ScopeFunc ¶
type ScopeFunc func(b *SubBuilder) *SubBuilder
type SubBuilder ¶
type SubBuilder struct {
// contains filtered or unexported fields
}
func NewSubBuilder ¶
func NewSubBuilder() *SubBuilder
NewSubBuilder creates a new SubBuilder without anything selected
func (*SubBuilder) AddGroupBy ¶
func (b *SubBuilder) AddGroupBy(columns ...string) *SubBuilder
GroupBy adds a "group by" clause to the query.
func (*SubBuilder) AddSelect ¶
func (b *SubBuilder) AddSelect(columns ...string) *SubBuilder
AddSelect adds new columns to be selected.
func (*SubBuilder) AddSelectFunction ¶
func (b *SubBuilder) AddSelectFunction(function, column string) *SubBuilder
SelectFunction adds a column to be selected with a function applied.
func (*SubBuilder) AddSelectSubquery ¶
func (b *SubBuilder) AddSelectSubquery(sb QueryBuilder, as string) *SubBuilder
AddSelectSubquery adds a subquery to be selected.
func (*SubBuilder) And ¶
func (b *SubBuilder) And(cb func(q *Conditions)) *SubBuilder
And adds a group of conditions to the query
func (*SubBuilder) Clone ¶
func (b *SubBuilder) Clone() *SubBuilder
func (*SubBuilder) Context ¶
func (b *SubBuilder) Context() context.Context
Context returns the context value from the query.
func (*SubBuilder) CrossJoin ¶
func (b *SubBuilder) CrossJoin(table, localColumn, operator, foreignColumn string) *SubBuilder
CrossJoin adds a cross join clause to the query.
func (*SubBuilder) CrossJoinOn ¶
func (b *SubBuilder) CrossJoinOn(table string, cb func(q *Conditions)) *SubBuilder
CrossJoinOn adds a cross join clause to the query with a complex on statement.
func (*SubBuilder) Deleter ¶ added in v0.7.0
func (b *SubBuilder) Deleter() *Deleter
func (*SubBuilder) Distinct ¶
func (b *SubBuilder) Distinct() *SubBuilder
Distinct forces the query to only return distinct results.
func (*SubBuilder) Dump ¶
func (b *SubBuilder) Dump() *SubBuilder
func (*SubBuilder) From ¶
func (b *SubBuilder) From(table string) *SubBuilder
From sets the table which the query is targeting.
func (*SubBuilder) GetTable ¶
func (b *SubBuilder) GetTable() string
GetTable returns the table the query is targeting
func (*SubBuilder) GroupBy ¶
func (b *SubBuilder) GroupBy(columns ...string) *SubBuilder
GroupBy sets the "group by" clause to the query.
func (*SubBuilder) Having ¶
func (b *SubBuilder) Having(column, operator string, value any) *SubBuilder
Having adds a basic having clause to the query.
func (*SubBuilder) HavingAnd ¶
func (b *SubBuilder) HavingAnd(cb func(q *Conditions)) *SubBuilder
HavingAnd adds a group of conditions to the query
func (*SubBuilder) HavingColumn ¶
func (b *SubBuilder) HavingColumn(column, operator string, valueColumn string) *SubBuilder
HavingColumn adds a having clause to the query comparing two columns.
func (*SubBuilder) HavingExists ¶
func (b *SubBuilder) HavingExists(query QueryBuilder) *SubBuilder
HavingExists add an exists clause to the query.
func (*SubBuilder) HavingHas ¶
func (b *SubBuilder) HavingHas(relation string, cb func(q *SubBuilder) *SubBuilder) *SubBuilder
HavingHas adds a relationship exists condition to the query with having clauses.
func (*SubBuilder) HavingIn ¶
func (b *SubBuilder) HavingIn(column string, values []any) *SubBuilder
HavingIn adds a having in clause to the query.
func (*SubBuilder) HavingOr ¶
func (b *SubBuilder) HavingOr(cb func(q *Conditions)) *SubBuilder
HavingOr adds a group of conditions to the query with an or
func (*SubBuilder) HavingRaw ¶
func (b *SubBuilder) HavingRaw(rawSql string, bindings ...any) *SubBuilder
HavingRaw adds a raw having clause to the query.
func (*SubBuilder) HavingSubquery ¶
func (b *SubBuilder) HavingSubquery(subquery QueryBuilder, operator string, value any) *SubBuilder
HavingSubquery adds a having clause to the query comparing a column and a subquery.
func (*SubBuilder) InnerJoin ¶
func (b *SubBuilder) InnerJoin(table, localColumn, operator, foreignColumn string) *SubBuilder
InnerJoin adds an inner join clause to the query.
func (*SubBuilder) InnerJoinOn ¶
func (b *SubBuilder) InnerJoinOn(table string, cb func(q *Conditions)) *SubBuilder
InnerJoinOn adds an inner join clause to the query with a complex on statement.
func (*SubBuilder) Join ¶
func (b *SubBuilder) Join(table, localColumn, operator, foreignColumn string) *SubBuilder
Join adds a join clause to the query.
func (*SubBuilder) JoinOn ¶
func (b *SubBuilder) JoinOn(table string, cb func(q *Conditions)) *SubBuilder
JoinOn adds a join clause to the query with a complex on statement.
func (*SubBuilder) LeftJoin ¶
func (b *SubBuilder) LeftJoin(table, localColumn, operator, foreignColumn string) *SubBuilder
LeftJoin adds a left join clause to the query.
func (*SubBuilder) LeftJoinOn ¶
func (b *SubBuilder) LeftJoinOn(table string, cb func(q *Conditions)) *SubBuilder
LeftJoinOn adds a left join clause to the query with a complex on statement.
func (*SubBuilder) Limit ¶
func (b *SubBuilder) Limit(limit int) *SubBuilder
Limit set the maximum number of rows to return.
func (*SubBuilder) Load ¶ added in v0.8.0
func (b *SubBuilder) Load(tx database.DB, v any) error
Load executes the query as a select statement and sets v to the result.
func (*SubBuilder) LoadOne ¶ added in v0.8.0
func (b *SubBuilder) LoadOne(tx database.DB, v any) error
Load executes the query as a select statement and sets v to the first record.
func (*SubBuilder) Offset ¶
func (b *SubBuilder) Offset(offset int) *SubBuilder
Offset sets the number of rows to skip before returning the result.
func (*SubBuilder) Or ¶
func (b *SubBuilder) Or(cb func(q *Conditions)) *SubBuilder
Or adds a group of conditions to the query with an or
func (*SubBuilder) OrHaving ¶
func (b *SubBuilder) OrHaving(column, operator string, value any) *SubBuilder
OrHaving adds an or having clause to the query
func (*SubBuilder) OrHavingColumn ¶
func (b *SubBuilder) OrHavingColumn(column, operator string, valueColumn string) *SubBuilder
OrHavingColumn adds an or having clause to the query comparing two columns.
func (*SubBuilder) OrHavingExists ¶
func (b *SubBuilder) OrHavingExists(query QueryBuilder) *SubBuilder
WhereExists add an exists clause to the query.
func (*SubBuilder) OrHavingHas ¶
func (b *SubBuilder) OrHavingHas(relation string, cb func(q *SubBuilder) *SubBuilder) *SubBuilder
OrHavingHas adds a relationship exists condition to the query with having clauses and an or.
func (*SubBuilder) OrHavingIn ¶
func (b *SubBuilder) OrHavingIn(column string, values []any) *SubBuilder
OrHavingIn adds an or having in clause to the query.
func (*SubBuilder) OrHavingRaw ¶
func (b *SubBuilder) OrHavingRaw(rawSql string, bindings ...any) *SubBuilder
OrHavingRaw adds a raw or having clause to the query.
func (*SubBuilder) OrHavingSubquery ¶
func (b *SubBuilder) OrHavingSubquery(subquery QueryBuilder, operator string, value any) *SubBuilder
OrHavingSubquery adds an or having clause to the query comparing a column and a subquery.
func (*SubBuilder) OrWhere ¶
func (b *SubBuilder) OrWhere(column, operator string, value any) *SubBuilder
OrWhere adds an or where clause to the query
func (*SubBuilder) OrWhereColumn ¶
func (b *SubBuilder) OrWhereColumn(column, operator string, valueColumn string) *SubBuilder
OrWhereColumn adds an or where clause to the query comparing two columns.
func (*SubBuilder) OrWhereExists ¶
func (b *SubBuilder) OrWhereExists(query QueryBuilder) *SubBuilder
WhereExists add an exists clause to the query.
func (*SubBuilder) OrWhereHas ¶
func (b *SubBuilder) OrWhereHas(relation string, cb func(q *SubBuilder) *SubBuilder) *SubBuilder
OrWhereHas adds a relationship exists condition to the query with where clauses and an or.
func (*SubBuilder) OrWhereIn ¶
func (b *SubBuilder) OrWhereIn(column string, values []any) *SubBuilder
OrWhereIn adds an or where in clause to the query.
func (*SubBuilder) OrWhereRaw ¶
func (b *SubBuilder) OrWhereRaw(rawSql string, bindings ...any) *SubBuilder
OrWhereRaw adds a raw or where clause to the query.
func (*SubBuilder) OrWhereSubquery ¶
func (b *SubBuilder) OrWhereSubquery(subquery QueryBuilder, operator string, value any) *SubBuilder
OrWhereSubquery adds an or where clause to the query comparing a column and a subquery.
func (*SubBuilder) OrderBy ¶
func (b *SubBuilder) OrderBy(column string) *SubBuilder
OrderBy adds an order by clause to the query.
func (*SubBuilder) OrderByDesc ¶
func (b *SubBuilder) OrderByDesc(column string) *SubBuilder
OrderByDesc adds a descending order by clause to the query.
func (*SubBuilder) RightJoin ¶
func (b *SubBuilder) RightJoin(table, localColumn, operator, foreignColumn string) *SubBuilder
RightJoin adds a right join clause to the query.
func (*SubBuilder) RightJoinOn ¶
func (b *SubBuilder) RightJoinOn(table string, cb func(q *Conditions)) *SubBuilder
RightJoinOn adds a right join clause to the query with a complex on statement.
func (*SubBuilder) Select ¶
func (b *SubBuilder) Select(columns ...string) *SubBuilder
Select sets the columns to be selected.
func (*SubBuilder) SelectFunction ¶
func (b *SubBuilder) SelectFunction(function, column string) *SubBuilder
SelectFunction sets a column to be selected with a function applied.
func (*SubBuilder) SelectSubquery ¶
func (b *SubBuilder) SelectSubquery(sb QueryBuilder, as string) *SubBuilder
SelectSubquery sets a subquery to be selected.
func (*SubBuilder) Unordered ¶
func (b *SubBuilder) Unordered() *SubBuilder
Unordered removes all order by clauses from the query.
func (*SubBuilder) Where ¶
func (b *SubBuilder) Where(column, operator string, value any) *SubBuilder
Where adds a basic where clause to the query.
func (*SubBuilder) WhereColumn ¶
func (b *SubBuilder) WhereColumn(column, operator string, valueColumn string) *SubBuilder
WhereColumn adds a where clause to the query comparing two columns.
func (*SubBuilder) WhereExists ¶
func (b *SubBuilder) WhereExists(query QueryBuilder) *SubBuilder
WhereExists add an exists clause to the query.
func (*SubBuilder) WhereHas ¶
func (b *SubBuilder) WhereHas(relation string, cb func(q *SubBuilder) *SubBuilder) *SubBuilder
WhereHas adds a relationship exists condition to the query with where clauses.
func (*SubBuilder) WhereIn ¶
func (b *SubBuilder) WhereIn(column string, values []any) *SubBuilder
WhereIn adds a where in clause to the query.
func (*SubBuilder) WhereRaw ¶
func (b *SubBuilder) WhereRaw(rawSql string, bindings ...any) *SubBuilder
WhereRaw adds a raw where clause to the query.
func (*SubBuilder) WhereSubquery ¶
func (b *SubBuilder) WhereSubquery(subquery QueryBuilder, operator string, value any) *SubBuilder
WhereSubquery adds a where clause to the query comparing a column and a subquery.
func (*SubBuilder) WithContext ¶
func (b *SubBuilder) WithContext(ctx context.Context) *SubBuilder
WithContext adds a context to the query that will be used when fetching results.
func (*SubBuilder) WithScope ¶
func (b *SubBuilder) WithScope(scope *Scope) *SubBuilder
WithScope adds a local scope to a query.
func (*SubBuilder) WithoutGlobalScope ¶
func (b *SubBuilder) WithoutGlobalScope(scope *Scope) *SubBuilder
WithoutGlobalScope removes a global scope from the query.
func (*SubBuilder) WithoutScope ¶
func (b *SubBuilder) WithoutScope(scope *Scope) *SubBuilder
WithoutScope removes the given scope from the local scopes.
Source Files ¶
- belongs-to-one.go
- builder.go
- clone.go
- context.go
- default-scopes.go
- delete.go
- from.go
- generated_Builder.go
- generated_SubBuilder.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