pgdriver

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 25 Imported by: 25

Documentation

Overview

Package pgdriver provides a PostgreSQL driver for the Grove ORM, built on top of pgxpool from github.com/jackc/pgx/v5.

Index

Constants

This section is empty.

Variables

View Source
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) Exec

Exec executes the CREATE TABLE statement.

func (*CreateTableQuery) IfNotExists

func (q *CreateTableQuery) IfNotExists() *CreateTableQuery

IfNotExists adds the IF NOT EXISTS clause.

func (*CreateTableQuery) Temp

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

func (q *DeleteQuery) Exec(ctx context.Context) (driver.Result, error)

Exec executes the DELETE (or soft-delete UPDATE).

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

func (q *DropTableQuery) Exec(ctx context.Context) (driver.Result, error)

Exec executes the DROP TABLE statement.

func (*DropTableQuery) IfExists

func (q *DropTableQuery) IfExists() *DropTableQuery

IfExists adds the IF EXISTS clause.

type Hstore

type Hstore map[string]*string

Hstore represents a PostgreSQL hstore column (key-value string map). NULL values are represented as nil *string entries.

func (*Hstore) Scan

func (h *Hstore) Scan(src any) error

Scan parses a PostgreSQL hstore string representation into the map. Accepts []byte or string source values. The expected format is: "key1"=>"value1", "key2"=>NULL

func (Hstore) Value

func (h Hstore) Value() (driver.Value, error)

Value serializes the Hstore to the PostgreSQL hstore literal format: "key1"=>"value1","key2"=>"value2". Keys with nil values are encoded as "key"=>NULL.

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

func (q *InsertQuery) Exec(ctx context.Context) (driver.Result, error)

Exec executes the 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}.

func (Int64Array) Value

func (a Int64Array) Value() (driver.Value, error)

Value serializes the int64 slice to the PostgreSQL array literal format: {1,2,3}.

type JSONMap

type JSONMap map[string]any

JSONMap is a map[string]any that implements database/sql/driver.Valuer and sql.Scanner for transparent serialization to/from PostgreSQL jsonb columns.

func (*JSONMap) Scan

func (m *JSONMap) Scan(src any) error

Scan unmarshals JSON data from the database into the map. Accepts []byte or string source values.

func (JSONMap) Value

func (m JSONMap) Value() (driver.Value, error)

Value marshals the map to JSON for storage.

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

func (l *Listener) Close() error

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

func (l *Listener) Listen(ctx context.Context, channel string) error

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

func (l *Listener) Notify(ctx context.Context, channel, payload string) error

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

func (l *Listener) Start(ctx context.Context) error

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.

func (*Listener) Unlisten

func (l *Listener) Unlisten(ctx context.Context, channel string) error

Unlisten unsubscribes from a PostgreSQL notification channel. The UNLISTEN statement is executed by the listen goroutine, which owns the dedicated connection. The listener must be started (via Start) before calling Unlisten.

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

func Unwrap(db *grove.DB) *PgDB

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

func (db *PgDB) AcquireConn(ctx context.Context) (driver.DedicatedConn, error)

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

func (db *PgDB) BeginTx(ctx context.Context, opts *driver.TxOptions) (driver.Tx, error)

BeginTx starts a new database transaction with the specified options.

func (*PgDB) BeginTxQuery

func (db *PgDB) BeginTxQuery(ctx context.Context, opts *driver.TxOptions) (*PgTx, error)

BeginTx starts a new transaction and returns a PgTx that exposes query builder methods operating within that transaction.

func (*PgDB) Close

func (db *PgDB) Close() error

Close terminates all connections in the pool.

func (*PgDB) Dialect

func (db *PgDB) Dialect() driver.Dialect

Dialect returns the PostgreSQL dialect.

func (*PgDB) Exec

func (db *PgDB) Exec(ctx context.Context, query string, args ...any) (driver.Result, error)

Exec executes a query that does not return rows (INSERT, UPDATE, DELETE, DDL) and returns a driver.Result.

func (*PgDB) GroveDelete

func (db *PgDB) GroveDelete(model any) any

GroveDelete is the adapter method for grove.DB.NewDelete().

func (*PgDB) GroveInsert

func (db *PgDB) GroveInsert(model any) any

GroveInsert is the adapter method for grove.DB.NewInsert().

func (*PgDB) GroveSelect

func (db *PgDB) GroveSelect(model ...any) any

GroveSelect is the adapter method for grove.DB.NewSelect().

func (*PgDB) GroveTx

func (db *PgDB) GroveTx(ctx context.Context, isolationLevel int, readOnly bool) (any, error)

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

func (db *PgDB) GroveUpdate(model any) any

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

func (db *PgDB) Name() string

Name returns the driver identifier.

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

func (db *PgDB) NewListener() *Listener

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

func (db *PgDB) NewRaw(query string, args ...any) *RawQuery

NewRaw creates a raw SQL query.

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

func (db *PgDB) Open(ctx context.Context, dsn string, opts ...driver.Option) error

Open parses the DSN, applies configuration options, creates the pgxpool connection pool, and verifies connectivity.

func (*PgDB) Ping

func (db *PgDB) Ping(ctx context.Context) error

Ping verifies that the database is reachable.

func (*PgDB) Prepare

func (db *PgDB) Prepare(ctx context.Context, query string) (driver.Stmt, error)

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

func (db *PgDB) Query(ctx context.Context, query string, args ...any) (driver.Rows, error)

Query executes a query that returns rows and wraps the result in a driver.Rows.

func (*PgDB) QueryRow

func (db *PgDB) QueryRow(ctx context.Context, query string, args ...any) driver.Row

QueryRow executes a query expected to return at most one row.

func (*PgDB) SetHooks

func (db *PgDB) SetHooks(engine *hook.Engine)

SetHooks attaches a hook engine for lifecycle hooks (pre/post query and mutation). If engine is nil, hooks are disabled.

func (*PgDB) SupportsCDC

func (db *PgDB) SupportsCDC() bool

SupportsCDC returns true because PostgreSQL supports logical replication for change data capture.

func (*PgDB) SupportsReturning

func (db *PgDB) SupportsReturning() bool

SupportsReturning returns true because PostgreSQL supports INSERT ... RETURNING.

func (*PgDB) SupportsStreaming

func (db *PgDB) SupportsStreaming() bool

SupportsStreaming returns true because PostgreSQL supports server-side cursors.

type PgDialect

type PgDialect struct{}

PgDialect implements driver.Dialect for PostgreSQL.

func (*PgDialect) AppendBytes

func (d *PgDialect) AppendBytes(b []byte, v []byte) []byte

AppendBytes appends a hex-encoded PostgreSQL bytea literal to b and returns the extended slice. The format is: '\x<hex>'

func (*PgDialect) AppendTime

func (d *PgDialect) AppendTime(b []byte, t time.Time) []byte

AppendTime appends a time value formatted as RFC3339Nano (wrapped in single quotes) to b and returns the extended slice.

func (*PgDialect) GoToDBType

func (d *PgDialect) GoToDBType(goType reflect.Type, opts schema.FieldOptions) string

GoToDBType maps a Go reflect.Type to the appropriate PostgreSQL column type string, taking field options into account.

Mapping rules (in order of precedence):

  1. If opts.SQLType is set, it is returned verbatim.
  2. bool -> "boolean"
  3. int, int32 -> "integer"
  4. int64 -> "bigint"
  5. int16 -> "smallint"
  6. int8 -> "smallint"
  7. float32 -> "real"
  8. float64 -> "double precision"
  9. string -> "text" (or "varchar(255)" if Unique is set)
  10. time.Time -> "timestamptz"
  11. *time.Time -> "timestamptz"
  12. []byte -> "bytea"
  13. map[string]any -> "jsonb"
  14. default -> "text"

func (*PgDialect) Name

func (d *PgDialect) Name() string

Name returns the dialect identifier.

func (*PgDialect) Placeholder

func (d *PgDialect) Placeholder(n int) string

Placeholder returns a positional parameter placeholder ($N) for PostgreSQL. n is 1-indexed.

func (*PgDialect) Quote

func (d *PgDialect) Quote(ident string) string

Quote wraps an identifier in double quotes, escaping any embedded double quotes by doubling them. This follows the standard PostgreSQL quoting convention for identifiers.

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

func (t *PgTx) Commit() error

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

func (t *PgTx) NewRaw(query string, args ...any) *RawQuery

NewRaw creates a raw SQL 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.

func (*PgTx) Rollback

func (t *PgTx) Rollback() error

Rollback rolls back the transaction. Safe to call after Commit.

type RawQuery

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

RawQuery executes arbitrary SQL with optional model scanning.

func (*RawQuery) Exec

func (q *RawQuery) Exec(ctx context.Context) (driver.Result, error)

Exec executes the raw query without returning rows.

func (*RawQuery) Scan

func (q *RawQuery) Scan(ctx context.Context, dest ...any) error

Scan executes the raw query and scans results into dest. dest can be:

  • *[]Model (slice pointer for multi-row)
  • *Model (struct pointer for single row)
  • scalar pointers (passed directly to row.Scan)

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

func (q *SelectQuery) Limit(n int) *SelectQuery

Limit sets LIMIT.

func (*SelectQuery) Offset

func (q *SelectQuery) Offset(n int) *SelectQuery

Offset sets OFFSET.

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

func (q *SelectQuery) Stream(ctx context.Context) (*stream.Stream[any], error)

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

func (q *SelectQuery) StreamBatch(ctx context.Context, fetchSize int) (*stream.Stream[any], error)

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"}.

func (StringArray) Value

func (a StringArray) Value() (driver.Value, error)

Value serializes the string slice to the PostgreSQL array literal format: {"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) Exec

func (q *UpdateQuery) Exec(ctx context.Context) (driver.Result, error)

Exec executes the UPDATE.

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.

Directories

Path Synopsis
Package pgmigrate provides a PostgreSQL-specific migration executor for the Grove migration system.
Package pgmigrate provides a PostgreSQL-specific migration executor for the Grove migration system.

Jump to

Keyboard shortcuts

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