pgxer

package module
v0.0.0-...-904665a Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2021 License: MIT Imports: 8 Imported by: 0

README

PGXer

Purpose

pgxer is a thin wrapper around jackc/pgx that exposes the pgx 4.x API, but using interface types instead of concrete types, to make it easier to mock DB access at any level from the Pool downwards.

In v4, pgx provides a mix of interfaces (e.g. Rows) and concrete types (e.g. Pool), making it essentially impossible to write tests relying on a DB without accessing the DB, causing tests to be slow, hard to run concurrently, and require external setup.

pgxer uses identical public type names, but modifies the signatures of all public methods to take and return interfaces to support mocking all types; and includes a mock implementation for all of them.

Where public fields exists, each one is replaced by an equivalent methods pair, to support mocking: a getter and a fluent setter.

Application Use

For the simpler cases, just applying three changes should be sufficient, as the examples, converted from the original pgx examples, demonstrate:

  • Import paths:
    • import "github.com/jackc/pgx/v4" -> import pgx "github.com/fgm/pgxer"
    • import "github.com/jackc/pgx/v4/pgxpool" -> import "github.com/fgm/pgxer/pgxpool"
  • Concrete types and their pointers:
    • var conn *pgx.Conn -> var conn pgxer.Conn
    • var pool *pgxpool.Pool -> var pool pgxpool.Pool
  • Fields to equivalent getter/setter methods:
    • After poolConfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
    • poolConfig.ConnConfig.Logger = logger -> poolConfig.ConnConfig().SetLogger(logger)

Using the demos

  • Assumes a PostgreSQL instance available without a password as postgres://localhost/postgres, with the psql command available
  • Build the DB, the binaries, and install them as go executables:
    • make && make install
  • After you are finished using them, remove the demo databases:
    • make clean

Documentation

Index

Constants

View Source
const (
	LargeObjectModeWrite = LargeObjectMode(pgx.LargeObjectModeWrite)
	LargeObjectModeRead  = LargeObjectMode(pgx.LargeObjectModeRead)
)
View Source
const (
	LogLevelTrace = LogLevel(pgx.LogLevelTrace)
	LogLevelDebug = LogLevel(pgx.LogLevelDebug)
	LogLevelInfo  = LogLevel(pgx.LogLevelInfo)
	LogLevelWarn  = LogLevel(pgx.LogLevelWarn)
	LogLevelError = LogLevel(pgx.LogLevelError)
	LogLevelNone  = LogLevel(pgx.LogLevelNone)
)
View Source
const (
	ReadWrite = TxAccessMode(pgx.ReadWrite)
	ReadOnly  = TxAccessMode(pgx.ReadOnly)
)
View Source
const (
	Deferrable    = TxDeferrableMode(pgx.Deferrable)
	NotDeferrable = TxDeferrableMode(pgx.NotDeferrable)
)
View Source
const (
	Serializable    = TxIsoLevel(pgx.Serializable)
	RepeatableRead  = TxIsoLevel(pgx.RepeatableRead)
	ReadCommitted   = TxIsoLevel(pgx.ReadCommitted)
	ReadUncommitted = TxIsoLevel(pgx.ReadUncommitted)
)
View Source
const (
	TextFormatCode   = pgx.TextFormatCode
	BinaryFormatCode = pgx.BinaryFormatCode
)

Variables

View Source
var ErrInvalidLogLevel = basePgx.ErrInvalidLogLevel
View Source
var ErrNoRows = basePgx.ErrNoRows
View Source
var ErrTxClosed = pgx.ErrTxClosed
View Source
var ErrTxCommitRollback = pgx.ErrTxCommitRollback

Functions

func BuildStatementCacheFromBaseBuildStatementCache

func BuildStatementCacheFromBaseBuildStatementCache(func(conn *basePgconn.PgConn) baseStmtCache.Cache) func(conn pgconn.PgConn) stmtcache.Cache

func ScanRow

func ScanRow(connInfo *pgtype.ConnInfo, fieldDescriptions []pgproto3.FieldDescription, values [][]byte, dest ...interface{}) error

Types

type Batch

type Batch interface {
	Len() int
	Queue(query string, arguments ...interface{})
}

type BatchResults

type BatchResults interface {
	Close() error
	Exec() (pgconn.CommandTag, error)
	Query() (Rows, error)
	QueryRow() Row
}

type BuildStatementCacheFunc

type BuildStatementCacheFunc func(conn pgconn.PgConn) stmtcache.Cache

type ConcreteConn

type ConcreteConn basePgx.Conn

func (*ConcreteConn) Begin

func (c *ConcreteConn) Begin(ctx context.Context) (Tx, error)

func (*ConcreteConn) BeginFunc

func (c *ConcreteConn) BeginFunc(ctx context.Context, f func(Tx) error) (err error)

func (*ConcreteConn) BeginTx

func (c *ConcreteConn) BeginTx(ctx context.Context, txOptions TxOptions) (Tx, error)

func (*ConcreteConn) BeginTxFunc

func (c *ConcreteConn) BeginTxFunc(ctx context.Context, txOptions TxOptions, f func(Tx) error) (err error)

func (*ConcreteConn) Close

func (c *ConcreteConn) Close(ctx context.Context) error

func (*ConcreteConn) Config

func (c *ConcreteConn) Config() ConnConfig

func (*ConcreteConn) CopyFrom

func (c *ConcreteConn) CopyFrom(ctx context.Context, tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int64, error)

func (*ConcreteConn) Deallocate

func (c *ConcreteConn) Deallocate(ctx context.Context, name string) error

func (*ConcreteConn) Exec

func (c *ConcreteConn) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)

func (*ConcreteConn) IsClosed

func (c *ConcreteConn) IsClosed() bool

func (*ConcreteConn) PgConn

func (c *ConcreteConn) PgConn() pgconn.PgConn

func (*ConcreteConn) Ping

func (c *ConcreteConn) Ping(ctx context.Context) error

func (*ConcreteConn) Prepare

func (c *ConcreteConn) Prepare(ctx context.Context, name, sql string) (sd pgconn.StatementDescription, err error)

func (*ConcreteConn) Query

func (c *ConcreteConn) Query(ctx context.Context, sql string, args ...interface{}) (Rows, error)

func (*ConcreteConn) QueryFunc

func (c *ConcreteConn) QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error)

func (*ConcreteConn) QueryRow

func (c *ConcreteConn) QueryRow(ctx context.Context, sql string, args ...interface{}) Row

func (*ConcreteConn) SendBatch

func (c *ConcreteConn) SendBatch(ctx context.Context, b Batch) BatchResults

func (*ConcreteConn) StatementCache

func (c *ConcreteConn) StatementCache() stmtcache.Cache

func (*ConcreteConn) WaitForNotification

func (c *ConcreteConn) WaitForNotification(ctx context.Context) (pgconn.Notification, error)

type ConcreteConnConfig

type ConcreteConnConfig struct {
	*basePgx.ConnConfig
}

func (*ConcreteConnConfig) BuildStatementCache

func (cc *ConcreteConnConfig) BuildStatementCache() BuildStatementCacheFunc

func (*ConcreteConnConfig) Config

func (cc *ConcreteConnConfig) Config() pgconn.Config

func (*ConcreteConnConfig) ConnString

func (cc *ConcreteConnConfig) ConnString() string

func (*ConcreteConnConfig) Copy

func (cc *ConcreteConnConfig) Copy() ConnConfig

func (*ConcreteConnConfig) LogLevel

func (cc *ConcreteConnConfig) LogLevel() LogLevel

func (*ConcreteConnConfig) Logger

func (cc *ConcreteConnConfig) Logger() Logger

func (*ConcreteConnConfig) PreferSimpleProtocol

func (cc *ConcreteConnConfig) PreferSimpleProtocol() bool

func (*ConcreteConnConfig) SetBuildStatementCache

func (cc *ConcreteConnConfig) SetBuildStatementCache(BuildStatementCacheFunc) ConnConfig

func (*ConcreteConnConfig) SetConfig

func (cc *ConcreteConnConfig) SetConfig(config pgconn.Config) ConnConfig

func (*ConcreteConnConfig) SetLogLevel

func (cc *ConcreteConnConfig) SetLogLevel(LogLevel) ConnConfig

func (*ConcreteConnConfig) SetLogger

func (cc *ConcreteConnConfig) SetLogger(Logger) ConnConfig

func (*ConcreteConnConfig) SetPreferSimpleProtocol

func (cc *ConcreteConnConfig) SetPreferSimpleProtocol(bool) ConnConfig

type ConcreteRow

type ConcreteRow struct {
	pgx.Row
}

ConcreteRow method Scan is not needed: the base implementation is directly usable.

type ConcreteRows

type ConcreteRows struct {
	pgx.Rows
}

ConcreteRows methods are not needed except for CommandTag: the base implementation is directly usable.

func (ConcreteRows) CommandTag

func (c ConcreteRows) CommandTag() pgconn.CommandTag

type Conn

type Conn interface {
	Begin(ctx context.Context) (Tx, error)
	BeginFunc(ctx context.Context, f func(Tx) error) (err error)
	BeginTx(ctx context.Context, txOptions TxOptions) (Tx, error)
	BeginTxFunc(ctx context.Context, txOptions TxOptions, f func(Tx) error) (err error)
	Close(ctx context.Context) error
	Config() ConnConfig
	CopyFrom(ctx context.Context, tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int64, error)
	Deallocate(ctx context.Context, name string) error
	Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error)
	IsClosed() bool
	PgConn() pgconn.PgConn
	Ping(ctx context.Context) error
	Prepare(ctx context.Context, name, sql string) (sd pgconn.StatementDescription, err error)
	Query(ctx context.Context, sql string, args ...interface{}) (Rows, error)
	QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error)
	QueryRow(ctx context.Context, sql string, args ...interface{}) Row
	SendBatch(ctx context.Context, b Batch) BatchResults
	StatementCache() stmtcache.Cache
	WaitForNotification(ctx context.Context) (pgconn.Notification, error)
}

func Connect

func Connect(ctx context.Context, connString string) (Conn, error)

func ConnectConfig

func ConnectConfig(ctx context.Context, connConfig ConnConfig) (Conn, error)

type ConnConfig

type ConnConfig interface {
	Config() pgconn.Config
	SetConfig(config pgconn.Config) ConnConfig

	BuildStatementCache() BuildStatementCacheFunc
	Logger() Logger
	LogLevel() LogLevel
	PreferSimpleProtocol() bool

	SetBuildStatementCache(BuildStatementCacheFunc) ConnConfig
	SetLogger(Logger) ConnConfig
	SetLogLevel(LogLevel) ConnConfig
	SetPreferSimpleProtocol(bool) ConnConfig

	ConnString() string
	Copy() ConnConfig
}

func ParseConfig

func ParseConfig(connString string) (ConnConfig, error)

type CopyFromSource

type CopyFromSource interface {
	Err() error
	Next() bool
	Values() ([]interface{}, error)
}

func CopyFromRows

func CopyFromRows(rows [][]interface{}) CopyFromSource

func CopyFromSlice

func CopyFromSlice(length int, next func(int) ([]interface{}, error)) CopyFromSource

type Identifier

type Identifier = basePgx.Identifier

type LargeObject

type LargeObject interface {
	Close() error
	Read(p []byte) (int, error)
	Seek(offset int64, whence int) (n int64, err error)
	Tell() (n int64, err error)
	Truncate(size int64) (err error)
	Write(p []byte) (int, error)
}

type LargeObjectMode

type LargeObjectMode = pgx.LargeObjectMode

type LargeObjects

type LargeObjects interface {
	Create(ctx context.Context, oid uint32) (uint32, error)
	Open(ctx context.Context, oid uint32, mode LargeObjectMode) (*LargeObject, error)
	Unlink(ctx context.Context, oid uint32) error
}

type LogLevel

type LogLevel = pgx.LogLevel

func LogLevelFromString

func LogLevelFromString(s string) (LogLevel, error)

type Logger

type Logger interface {
	Log(ctx context.Context, level LogLevel, msg string, data map[string]interface{})
}

type QueryFuncRow

type QueryFuncRow interface {
	FieldDescriptions() []pgproto3.FieldDescription
	RawValues() [][]byte
}

type QueryResultFormats

type QueryResultFormats = basePgx.QueryResultFormats

type QueryResultFormatsByOID

type QueryResultFormatsByOID = basePgx.QueryResultFormatsByOID

type QuerySimpleProtocol

type QuerySimpleProtocol = basePgx.QuerySimpleProtocol

type Row

type Row interface {
	Scan(dest ...interface{}) error
}

type Rows

type Rows interface {
	Close()
	CommandTag() pgconn.CommandTag
	Err() error
	FieldDescriptions() []pgproto3.FieldDescription
	Next() bool
	RawValues() [][]byte
	Scan(dest ...interface{}) error
	Values() ([]interface{}, error)
}

type ScanArgError

type ScanArgError interface {
	ColumnIndex() int
	Err() error

	SetColumnIndex(int) ScanArgError
	SetErr(error) ScanArgError

	Error() string
	Unwrap() error
}

type SerializationError

type SerializationError = pgx.SerializationError

type Tx

type Tx interface {
	Begin(ctx context.Context) (Tx, error)
	BeginFunc(ctx context.Context, f func(Tx) error) (err error)
	Commit(ctx context.Context) error
	Conn() *Conn
	CopyFrom(ctx context.Context, tableName Identifier, columnNames []string, rowSrc CopyFromSource) (int64, error)
	Exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error)
	LargeObjects() LargeObjects
	Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error)
	Query(ctx context.Context, sql string, args ...interface{}) (Rows, error)
	QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(QueryFuncRow) error) (pgconn.CommandTag, error)
	QueryRow(ctx context.Context, sql string, args ...interface{}) Row
	Rollback(ctx context.Context) error
	SendBatch(ctx context.Context, b *Batch) BatchResults
}

type TxAccessMode

type TxAccessMode = pgx.TxAccessMode

type TxDeferrableMode

type TxDeferrableMode = pgx.TxDeferrableMode

type TxIsoLevel

type TxIsoLevel = pgx.TxIsoLevel

type TxOptions

type TxOptions interface {
	AccessMode() TxAccessMode
	DeferrableMode() TxDeferrableMode
	IsoLevel() TxIsoLevel

	SetAccessMode(TxAccessMode) TxOptions
	SetDeferrableMode(TxDeferrableMode) TxOptions
	SetIsoLevel(TxIsoLevel) TxOptions
}

Directories

Path Synopsis
examples
log
Package stdlib in pgx implements the database/sql/driver SPI.
Package stdlib in pgx implements the database/sql/driver SPI.

Jump to

Keyboard shortcuts

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