postgres

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2022 License: GPL-3.0 Imports: 7 Imported by: 0

README

postgres-golang

Documentation

Index

Constants

View Source
const (
	DefaultBatchItemsMaxSize = 500
	DefaultSchemaName        = "public"

	TimeLayoutISO = "2006-01-02 15:04:05"
)
View Source
const (
	Serializable    = TxIsoLevel("serializable")
	RepeatableRead  = TxIsoLevel("repeatable read")
	ReadCommitted   = TxIsoLevel("read committed")
	ReadUncommitted = TxIsoLevel("read uncommitted")
)

Transaction isolation levels

View Source
const (
	ReadWrite = TxAccessMode("read write")
	ReadOnly  = TxAccessMode("read only")
)

Transaction access modes

View Source
const (
	Deferrable    = TxDeferrableMode("deferrable")
	NotDeferrable = TxDeferrableMode("not deferrable")
)

Transaction deferrable modes

Variables

View Source
var ErrNoRows = pgx.ErrNoRows

Functions

This section is empty.

Types

type Batch

type Batch interface {
	SetItem(BatchItem)
	GetItems() []BatchItem
	Len() int
}

func NewBatch

func NewBatch() Batch

type BatchItem

type BatchItem interface {
	SetQuery(query string)
	SetArgs(args ...any)
	GetQuery() string
	GetArgs() []any
}

func NewBatchItem

func NewBatchItem() BatchItem

type Client

type Client interface {
	ClientReader
	ClientWriter
}

func New

func New(cfg *Config) (Client, error)

type ClientReader

type ClientReader interface {
	Reader
	Pool
}

func NewReader

func NewReader(cfg *Config) (ClientReader, error)

type ClientWriter

type ClientWriter interface {
	Writer
	Pool
	BeginTx(ctx context.Context, opts TxOptions) (Transaction, error)
}

func NewWriter

func NewWriter(cfg *Config) (ClientWriter, error)

type Config

type Config struct {
	Username          string
	Password          string
	DBName            string
	Host              string
	Port              int
	BatchItemsMaxSize int
}

type Conn

type Conn interface {
	Reader
	Writer
}

type PgxConn

type PgxConn interface {
	// Release returns c to the pool it was acquired from. Once Release has been called, other methods must not be called.
	// However, it is safe to call Release multiple times. Subsequent calls after the first will be ignored.
	Release()

	Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)

	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)

	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

	QueryFunc(ctx context.Context, sql string, args []any, scans []any, f func(pgx.QueryFuncRow) error) (pgconn.CommandTag, error)

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

	CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)

	Begin(ctx context.Context) (pgx.Tx, error)

	BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)

	BeginFunc(ctx context.Context, f func(pgx.Tx) error) error
	Ping(ctx context.Context) error
	Conn() *pgx.Conn
}

type Pool

type Pool interface {
	AcquireConn(ctx context.Context) (PgxConn, error)
	ClosePool()
}

type Reader

type Reader interface {
	QueryRow(ctx context.Context, query string, args ...any) (Row, error)
	QueryRows(ctx context.Context, conn PgxConn, query string, args ...any) (Rows, error)
	CountRows(ctx context.Context, query string, args ...any) (int, error)
	TruncateTables(ctx context.Context, tables []string) error
}

type Row

type Row interface {
	// Scan works the same as Rows. with the following exceptions. If no
	// rows were found it returns ErrNoRows. If multiple rows are returned it
	// ignores all but the first.
	Scan(dest ...any) error
}

type Rows

type Rows interface {
	// Close closes the rows, making the connection ready for use again. It is safe
	// to call Close after rows is already closed.
	Close()

	// Err returns any error that occurred while reading.
	Err() error

	// Next prepares the next row for reading. It returns true if there is another
	// row and false if no more rows are available. It automatically closes rows
	// when all rows are read.
	Next() bool

	// Scan reads the values from the current row into dest values positionally.
	// dest can include pointers to core types, values implementing the Scanner
	// interface, and nil. nil will skip the value entirely.
	Scan(dest ...any) error

	// Values returns the decoded row values.
	Values() ([]any, error)

	// RawValues returns the unparsed bytes of the row values. The returned [][]byte is only valid until the Next
	// call or the Rows is closed. However, the underlying byte data is safe to retain a reference to and mutate.
	RawValues() [][]byte
}

type Transaction

type Transaction interface {
	Reader
	Writer
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

type TxAccessMode

type TxAccessMode string

type TxDeferrableMode

type TxDeferrableMode string

type TxIsoLevel

type TxIsoLevel string

type TxOptions

type TxOptions struct {
	IsoLevel       TxIsoLevel
	AccessMode     TxAccessMode
	DeferrableMode TxDeferrableMode
}

type Writer

type Writer interface {
	InsertRow(ctx context.Context, query string, args ...interface{}) (uuid.UUID, error)
	InsertRowWithStringID(ctx context.Context, query string, args ...interface{}) (string, error)
	UpdateRowByID(ctx context.Context, sqlData map[string]interface{}, schemaName, tableName, id string) error
	SendBatch(ctx context.Context, batch Batch) error
	ExecuteQuery(ctx context.Context, query string, args ...interface{}) error
	TruncateTables(ctx context.Context, tables []string) error
}

Jump to

Keyboard shortcuts

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