Documentation
¶
Index ¶
- Variables
- func GroupBySource[S, T comparable](pairs []JoinPair[S, T]) map[S][]T
- func ResolveTableName[T any](fallback string) string
- func UniqueTargets[S, T comparable](pairs []JoinPair[S, T]) []T
- type ColumnValueFunc
- type DB
- func (db *DB) Begin(ctx context.Context) (*Tx, error)
- func (db *DB) Close() error
- func (db *DB) Debug(l Logger) *DB
- func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (db *DB) Transaction(ctx context.Context, fn func(tx *Tx) error) (err error)
- type Dialect
- type JoinConfig
- type JoinPair
- type Logger
- type PreloaderFunc
- type Querier
- type Query
- func (q *Query[T]) All(ctx context.Context) ([]T, error)
- func (q *Query[T]) ApplyLimit(n int)
- func (q *Query[T]) ApplyOffset(n int)
- func (q *Query[T]) ApplyOrderBy(clause string)
- func (q *Query[T]) ApplySelect(columns string)
- func (q *Query[T]) ApplyWhere(clause string, args []any)
- func (q *Query[T]) Count(ctx context.Context) (int64, error)
- func (q *Query[T]) Create(ctx context.Context, t *T) error
- func (q *Query[T]) CreateAll(ctx context.Context, items []*T) error
- func (q *Query[T]) Delete(ctx context.Context) error
- func (q *Query[T]) Exists(ctx context.Context) (bool, error)
- func (q *Query[T]) First(ctx context.Context) (T, error)
- func (q *Query[T]) Join(name string) *Query[T]
- func (q *Query[T]) LeftJoin(name string) *Query[T]
- func (q *Query[T]) Limit(n int) *Query[T]
- func (q *Query[T]) Offset(n int) *Query[T]
- func (q *Query[T]) OrderBy(clause string) *Query[T]
- func (q *Query[T]) Preload(name string) *Query[T]
- func (q *Query[T]) RegisterJoin(name string, cfg JoinConfig)
- func (q *Query[T]) RegisterPreloader(name string, fn PreloaderFunc[T])
- func (q *Query[T]) Scopes(scopes ...scope.Scope) *Query[T]
- func (q *Query[T]) Select(columns string) *Query[T]
- func (q *Query[T]) Update(ctx context.Context, t *T) error
- func (q *Query[T]) Upsert(ctx context.Context, t *T) error
- func (q *Query[T]) Where(clause string, args ...any) *Query[T]
- type ScanFunc
- type SetPKFunc
- type TableNamer
- type Tx
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("orm: not found")
ErrNotFound is returned when a query expects exactly one row but finds none.
Functions ¶
func GroupBySource ¶
func GroupBySource[S, T comparable](pairs []JoinPair[S, T]) map[S][]T
GroupBySource groups JoinPair values by source key into a map[S][]T.
func ResolveTableName ¶ added in v0.0.7
ResolveTableName returns the table name for type T. If T implements TableNamer (value or pointer receiver), that name is used; otherwise fallback is returned.
func UniqueTargets ¶
func UniqueTargets[S, T comparable](pairs []JoinPair[S, T]) []T
UniqueTargets extracts deduplicated target values from a slice of JoinPair.
Types ¶
type ColumnValueFunc ¶
ColumnValueFunc extracts column names and their values from a *T. When includesPK is false the primary key column is excluded (for INSERT with auto-increment).
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB wraps *sql.DB with a Dialect and satisfies Querier.
func (*DB) Debug ¶ added in v0.0.5
Debug returns a new *DB that logs every query using the given Logger. The original DB is not modified.
func (*DB) ExecContext ¶
func (*DB) QueryContext ¶
type Dialect ¶
type Dialect interface {
// Placeholder returns the bind parameter placeholder for the given
// 1-based index. MySQL returns "?" regardless of index; PostgreSQL
// returns "$1", "$2", etc.
Placeholder(index int) string
// QuoteIdent quotes an identifier (table name, column name) to safely
// handle SQL reserved words. MySQL uses backticks; PostgreSQL uses
// double quotes.
QuoteIdent(name string) string
// UseReturning reports whether INSERT should use a RETURNING clause
// to retrieve the auto-generated primary key (PostgreSQL) rather
// than relying on LastInsertId (MySQL).
UseReturning() bool
// ReturningClause returns the RETURNING clause appended to INSERT
// statements. Returns an empty string for dialects that do not
// support RETURNING (MySQL).
ReturningClause(pk string) string
}
Dialect abstracts SQL differences between database engines.
var MySQL Dialect = mysqlDialect{}
MySQL is the Dialect for MySQL / MariaDB.
var PostgreSQL Dialect = postgresDialect{}
PostgreSQL is the Dialect for PostgreSQL.
type JoinConfig ¶
type JoinConfig struct {
TargetTable string
TargetColumn string
SourceTable string
SourceColumn string
}
JoinConfig holds the metadata needed to build a JOIN clause at runtime.
type JoinPair ¶
type JoinPair[S, T comparable] struct { Source S Target T }
JoinPair holds a source–target pair read from a join table.
func QueryJoinTable ¶
func QueryJoinTable[S, T comparable]( ctx context.Context, db Querier, table, sourceCol, targetCol string, sourceIDs []S, ) ([]JoinPair[S, T], error)
QueryJoinTable reads (sourceCol, targetCol) rows from the given join table where sourceCol IN (sourceIDs). It returns a slice of JoinPair.
type PreloaderFunc ¶
PreloaderFunc executes a preload query and assigns results to the parent slice. Generated per-relation by ormgen.
type Querier ¶
type Querier interface {
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
// contains filtered or unexported methods
}
Querier is the common interface for DB and Tx. Generated factory functions accept this so that queries work with both.
type Query ¶
type Query[T any] struct { // contains filtered or unexported fields }
Query represents a pending query against a single table. All builder methods return a new Query; the receiver is never modified.
func NewQuery ¶
func NewQuery[T any]( db Querier, table string, columns []string, pk string, scan ScanFunc[T], colValPairs ColumnValueFunc[T], setPK SetPKFunc[T], ) *Query[T]
NewQuery is called by generated factory functions.
func (*Query[T]) ApplyLimit ¶
func (*Query[T]) ApplyOffset ¶
func (*Query[T]) ApplyOrderBy ¶
func (*Query[T]) ApplySelect ¶
func (*Query[T]) ApplyWhere ¶
func (*Query[T]) Create ¶
Create inserts a new row. If setPK is set, the primary key is populated via RETURNING (PostgreSQL) or LastInsertId (MySQL).
func (*Query[T]) CreateAll ¶
CreateAll inserts multiple rows in a single INSERT statement. If setPK is set, primary keys are populated for each row.
func (*Query[T]) Delete ¶
Delete deletes rows matching the accumulated WHERE clauses. Returns an error if no WHERE clauses are set (safety guard).
func (*Query[T]) Exists ¶
Exists returns true if at least one row matches the current query conditions.
func (*Query[T]) First ¶
First executes a SELECT with LIMIT 1 and returns the first row. Returns ErrNotFound if no rows match.
func (*Query[T]) RegisterJoin ¶
func (q *Query[T]) RegisterJoin(name string, cfg JoinConfig)
RegisterJoin registers a named join definition for use with Join/LeftJoin.
func (*Query[T]) RegisterPreloader ¶
func (q *Query[T]) RegisterPreloader(name string, fn PreloaderFunc[T])
RegisterPreloader registers a named preloader for use with Preload.
func (*Query[T]) Update ¶
Update updates the row identified by the primary key of t. All non-PK columns are SET.
type SetPKFunc ¶
SetPKFunc sets the auto-generated primary key on *T after INSERT. May be nil when the primary key is not auto-generated.
type TableNamer ¶ added in v0.0.7
type TableNamer interface {
TableName() string
}
TableNamer can be implemented by model structs to override the auto-derived table name.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx wraps *sql.Tx with a Dialect and satisfies Querier.