sql

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: MPL-2.0 Imports: 28 Imported by: 0

Documentation

Overview

Package sql implements persistent storage using the postgres database.

Index

Constants

View Source
const (
	InsertAction = "INSERT"
	UpdateAction = "UPDATE"
	DeleteAction = "DELETE"
)
View Source
const TestDatabaseURL = "OTF_TEST_DATABASE_URL"

Variables

View Source
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 Bool added in v0.2.0

func Bool(b bool) pgtype.Bool

Bool converts a go-boolean into a postgres non-null boolean

func BoolPtr added in v0.2.0

func BoolPtr(s *bool) pgtype.Bool

BoolPtr converts a go-boolean pointer into a postgres nullable boolean

func Error

func Error(err error) error

func FromStringArray added in v0.3.0

func FromStringArray(pta []pgtype.Text) []string

FromStringArray converts a postgres text array into a go string slice.

func GetLimit added in v0.3.6

func GetLimit(opts resource.PageOptions) pgtype.Int4

GetLimit calculates the limit for use in SQL queries.

func GetOffset added in v0.3.6

func GetOffset(opts resource.PageOptions) pgtype.Int4

GetOffset calculates the offset for use in SQL queries.

func ID added in v0.3.6

func ID(s resource.ID) pgtype.Text

ID converts a resource ID into an ID suitable for postgres.

func IDPtr added in v0.3.6

func IDPtr(s *resource.ID) pgtype.Text

IDPtr converts a resource ID pointer into an ID suitable for postgres.

func Int4 added in v0.0.50

func Int4(s int) pgtype.Int4

Int4 converts a go-int into a postgres non-null int4

func Int4Ptr added in v0.0.50

func Int4Ptr(s *int) pgtype.Int4

Int4Ptr converts a go-int pointer into a postgres nullable int4

func Int8 added in v0.0.50

func Int8(s int) pgtype.Int8

Int8 converts a go-int into a postgres non-null int8

func Int8Ptr added in v0.0.50

func Int8Ptr(s *int) pgtype.Int8

Int8Ptr converts a go-int pointer into a postgres nullable int8

func NewTestDB

func NewTestDB(t *testing.T) string

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 NullString

func NullString() pgtype.Text

NullString returns a postgres null string

func String

func String(s string) pgtype.Text

String converts a go-string into a postgres non-null string

func StringArray added in v0.3.0

func StringArray(ss []string) []pgtype.Text

StringArray converts a go-string slice into a postgres text array

func StringPtr added in v0.0.50

func StringPtr(s *string) pgtype.Text

StringPtr converts a go-string pointer into a postgres nullable string

func Timestamptz

func Timestamptz(t time.Time) pgtype.Timestamptz

Timestamptz converts a go-time into a postgres non-null timestamptz

func TimestamptzPtr added in v0.1.0

func TimestamptzPtr(t *time.Time) pgtype.Timestamptz

TimestamptzPtr converts a go-time pointer into a postgres nullable timestamptz

func UUID

func UUID(s uuid.UUID) pgtype.UUID

UUID converts a google-go-uuid into a postgres non-null UUID

Types

type Action added in v0.2.0

type Action string

Action is the action that was carried out on a database table

type DB

type DB struct {
	*pgxpool.Pool // db connection pool
	logr.Logger
}

DB provides access to the postgres db as well as queries generated from SQL

func New

func New(ctx context.Context, logger logr.Logger, connString string) (*DB, error)

New migrates the database to the latest migration version, and then constructs and returns a connection pool.

func (*DB) Lock added in v0.0.51

func (db *DB) Lock(ctx context.Context, table string, fn func(context.Context, *sqlc.Queries) error) error

func (*DB) Querier added in v0.3.0

func (db *DB) Querier(ctx context.Context) *sqlc.Queries

Querier provides pre-generated queries

func (*DB) Tx

func (db *DB) Tx(ctx context.Context, callback func(context.Context, *sqlc.Queries) error) error

Tx provides the caller with a callback in which all operations are conducted within a transaction.

func (*DB) WaitAndLock

func (db *DB) 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.

type ForwardFunc added in v0.2.0

type ForwardFunc func(ctx context.Context, id string, action Action)

ForwardFunc handles forwarding the id and action onto subscribers.

type Listener added in v0.2.0

type Listener struct {
	logr.Logger
	// contains filtered or unexported fields
}

Listener listens for postgres events

func NewListener added in v0.2.0

func NewListener(logger logr.Logger, db pool) *Listener

func (*Listener) RegisterFunc added in v0.2.0

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.

func (*Listener) Start added in v0.2.0

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

Start the pubsub daemon; listen to notifications from postgres and forward to local pubsub broker. The listening channel is closed once the broker has started listening; from this point onwards published messages will be forwarded.

func (*Listener) Started added in v0.2.0

func (b *Listener) Started() <-chan struct{}

Directories

Path Synopsis
Package sqlc is mostly auto-generated by the sqlc tool
Package sqlc is mostly auto-generated by the sqlc tool

Jump to

Keyboard shortcuts

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