Help us shape the future of Go! Take the 2026 Go Developer Survey

db

package
v1.20.0-alpha.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NameMapper = func(s string) string {
	return str.Of(s).Snake().String()
}

NameMapper maps a Go field name to its database column for sqlx, which scans rows into structs. Explicit db tags override this mapper, including fields of embedded structs.

Functions

func DisableQueryLog

func DisableQueryLog(ctx context.Context) context.Context

func EnableQueryLog

func EnableQueryLog(ctx context.Context) context.Context

func Explain

func Explain(statement string, args ...any) string

Explain writes the arguments into a statement's placeholders. It is the same rendering the builders use for the raw form of a query, exposed so a caller holding only compiled SQL can report it that way too.

func NewLogger

func NewLogger(config config.Config, log log.Log) logger.Logger

func Raw

func Raw(expr string, args ...any) any

Raw creates a raw SQL fragment.

Laravel's DB::raw() takes only a string, with bindings supplied separately by whereRaw and friends. Goravel's Raw also accepts arguments, so they are rendered into the fragment here; a caller that needs parameterisation should use WhereRaw with its bindings instead.

Types

type Builder

type Builder struct {
	*sqlx.DB
}

Builder executes statements against a database pool. sqlx supplies the struct scanning and the transaction plumbing; the SQL itself is produced by database/query.

func NewBuilder

func NewBuilder(pool *sql.DB, driver string) (*Builder, error)

NewBuilder wraps a pool for statement execution. driver is the connection's driver name; a recognised driver is reduced to the dialect string sqlx keys its bind-type lookup on.

func (*Builder) Explain

func (r *Builder) Explain(sql string, args ...any) string

Explain renders a statement with its arguments interpolated, which is what ToRawSql reports.

func (*Builder) GetContext

func (b *Builder) GetContext(ctx context.Context, dest any, statement string, args ...any) error

func (*Builder) SelectContext

func (b *Builder) SelectContext(ctx context.Context, dest any, statement string, args ...any) error

type DB

type DB struct {
	contractsdb.Tx
	// contains filtered or unexported fields
}

func BuildDB

func BuildDB(ctx context.Context, config config.Config, log log.Log, connection string, telemetryResolver contractstelemetry.Resolver) (*DB, error)

func NewDB

func NewDB(ctx context.Context, config config.Config, driver contractsdriver.Driver, logger contractslogger.Logger, connection *databasedriver.Connection, telemetryResolver contractstelemetry.Resolver) (*DB, error)

func (*DB) AcquireWriteConnection

func (r *DB) AcquireWriteConnection() (db.WriteConnection, error)

func (*DB) AllowQueryDurationHandlersToRunAgain

func (r *DB) AllowQueryDurationHandlersToRunAgain()

func (*DB) BeforeExecuting

func (r *DB) BeforeExecuting(callback func(context.Context, string, []any) error)

func (*DB) BeginTransaction

func (r *DB) BeginTransaction() (contractsdb.Tx, error)

func (*DB) Connection

func (r *DB) Connection(name string) contractsdb.DB

func (*DB) DisableQueryLog

func (r *DB) DisableQueryLog()

func (*DB) Disconnect

func (r *DB) Disconnect(connection ...string) error

func (*DB) DriverName

func (r *DB) DriverName() string

func (*DB) EnableQueryLog

func (r *DB) EnableQueryLog()

func (*DB) Escape

func (r *DB) Escape(value any, binary ...bool) (string, error)

func (*DB) FlushQueryLog

func (r *DB) FlushQueryLog()

func (*DB) ForgetRecordModificationState

func (r *DB) ForgetRecordModificationState()

func (*DB) GetQueryLog

func (r *DB) GetQueryLog() []db.QueryExecuted

func (*DB) HasModifiedRecords

func (r *DB) HasModifiedRecords() bool

func (*DB) Listen

func (r *DB) Listen(callback func(db.QueryExecuted))

func (*DB) Name

func (r *DB) Name() string

func (*DB) Pretend

func (r *DB) Pretend(callback func(db.DB) error) ([]db.QueryExecuted, error)

Pretend receives an isolated session sharing the connection configuration and hooks. Queries issued through the callback's handle are recorded without reaching the driver.

func (*DB) Purge

func (r *DB) Purge(connection ...string) error

func (*DB) Reconnect

func (r *DB) Reconnect(connection ...string) (db.DB, error)

func (*DB) RecordsHaveBeenModified

func (r *DB) RecordsHaveBeenModified(modified ...bool)

func (*DB) ResetTotalQueryDuration

func (r *DB) ResetTotalQueryDuration()

func (*DB) ServerVersion

func (r *DB) ServerVersion() (string, error)

func (*DB) Sticky

func (r *DB) Sticky(enabled bool) db.DB

func (*DB) TotalQueryDuration

func (r *DB) TotalQueryDuration() time.Duration

func (*DB) Transaction

func (r *DB) Transaction(callback func(contractsdb.Tx) error, attempts ...int) error

func (*DB) UseWriteConnectionWhenReading

func (r *DB) UseWriteConnectionWhenReading(enabled bool) db.DB

func (*DB) WhenQueryingForLongerThan

func (r *DB) WhenQueryingForLongerThan(duration time.Duration, callback func(db.QueryExecuted))

func (*DB) WithContext

func (r *DB) WithContext(ctx context.Context) contractsdb.DB

func (*DB) WithWriteConnection

func (r *DB) WithWriteConnection(callback func(db.Tx) error) (err error)

type Expr

type Expr = querybuilder.Expression

Expr is a raw SQL fragment. It is an alias for the query builder's Expression, so the two names denote the same type: callers of this package need not import the builder directly, and a value built under either name is accepted everywhere an Expression is - by the builder's IsExpression and ExpressionValue type switches and by renderLiteral's interpolation switch alike.

It used to be a distinct struct embedding querybuilder.Expression, but Go embedding is not type identity: a db.Expr then failed every type switch that names querybuilder.Expression, so a raw fragment passed to Raw fell through to the fmt.Sprint fallback and rendered as the struct's "{value}" text, producing a malformed statement.

type Logger

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

func (*Logger) Errorf

func (r *Logger) Errorf(ctx context.Context, msg string, data ...any)

func (*Logger) Infof

func (r *Logger) Infof(ctx context.Context, msg string, data ...any)

func (*Logger) Level

func (r *Logger) Level(level logger.Level) logger.Logger

func (*Logger) Log

func (r *Logger) Log() log.Log

func (*Logger) Panicf

func (r *Logger) Panicf(ctx context.Context, msg string, data ...any)

func (*Logger) Trace

func (r *Logger) Trace(ctx context.Context, begin *carbon.Carbon, sql string, rowsAffected int64, err error)

func (*Logger) Warningf

func (r *Logger) Warningf(ctx context.Context, msg string, data ...any)

type Query

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

Query is the DB facade query builder. All SQL is produced by database/query, a port of Illuminate\Database's builder and its grammars; this type adapts that builder to the framework's db.Query contract and executes through CommonBuilder, keeping the tracing and transaction behaviour unchanged.

func NewQuery

func NewQuery(ctx context.Context, readBuilder db.CommonBuilder, writeBuilder db.CommonBuilder, grammar querybuilder.Grammar, driver string, logger logger.Logger, table string, txLogs *[]TxLog, supplied ...databasedriver.QueryComponents) *Query

NewQuery creates a query bound to a table. grammar selects the dialect and driver is its name, used to convert placeholders at execution time.

func (*Query) AddBinding

func (r *Query) AddBinding(value any, component string) db.Query

func (*Query) AddSelect

func (r *Query) AddSelect(columns ...any) db.Query

func (*Query) AfterQuery

func (r *Query) AfterQuery(callback func(any) any) db.Query

func (*Query) Avg

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

Avg aggregates a column with AVG.

func (*Query) BeforeQuery

func (r *Query) BeforeQuery(callback func(db.Query) db.Query) db.Query

func (*Query) Chunk

func (r *Query) Chunk(size uint64, callback func(rows []db.Row) error) error

Chunk follows BuildsQueries::chunk, including the caller's starting offset and limit.

func (*Query) ChunkById

func (r *Query) ChunkById(size uint64, callback func(rows []db.Row) error, column ...string) error

ChunkById chunks the query by comparing ids, mirroring Laravel's BuildsQueries::chunkById. Each page is fetched with ForPageAfterId (ascending) using the last id of the previous page as the keyset, so a large result set is walked without the growing offset Chunk pays. The optional column is the key column (default "id"); an optional second value names the result-key alias the id is read from (default the column itself). A callback returning errors.DatabaseChunkStopped halts the loop cleanly, mirroring Laravel's `return false`.

func (*Query) ChunkByIdDesc

func (r *Query) ChunkByIdDesc(size uint64, callback func(rows []db.Row) error, column ...string) error

ChunkByIdDesc is ChunkById in descending id order, mirroring Laravel's BuildsQueries::chunkByIdDesc: each page is fetched with ForPageBeforeId.

func (*Query) ChunkMap

func (r *Query) ChunkMap(size uint64, callback func(db.Row) (any, error)) ([]any, error)

func (*Query) Clone

func (r *Query) Clone() db.Query

func (*Query) CloneWithout

func (r *Query) CloneWithout(components ...string) db.Query

func (*Query) CloneWithoutBindings

func (r *Query) CloneWithoutBindings(bindings ...string) db.Query

func (*Query) Count

func (r *Query) Count() (int64, error)

Count returns the number of matching rows.

func (*Query) CrossJoin

func (r *Query) CrossJoin(query string, args ...any) db.Query

CrossJoin adds a cross join from raw SQL. A query that carries its own leading join keyword overrides the default cross type.

func (*Query) CrossJoinSub

func (r *Query) CrossJoinSub(query any, as string) db.Query

func (*Query) Cursor

func (r *Query) Cursor() chan db.Row

Cursor streams the matching rows.

func (*Query) CursorPaginate

func (r *Query) CursorPaginate(perPage int, cursor string, dest any) (db.CursorPaginator, error)

CursorPaginate fetches one keyset ("seek") page and scans it into dest, which must be a pointer to a slice: a *[]map[string]any for an untyped page, or a pointer to a slice of structs for a typed one. cursor is the encoded anchor — an empty string is the first page, and a non-empty malformed string is refused with errors.QueryCursorInvalid rather than silently restarting at the first page.

It mirrors Paginate but never counts: the page carries no total. The builder entry does the SQL work — the keyset predicate, the per-column order flip for a backwards cursor and the perPage+1 probe limit; this method executes that statement once and projects the scanned rows onto the paginator. The name is deliberately distinct from Cursor, which streams rows.

func (*Query) Decrement

func (r *Query) Decrement(column string, value ...uint64) error

Decrement subtracts from a numeric column.

func (*Query) DecrementEach

func (r *Query) DecrementEach(columns any, extra ...map[string]any) (*db.Result, error)

func (*Query) Delete

func (r *Query) Delete() (*db.Result, error)

Delete deletes every matching row.

func (*Query) Distinct

func (r *Query) Distinct(columns ...string) db.Query

Distinct preserves the select list and sets the distinct columns, mirroring Laravel's distinct().

func (*Query) DoesntExist

func (r *Query) DoesntExist() (bool, error)

func (*Query) DoesntExistOr

func (r *Query) DoesntExistOr(callback func() any) (any, error)

func (*Query) DynamicWhere

func (r *Query) DynamicWhere(method string, parameters ...any) db.Query

func (*Query) Each

func (r *Query) Each(callback func(row db.Row) error) error

Each iterates the results one row at a time.

func (*Query) EachById

func (r *Query) EachById(size uint64, callback func(db.Row) error, column ...string) error

func (*Query) Exists

func (r *Query) Exists() (bool, error)

Exists executes the grammar's existence query, preserving limit and offset semantics.

func (*Query) ExistsOr

func (r *Query) ExistsOr(callback func() any) (any, error)

func (*Query) Explain

func (r *Query) Explain(dest any) error

Explain executes the query plan command; it is distinct from literal interpolation.

func (*Query) Find

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

Find retrieves rows matching the given conditions.

func (*Query) FindOr

func (r *Query) FindOr(dest any, id any, callback func() error) error

FindOr retrieves the first row matching the given id, or runs the callback when there is none, mirroring Laravel's Builder::findOr.

func (*Query) FindOrFail

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

FindOrFail retrieves rows matching the given conditions or fails.

func (*Query) First

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

First follows Laravel's limit(1)->get()->first() data semantics.

func (*Query) FirstOr

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

func (*Query) FirstOrFail

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

func (*Query) ForPage

func (r *Query) ForPage(page int, perPage ...int) db.Query

func (*Query) ForPageAfterId

func (r *Query) ForPageAfterId(perPage int, lastID any, column ...string) db.Query

func (*Query) ForPageBeforeId

func (r *Query) ForPageBeforeId(perPage int, lastID any, column ...string) db.Query

func (*Query) ForceIndex

func (r *Query) ForceIndex(index string) db.Query

func (*Query) From

func (r *Query) From(table any, alias ...string) db.Query

func (*Query) FromRaw

func (r *Query) FromRaw(sql string, bindings ...any) db.Query

func (*Query) FromSub

func (r *Query) FromSub(query any, as string) db.Query

func (*Query) Get

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

Get retrieves every matching row into dest.

func (*Query) GetBindings

func (r *Query) GetBindings() []any

func (*Query) GetColumns

func (r *Query) GetColumns() []string

func (*Query) GetCountForPagination

func (r *Query) GetCountForPagination(columns ...string) (int64, error)

func (*Query) GetLimit

func (r *Query) GetLimit() *int

func (*Query) GetOffset

func (r *Query) GetOffset() *int

func (*Query) GetRawBindings

func (r *Query) GetRawBindings() map[string][]any

func (*Query) GroupBy

func (r *Query) GroupBy(column ...string) db.Query

GroupBy appends grouping columns.

func (*Query) GroupByRaw

func (r *Query) GroupByRaw(sql string, bindings ...any) db.Query

func (*Query) GroupLimit

func (r *Query) GroupLimit(limit int, column string) db.Query

func (*Query) Having

func (r *Query) Having(query any, args ...any) db.Query

Having adds a having clause.

As with applyWhere, a string that carries a binding placeholder is a raw fragment rather than a column name, so it is routed to HavingRaw; anything else keeps the builder's Laravel-faithful Having, which takes a column, an operator and a value (or a raw expression).

func (*Query) HavingBetween

func (r *Query) HavingBetween(column string, lower, upper any) db.Query

func (*Query) HavingNested

func (r *Query) HavingNested(callback func(db.Query) db.Query) db.Query

func (*Query) HavingNotBetween

func (r *Query) HavingNotBetween(column string, lower, upper any) db.Query

func (*Query) HavingNotNull

func (r *Query) HavingNotNull(columns ...string) db.Query

func (*Query) HavingNull

func (r *Query) HavingNull(columns ...string) db.Query

func (*Query) HavingRaw

func (r *Query) HavingRaw(sql string, bindings ...any) db.Query

func (*Query) IgnoreIndex

func (r *Query) IgnoreIndex(index string) db.Query

func (*Query) Implode

func (r *Query) Implode(column string, glue string, dest any) error

Implode concatenates the values of a single column into a string separated by glue. It mirrors Laravel's Builder::implode, which plucks the column and joins the plucked values.

func (*Query) InRandomOrder

func (r *Query) InRandomOrder(seed ...any) db.Query

InRandomOrder orders randomly.

func (*Query) Increment

func (r *Query) Increment(column string, value ...uint64) error

Increment adds to a numeric column.

func (*Query) IncrementEach

func (r *Query) IncrementEach(columns any, extra ...map[string]any) (*db.Result, error)

func (*Query) Insert

func (r *Query) Insert(data any) (*db.Result, error)

Insert inserts one or many rows.

func (*Query) InsertGetID

func (r *Query) InsertGetID(data any, sequence ...string) (int64, error)

InsertGetID inserts one row and returns its generated id.

func (*Query) InsertOrIgnore

func (r *Query) InsertOrIgnore(data any) (*db.Result, error)

func (*Query) InsertOrIgnoreUsing

func (r *Query) InsertOrIgnoreUsing(columns []string, query any) (*db.Result, error)

func (*Query) InsertUsing

func (r *Query) InsertUsing(columns []string, query any) (*db.Result, error)

func (*Query) Join

func (r *Query) Join(query string, args ...any) db.Query

Join adds an inner join from raw SQL. query is a "<table> on <condition>" fragment, or a whole join clause whose leading keyword ("left join ...") then overrides the default inner type.

func (*Query) JoinLateral

func (r *Query) JoinLateral(query any, as string) db.Query

func (*Query) JoinSub

func (r *Query) JoinSub(query any, as string, first any, args ...any) db.Query

func (*Query) JoinWhere

func (r *Query) JoinWhere(table, column, operator string, value any) db.Query

func (*Query) Latest

func (r *Query) Latest(column ...string) db.Query

Latest orders by created_at descending, or by the given column.

func (*Query) Lazy

func (r *Query) Lazy(chunkSize ...uint64) db.RowIterator

func (*Query) LazyById

func (r *Query) LazyById(size uint64, column ...string) db.RowIterator

func (*Query) LazyByIdDesc

func (r *Query) LazyByIdDesc(size uint64, column ...string) db.RowIterator

func (*Query) LeftJoin

func (r *Query) LeftJoin(query string, args ...any) db.Query

LeftJoin adds a left join from raw SQL. A query that carries its own leading join keyword overrides the default left type.

func (*Query) LeftJoinLateral

func (r *Query) LeftJoinLateral(query any, as string) db.Query

func (*Query) LeftJoinSub

func (r *Query) LeftJoinSub(query any, as string, first any, args ...any) db.Query

func (*Query) LeftJoinWhere

func (r *Query) LeftJoinWhere(table, column, operator string, value any) db.Query

func (*Query) Limit

func (r *Query) Limit(limit uint64) db.Query

Limit sets the limit.

func (*Query) Lock

func (r *Query) Lock(value any) db.Query

func (*Query) LockForUpdate

func (r *Query) LockForUpdate() db.Query

LockForUpdate locks the selected rows for update.

func (*Query) Max

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

Max aggregates a column with MAX.

func (*Query) Min

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

Min aggregates a column with MIN.

func (*Query) NewQuery

func (r *Query) NewQuery() db.Query

func (*Query) Offset

func (r *Query) Offset(offset uint64) db.Query

Offset sets the offset.

func (*Query) Oldest

func (r *Query) Oldest(column ...string) db.Query

func (*Query) OrHaving

func (r *Query) OrHaving(column any, args ...any) db.Query

func (*Query) OrHavingNotNull

func (r *Query) OrHavingNotNull(columns ...string) db.Query

func (*Query) OrHavingNull

func (r *Query) OrHavingNull(columns ...string) db.Query

func (*Query) OrHavingRaw

func (r *Query) OrHavingRaw(sql string, bindings ...any) db.Query

func (*Query) OrWhere

func (r *Query) OrWhere(query any, args ...any) db.Query

OrWhere adds an or where clause.

func (*Query) OrWhereAfterToday

func (r *Query) OrWhereAfterToday(columns ...string) db.Query

func (*Query) OrWhereAll

func (r *Query) OrWhereAll(columns []string, args ...any) db.Query

func (*Query) OrWhereAny

func (r *Query) OrWhereAny(columns []string, args ...any) db.Query

func (*Query) OrWhereBeforeToday

func (r *Query) OrWhereBeforeToday(columns ...string) db.Query

func (*Query) OrWhereBetween

func (r *Query) OrWhereBetween(column string, x, y any) db.Query

OrWhereBetween adds an or between clause.

func (*Query) OrWhereBetweenColumns

func (r *Query) OrWhereBetweenColumns(column, lower, upper string) db.Query

func (*Query) OrWhereColumn

func (r *Query) OrWhereColumn(column1 string, column2 ...string) db.Query

OrWhereColumn compares two columns with or.

func (*Query) OrWhereDate

func (r *Query) OrWhereDate(column string, args ...any) db.Query

func (*Query) OrWhereDay

func (r *Query) OrWhereDay(column string, args ...any) db.Query

func (*Query) OrWhereExists

func (r *Query) OrWhereExists(query any) db.Query

func (*Query) OrWhereFullText

func (r *Query) OrWhereFullText(columns []string, value string, options ...string) db.Query

func (*Query) OrWhereFuture

func (r *Query) OrWhereFuture(columns ...string) db.Query

func (*Query) OrWhereIn

func (r *Query) OrWhereIn(column string, values any) db.Query

OrWhereIn adds an or in clause.

func (*Query) OrWhereIntegerInRaw

func (r *Query) OrWhereIntegerInRaw(column string, values []any) db.Query

func (*Query) OrWhereIntegerNotInRaw

func (r *Query) OrWhereIntegerNotInRaw(column string, values []any) db.Query

func (*Query) OrWhereJsonContains

func (r *Query) OrWhereJsonContains(column string, value any) db.Query

OrWhereJsonContains adds an or JSON contains clause.

func (*Query) OrWhereJsonContainsKey

func (r *Query) OrWhereJsonContainsKey(column string) db.Query

OrWhereJsonContainsKey adds an or JSON key existence clause.

func (*Query) OrWhereJsonDoesntContain

func (r *Query) OrWhereJsonDoesntContain(column string, value any) db.Query

OrWhereJsonDoesntContain adds a negated or JSON contains clause.

func (*Query) OrWhereJsonDoesntContainKey

func (r *Query) OrWhereJsonDoesntContainKey(column string) db.Query

OrWhereJsonDoesntContainKey adds a negated or JSON key existence clause.

func (*Query) OrWhereJsonDoesntOverlap

func (r *Query) OrWhereJsonDoesntOverlap(column string, value any) db.Query

func (*Query) OrWhereJsonLength

func (r *Query) OrWhereJsonLength(column string, args ...any) db.Query

OrWhereJsonLength compares a JSON array length with or. Mirrors Laravel's Builder::orWhereJsonLength: args is either a single length (equality) or an operator and a length.

func (*Query) OrWhereJsonOverlaps

func (r *Query) OrWhereJsonOverlaps(column string, value any) db.Query

func (*Query) OrWhereLike

func (r *Query) OrWhereLike(column string, value string, caseSensitive ...bool) db.Query

OrWhereLike adds an or like clause.

func (*Query) OrWhereMonth

func (r *Query) OrWhereMonth(column string, args ...any) db.Query

func (*Query) OrWhereNone

func (r *Query) OrWhereNone(columns []string, args ...any) db.Query

func (*Query) OrWhereNot

func (r *Query) OrWhereNot(query any, args ...any) db.Query

OrWhereNot adds a negated or where clause.

func (*Query) OrWhereNotBetween

func (r *Query) OrWhereNotBetween(column string, x, y any) db.Query

OrWhereNotBetween adds an or not between clause.

func (*Query) OrWhereNotBetweenColumns

func (r *Query) OrWhereNotBetweenColumns(column, lower, upper string) db.Query

func (*Query) OrWhereNotExists

func (r *Query) OrWhereNotExists(query any) db.Query

func (*Query) OrWhereNotIn

func (r *Query) OrWhereNotIn(column string, values any) db.Query

OrWhereNotIn adds an or not in clause.

func (*Query) OrWhereNotLike

func (r *Query) OrWhereNotLike(column string, value string, caseSensitive ...bool) db.Query

OrWhereNotLike adds an or not like clause.

func (*Query) OrWhereNotNull

func (r *Query) OrWhereNotNull(column string) db.Query

OrWhereNotNull adds an or is not null clause.

func (*Query) OrWhereNowOrFuture

func (r *Query) OrWhereNowOrFuture(columns ...string) db.Query

func (*Query) OrWhereNowOrPast

func (r *Query) OrWhereNowOrPast(columns ...string) db.Query

func (*Query) OrWhereNull

func (r *Query) OrWhereNull(column string) db.Query

OrWhereNull adds an or is null clause.

func (*Query) OrWherePast

func (r *Query) OrWherePast(columns ...string) db.Query

func (*Query) OrWhereRaw

func (r *Query) OrWhereRaw(raw string, args []any) db.Query

OrWhereRaw adds a raw or where clause.

func (*Query) OrWhereRowValues

func (r *Query) OrWhereRowValues(columns []string, operator string, values []any) db.Query

func (*Query) OrWhereTime

func (r *Query) OrWhereTime(column string, args ...any) db.Query

func (*Query) OrWhereToday

func (r *Query) OrWhereToday(columns ...string) db.Query

func (*Query) OrWhereTodayOrAfter

func (r *Query) OrWhereTodayOrAfter(columns ...string) db.Query

func (*Query) OrWhereTodayOrBefore

func (r *Query) OrWhereTodayOrBefore(columns ...string) db.Query

func (*Query) OrWhereValueBetween

func (r *Query) OrWhereValueBetween(value any, lower, upper string) db.Query

func (*Query) OrWhereValueNotBetween

func (r *Query) OrWhereValueNotBetween(value any, lower, upper string) db.Query

func (*Query) OrWhereYear

func (r *Query) OrWhereYear(column string, args ...any) db.Query

func (*Query) OrderBy

func (r *Query) OrderBy(column any, directions ...string) db.Query

OrderBy adds an order clause.

func (*Query) OrderByDesc

func (r *Query) OrderByDesc(column any) db.Query

OrderByDesc adds a descending order clause.

func (*Query) OrderByRaw

func (r *Query) OrderByRaw(raw string, bindings ...any) db.Query

OrderByRaw adds a raw order clause.

func (*Query) Paginate

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

Paginate retrieves one page of rows, writing the total into total when it is not nil. It mirrors Laravel's Builder::paginate: a non-positive page and limit fall back to the defaults of page 1 and 15 rows, a total already carried by the caller is an override that skips the count, and a zero total skips the page fetch (Laravel returns an empty collection when the count is zero).

func (*Query) Pipe

func (r *Query) Pipe(callback func(db.Query) any) any

func (*Query) Pluck

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

Pluck retrieves a single column of every row.

func (*Query) RawValue

func (r *Query) RawValue(expression string, dest any) error

RawValue retrieves a single raw expression value from the first row. It mirrors Laravel's Builder::rawValue: selectRaw($expression)->first(). Unlike Value, the expression is not wrapped as an identifier, so it can carry an arbitrary SQL fragment such as UPPER("foo").

func (*Query) Reorder

func (r *Query) Reorder(column ...string) db.Query

func (*Query) ReorderDesc

func (r *Query) ReorderDesc(column string) db.Query

func (*Query) RightJoin

func (r *Query) RightJoin(query string, args ...any) db.Query

RightJoin adds a right join from raw SQL. A query that carries its own leading join keyword overrides the default right type.

func (*Query) RightJoinSub

func (r *Query) RightJoinSub(query any, as string, first any, args ...any) db.Query

func (*Query) RightJoinWhere

func (r *Query) RightJoinWhere(table, column, operator string, value any) db.Query

func (*Query) Select

func (r *Query) Select(columns ...string) db.Query

Select replaces the selected columns and their bindings, mirroring Laravel's select().

func (*Query) SelectRaw

func (r *Query) SelectRaw(sql string, bindings ...any) db.Query

func (*Query) SelectSub

func (r *Query) SelectSub(query any, as string) db.Query

func (*Query) SetBindings

func (r *Query) SetBindings(values []any, component string) db.Query

func (*Query) SharedLock

func (r *Query) SharedLock() db.Query

SharedLock takes a shared lock on the selected rows.

func (*Query) SimplePaginate

func (r *Query) SimplePaginate(page, limit int, dest any) (bool, error)

SimplePaginate probes one extra row. Data stays in dest; pagination has no HTTP metadata.

func (*Query) Skip

func (r *Query) Skip(offset int) db.Query

func (*Query) Sole

func (r *Query) Sole(dest any) error

Sole reads at most two rows so both absence and ambiguity are observable.

func (*Query) SoleValue

func (r *Query) SoleValue(column string, dest any) error

func (*Query) Sum

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

Sum aggregates a column with SUM.

func (*Query) Take

func (r *Query) Take(limit int) db.Query

func (*Query) Tap

func (r *Query) Tap(callback func(db.Query)) db.Query

func (*Query) ToRawSql

func (r *Query) ToRawSql() db.ToSql

ToRawSql returns the query as a SQL string with the bindings inlined.

func (*Query) ToSql

func (r *Query) ToSql() db.ToSql

ToSql returns the query as a parameterised SQL string.

func (*Query) Truncate

func (r *Query) Truncate() error

func (*Query) Union

func (r *Query) Union(query any) db.Query

func (*Query) UnionAll

func (r *Query) UnionAll(query any) db.Query

func (*Query) Unless

func (r *Query) Unless(condition bool, callback func(db.Query) db.Query, otherwise ...func(db.Query) db.Query) db.Query

func (*Query) Update

func (r *Query) Update(column any, value ...any) (*db.Result, error)

Update updates rows with the given column and value, or with a map of columns.

func (*Query) UpdateFrom

func (r *Query) UpdateFrom(values any) (*db.Result, error)

func (*Query) UpdateOrInsert

func (r *Query) UpdateOrInsert(attributes any, values any) (*db.Result, error)

UpdateOrInsert updates the first matching row, or inserts a new one.

func (*Query) Upsert

func (r *Query) Upsert(data any, uniqueBy []string, update ...any) (*db.Result, error)

Upsert follows Laravel's distinction between an omitted update argument and an empty array.

func (*Query) UseIndex

func (r *Query) UseIndex(index string) db.Query

func (*Query) UseWritePdo

func (r *Query) UseWritePdo() db.Query

UseWritePdo forces the query's reads to run on the write connection instead of the reader replica, so a read that has to observe a write issued on the same connection is not answered by a lagging replica. Mirrors Laravel's Builder::useWritePdo, whose flag reaches the connection as the inverse of its $useReadPdo argument. Here the read routing is the builder a read executes through, so the write builder replaces the read one for this query and its clones.

func (*Query) Value

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

Value retrieves a single column of the first row.

func (*Query) When

func (r *Query) When(condition bool, callback func(query db.Query) db.Query, falseCallback ...func(query db.Query) db.Query) db.Query

When applies a callback when the condition holds.

func (*Query) Where

func (r *Query) Where(query any, args ...any) db.Query

Where adds a where clause. Mirrors db.Query::Where.

func (*Query) WhereAfterToday

func (r *Query) WhereAfterToday(columns ...string) db.Query

func (*Query) WhereAll

func (r *Query) WhereAll(columns []string, args ...any) db.Query

WhereAll repeats one condition across columns joined by and.

func (*Query) WhereAny

func (r *Query) WhereAny(columns []string, args ...any) db.Query

WhereAny repeats one condition across columns joined by or, and joins that group to the clauses before it with and - the group connector Laravel's whereAny defaults to; the or lives inside the group.

func (*Query) WhereBeforeToday

func (r *Query) WhereBeforeToday(columns ...string) db.Query

func (*Query) WhereBetween

func (r *Query) WhereBetween(column string, x, y any) db.Query

WhereBetween adds a between clause.

func (*Query) WhereBetweenColumns

func (r *Query) WhereBetweenColumns(column, lower, upper string) db.Query

func (*Query) WhereColumn

func (r *Query) WhereColumn(column1 string, column2 ...string) db.Query

WhereColumn compares two columns. One trailing column means equality, two mean operator then column.

func (*Query) WhereDate

func (r *Query) WhereDate(column string, args ...any) db.Query

func (*Query) WhereDay

func (r *Query) WhereDay(column string, args ...any) db.Query

func (*Query) WhereExists

func (r *Query) WhereExists(query any) db.Query

WhereExists adds an exists clause.

func (*Query) WhereFullText

func (r *Query) WhereFullText(columns []string, value string, options ...string) db.Query

func (*Query) WhereFuture

func (r *Query) WhereFuture(columns ...string) db.Query

func (*Query) WhereIn

func (r *Query) WhereIn(column string, values any) db.Query

WhereIn adds an in clause.

func (*Query) WhereIntegerInRaw

func (r *Query) WhereIntegerInRaw(column string, values []any) db.Query

func (*Query) WhereIntegerNotInRaw

func (r *Query) WhereIntegerNotInRaw(column string, values []any) db.Query

func (*Query) WhereJsonContains

func (r *Query) WhereJsonContains(column string, value any) db.Query

WhereJsonContains adds a JSON contains clause.

func (*Query) WhereJsonContainsKey

func (r *Query) WhereJsonContainsKey(column string) db.Query

WhereJsonContainsKey adds a JSON key existence clause.

func (*Query) WhereJsonDoesntContain

func (r *Query) WhereJsonDoesntContain(column string, value any) db.Query

WhereJsonDoesntContain adds a negated JSON contains clause.

func (*Query) WhereJsonDoesntContainKey

func (r *Query) WhereJsonDoesntContainKey(column string) db.Query

WhereJsonDoesntContainKey adds a negated JSON key existence clause.

func (*Query) WhereJsonDoesntOverlap

func (r *Query) WhereJsonDoesntOverlap(column string, value any) db.Query

func (*Query) WhereJsonLength

func (r *Query) WhereJsonLength(column string, args ...any) db.Query

WhereJsonLength compares a JSON array length. Mirrors Laravel's Builder::whereJsonLength: args is either a single length (equality) or an operator and a length.

func (*Query) WhereJsonOverlaps

func (r *Query) WhereJsonOverlaps(column string, value any) db.Query

func (*Query) WhereLike

func (r *Query) WhereLike(column string, value string, caseSensitive ...bool) db.Query

WhereLike adds a like clause.

func (*Query) WhereMonth

func (r *Query) WhereMonth(column string, args ...any) db.Query

func (*Query) WhereNested

func (r *Query) WhereNested(callback func(db.Query) db.Query) db.Query

func (*Query) WhereNone

func (r *Query) WhereNone(columns []string, args ...any) db.Query

WhereNone negates a condition repeated across columns.

func (*Query) WhereNot

func (r *Query) WhereNot(query any, args ...any) db.Query

WhereNot adds a negated where clause.

func (*Query) WhereNotBetween

func (r *Query) WhereNotBetween(column string, x, y any) db.Query

WhereNotBetween adds a not between clause.

func (*Query) WhereNotBetweenColumns

func (r *Query) WhereNotBetweenColumns(column, lower, upper string) db.Query

func (*Query) WhereNotExists

func (r *Query) WhereNotExists(query any) db.Query

func (*Query) WhereNotIn

func (r *Query) WhereNotIn(column string, values any) db.Query

WhereNotIn adds a not in clause.

func (*Query) WhereNotLike

func (r *Query) WhereNotLike(column string, value string, caseSensitive ...bool) db.Query

WhereNotLike adds a not like clause.

func (*Query) WhereNotNull

func (r *Query) WhereNotNull(column string) db.Query

WhereNotNull adds an is not null clause.

func (*Query) WhereNowOrFuture

func (r *Query) WhereNowOrFuture(columns ...string) db.Query

func (*Query) WhereNowOrPast

func (r *Query) WhereNowOrPast(columns ...string) db.Query

func (*Query) WhereNull

func (r *Query) WhereNull(column string) db.Query

WhereNull adds an is null clause.

func (*Query) WherePast

func (r *Query) WherePast(columns ...string) db.Query

func (*Query) WhereRaw

func (r *Query) WhereRaw(raw string, args []any) db.Query

WhereRaw adds a raw where clause.

func (*Query) WhereRowValues

func (r *Query) WhereRowValues(columns []string, operator string, values []any) db.Query

func (*Query) WhereTime

func (r *Query) WhereTime(column string, args ...any) db.Query

func (*Query) WhereToday

func (r *Query) WhereToday(columns ...string) db.Query

func (*Query) WhereTodayOrAfter

func (r *Query) WhereTodayOrAfter(columns ...string) db.Query

func (*Query) WhereTodayOrBefore

func (r *Query) WhereTodayOrBefore(columns ...string) db.Query

func (*Query) WhereValueBetween

func (r *Query) WhereValueBetween(value any, lower, upper string) db.Query

func (*Query) WhereValueNotBetween

func (r *Query) WhereValueNotBetween(value any, lower, upper string) db.Query

func (*Query) WhereYear

func (r *Query) WhereYear(column string, args ...any) db.Query

type QueryLog

type QueryLog struct {
	Query string  `json:"query"`
	Time  float64 `json:"time"`
}

func GetQueryLog

func GetQueryLog(ctx context.Context) []QueryLog

type Row

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

func NewRow

func NewRow(row map[string]any, err error) *Row

func (*Row) Err

func (r *Row) Err() error

func (*Row) Scan

func (r *Row) Scan(value any) error

Scan maps column values using db tags and flattens embedded structs.

type ToSql

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

func NewToSql

func NewToSql(query *Query, raw bool) *ToSql

func (*ToSql) Count

func (r *ToSql) Count() string

func (*ToSql) DecrementEach

func (r *ToSql) DecrementEach(columns any, extra ...map[string]any) string

func (*ToSql) Delete

func (r *ToSql) Delete() string

func (*ToSql) First

func (r *ToSql) First() string

func (*ToSql) Get

func (r *ToSql) Get() string

func (*ToSql) IncrementEach

func (r *ToSql) IncrementEach(columns any, extra ...map[string]any) string

func (*ToSql) Insert

func (r *ToSql) Insert(data any) string

func (*ToSql) InsertGetID

func (r *ToSql) InsertGetID(data any, sequence ...string) string

func (*ToSql) InsertOrIgnore

func (r *ToSql) InsertOrIgnore(data any) string

func (*ToSql) InsertOrIgnoreUsing

func (r *ToSql) InsertOrIgnoreUsing(columns []string, query any) string

func (*ToSql) InsertUsing

func (r *ToSql) InsertUsing(columns []string, query any) string

func (*ToSql) Pluck

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

func (*ToSql) Truncate

func (r *ToSql) Truncate() []string

func (*ToSql) Update

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

func (*ToSql) UpdateFrom

func (r *ToSql) UpdateFrom(values any) string

func (*ToSql) Upsert

func (r *ToSql) Upsert(data any, uniqueBy []string, update ...any) string

type Tx

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

func NewTx

func NewTx(
	ctx context.Context,
	config config.Config,
	driver contractsdriver.Driver,
	logger contractslogger.Logger,
	connection *databasedriver.Connection,
	txBuilder contractsdb.TxBuilder,
	txLogs *[]TxLog,
	instrument *instrumentationdatabase.Instrument,
) *Tx

func (*Tx) AffectingStatement

func (r *Tx) AffectingStatement(statement string, args ...any) (*db.Result, error)

func (*Tx) AfterCommit

func (r *Tx) AfterCommit(callback func() error) error

func (*Tx) AfterRollBack

func (r *Tx) AfterRollBack(callback func() error) error

func (*Tx) AfterRollback

func (r *Tx) AfterRollback(callback func() error) error

func (*Tx) BeforeStartingTransaction

func (r *Tx) BeforeStartingTransaction(callback func(db.Tx) error)

func (*Tx) BeginTransaction

func (r *Tx) BeginTransaction() (db.Tx, error)

func (*Tx) CanDiscard

func (r *Tx) CanDiscard() bool

CanDiscard reports whether this handle owns a removable physical connection.

func (*Tx) Commit

func (r *Tx) Commit() error

func (*Tx) Delete

func (r *Tx) Delete(sql string, args ...any) (*contractsdb.Result, error)

func (*Tx) Discard

func (r *Tx) Discard() error

Discard rolls back work and removes a leased physical connection from its pool. It is optional for db.Tx implementations and used when session cleanup fails.

func (*Tx) Insert

func (r *Tx) Insert(sql string, args ...any) (*contractsdb.Result, error)

func (*Tx) Query

func (r *Tx) Query() db.Query

func (*Tx) Rollback

func (r *Tx) Rollback() error

func (*Tx) RollbackTo

func (r *Tx) RollbackTo(level int) error

func (*Tx) Scalar

func (r *Tx) Scalar(statement string, args ...any) (any, error)

func (*Tx) Select

func (r *Tx) Select(dest any, sql string, args ...any) error

func (*Tx) SelectFromWriteConnection

func (r *Tx) SelectFromWriteConnection(dest any, statement string, args ...any) error

func (*Tx) SelectOne

func (r *Tx) SelectOne(dest any, statement string, args ...any) error

func (*Tx) SelectResultSets

func (r *Tx) SelectResultSets(statement string, args ...any) (result [][]map[string]any, err error)

func (*Tx) Statement

func (r *Tx) Statement(sql string, args ...any) error

func (*Tx) StatementContext

func (r *Tx) StatementContext(ctx context.Context, statement string, args ...any) error

func (*Tx) Table

func (r *Tx) Table(name string) contractsdb.Query

func (*Tx) Transaction

func (r *Tx) Transaction(callback func(db.Tx) error, attempts ...int) error

func (*Tx) TransactionLevel

func (r *Tx) TransactionLevel() int

func (*Tx) Unprepared

func (r *Tx) Unprepared(statement string) error

func (*Tx) Update

func (r *Tx) Update(sql string, args ...any) (*contractsdb.Result, error)

type TxBuilder

type TxBuilder struct {
	*sqlx.Tx
}

TxBuilder executes statements inside a transaction.

func NewTxBuilder

func NewTxBuilder(pool *sql.DB, driver string, options *sql.TxOptions) (*TxBuilder, error)

NewTxBuilder opens a transaction on the given pool.

func NewTxBuilderWithContext

func NewTxBuilderWithContext(ctx context.Context, pool *sql.DB, driver string, options *sql.TxOptions) (*TxBuilder, error)

func (*TxBuilder) Explain

func (r *TxBuilder) Explain(sql string, args ...any) string

Explain renders a statement with its arguments interpolated, which is what ToRawSql reports.

func (*TxBuilder) GetContext

func (b *TxBuilder) GetContext(ctx context.Context, dest any, statement string, args ...any) error

func (*TxBuilder) SelectContext

func (b *TxBuilder) SelectContext(ctx context.Context, dest any, statement string, args ...any) error

type TxLog

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

Jump to

Keyboard shortcuts

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