sql

package
v0.1.17 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2023 License: MPL-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package sql implements persistent storage using the postgres database.

Index

Constants

View Source
const TestDatabaseURL = "OTF_TEST_DATABASE_URL"

Variables

This section is empty.

Functions

func Error

func Error(err error) error

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 JSON

func JSON(b []byte) pgtype.JSON

JSON converts a []byte into a postgres JSON type

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 NoRowsInResultError added in v0.0.51

func NoRowsInResultError(err error) bool

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 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 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, opts Options) (*DB, error)

New constructs a new DB connection pool, and migrates the schema to the latest version.

func (*DB) Conn added in v0.0.51

func (db *DB) Conn(ctx context.Context) *pggen.DBQuerier

Conn provides pre-generated queries

func (*DB) Exec

func (db *DB) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)

Exec acquires a connection from the pool and executes the given SQL. If the context contains a transaction then that is used.

func (*DB) Lock added in v0.0.51

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

func (*DB) QueryRow added in v0.0.53

func (db *DB) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

func (*DB) SendBatch added in v0.1.8

func (db *DB) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults

func (*DB) Tx

func (db *DB) Tx(ctx context.Context, callback func(context.Context, pggen.Querier) 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 Options

type Options struct {
	Logger     logr.Logger
	ConnString string
}

Options for constructing a DB

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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