Documentation
¶
Overview ¶
Package pgdriver provides a PostgreSQL driver for the Grove ORM, built on top of pgxpool from github.com/jackc/pgx/v5.
Index ¶
- Variables
- type CreateTableQuery
- func (q *CreateTableQuery) Build() (string, []any, error)
- func (q *CreateTableQuery) Exec(ctx context.Context) (driver.Result, error)
- func (q *CreateTableQuery) IfNotExists() *CreateTableQuery
- func (q *CreateTableQuery) Temp() *CreateTableQuery
- func (q *CreateTableQuery) WithForeignKey(fk string) *CreateTableQuery
- type DeleteQuery
- func (q *DeleteQuery) Build() (string, []any, error)
- func (q *DeleteQuery) Exec(ctx context.Context) (driver.Result, error)
- func (q *DeleteQuery) ForceDelete() *DeleteQuery
- func (q *DeleteQuery) Returning(columns ...string) *DeleteQuery
- func (q *DeleteQuery) Scan(ctx context.Context, dest ...any) error
- func (q *DeleteQuery) Where(query string, args ...any) *DeleteQuery
- func (q *DeleteQuery) WhereOr(query string, args ...any) *DeleteQuery
- func (q *DeleteQuery) WherePK() *DeleteQuery
- type DropTableQuery
- type Hstore
- type InsertQuery
- func (q *InsertQuery) Build() (string, []any, error)
- func (q *InsertQuery) Column(columns ...string) *InsertQuery
- func (q *InsertQuery) Exec(ctx context.Context) (driver.Result, error)
- func (q *InsertQuery) MultiRow() *InsertQuery
- func (q *InsertQuery) OnConflict(clause string) *InsertQuery
- func (q *InsertQuery) Returning(columns ...string) *InsertQuery
- func (q *InsertQuery) Scan(ctx context.Context, dest ...any) error
- func (q *InsertQuery) Set(expr string, args ...any) *InsertQuery
- func (q *InsertQuery) Value(values ...any) *InsertQuery
- type Int64Array
- type JSONMap
- type Listener
- func (l *Listener) Close() error
- func (l *Listener) Done() <-chan struct{}
- func (l *Listener) Listen(ctx context.Context, channel string) error
- func (l *Listener) Notify(ctx context.Context, channel, payload string) error
- func (l *Listener) OnNotification(channel string, handler func(*Notification))
- func (l *Listener) Start(ctx context.Context) error
- func (l *Listener) Unlisten(ctx context.Context, channel string) error
- type Notification
- type PgDB
- func (db *PgDB) AcquireConn(ctx context.Context) (driver.DedicatedConn, error)
- func (db *PgDB) BeginTx(ctx context.Context, opts *driver.TxOptions) (driver.Tx, error)
- func (db *PgDB) BeginTxQuery(ctx context.Context, opts *driver.TxOptions) (*PgTx, error)
- func (db *PgDB) Close() error
- func (db *PgDB) Dialect() driver.Dialect
- func (db *PgDB) Exec(ctx context.Context, query string, args ...any) (driver.Result, error)
- func (db *PgDB) GroveDelete(model any) any
- func (db *PgDB) GroveInsert(model any) any
- func (db *PgDB) GroveSelect(model ...any) any
- func (db *PgDB) GroveTx(ctx context.Context, isolationLevel int, readOnly bool) (any, error)
- func (db *PgDB) GroveUpdate(model any) any
- func (db *PgDB) Listen(ctx context.Context, channel string, handler func(*Notification)) (*Listener, error)
- func (db *PgDB) Name() string
- func (db *PgDB) NewCreateTable(model any) *CreateTableQuery
- func (db *PgDB) NewDelete(model any) *DeleteQuery
- func (db *PgDB) NewDropTable(model any) *DropTableQuery
- func (db *PgDB) NewInsert(model any) *InsertQuery
- func (db *PgDB) NewListener() *Listener
- func (db *PgDB) NewRaw(query string, args ...any) *RawQuery
- func (db *PgDB) NewSelect(model ...any) *SelectQuery
- func (db *PgDB) NewUpdate(model any) *UpdateQuery
- func (db *PgDB) Open(ctx context.Context, dsn string, opts ...driver.Option) error
- func (db *PgDB) Ping(ctx context.Context) error
- func (db *PgDB) Prepare(ctx context.Context, query string) (driver.Stmt, error)
- func (db *PgDB) Query(ctx context.Context, query string, args ...any) (driver.Rows, error)
- func (db *PgDB) QueryRow(ctx context.Context, query string, args ...any) driver.Row
- func (db *PgDB) SetHooks(engine *hook.Engine)
- func (db *PgDB) SupportsCDC() bool
- func (db *PgDB) SupportsReturning() bool
- func (db *PgDB) SupportsStreaming() bool
- type PgDialect
- func (d *PgDialect) AppendBytes(b []byte, v []byte) []byte
- func (d *PgDialect) AppendTime(b []byte, t time.Time) []byte
- func (d *PgDialect) GoToDBType(goType reflect.Type, opts schema.FieldOptions) string
- func (d *PgDialect) Name() string
- func (d *PgDialect) Placeholder(n int) string
- func (d *PgDialect) Quote(ident string) string
- type PgTx
- func (t *PgTx) Commit() error
- func (t *PgTx) NewDelete(model any) *DeleteQuery
- func (t *PgTx) NewInsert(model any) *InsertQuery
- func (t *PgTx) NewRaw(query string, args ...any) *RawQuery
- func (t *PgTx) NewSelect(model ...any) *SelectQuery
- func (t *PgTx) NewUpdate(model any) *UpdateQuery
- func (t *PgTx) Rollback() error
- type RawQuery
- type SelectQuery
- func (q *SelectQuery) Build() (string, []any, error)
- func (q *SelectQuery) BuildCount() (string, []any, error)
- func (q *SelectQuery) Column(columns ...string) *SelectQuery
- func (q *SelectQuery) ColumnExpr(expr string, args ...any) *SelectQuery
- func (q *SelectQuery) Count(ctx context.Context) (int64, error)
- func (q *SelectQuery) DistinctOn(columns ...string) *SelectQuery
- func (q *SelectQuery) ForShare() *SelectQuery
- func (q *SelectQuery) ForUpdate(tables ...string) *SelectQuery
- func (q *SelectQuery) GroupExpr(expr string) *SelectQuery
- func (q *SelectQuery) Having(query string, args ...any) *SelectQuery
- func (q *SelectQuery) Join(joinType, table, on string, args ...any) *SelectQuery
- func (q *SelectQuery) Lateral(subquery string, alias string, args ...any) *SelectQuery
- func (q *SelectQuery) Limit(n int) *SelectQuery
- func (q *SelectQuery) Offset(n int) *SelectQuery
- func (q *SelectQuery) OrderExpr(expr string) *SelectQuery
- func (q *SelectQuery) Relation(name string) *SelectQuery
- func (q *SelectQuery) Scan(ctx context.Context, dest ...any) error
- func (q *SelectQuery) Stream(ctx context.Context) (*stream.Stream[any], error)
- func (q *SelectQuery) StreamBatch(ctx context.Context, fetchSize int) (*stream.Stream[any], error)
- func (q *SelectQuery) TableExpr(expr string, args ...any) *SelectQuery
- func (q *SelectQuery) Where(query string, args ...any) *SelectQuery
- func (q *SelectQuery) WhereArray(col string, op string, arr any) *SelectQuery
- func (q *SelectQuery) WhereOr(query string, args ...any) *SelectQuery
- func (q *SelectQuery) WherePK() *SelectQuery
- func (q *SelectQuery) WithDeleted() *SelectQuery
- type StringArray
- type UpdateQuery
- func (q *UpdateQuery) Build() (string, []any, error)
- func (q *UpdateQuery) Column(columns ...string) *UpdateQuery
- func (q *UpdateQuery) Exec(ctx context.Context) (driver.Result, error)
- func (q *UpdateQuery) OmitZero() *UpdateQuery
- func (q *UpdateQuery) Returning(columns ...string) *UpdateQuery
- func (q *UpdateQuery) Scan(ctx context.Context, dest ...any) error
- func (q *UpdateQuery) Set(expr string, args ...any) *UpdateQuery
- func (q *UpdateQuery) Where(query string, args ...any) *UpdateQuery
- func (q *UpdateQuery) WherePK() *UpdateQuery
Constants ¶
This section is empty.
Variables ¶
var ErrLastInsertIDNotSupported = errors.New("pgdriver: LastInsertId is not supported by PostgreSQL; use RETURNING instead")
ErrLastInsertIDNotSupported is returned by pgResult.LastInsertId because PostgreSQL does not natively support a last-insert-id concept. Use INSERT ... RETURNING instead.
Functions ¶
This section is empty.
Types ¶
type CreateTableQuery ¶
type CreateTableQuery struct {
// contains filtered or unexported fields
}
CreateTableQuery builds PostgreSQL CREATE TABLE statements.
func (*CreateTableQuery) Build ¶
func (q *CreateTableQuery) Build() (string, []any, error)
Build generates the SQL and args.
func (*CreateTableQuery) IfNotExists ¶
func (q *CreateTableQuery) IfNotExists() *CreateTableQuery
IfNotExists adds the IF NOT EXISTS clause.
func (*CreateTableQuery) Temp ¶
func (q *CreateTableQuery) Temp() *CreateTableQuery
Temp marks the table as TEMPORARY.
func (*CreateTableQuery) WithForeignKey ¶
func (q *CreateTableQuery) WithForeignKey(fk string) *CreateTableQuery
WithForeignKey adds a raw foreign key constraint string. Example: "(user_id) REFERENCES users(id) ON DELETE CASCADE"
type DeleteQuery ¶
type DeleteQuery struct {
// contains filtered or unexported fields
}
DeleteQuery builds PostgreSQL DELETE statements.
func (*DeleteQuery) Build ¶
func (q *DeleteQuery) Build() (string, []any, error)
Build generates the SQL and args.
func (*DeleteQuery) ForceDelete ¶
func (q *DeleteQuery) ForceDelete() *DeleteQuery
ForceDelete bypasses soft delete, performing a real DELETE even if the model has a soft_delete field.
func (*DeleteQuery) Returning ¶
func (q *DeleteQuery) Returning(columns ...string) *DeleteQuery
Returning adds RETURNING columns.
func (*DeleteQuery) Scan ¶
func (q *DeleteQuery) Scan(ctx context.Context, dest ...any) error
Scan executes the DELETE with RETURNING and scans results into dest.
func (*DeleteQuery) Where ¶
func (q *DeleteQuery) Where(query string, args ...any) *DeleteQuery
Where adds a WHERE clause.
func (*DeleteQuery) WhereOr ¶
func (q *DeleteQuery) WhereOr(query string, args ...any) *DeleteQuery
WhereOr adds an OR WHERE clause.
func (*DeleteQuery) WherePK ¶
func (q *DeleteQuery) WherePK() *DeleteQuery
WherePK adds WHERE pk = $N using model's primary key values.
type DropTableQuery ¶
type DropTableQuery struct {
// contains filtered or unexported fields
}
DropTableQuery builds PostgreSQL DROP TABLE statements.
func (*DropTableQuery) Build ¶
func (q *DropTableQuery) Build() (string, []any, error)
Build generates the SQL and args.
func (*DropTableQuery) Cascade ¶
func (q *DropTableQuery) Cascade() *DropTableQuery
Cascade adds the CASCADE clause.
func (*DropTableQuery) IfExists ¶
func (q *DropTableQuery) IfExists() *DropTableQuery
IfExists adds the IF EXISTS clause.
type Hstore ¶
Hstore represents a PostgreSQL hstore column (key-value string map). NULL values are represented as nil *string entries.
type InsertQuery ¶
type InsertQuery struct {
// contains filtered or unexported fields
}
InsertQuery builds PostgreSQL INSERT statements.
func (*InsertQuery) Build ¶
func (q *InsertQuery) Build() (string, []any, error)
Build generates the SQL and args.
func (*InsertQuery) Column ¶
func (q *InsertQuery) Column(columns ...string) *InsertQuery
Column specifies which columns to insert.
func (*InsertQuery) MultiRow ¶
func (q *InsertQuery) MultiRow() *InsertQuery
MultiRow forces the insert to use a single multi-row VALUES statement instead of a prepared statement loop. This may be preferred for small batches where single-statement atomicity matters.
func (*InsertQuery) OnConflict ¶
func (q *InsertQuery) OnConflict(clause string) *InsertQuery
OnConflict adds an ON CONFLICT clause (e.g., "(email) DO UPDATE").
func (*InsertQuery) Returning ¶
func (q *InsertQuery) Returning(columns ...string) *InsertQuery
Returning adds RETURNING columns.
func (*InsertQuery) Scan ¶
func (q *InsertQuery) Scan(ctx context.Context, dest ...any) error
Scan executes the INSERT with RETURNING and scans results into dest.
func (*InsertQuery) Set ¶
func (q *InsertQuery) Set(expr string, args ...any) *InsertQuery
Set adds a SET expression for ON CONFLICT DO UPDATE.
func (*InsertQuery) Value ¶
func (q *InsertQuery) Value(values ...any) *InsertQuery
Value adds explicit values (for manual inserts without model data).
type Int64Array ¶
type Int64Array []int64
Int64Array is a []int64 that implements database/sql/driver.Valuer and sql.Scanner for transparent serialization to/from PostgreSQL bigint[] columns.
func (*Int64Array) Scan ¶
func (a *Int64Array) Scan(src any) error
Scan parses a PostgreSQL bigint[] array literal into an int64 slice. The expected format is: {1,2,3}.
type JSONMap ¶
JSONMap is a map[string]any that implements database/sql/driver.Valuer and sql.Scanner for transparent serialization to/from PostgreSQL jsonb columns.
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Listener manages PostgreSQL LISTEN/NOTIFY subscriptions. It holds a dedicated connection from the pool for receiving notifications.
func (*Listener) Close ¶
Close stops the listener goroutine, which releases the dedicated connection back to the pool as it exits. Close is safe to call multiple times and returns without waiting for the goroutine to finish; pool shutdown blocks until the connection is released, which happens promptly after the wake.
func (*Listener) Done ¶
func (l *Listener) Done() <-chan struct{}
Done returns a channel that is closed when the listen goroutine exits — after Close, context cancellation, or a fatal connection error. Consumers that need the listener to survive connection loss can watch Done and build a replacement (the listener does not reconnect by itself). For a listener that was never started, the channel never closes.
func (*Listener) Listen ¶
Listen subscribes to a PostgreSQL notification channel. The LISTEN statement is executed by the listen goroutine, which owns the dedicated connection. The listener must be started (via Start) before calling Listen.
func (*Listener) Notify ¶
Notify sends a notification on the given channel with the specified payload. It uses a connection from the pool (not the dedicated listener connection) so that notifications can be sent independently of the listener.
func (*Listener) OnNotification ¶
func (l *Listener) OnNotification(channel string, handler func(*Notification))
OnNotification registers a handler function that will be called whenever a notification arrives on the specified channel. Multiple handlers can be registered for the same channel and they will all be invoked.
func (*Listener) Start ¶
Start acquires a dedicated connection from the pool and begins listening for notifications in a background goroutine. The goroutine runs until the context is cancelled or Close is called.
type Notification ¶
type Notification struct {
Channel string
Payload string
PID uint32 // Backend PID that sent the notification
}
Notification represents a PostgreSQL NOTIFY message.
type PgDB ¶
type PgDB struct {
// contains filtered or unexported fields
}
PgDB implements driver.Driver for PostgreSQL using pgxpool. Call New() to create an instance and then Open() to establish the connection pool.
When txConn is set (by PgTx), Exec/Query/QueryRow route through the transaction instead of the pool.
func New ¶
func New() *PgDB
New creates a new unconnected PgDB. Call Open to establish a connection pool.
func Unwrap ¶
Unwrap extracts the underlying *PgDB from a *grove.DB handle. This allows access to PostgreSQL-specific query builders and features.
pgdb := pgdriver.Unwrap(db) // returns *pgdriver.PgDB
pgdb.NewSelect(&users).Where("email ILIKE $1", "%@test.com").Scan(ctx)
Panics if the driver is not a *PgDB.
func (*PgDB) AcquireConn ¶
AcquireConn acquires a dedicated connection from the pool. All operations on the returned DedicatedConn execute on the same underlying PostgreSQL session, making it safe for session-level state such as advisory locks.
func (*PgDB) BeginTxQuery ¶
BeginTx starts a new transaction and returns a PgTx that exposes query builder methods operating within that transaction.
func (*PgDB) Exec ¶
Exec executes a query that does not return rows (INSERT, UPDATE, DELETE, DDL) and returns a driver.Result.
func (*PgDB) GroveDelete ¶
GroveDelete is the adapter method for grove.DB.NewDelete().
func (*PgDB) GroveInsert ¶
GroveInsert is the adapter method for grove.DB.NewInsert().
func (*PgDB) GroveSelect ¶
GroveSelect is the adapter method for grove.DB.NewSelect().
func (*PgDB) GroveTx ¶
GroveTx is the adapter method for grove.DB.BeginTx(). It bridges the grove package's generic transaction interface with PgDB's typed BeginTx. The isolationLevel parameter maps to driver.IsolationLevel constants.
func (*PgDB) GroveUpdate ¶
GroveUpdate is the adapter method for grove.DB.NewUpdate().
func (*PgDB) Listen ¶
func (db *PgDB) Listen(ctx context.Context, channel string, handler func(*Notification)) (*Listener, error)
Listen is a convenience method that creates a Listener, starts it, subscribes to the given channel, and registers the handler. It returns the Listener so the caller can close it when done.
func (*PgDB) NewCreateTable ¶
func (db *PgDB) NewCreateTable(model any) *CreateTableQuery
NewCreateTable creates a CREATE TABLE query for the given model.
func (*PgDB) NewDelete ¶
func (db *PgDB) NewDelete(model any) *DeleteQuery
NewDelete creates a DELETE query.
func (*PgDB) NewDropTable ¶
func (db *PgDB) NewDropTable(model any) *DropTableQuery
NewDropTable creates a DROP TABLE query for the given model.
func (*PgDB) NewInsert ¶
func (db *PgDB) NewInsert(model any) *InsertQuery
NewInsert creates an INSERT query. model can be a struct pointer or a pointer to a slice (for bulk insert).
func (*PgDB) NewListener ¶
NewListener creates a new Listener associated with the given PgDB. The listener is not started and has no subscriptions. Call Start to begin receiving notifications, Listen to subscribe to channels, and OnNotification to register handlers.
func (*PgDB) NewSelect ¶
func (db *PgDB) NewSelect(model ...any) *SelectQuery
NewSelect creates a new SELECT query. model can be:
- *[]User (slice pointer for multi-row)
- *User (struct pointer for single row)
- (*User)(nil) (nil pointer for table reference without binding)
func (*PgDB) NewUpdate ¶
func (db *PgDB) NewUpdate(model any) *UpdateQuery
NewUpdate creates an UPDATE query.
func (*PgDB) Open ¶
Open parses the DSN, applies configuration options, creates the pgxpool connection pool, and verifies connectivity.
func (*PgDB) Prepare ¶
Prepare creates a prepared statement for repeated execution. If operating within a transaction, it delegates to the transaction's Prepare. For pool connections, it acquires a connection from the pool, prepares the statement on it, and returns a pgPoolStmt that releases the connection on Close.
func (*PgDB) Query ¶
Query executes a query that returns rows and wraps the result in a driver.Rows.
func (*PgDB) SetHooks ¶
SetHooks attaches a hook engine for lifecycle hooks (pre/post query and mutation). If engine is nil, hooks are disabled.
func (*PgDB) SupportsCDC ¶
SupportsCDC returns true because PostgreSQL supports logical replication for change data capture.
func (*PgDB) SupportsReturning ¶
SupportsReturning returns true because PostgreSQL supports INSERT ... RETURNING.
func (*PgDB) SupportsStreaming ¶
SupportsStreaming returns true because PostgreSQL supports server-side cursors.
type PgDialect ¶
type PgDialect struct{}
PgDialect implements driver.Dialect for PostgreSQL.
func (*PgDialect) AppendBytes ¶
AppendBytes appends a hex-encoded PostgreSQL bytea literal to b and returns the extended slice. The format is: '\x<hex>'
func (*PgDialect) AppendTime ¶
AppendTime appends a time value formatted as RFC3339Nano (wrapped in single quotes) to b and returns the extended slice.
func (*PgDialect) GoToDBType ¶
GoToDBType maps a Go reflect.Type to the appropriate PostgreSQL column type string, taking field options into account.
Mapping rules (in order of precedence):
- If opts.SQLType is set, it is returned verbatim.
- bool -> "boolean"
- int, int32 -> "integer"
- int64 -> "bigint"
- int16 -> "smallint"
- int8 -> "smallint"
- float32 -> "real"
- float64 -> "double precision"
- string -> "text" (or "varchar(255)" if Unique is set)
- time.Time -> "timestamptz"
- *time.Time -> "timestamptz"
- []byte -> "bytea"
- map[string]any -> "jsonb"
- default -> "text"
func (*PgDialect) Placeholder ¶
Placeholder returns a positional parameter placeholder ($N) for PostgreSQL. n is 1-indexed.
type PgTx ¶
type PgTx struct {
// contains filtered or unexported fields
}
PgTx wraps a driver.Tx and exposes query builder methods. Queries created from a PgTx execute within the transaction.
func (*PgTx) NewDelete ¶
func (t *PgTx) NewDelete(model any) *DeleteQuery
NewDelete creates a DELETE query that executes within the transaction.
func (*PgTx) NewInsert ¶
func (t *PgTx) NewInsert(model any) *InsertQuery
NewInsert creates an INSERT query that executes within the transaction.
func (*PgTx) NewSelect ¶
func (t *PgTx) NewSelect(model ...any) *SelectQuery
NewSelect creates a SELECT query that executes within the transaction.
func (*PgTx) NewUpdate ¶
func (t *PgTx) NewUpdate(model any) *UpdateQuery
NewUpdate creates an UPDATE query that executes within the transaction.
type RawQuery ¶
type RawQuery struct {
// contains filtered or unexported fields
}
RawQuery executes arbitrary SQL with optional model scanning.
type SelectQuery ¶
type SelectQuery struct {
// contains filtered or unexported fields
}
SelectQuery builds PostgreSQL SELECT statements.
func (*SelectQuery) Build ¶
func (q *SelectQuery) Build() (string, []any, error)
Build generates the SQL string and args without executing.
func (*SelectQuery) BuildCount ¶
func (q *SelectQuery) BuildCount() (string, []any, error)
BuildCount generates a SELECT COUNT(*) query string and args.
func (*SelectQuery) Column ¶
func (q *SelectQuery) Column(columns ...string) *SelectQuery
Column adds specific columns to select. If not called, selects all fields.
func (*SelectQuery) ColumnExpr ¶
func (q *SelectQuery) ColumnExpr(expr string, args ...any) *SelectQuery
ColumnExpr adds a raw column expression.
func (*SelectQuery) Count ¶
func (q *SelectQuery) Count(ctx context.Context) (int64, error)
Count executes a SELECT COUNT(*) and returns the count.
func (*SelectQuery) DistinctOn ¶
func (q *SelectQuery) DistinctOn(columns ...string) *SelectQuery
DistinctOn adds DISTINCT ON (columns).
func (*SelectQuery) ForShare ¶
func (q *SelectQuery) ForShare() *SelectQuery
ForShare adds FOR SHARE.
func (*SelectQuery) ForUpdate ¶
func (q *SelectQuery) ForUpdate(tables ...string) *SelectQuery
ForUpdate adds FOR UPDATE.
func (*SelectQuery) GroupExpr ¶
func (q *SelectQuery) GroupExpr(expr string) *SelectQuery
GroupExpr adds GROUP BY expression.
func (*SelectQuery) Having ¶
func (q *SelectQuery) Having(query string, args ...any) *SelectQuery
Having adds HAVING clause.
func (*SelectQuery) Join ¶
func (q *SelectQuery) Join(joinType, table, on string, args ...any) *SelectQuery
Join adds a JOIN clause.
func (*SelectQuery) Lateral ¶
func (q *SelectQuery) Lateral(subquery string, alias string, args ...any) *SelectQuery
Lateral adds a JOIN LATERAL subquery. It generates:
JOIN LATERAL (subquery) AS alias ON true
func (*SelectQuery) OrderExpr ¶
func (q *SelectQuery) OrderExpr(expr string) *SelectQuery
OrderExpr adds ORDER BY expression.
func (*SelectQuery) Relation ¶
func (q *SelectQuery) Relation(name string) *SelectQuery
Relation marks a relation for eager loading.
func (*SelectQuery) Scan ¶
func (q *SelectQuery) Scan(ctx context.Context, dest ...any) error
Scan executes the query and scans results into the model.
func (*SelectQuery) Stream ¶
Stream executes the query using a PG server-side cursor and returns a stream.Stream that yields one model instance at a time. The stream transparently fetches rows in batches of 100 from the cursor.
The stream owns a dedicated transaction; it is committed when the stream is closed. Always defer stream.Close().
s, err := db.NewSelect(&users).Where("active = true").Stream(ctx)
if err != nil { ... }
defer s.Close()
for s.Next(ctx) {
user := s.Value()
}
func (*SelectQuery) StreamBatch ¶
StreamBatch executes the query using a PG server-side cursor and returns a stream.Stream that yields one model instance at a time. Rows are fetched from the server in batches of fetchSize.
The stream owns a dedicated transaction; it is committed when the stream is closed. Always defer stream.Close().
func (*SelectQuery) TableExpr ¶
func (q *SelectQuery) TableExpr(expr string, args ...any) *SelectQuery
TableExpr sets the FROM clause to a raw SQL expression instead of deriving it from the model's table name. This is useful for queries against functions, CTEs, or subqueries, e.g.:
db.NewSelect().TableExpr("generate_series(1, 10) AS s(n)")
func (*SelectQuery) Where ¶
func (q *SelectQuery) Where(query string, args ...any) *SelectQuery
Where adds an AND WHERE clause.
func (*SelectQuery) WhereArray ¶
func (q *SelectQuery) WhereArray(col string, op string, arr any) *SelectQuery
WhereArray adds an AND WHERE clause using a PostgreSQL array operator. It generates SQL like: "col" op ($N), e.g. "role" = ANY($1). The op parameter should be a SQL operator such as "= ANY" or "<> ALL". The arr value is added directly to the query args (pass a Go slice).
func (*SelectQuery) WhereOr ¶
func (q *SelectQuery) WhereOr(query string, args ...any) *SelectQuery
WhereOr adds an OR WHERE clause.
func (*SelectQuery) WherePK ¶
func (q *SelectQuery) WherePK() *SelectQuery
WherePK adds WHERE conditions for the model's primary key fields. The user must have set the model so that PKFields are available. It generates conditions like "table"."pk_col" = $N using sequential placeholders.
func (*SelectQuery) WithDeleted ¶
func (q *SelectQuery) WithDeleted() *SelectQuery
WithDeleted includes soft-deleted rows in the result set. By default, models with a soft_delete field automatically filter out rows where the soft delete column is not NULL.
type StringArray ¶
type StringArray []string
StringArray is a []string that implements database/sql/driver.Valuer and sql.Scanner for transparent serialization to/from PostgreSQL text[] columns.
func (*StringArray) Scan ¶
func (a *StringArray) Scan(src any) error
Scan parses a PostgreSQL text[] array literal into a string slice. The expected format is: {elem1,elem2,elem3} or {"elem1","elem2","elem3"}.
type UpdateQuery ¶
type UpdateQuery struct {
// contains filtered or unexported fields
}
UpdateQuery builds PostgreSQL UPDATE statements.
func (*UpdateQuery) Build ¶
func (q *UpdateQuery) Build() (string, []any, error)
Build generates the SQL and args.
func (*UpdateQuery) Column ¶
func (q *UpdateQuery) Column(columns ...string) *UpdateQuery
Column limits which columns to update from the model.
func (*UpdateQuery) OmitZero ¶
func (q *UpdateQuery) OmitZero() *UpdateQuery
OmitZero skips fields with zero values when building SET from model.
func (*UpdateQuery) Returning ¶
func (q *UpdateQuery) Returning(columns ...string) *UpdateQuery
Returning adds RETURNING columns.
func (*UpdateQuery) Scan ¶
func (q *UpdateQuery) Scan(ctx context.Context, dest ...any) error
Scan executes the UPDATE with RETURNING and scans results into dest.
func (*UpdateQuery) Set ¶
func (q *UpdateQuery) Set(expr string, args ...any) *UpdateQuery
Set adds a raw SET expression (e.g., "name = $1", "Alice").
func (*UpdateQuery) Where ¶
func (q *UpdateQuery) Where(query string, args ...any) *UpdateQuery
Where adds a WHERE clause.
func (*UpdateQuery) WherePK ¶
func (q *UpdateQuery) WherePK() *UpdateQuery
WherePK adds WHERE pk = $N using model's primary key values.