query

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

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 NewBuilder

func NewBuilder(q *Query) *Builder

NewBuilder creates a new Builder instance.

func (*Builder) BuildDelete

func (b *Builder) BuildDelete() (string, []any)

BuildDelete builds a DELETE query from the query state.

func (*Builder) BuildInsert

func (b *Builder) BuildInsert(value any) (string, []any)

BuildInsert builds an INSERT query from the query state.

func (*Builder) BuildSelect

func (b *Builder) BuildSelect() (string, []any)

BuildSelect builds a SELECT query from the query state.

func (*Builder) BuildUpdate

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

BuildUpdate builds an UPDATE 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

func (q *Query) AfterCommit(callback func() error)

AfterCommit registers a callback to run after the transaction is committed.

func (*Query) AfterRollback

func (q *Query) AfterRollback(callback func() error)

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) Avg

func (q *Query) Avg(column string, dest any) error

Avg returns the average of the specified column.

func (*Query) BeforeCommit

func (q *Query) BeforeCommit(callback func() error)

BeforeCommit registers a callback to run before the transaction is committed.

func (*Query) BeforeRollback

func (q *Query) BeforeRollback(callback func() error)

BeforeRollback registers a callback to run before the transaction is rolled back.

func (*Query) Begin

func (q *Query) Begin(opts ...*sql.TxOptions) (contractsorm.Query, error)

Begin starts a new database transaction.

func (*Query) Chunk

func (q *Query) Chunk(size int, callback any) error

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) Commit

func (q *Query) Commit() error

Commit commits the current transaction.

func (*Query) Connection

func (q *Query) Connection(name string) contractsorm.Query

Connection returns a new Query instance scoped to the named connection.

func (*Query) Count

func (q *Query) Count(count *int64) error

Count returns the number of records matching the query.

func (*Query) Create

func (q *Query) Create(value any) error

Create inserts a new record into the database.

func (*Query) CrossJoin

func (q *Query) CrossJoin(query string, args ...any) orm.Query

CrossJoin adds a cross join clause to the query.

func (*Query) Cursor

func (q *Query) Cursor() (chan contractsorm.Cursor, error)

Cursor returns a cursor for streaming query results.

func (*Query) DB

func (q *Query) DB() (*sql.DB, error)

DB returns the write (primary) database connection.

func (*Query) Decrement

func (q *Query) Decrement(column string, amount ...any) (*contractsorm.Result, error)

Decrement decrements a column's value by a specified amount.

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) Distinct

func (q *Query) Distinct(args ...any) orm.Query

Distinct adds distinct to the query.

func (*Query) Driver

func (q *Query) Driver() database.Driver

Driver returns the database driver.

func (*Query) EnableQueryLog

func (q *Query) EnableQueryLog()

EnableQueryLog enables query logging.

func (*Query) Exec

func (q *Query) Exec(sql string, values ...any) (*contractsorm.Result, error)

Exec executes a raw SQL query.

func (*Query) Exists

func (q *Query) Exists(exists *bool) error

Exists checks if any records match the query.

func (*Query) Find

func (q *Query) Find(dest any, conds ...any) error

Find retrieves records matching the given conditions.

func (*Query) FindOrFail

func (q *Query) FindOrFail(dest any, conds ...any) error

FindOrFail retrieves records matching the given conditions or returns an error if not found.

func (*Query) First

func (q *Query) First(dest any) error

First retrieves the first record matching the query.

func (*Query) FirstOr

func (q *Query) FirstOr(dest any, callback func() error) error

FirstOr retrieves the first record or executes a callback if not found.

func (*Query) FirstOrCreate

func (q *Query) FirstOrCreate(dest any, conds ...any) error

FirstOrCreate retrieves the first record or creates it if not found.

func (*Query) FirstOrFail

func (q *Query) FirstOrFail(dest any) error

FirstOrFail retrieves the first record or returns an error if not found.

func (*Query) FirstOrNew

func (q *Query) FirstOrNew(dest any, attributes any, values ...any) error

FirstOrNew retrieves the first record or prepares a new instance if not found.

func (*Query) FlushQueryLog

func (q *Query) FlushQueryLog()

FlushQueryLog clears the query log.

func (*Query) ForceDelete

func (q *Query) ForceDelete(value ...any) (*contractsorm.Result, error)

ForceDelete permanently deletes a record (bypasses soft delete).

func (*Query) Get

func (q *Query) Get(dest any) error

Get retrieves all records matching the query.

func (*Query) GetQueryLog

func (q *Query) GetQueryLog() []contractsorm.QueryLog

GetQueryLog returns the query log.

func (*Query) Group

func (q *Query) Group(name string) orm.Query

Group adds a group by clause to the query.

func (*Query) Having

func (q *Query) Having(query any, args ...any) orm.Query

Having adds a having clause to the query.

func (*Query) InRandomOrder

func (q *Query) InRandomOrder() contractsorm.Query

InRandomOrder orders the query results randomly.

func (*Query) InTransaction

func (q *Query) InTransaction() bool

InTransaction returns true if the query is in a transaction.

func (*Query) Increment

func (q *Query) Increment(column string, amount ...any) (*contractsorm.Result, error)

Increment increments a column's value by a specified amount.

func (*Query) InsertGetId

func (q *Query) InsertGetId(values any) (uint, error)

InsertGetId inserts a record and returns the ID.

func (*Query) Join

func (q *Query) Join(query string, args ...any) orm.Query

Join adds a join clause to the query.

func (*Query) LeftJoin

func (q *Query) LeftJoin(query string, args ...any) orm.Query

LeftJoin adds a left join clause to the query.

func (*Query) Limit

func (q *Query) Limit(limit int) orm.Query

Limit adds a limit clause to the query.

func (*Query) Load

func (q *Query) Load(dest any, relation string, args ...any) error

Load loads a relation for the given model.

func (*Query) LoadMissing

func (q *Query) LoadMissing(dest any, relation string, args ...any) error

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) Max

func (q *Query) Max(column string, dest any) error

Max returns the maximum value of the specified column.

func (*Query) Min

func (q *Query) Min(column string, dest any) error

Min returns the minimum value of the specified column.

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) Offset

func (q *Query) Offset(offset int) orm.Query

Offset adds an offset clause to the query.

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

func (q *Query) OrWhere(query any, args ...any) orm.Query

OrWhere adds an or where clause to the query. Supports Laravel-style syntax: OrWhere("column", "value") automatically uses = operator.

func (*Query) OrWhereBetween

func (q *Query) OrWhereBetween(column string, x, y any) orm.Query

OrWhereBetween adds an or where between clause to the query.

func (*Query) OrWhereColumn

func (q *Query) OrWhereColumn(first, operator, second string) orm.Query

OrWhereColumn adds an or where column clause to the query.

func (*Query) OrWhereIn

func (q *Query) OrWhereIn(column string, values []any) orm.Query

OrWhereIn adds an or where in clause to the query.

func (*Query) OrWhereJsonContains

func (q *Query) OrWhereJsonContains(column string, value any) orm.Query

OrWhereJsonContains adds an or where json contains clause to the query.

func (*Query) OrWhereJsonContainsKey

func (q *Query) OrWhereJsonContainsKey(column string) orm.Query

OrWhereJsonContainsKey adds an or where json contains key clause to the query.

func (*Query) OrWhereJsonDoesntContain

func (q *Query) OrWhereJsonDoesntContain(column string, value any) orm.Query

OrWhereJsonDoesntContain adds an or where json doesnt contain clause to the query.

func (*Query) OrWhereJsonDoesntContainKey

func (q *Query) OrWhereJsonDoesntContainKey(column string) orm.Query

OrWhereJsonDoesntContainKey adds an or where json doesnt contain key clause to the query.

func (*Query) OrWhereNot

func (q *Query) OrWhereNot(query any, args ...any) orm.Query

OrWhereNot adds an or where not clause to the query.

func (*Query) OrWhereNotBetween

func (q *Query) OrWhereNotBetween(column string, x, y any) orm.Query

OrWhereNotBetween adds an or where not between clause to the query.

func (*Query) OrWhereNotIn

func (q *Query) OrWhereNotIn(column string, values []any) orm.Query

OrWhereNotIn adds an or where not in clause to the query.

func (*Query) OrWhereNull

func (q *Query) OrWhereNull(column string) orm.Query

OrWhereNull adds an or where null clause to the query.

func (*Query) Order

func (q *Query) Order(value any) orm.Query

Order adds an order by clause to the query.

func (*Query) OrderBy

func (q *Query) OrderBy(column string, direction ...string) orm.Query

OrderBy adds an order by clause with direction.

func (*Query) OrderByDesc

func (q *Query) OrderByDesc(column string) orm.Query

OrderByDesc adds an order by clause with desc direction.

func (*Query) Paginate

func (q *Query) Paginate(page, limit int, dest any, total *int64) error

Paginate paginates the query results.

func (*Query) Pluck

func (q *Query) Pluck(column string, dest any) error

Pluck retrieves a single column's values from the query results.

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

func (q *Query) ReadDB() (*sql.DB, error)

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) RightJoin

func (q *Query) RightJoin(query string, args ...any) orm.Query

RightJoin adds a right join clause to the query.

func (*Query) Rollback

func (q *Query) Rollback() error

Rollback rolls back the current transaction.

func (*Query) RollbackTo

func (q *Query) RollbackTo(level string) error

RollbackTo rolls back to a specific savepoint.

func (*Query) Save

func (q *Query) Save(value any) error

Save saves the model to the database (INSERT if no primary key, UPDATE otherwise).

func (*Query) SavePoint

func (q *Query) SavePoint(name string) error

SavePoint creates a new savepoint within the transaction.

func (*Query) SaveQuietly

func (q *Query) SaveQuietly(value any) error

SaveQuietly saves the model without firing events.

func (*Query) Scan

func (q *Query) Scan(dest any) error

Scan executes the query and scans the results into the destination.

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) Select

func (q *Query) Select(query any, args ...any) orm.Query

Select adds columns to the select clause.

func (*Query) SharedLock

func (q *Query) SharedLock() contractsorm.Query

SharedLock locks the selected rows with a shared lock.

func (*Query) Sum

func (q *Query) Sum(column string, dest any) error

Sum returns the sum of the specified column.

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

func (q *Query) Transaction(txFunc func(tx contractsorm.Query) error, opts ...*sql.TxOptions) error

Transaction runs a callback wrapped in a database transaction.

func (*Query) Update

func (q *Query) Update(column any, value ...any) (*contractsorm.Result, error)

Update updates records in the database.

func (*Query) UpdateOrCreate

func (q *Query) UpdateOrCreate(dest any, attributes any, values any) error

UpdateOrCreate updates a record if it exists, or creates it if it doesn't.

func (*Query) UpdateOrInsert

func (q *Query) UpdateOrInsert(attributes any, values any) error

func (*Query) Value

func (q *Query) Value(column string, dest any) error

Value retrieves a single column's value from the first result.

func (*Query) Where

func (q *Query) Where(query any, args ...any) orm.Query

Where adds a where clause to the query. Supports Laravel-style syntax: Where("column", "value") automatically uses = operator.

func (*Query) WhereAll

func (q *Query) WhereAll(columns []string, operator string, value any) orm.Query

WhereAll adds a where all clause to the query.

func (*Query) WhereAny

func (q *Query) WhereAny(columns []string, operator string, value any) orm.Query

WhereAny adds a where any clause to the query.

func (*Query) WhereBetween

func (q *Query) WhereBetween(column string, x, y any) orm.Query

WhereBetween adds a where between clause to the query.

func (*Query) WhereColumn

func (q *Query) WhereColumn(first, operator, second string) orm.Query

WhereColumn adds a where column clause to the query.

func (*Query) WhereExists

func (q *Query) WhereExists(callback func(orm.Query) orm.Query) orm.Query

WhereExists adds a where exists clause to the query.

func (*Query) WhereIn

func (q *Query) WhereIn(column string, values []any) orm.Query

WhereIn adds a where in clause to the query.

func (*Query) WhereJsonContains

func (q *Query) WhereJsonContains(column string, value any) orm.Query

WhereJsonContains adds a where json contains clause to the query.

func (*Query) WhereJsonContainsKey

func (q *Query) WhereJsonContainsKey(column string) orm.Query

WhereJsonContainsKey adds a where json contains key clause to the query.

func (*Query) WhereJsonDoesntContain

func (q *Query) WhereJsonDoesntContain(column string, value any) orm.Query

WhereJsonDoesntContain adds a where json doesnt contain clause to the query.

func (*Query) WhereJsonDoesntContainKey

func (q *Query) WhereJsonDoesntContainKey(column string) orm.Query

WhereJsonDoesntContainKey adds a where json doesnt contain key clause to the query.

func (*Query) WhereJsonLength

func (q *Query) WhereJsonLength(column string, operator string, value any) orm.Query

WhereJsonLength adds a where json length clause to the query.

func (*Query) WhereNone

func (q *Query) WhereNone(columns []string, operator string, value any) orm.Query

WhereNone adds a where none clause to the query.

func (*Query) WhereNot

func (q *Query) WhereNot(query any, args ...any) orm.Query

WhereNot adds a where not clause to the query.

func (*Query) WhereNotBetween

func (q *Query) WhereNotBetween(column string, x, y any) orm.Query

WhereNotBetween adds a where not between clause to the query.

func (*Query) WhereNotIn

func (q *Query) WhereNotIn(column string, values []any) orm.Query

WhereNotIn adds a where not in clause to the query.

func (*Query) WhereNotNull

func (q *Query) WhereNotNull(column string) orm.Query

WhereNotNull adds a where not null clause to the query.

func (*Query) WhereNull

func (q *Query) WhereNull(column string) orm.Query

WhereNull adds a where 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(query string, args ...any) contractsorm.Query

WithCount adds a count query to the relations (not yet implemented).

func (*Query) WithExists

func (q *Query) WithExists(query string, args ...any) contractsorm.Query

WithExists adds an exists query to the relations (not yet implemented).

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

type RawExpression struct {
	SQL  string
	Args []any
}

RawExpression represents a raw SQL expression that can be used as a value in maps

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 NewToSql

func NewToSql(q *Query) *ToSql

NewToSql creates a new ToSql instance.

func (*ToSql) Avg

func (t *ToSql) Avg(column string, dest any) string

Avg generates the SQL for an AVG aggregation query.

func (*ToSql) Count

func (t *ToSql) Count() string

Count generates the SQL for a COUNT query.

func (*ToSql) Create

func (t *ToSql) Create(value any) string

Create generates the SQL for an INSERT query.

func (*ToSql) Decrement

func (t *ToSql) Decrement(column string, amount ...any) string

Decrement generates the SQL for a DECREMENT query.

func (*ToSql) Delete

func (t *ToSql) Delete(value ...any) string

Delete generates the SQL for a DELETE query.

func (*ToSql) Find

func (t *ToSql) Find(dest any, conds ...any) string

Find generates the SQL for a SELECT query.

func (*ToSql) First

func (t *ToSql) First(dest any) string

First generates the SQL for a SELECT query with LIMIT 1.

func (*ToSql) ForceDelete

func (t *ToSql) ForceDelete(value ...any) string

ForceDelete generates the SQL for a DELETE query (same as Delete for now).

func (*ToSql) Get

func (t *ToSql) Get(dest any) string

Get generates the SQL for a SELECT query.

func (*ToSql) Increment

func (t *ToSql) Increment(column string, amount ...any) string

Increment generates the SQL for an INCREMENT query.

func (*ToSql) InsertGetId

func (t *ToSql) InsertGetId(values any) string

InsertGetId generates the SQL for an INSERT query with ID retrieval.

func (*ToSql) Max

func (t *ToSql) Max(column string, dest any) string

Max generates the SQL for a MAX aggregation query.

func (*ToSql) Min

func (t *ToSql) Min(column string, dest any) string

Min generates the SQL for a MIN aggregation query.

func (*ToSql) Pluck

func (t *ToSql) Pluck(column string, dest any) string

Pluck generates the SQL for a SELECT query with a single column.

func (*ToSql) Save

func (t *ToSql) Save(value any) string

Save generates the SQL for an INSERT or UPDATE query based on primary key.

func (*ToSql) Sum

func (t *ToSql) Sum(column string, dest any) string

Sum generates the SQL for a SUM aggregation query.

func (*ToSql) Update

func (t *ToSql) Update(column any, value ...any) string

Update generates the SQL for an UPDATE query.

func (*ToSql) Value

func (t *ToSql) Value(column string, dest any) string

Value generates the SQL for a SELECT query with a single column and LIMIT 1.

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

Jump to

Keyboard shortcuts

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