Documentation
¶
Index ¶
- type Builder
- type Clause
- type ClauseType
- type JoinClauseBuilder
- type LimitClauseBuilder
- type OffsetClauseBuilder
- type OrderClauseBuilder
- type Query
- func (q *Query) AfterCommit(callback func() error)
- func (q *Query) AfterRollback(callback func() error)
- func (q *Query) Association(assocName string) contractsorm.Association
- func (q *Query) Avg(column string, dest any) error
- func (q *Query) BeforeCommit(callback func() error)
- func (q *Query) BeforeRollback(callback func() error)
- func (q *Query) Begin(opts ...*sql.TxOptions) (contractsorm.Query, error)
- func (q *Query) Chunk(size int, callback any) error
- func (q *Query) Clone() contractsorm.Query
- func (q *Query) Commit() error
- func (q *Query) Connection(name string) contractsorm.Query
- func (q *Query) Count(count *int64) error
- func (q *Query) Create(value any) error
- func (q *Query) CrossJoin(query string, args ...any) orm.Query
- func (q *Query) Cursor() (chan contractsorm.Cursor, error)
- func (q *Query) DB() (*sql.DB, error)
- func (q *Query) Decrement(column string, amount ...any) (*contractsorm.Result, error)
- func (q *Query) Delete(value ...any) (*contractsorm.Result, error)
- func (q *Query) DisableQueryLog()
- func (q *Query) Distinct(args ...any) orm.Query
- func (q *Query) Driver() database.Driver
- func (q *Query) EnableQueryLog()
- func (q *Query) Exec(sql string, values ...any) (*contractsorm.Result, error)
- func (q *Query) Exists(exists *bool) error
- func (q *Query) Find(dest any, conds ...any) error
- func (q *Query) FindOrFail(dest any, conds ...any) error
- func (q *Query) First(dest any) error
- func (q *Query) FirstOr(dest any, callback func() error) error
- func (q *Query) FirstOrCreate(dest any, conds ...any) error
- func (q *Query) FirstOrFail(dest any) error
- func (q *Query) FirstOrNew(dest any, attributes any, values ...any) error
- func (q *Query) FlushQueryLog()
- func (q *Query) ForceDelete(value ...any) (*contractsorm.Result, error)
- func (q *Query) Get(dest any) error
- func (q *Query) GetQueryLog() []contractsorm.QueryLog
- func (q *Query) Group(name string) orm.Query
- func (q *Query) Having(query any, args ...any) orm.Query
- func (q *Query) InRandomOrder() contractsorm.Query
- func (q *Query) InTransaction() bool
- func (q *Query) Increment(column string, amount ...any) (*contractsorm.Result, error)
- func (q *Query) InsertGetId(values any) (uint, error)
- func (q *Query) Join(query string, args ...any) orm.Query
- func (q *Query) LeftJoin(query string, args ...any) orm.Query
- func (q *Query) Limit(limit int) orm.Query
- func (q *Query) Load(dest any, relation string, args ...any) error
- func (q *Query) LoadMissing(dest any, relation string, args ...any) error
- func (q *Query) LockForUpdate() contractsorm.Query
- func (q *Query) Max(column string, dest any) error
- func (q *Query) Min(column string, dest any) error
- func (q *Query) Model(value any) contractsorm.Query
- func (q *Query) Observe(model any, observer contractsorm.Observer)
- func (q *Query) Offset(offset int) orm.Query
- func (q *Query) Omit(columns ...string) contractsorm.Query
- func (q *Query) OnlyTrashed() contractsorm.Query
- func (q *Query) OrWhere(query any, args ...any) orm.Query
- func (q *Query) OrWhereBetween(column string, x, y any) orm.Query
- func (q *Query) OrWhereColumn(first, operator, second string) orm.Query
- func (q *Query) OrWhereIn(column string, values []any) orm.Query
- func (q *Query) OrWhereJsonContains(column string, value any) orm.Query
- func (q *Query) OrWhereJsonContainsKey(column string) orm.Query
- func (q *Query) OrWhereJsonDoesntContain(column string, value any) orm.Query
- func (q *Query) OrWhereJsonDoesntContainKey(column string) orm.Query
- func (q *Query) OrWhereNot(query any, args ...any) orm.Query
- func (q *Query) OrWhereNotBetween(column string, x, y any) orm.Query
- func (q *Query) OrWhereNotIn(column string, values []any) orm.Query
- func (q *Query) OrWhereNull(column string) orm.Query
- func (q *Query) Order(value any) orm.Query
- func (q *Query) OrderBy(column string, direction ...string) orm.Query
- func (q *Query) OrderByDesc(column string) orm.Query
- func (q *Query) Paginate(page, limit int, dest any, total *int64) error
- func (q *Query) Pluck(column string, dest any) error
- func (q *Query) Raw(sql string, values ...any) contractsorm.Query
- func (q *Query) ReadDB() (*sql.DB, error)
- func (q *Query) Restore(model ...any) (*contractsorm.Result, error)
- func (q *Query) RightJoin(query string, args ...any) orm.Query
- func (q *Query) Rollback() error
- func (q *Query) RollbackTo(level string) error
- func (q *Query) Save(value any) error
- func (q *Query) SavePoint(name string) error
- func (q *Query) SaveQuietly(value any) error
- func (q *Query) Scan(dest any) error
- func (q *Query) Scopes(funcs ...func(contractsorm.Query) contractsorm.Query) contractsorm.Query
- func (q *Query) Select(query any, args ...any) orm.Query
- func (q *Query) SharedLock() contractsorm.Query
- func (q *Query) Sum(column string, dest any) error
- func (q *Query) Table(name string, args ...any) contractsorm.Query
- func (q *Query) ToRawSql() contractsorm.ToSql
- func (q *Query) ToSql() contractsorm.ToSql
- func (q *Query) Transaction(txFunc func(tx contractsorm.Query) error, opts ...*sql.TxOptions) error
- func (q *Query) Update(column any, value ...any) (*contractsorm.Result, error)
- func (q *Query) UpdateOrCreate(dest any, attributes any, values any) error
- func (q *Query) UpdateOrInsert(attributes any, values any) error
- func (q *Query) Value(column string, dest any) error
- func (q *Query) Where(query any, args ...any) orm.Query
- func (q *Query) WhereAll(columns []string, operator string, value any) orm.Query
- func (q *Query) WhereAny(columns []string, operator string, value any) orm.Query
- func (q *Query) WhereBetween(column string, x, y any) orm.Query
- func (q *Query) WhereColumn(first, operator, second string) orm.Query
- func (q *Query) WhereExists(callback func(orm.Query) orm.Query) orm.Query
- func (q *Query) WhereIn(column string, values []any) orm.Query
- func (q *Query) WhereJsonContains(column string, value any) orm.Query
- func (q *Query) WhereJsonContainsKey(column string) orm.Query
- func (q *Query) WhereJsonDoesntContain(column string, value any) orm.Query
- func (q *Query) WhereJsonDoesntContainKey(column string) orm.Query
- func (q *Query) WhereJsonLength(column string, operator string, value any) orm.Query
- func (q *Query) WhereNone(columns []string, operator string, value any) orm.Query
- func (q *Query) WhereNot(query any, args ...any) orm.Query
- func (q *Query) WhereNotBetween(column string, x, y any) orm.Query
- func (q *Query) WhereNotIn(column string, values []any) orm.Query
- func (q *Query) WhereNotNull(column string) orm.Query
- func (q *Query) WhereNull(column string) orm.Query
- func (q *Query) With(query string, args ...any) contractsorm.Query
- func (q *Query) WithContext(ctx context.Context) contractsorm.Query
- func (q *Query) WithCount(relation string, args ...any) contractsorm.Query
- func (q *Query) WithExists(relation string, args ...any) contractsorm.Query
- func (q *Query) WithTrashed() contractsorm.Query
- func (q *Query) Without(relations ...string) contractsorm.Query
- func (q *Query) WithoutEvents() contractsorm.Query
- func (q *Query) WithoutTrashed() contractsorm.Query
- type RawExpression
- type SelectClauseBuilder
- type ToSql
- func (t *ToSql) Avg(column string, dest any) string
- func (t *ToSql) Count() string
- func (t *ToSql) Create(value any) string
- func (t *ToSql) Decrement(column string, amount ...any) string
- func (t *ToSql) Delete(value ...any) string
- func (t *ToSql) Find(dest any, conds ...any) string
- func (t *ToSql) First(dest any) string
- func (t *ToSql) ForceDelete(value ...any) string
- func (t *ToSql) Get(dest any) string
- func (t *ToSql) Increment(column string, amount ...any) string
- func (t *ToSql) InsertGetId(values any) string
- func (t *ToSql) Max(column string, dest any) string
- func (t *ToSql) Min(column string, dest any) string
- func (t *ToSql) Pluck(column string, dest any) string
- func (t *ToSql) Save(value any) string
- func (t *ToSql) Sum(column string, dest any) string
- func (t *ToSql) Update(column any, value ...any) string
- func (t *ToSql) Value(column string, dest any) string
- type WhereClauseBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder handles SQL query building.
func (*Builder) BuildDelete ¶
BuildDelete builds a DELETE query from the query state.
func (*Builder) BuildInsert ¶
BuildInsert builds an INSERT query from the query state.
func (*Builder) BuildSelect ¶
BuildSelect builds a SELECT query from the query state.
type Clause ¶
type Clause interface {
Type() ClauseType
ToSQL() (string, []any)
}
Clause represents a query clause.
type ClauseType ¶
type ClauseType int
ClauseType represents the type of a query clause.
const ( WhereClause ClauseType = iota SelectClause JoinClause GroupClause HavingClause OrderClause LimitClause OffsetClause )
type JoinClauseBuilder ¶
type JoinClauseBuilder struct {
// contains filtered or unexported fields
}
JoinClauseBuilder builds JOIN clauses.
func (*JoinClauseBuilder) ToSQL ¶
func (j *JoinClauseBuilder) ToSQL() (string, []any)
func (*JoinClauseBuilder) Type ¶
func (j *JoinClauseBuilder) Type() ClauseType
type LimitClauseBuilder ¶
type LimitClauseBuilder struct {
// contains filtered or unexported fields
}
LimitClauseBuilder builds LIMIT clauses.
func (*LimitClauseBuilder) ToSQL ¶
func (l *LimitClauseBuilder) ToSQL() (string, []any)
func (*LimitClauseBuilder) Type ¶
func (l *LimitClauseBuilder) Type() ClauseType
type OffsetClauseBuilder ¶
type OffsetClauseBuilder struct {
// contains filtered or unexported fields
}
OffsetClauseBuilder builds OFFSET clauses.
func (*OffsetClauseBuilder) ToSQL ¶
func (o *OffsetClauseBuilder) ToSQL() (string, []any)
func (*OffsetClauseBuilder) Type ¶
func (o *OffsetClauseBuilder) Type() ClauseType
type OrderClauseBuilder ¶
type OrderClauseBuilder struct {
// contains filtered or unexported fields
}
OrderClauseBuilder builds ORDER BY clauses.
func (*OrderClauseBuilder) ToSQL ¶
func (o *OrderClauseBuilder) ToSQL() (string, []any)
func (*OrderClauseBuilder) Type ¶
func (o *OrderClauseBuilder) Type() ClauseType
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query implements the Query interface using native database/sql.
func NewQuery ¶
func NewQuery(ctx context.Context, db *sql.DB, drv driver.Driver, connection string, dbConfig *db.DBConfig, log log.Log) *Query
NewQuery creates a new Query instance.
func NewQueryWithReplicas ¶ added in v0.2.0
func NewQueryWithReplicas(ctx context.Context, writeConn, readConn *sql.DB, drv driver.Driver, connection string, dbConfig *db.DBConfig, lg log.Log) *Query
NewQueryWithReplicas creates a Query with separate read and write sql.DB connections.
func (*Query) AfterCommit ¶
AfterCommit registers a callback to run after the transaction is committed.
func (*Query) AfterRollback ¶
AfterRollback registers a callback to run after the transaction is rolled back.
func (*Query) Association ¶
func (q *Query) Association(assocName string) contractsorm.Association
Association returns an association for the given relationship name.
func (*Query) BeforeCommit ¶
BeforeCommit registers a callback to run before the transaction is committed.
func (*Query) BeforeRollback ¶
BeforeRollback registers a callback to run before the transaction is rolled back.
func (*Query) Chunk ¶
Chunk processes the query results in chunks and calls the callback for each chunk.
func (*Query) Clone ¶ added in v0.2.0
func (q *Query) Clone() contractsorm.Query
Clone returns a new Query with shared connection state but empty query-builder state.
func (*Query) Connection ¶
func (q *Query) Connection(name string) contractsorm.Query
Connection returns a new Query instance scoped to the named connection.
func (*Query) Cursor ¶
func (q *Query) Cursor() (chan contractsorm.Cursor, error)
Cursor returns a cursor for streaming query results.
func (*Query) Delete ¶
func (q *Query) Delete(value ...any) (*contractsorm.Result, error)
Delete deletes records from the database.
func (*Query) DisableQueryLog ¶
func (q *Query) DisableQueryLog()
DisableQueryLog disables query logging.
func (*Query) EnableQueryLog ¶
func (q *Query) EnableQueryLog()
EnableQueryLog enables query logging.
func (*Query) FindOrFail ¶
FindOrFail retrieves records matching the given conditions or returns an error if not found.
func (*Query) FirstOrCreate ¶
FirstOrCreate retrieves the first record or creates it if not found.
func (*Query) FirstOrFail ¶
FirstOrFail retrieves the first record or returns an error if not found.
func (*Query) FirstOrNew ¶
FirstOrNew retrieves the first record or prepares a new instance if not found.
func (*Query) ForceDelete ¶
func (q *Query) ForceDelete(value ...any) (*contractsorm.Result, error)
ForceDelete permanently deletes a record (bypasses soft delete).
func (*Query) GetQueryLog ¶
func (q *Query) GetQueryLog() []contractsorm.QueryLog
GetQueryLog returns the query log.
func (*Query) InRandomOrder ¶
func (q *Query) InRandomOrder() contractsorm.Query
InRandomOrder orders the query results randomly.
func (*Query) InTransaction ¶
InTransaction returns true if the query is in a transaction.
func (*Query) InsertGetId ¶
InsertGetId inserts a record and returns the ID.
func (*Query) LoadMissing ¶
LoadMissing loads a relation only if it's not already loaded.
func (*Query) LockForUpdate ¶
func (q *Query) LockForUpdate() contractsorm.Query
LockForUpdate locks the selected rows for update.
func (*Query) Model ¶
func (q *Query) Model(value any) contractsorm.Query
Model sets the model for the query.
func (*Query) Observe ¶
func (q *Query) Observe(model any, observer contractsorm.Observer)
Observe registers an observer for the given model.
func (*Query) Omit ¶
func (q *Query) Omit(columns ...string) contractsorm.Query
Omit specifies columns to omit from INSERT/UPDATE operations.
func (*Query) OnlyTrashed ¶
func (q *Query) OnlyTrashed() contractsorm.Query
OnlyTrashed returns only soft-deleted records.
func (*Query) OrWhere ¶
OrWhere adds an or where clause to the query. Supports Laravel-style syntax: OrWhere("column", "value") automatically uses = operator.
func (*Query) OrWhereBetween ¶
OrWhereBetween adds an or where between clause to the query.
func (*Query) OrWhereColumn ¶
OrWhereColumn adds an or where column clause to the query.
func (*Query) OrWhereJsonContains ¶
OrWhereJsonContains adds an or where json contains clause to the query.
func (*Query) OrWhereJsonContainsKey ¶
OrWhereJsonContainsKey adds an or where json contains key clause to the query.
func (*Query) OrWhereJsonDoesntContain ¶
OrWhereJsonDoesntContain adds an or where json doesnt contain clause to the query.
func (*Query) OrWhereJsonDoesntContainKey ¶
OrWhereJsonDoesntContainKey adds an or where json doesnt contain key clause to the query.
func (*Query) OrWhereNot ¶
OrWhereNot adds an or where not clause to the query.
func (*Query) OrWhereNotBetween ¶
OrWhereNotBetween adds an or where not between clause to the query.
func (*Query) OrWhereNotIn ¶
OrWhereNotIn adds an or where not in clause to the query.
func (*Query) OrWhereNull ¶
OrWhereNull adds an or where null clause to the query.
func (*Query) OrderByDesc ¶
OrderByDesc adds an order by clause with desc direction.
func (*Query) Raw ¶
func (q *Query) Raw(sql string, values ...any) contractsorm.Query
Raw sets a raw SQL query to be executed.
func (*Query) ReadDB ¶ added in v0.2.0
ReadDB returns the read-replica connection (falls back to primary if none configured).
func (*Query) Restore ¶
func (q *Query) Restore(model ...any) (*contractsorm.Result, error)
Restore restores a soft-deleted record.
func (*Query) RollbackTo ¶
RollbackTo rolls back to a specific savepoint.
func (*Query) Save ¶
Save saves the model to the database (INSERT if no primary key, UPDATE otherwise).
func (*Query) SaveQuietly ¶
SaveQuietly saves the model without firing events.
func (*Query) Scopes ¶
func (q *Query) Scopes(funcs ...func(contractsorm.Query) contractsorm.Query) contractsorm.Query
Scopes registers scope functions to be applied to the query.
func (*Query) SharedLock ¶
func (q *Query) SharedLock() contractsorm.Query
SharedLock locks the selected rows with a shared lock.
func (*Query) Table ¶
func (q *Query) Table(name string, args ...any) contractsorm.Query
Table sets the table for the query.
func (*Query) ToRawSql ¶
func (q *Query) ToRawSql() contractsorm.ToSql
ToRawSql returns the raw SQL with placeholders replaced by values.
func (*Query) ToSql ¶
func (q *Query) ToSql() contractsorm.ToSql
ToSql returns a ToSql instance for generating SQL without execution.
func (*Query) Transaction ¶
Transaction runs a callback wrapped in a database transaction.
func (*Query) UpdateOrCreate ¶
UpdateOrCreate updates a record if it exists, or creates it if it doesn't.
func (*Query) Where ¶
Where adds a where clause to the query. Supports Laravel-style syntax: Where("column", "value") automatically uses = operator.
func (*Query) WhereBetween ¶
WhereBetween adds a where between clause to the query.
func (*Query) WhereColumn ¶
WhereColumn adds a where column clause to the query.
func (*Query) WhereExists ¶
WhereExists adds a where exists clause to the query.
func (*Query) WhereJsonContains ¶
WhereJsonContains adds a where json contains clause to the query.
func (*Query) WhereJsonContainsKey ¶
WhereJsonContainsKey adds a where json contains key clause to the query.
func (*Query) WhereJsonDoesntContain ¶
WhereJsonDoesntContain adds a where json doesnt contain clause to the query.
func (*Query) WhereJsonDoesntContainKey ¶
WhereJsonDoesntContainKey adds a where json doesnt contain key clause to the query.
func (*Query) WhereJsonLength ¶
WhereJsonLength adds a where json length clause to the query.
func (*Query) WhereNotBetween ¶
WhereNotBetween adds a where not between clause to the query.
func (*Query) WhereNotIn ¶
WhereNotIn adds a where not in clause to the query.
func (*Query) WhereNotNull ¶
WhereNotNull adds a where not null clause to the query.
func (*Query) With ¶
func (q *Query) With(query string, args ...any) contractsorm.Query
With specifies relations to eager load.
func (*Query) WithContext ¶
func (q *Query) WithContext(ctx context.Context) contractsorm.Query
WithContext returns a new Query instance with the specified context.
func (*Query) WithCount ¶
func (q *Query) WithCount(relation string, args ...any) contractsorm.Query
WithCount adds a count subquery to the SELECT clause for the given relationship. The count is added as a column named "{relation}_count". A constraint callback can be provided to filter the count query.
func (*Query) WithExists ¶
func (q *Query) WithExists(relation string, args ...any) contractsorm.Query
WithExists adds an exists subquery to the SELECT clause for the given relationship. The exists result is added as a boolean column named "{relation}_exists". A constraint callback can be provided to filter the exists query.
func (*Query) WithTrashed ¶
func (q *Query) WithTrashed() contractsorm.Query
WithTrashed includes soft-deleted records in the query results.
func (*Query) Without ¶
func (q *Query) Without(relations ...string) contractsorm.Query
Without removes specified relations from eager loading.
func (*Query) WithoutEvents ¶
func (q *Query) WithoutEvents() contractsorm.Query
WithoutEvents disables event firing for the query.
func (*Query) WithoutTrashed ¶
func (q *Query) WithoutTrashed() contractsorm.Query
WithoutTrashed excludes soft-deleted records from the query results (default behavior).
type RawExpression ¶ added in v0.2.0
RawExpression represents a raw SQL expression that can be used as a value in maps
func RawExpr ¶ added in v0.6.0
func RawExpr(sql string, args ...any) RawExpression
RawExpr creates a new raw SQL expression for use in Create/Update values. The SQL will be injected directly without parameterization. Example: db.Table("users").Create(map[string]any{"created_at": RawExpr("NOW()")})
type SelectClauseBuilder ¶
type SelectClauseBuilder struct {
// contains filtered or unexported fields
}
SelectClauseBuilder builds SELECT clauses.
func (*SelectClauseBuilder) ToSQL ¶
func (s *SelectClauseBuilder) ToSQL() (string, []any)
func (*SelectClauseBuilder) Type ¶
func (s *SelectClauseBuilder) Type() ClauseType
type ToSql ¶
type ToSql struct {
// contains filtered or unexported fields
}
ToSql implements the ToSql interface for generating SQL without execution.
func (*ToSql) ForceDelete ¶
ForceDelete generates the SQL for a DELETE query (same as Delete for now).
func (*ToSql) InsertGetId ¶
InsertGetId generates the SQL for an INSERT query with ID retrieval.
type WhereClauseBuilder ¶
type WhereClauseBuilder struct {
// contains filtered or unexported fields
}
WhereClauseBuilder builds WHERE clauses.
func (*WhereClauseBuilder) ToSQL ¶
func (w *WhereClauseBuilder) ToSQL() (string, []any)
func (*WhereClauseBuilder) Type ¶
func (w *WhereClauseBuilder) Type() ClauseType
Source Files
¶
- builder.go
- builder_delete.go
- builder_extract.go
- builder_insert.go
- builder_quote.go
- builder_select.go
- builder_update.go
- builder_where.go
- clause.go
- query.go
- query_accessors.go
- query_advanced.go
- query_aggregate.go
- query_builder.go
- query_chunk.go
- query_create.go
- query_cursor.go
- query_delete.go
- query_find.go
- query_first.go
- query_get.go
- query_paginate.go
- query_relations.go
- query_save.go
- query_scan.go
- query_scopes.go
- query_soft_delete.go
- query_transaction.go
- query_update.go
- query_where.go
- to_sql.go
- utils.go