Documentation
¶
Overview ¶
Package sql implements persistent storage using the postgres database.
Index ¶
- Constants
- Variables
- func Bool(b bool) pgtype.Bool
- func BoolPtr(s *bool) pgtype.Bool
- func Error(err error) error
- func Inet(address net.IP) net.IPNet
- func Int4(s int) pgtype.Int4
- func Int4Ptr(s *int) pgtype.Int4
- func Int8(s int) pgtype.Int8
- func Int8Ptr(s *int) pgtype.Int8
- func NewTestContainer(t TestingT) *postgres.PostgresContainer
- func NewTestDB(t TestingT) string
- func NoRowsInResultError(err error) bool
- func NullString() pgtype.Text
- func Query[T any](ctx context.Context, pool *Pool, ...) (T, error)
- func String(s string) pgtype.Text
- func StringPtr(s *string) pgtype.Text
- func Timestamptz(t time.Time) pgtype.Timestamptz
- func TimestamptzPtr(t *time.Time) pgtype.Timestamptz
- func Tx[T any](ctx context.Context, pool *Pool, ...) (T, error)
- func UUID(s uuid.UUID) pgtype.UUID
- type Action
- type ForwardFunc
- type Listener
- type Options
- type Pool
- func (p *Pool) Acquire(ctx context.Context) (*pgxpool.Conn, error)
- func (p *Pool) Close()
- func (p *Pool) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
- func (p *Pool) Lock(ctx context.Context, table string, ...) error
- func (p *Pool) Query(ctx context.Context, callback func(context.Context, pggen.Querier) error) error
- func (p *Pool) Tx(ctx context.Context, callback func(context.Context, pggen.Querier) error) error
- func (db *Pool) WaitAndLock(ctx context.Context, id int64, fn func(context.Context) error) (err error)
- type TestingT
Constants ¶
const ( InsertAction = "INSERT" UpdateAction = "UPDATE" DeleteAction = "DELETE" )
const TestDatabaseURL = "OTF_TEST_DATABASE_URL"
Variables ¶
var ErrSubscriptionTerminated = errors.New("broker terminated the subscription")
ErrSubscriptionTerminated is for use by subscribers to indicate that their subscription has been terminated by the broker.
Functions ¶
func NewTestContainer ¶ added in v0.10.0
func NewTestContainer(t TestingT) *postgres.PostgresContainer
NewTestContainer returns a new test container.
func NewTestDB ¶
NewTestDB creates a logical database in postgres for a test and returns a connection string for connecting to the database. The database is dropped upon test completion.
func NoRowsInResultError ¶
func Query ¶ added in v0.10.0
func Query[T any](ctx context.Context, pool *Pool, fn func(context.Context, pggen.Querier) (T, error)) (T, error)
Query obtains a connection for the pool, executes the given function, and returns the connection to the pool.
func Timestamptz ¶
func Timestamptz(t time.Time) pgtype.Timestamptz
Timestamptz converts a go-time into a postgres non-null timestamptz
func TimestamptzPtr ¶
func TimestamptzPtr(t *time.Time) pgtype.Timestamptz
TimestamptzPtr converts a go-time pointer into a postgres nullable timestamptz
Types ¶
type ForwardFunc ¶
ForwardFunc handles forwarding the id and action onto subscribers.
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Listener listens for postgres events
func NewListener ¶
func (*Listener) RegisterFunc ¶
func (b *Listener) RegisterFunc(table string, getter ForwardFunc)
RegisterFunc registers a function that is capable of converting database events for the given table into an OTF event.
type Pool ¶ added in v0.10.0
type Pool struct {
// contains filtered or unexported fields
}
Pool provides access to the postgres db as well as queries generated from SQL
func NewTestContainerPool ¶ added in v0.10.0
func NewTestContainerPool(t TestingT) (*postgres.PostgresContainer, *Pool)
NewTestContainerPool returns a new Pool that is connected to the returned PostgresContainer.
func TestContainerReset ¶ added in v0.10.0
func TestContainerReset(t TestingT, pg *postgres.PostgresContainer) *Pool
TestContainerReset resets the test container and provides a new pool that connects to it.
func (*Pool) Query ¶ added in v0.10.0
func (p *Pool) Query(ctx context.Context, callback func(context.Context, pggen.Querier) error) error
Query obtains a connection for the pool, executes the given function, and returns the connection to the pool.
func (*Pool) Tx ¶ added in v0.10.0
Tx provides the caller with a callback in which all operations are conducted within a transaction.
func (*Pool) WaitAndLock ¶ added in v0.10.0
func (db *Pool) WaitAndLock(ctx context.Context, id int64, fn func(context.Context) error) (err error)
WaitAndLock obtains an exclusive session-level advisory lock. If another session holds the lock with the given id then it'll wait until the other session releases the lock. The given fn is called once the lock is obtained and when the fn finishes the lock is released.