query

package
v0.0.0-20260608 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Listen

func Listen(listener QueryListener)

Listen registers a new query listener.

Types

type Builder

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

Builder provides a fluent API for building SQL queries.

func NewBuilder

func NewBuilder(conn QueryExecer, d dialect.Dialect) *Builder

NewBuilder creates a new query builder.

func (*Builder) Avg

func (b *Builder) Avg(column string) (int, error)

func (*Builder) Clone

func (b *Builder) Clone() *Builder

Clone creates a deep copy of the builder.

func (*Builder) Count

func (b *Builder) Count(column string) (int, error)

func (*Builder) CrossJoin

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

func (*Builder) Delete

func (b *Builder) Delete() (sql.Result, error)

Delete executes a delete statement.

func (*Builder) Distinct

func (b *Builder) Distinct() *Builder

Distinct adds a DISTINCT clause to the query.

func (*Builder) Get

func (b *Builder) Get() (*sql.Rows, error)

Get executes the query and returns the raw rows. In Goquent (ORM), we will wrap this to hydrate models.

func (*Builder) GroupBy

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

GroupBy adds GROUP BY columns.

func (*Builder) Having

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

Having adds a HAVING clause (after GROUP BY).

func (*Builder) Insert

func (b *Builder) Insert(values map[string]any) (sql.Result, error)

Insert executes an insert statement.

func (*Builder) Join

func (b *Builder) Join(table, first, operator, second string) *Builder

func (*Builder) LeftJoin

func (b *Builder) LeftJoin(table, first, operator, second string) *Builder

func (*Builder) Limit

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

Limit sets the limit.

func (*Builder) LockForUpdate

func (b *Builder) LockForUpdate() *Builder

LockForUpdate adds FOR UPDATE pessimistic lock (useful in transactions).

func (*Builder) Max

func (b *Builder) Max(column string) (int, error)

func (*Builder) Min

func (b *Builder) Min(column string) (int, error)

func (*Builder) Offset

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

Offset sets the offset.

func (*Builder) OrHaving

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

OrHaving adds an OR HAVING clause.

func (*Builder) OrWhere

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

OrWhere adds an OR where clause.

func (*Builder) OrWhereRaw

func (b *Builder) OrWhereRaw(sql string, args ...any) *Builder

OrWhereRaw adds a raw OR WHERE condition.

func (*Builder) OrderBy

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

OrderBy adds an order by clause.

func (*Builder) RightJoin

func (b *Builder) RightJoin(table, first, operator, second string) *Builder

func (*Builder) Select

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

Select specifies the columns to select.

func (*Builder) SelectRaw

func (b *Builder) SelectRaw(sql string, args ...any) *Builder

SelectRaw allows raw SELECT expressions (e.g. "COUNT(*) as total").

func (*Builder) SharedLock

func (b *Builder) SharedLock() *Builder

SharedLock adds FOR SHARE / FOR READ ONLY lock.

func (*Builder) Sum

func (b *Builder) Sum(column string) (int, error)

func (*Builder) Table

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

Table sets the target table.

func (*Builder) ToSQL

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

ToSQL compiles the query to SQL.

func (*Builder) Update

func (b *Builder) Update(values map[string]any) (sql.Result, error)

Update executes an update statement.

func (*Builder) Upsert

func (b *Builder) Upsert(values map[string]any, updateColumns []string, conflictColumns []string) (sql.Result, error)

Upsert inserts or updates on conflict using dialect-specific syntax.

func (*Builder) When

func (b *Builder) When(condition bool, callback func(*Builder)) *Builder

When executes the given callback if the condition is true.

func (*Builder) Where

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

Where adds a basic where clause.

func (*Builder) WhereBetween

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

func (*Builder) WhereIn

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

func (*Builder) WhereNotNull

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

func (*Builder) WhereNull

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

func (*Builder) WhereRaw

func (b *Builder) WhereRaw(sql string, args ...any) *Builder

WhereRaw adds a raw WHERE condition.

func (*Builder) WithConn

func (b *Builder) WithConn(conn QueryExecer) *Builder

WithConn returns a new builder with the given connection (useful for transactions).

type QueryEvent

type QueryEvent struct {
	SQL      string
	Bindings []any
	Duration time.Duration
}

QueryEvent represents an executed database query.

type QueryExecer

type QueryExecer interface {
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

QueryExecer abstracts *sql.DB and *sql.Tx

type QueryListener

type QueryListener func(QueryEvent)

QueryListener is a callback function that listens for query events.

Jump to

Keyboard shortcuts

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