xsql

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package xsql provides utilities for working with SQL databases.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecOne

func ExecOne(
	ctx context.Context,
	x Executor,
	query string,
	args ...any,
) error

ExecOne executes a query and asserts that exactly one row was affected.

func PanicOnDeadlock

func PanicOnDeadlock(err error)

PanicOnDeadlock panics if err is a PostgreSQL deadlock_detected error (SQLSTATE 40P01) and the program is running under go test.

Deadlocks indicate a lock-ordering bug in the engine and should never occur.

In production the function is a no-op because the transaction is already aborted and the retry path recovers gracefully - crashing the process would cause more harm than the wasted round-trip.

func Transact

func Transact(
	ctx context.Context,
	db *sql.DB,
	fn func(context.Context, *sql.Tx) error,
) (err error)

Transact executes the given function within a transaction.

If fn returns an error the transaction is rolled back, otherwise it is committed.

func UUIDSeq

func UUIDSeq(seq iter.Seq[*uuidpb.UUID]) any

UUIDSeq collects UUIDs from an iterator into a []string containing the text representation of each UUID, suitable for use with PostgreSQL's ANY($n) operator.

func UUIDs

func UUIDs(ids ...*uuidpb.UUID) any

UUIDs returns a []string containing the text representation of each UUID, suitable for use with PostgreSQL's ANY($n) operator.

Types

type Executor

type Executor interface {
	Querier
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

Executor is the common interface for running queries and executing statements, it is implemented by both sql.DB and sql.Tx.

type Querier

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

Querier is the common interface for running queriesshared by both sql.DB and sql.Tx.

type ScannerValuer

type ScannerValuer interface {
	driver.Value
	sql.Scanner
}

ScannerValuer is an interface for values that can be stored in the database.

func Envelope

func Envelope(envelope *envelopepb.Envelope) ScannerValuer

Envelope returns a value that marshals and unmarshals an envelope to/from its binary representation as used in the database.

func UUID

func UUID(id *uuidpb.UUID) ScannerValuer

UUID returns a value that marshals and unmarshals a UUID to/from its binary representation as used in the database.

Jump to

Keyboard shortcuts

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