sql

package
v0.0.0-...-811544e Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTxClosed = pgx.ErrTxClosed

Functions

func GetStruct

func GetStruct[T any](ctx context.Context, p Querier, query string, args ...any) (T, error)

func IsUniqueError

func IsUniqueError(err error, opts ...Option) bool

func NewConnection

func NewConnection(config Config) (*pgxpool.Pool, error)

NewConnection creates a new connection pool with the given configurationand returns a pointer to the pool.

func NewQueryBuilder

func NewQueryBuilder() goqu.DialectWrapper

func SelectStruct

func SelectStruct[T any](ctx context.Context, p Querier, query string, args ...any) ([]T, error)

Types

type Acquirer

type Acquirer interface {
	Acquire(ctx context.Context) (*pgxpool.Conn, error)
}

type Config

type Config struct {
	DataSourceName  string        // PostgreSQL connection string
	MaxIdleConns    int           // Maximum number of idle connections in the pool
	MaxOpenConns    int           // Maximum number of open connections in the pool
	ConnMaxLifetime time.Duration // Maximum amount of time a connection can be reused
	ConnMaxIdleTime time.Duration // Maximum amount of time a connection can be idle
	BeforeConnect   func(context.Context, *pgx.ConnConfig) error
	AfterConnect    func(context.Context, *pgx.Conn) error
	Logger          *slog.Logger
	OmitArgs        bool
}

Config represents the configuration for establishing a connection pool.

type Connector

type Connector interface {
	Executor
	Pinger
}

type Database

type Database interface {
	Connector
	Close()
}

type Executor

type Executor interface {
	Begin(ctx context.Context) (*PgxTx, error)
	BeginFunc(ctx context.Context, f func(conn Tx) error) error
	Exec(ctx context.Context, query string, arguments ...any) (pgconn.CommandTag, error)
	Get(ctx context.Context, dst any, query string, args ...any) error
	Select(ctx context.Context, dest any, query string, args ...any) error
}

type Listener

type Listener interface {
	WaitForNotification(ctx context.Context) (*pgconn.Notification, error)
	Release()
}

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(l *slog.Logger, omitArgs bool) *Logger

func (*Logger) Log

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

type MockQuerier

type MockQuerier struct {
	mock.Mock
}

MockQuerier is an autogenerated mock type for the Querier type

func NewMockQuerier

func NewMockQuerier(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockQuerier

NewMockQuerier creates a new instance of MockQuerier. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockQuerier) EXPECT

func (_m *MockQuerier) EXPECT() *MockQuerier_Expecter

func (*MockQuerier) Query

func (_m *MockQuerier) Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)

Query provides a mock function with given fields: ctx, query, args

type MockQuerier_Expecter

type MockQuerier_Expecter struct {
	// contains filtered or unexported fields
}

func (*MockQuerier_Expecter) Query

func (_e *MockQuerier_Expecter) Query(ctx interface{}, query interface{}, args ...interface{}) *MockQuerier_Query_Call

Query is a helper method to define mock.On call

  • ctx context.Context
  • query string
  • args ...interface{}

type MockQuerier_Query_Call

type MockQuerier_Query_Call struct {
	*mock.Call
}

MockQuerier_Query_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Query'

func (*MockQuerier_Query_Call) Return

func (*MockQuerier_Query_Call) Run

func (_c *MockQuerier_Query_Call) Run(run func(ctx context.Context, query string, args ...interface{})) *MockQuerier_Query_Call

func (*MockQuerier_Query_Call) RunAndReturn

func (_c *MockQuerier_Query_Call) RunAndReturn(run func(context.Context, string, ...interface{}) (pgx.Rows, error)) *MockQuerier_Query_Call

type MockRows

type MockRows struct {
	mock.Mock
}

MockRows is an autogenerated mock type for the Rows type

func NewMockRows

func NewMockRows(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockRows

NewMockRows creates a new instance of MockRows. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockRows) Close

func (_m *MockRows) Close()

Close provides a mock function with given fields:

func (*MockRows) CommandTag

func (_m *MockRows) CommandTag() pgconn.CommandTag

CommandTag provides a mock function with given fields:

func (*MockRows) Conn

func (_m *MockRows) Conn() *pgx.Conn

Conn provides a mock function with given fields:

func (*MockRows) EXPECT

func (_m *MockRows) EXPECT() *MockRows_Expecter

func (*MockRows) Err

func (_m *MockRows) Err() error

Err provides a mock function with given fields:

func (*MockRows) FieldDescriptions

func (_m *MockRows) FieldDescriptions() []pgconn.FieldDescription

FieldDescriptions provides a mock function with given fields:

func (*MockRows) Next

func (_m *MockRows) Next() bool

Next provides a mock function with given fields:

func (*MockRows) RawValues

func (_m *MockRows) RawValues() [][]byte

RawValues provides a mock function with given fields:

func (*MockRows) Scan

func (_m *MockRows) Scan(dest ...interface{}) error

Scan provides a mock function with given fields: dest

func (*MockRows) Values

func (_m *MockRows) Values() ([]interface{}, error)

Values provides a mock function with given fields:

type MockRows_Close_Call

type MockRows_Close_Call struct {
	*mock.Call
}

MockRows_Close_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Close'

func (*MockRows_Close_Call) Return

func (*MockRows_Close_Call) Run

func (_c *MockRows_Close_Call) Run(run func()) *MockRows_Close_Call

func (*MockRows_Close_Call) RunAndReturn

func (_c *MockRows_Close_Call) RunAndReturn(run func()) *MockRows_Close_Call

type MockRows_CommandTag_Call

type MockRows_CommandTag_Call struct {
	*mock.Call
}

MockRows_CommandTag_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'CommandTag'

func (*MockRows_CommandTag_Call) Return

func (*MockRows_CommandTag_Call) Run

func (*MockRows_CommandTag_Call) RunAndReturn

type MockRows_Conn_Call

type MockRows_Conn_Call struct {
	*mock.Call
}

MockRows_Conn_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Conn'

func (*MockRows_Conn_Call) Return

func (_c *MockRows_Conn_Call) Return(_a0 *pgx.Conn) *MockRows_Conn_Call

func (*MockRows_Conn_Call) Run

func (_c *MockRows_Conn_Call) Run(run func()) *MockRows_Conn_Call

func (*MockRows_Conn_Call) RunAndReturn

func (_c *MockRows_Conn_Call) RunAndReturn(run func() *pgx.Conn) *MockRows_Conn_Call

type MockRows_Err_Call

type MockRows_Err_Call struct {
	*mock.Call
}

MockRows_Err_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Err'

func (*MockRows_Err_Call) Return

func (_c *MockRows_Err_Call) Return(_a0 error) *MockRows_Err_Call

func (*MockRows_Err_Call) Run

func (_c *MockRows_Err_Call) Run(run func()) *MockRows_Err_Call

func (*MockRows_Err_Call) RunAndReturn

func (_c *MockRows_Err_Call) RunAndReturn(run func() error) *MockRows_Err_Call

type MockRows_Expecter

type MockRows_Expecter struct {
	// contains filtered or unexported fields
}

func (*MockRows_Expecter) Close

Close is a helper method to define mock.On call

func (*MockRows_Expecter) CommandTag

func (_e *MockRows_Expecter) CommandTag() *MockRows_CommandTag_Call

CommandTag is a helper method to define mock.On call

func (*MockRows_Expecter) Conn

Conn is a helper method to define mock.On call

func (*MockRows_Expecter) Err

Err is a helper method to define mock.On call

func (*MockRows_Expecter) FieldDescriptions

func (_e *MockRows_Expecter) FieldDescriptions() *MockRows_FieldDescriptions_Call

FieldDescriptions is a helper method to define mock.On call

func (*MockRows_Expecter) Next

Next is a helper method to define mock.On call

func (*MockRows_Expecter) RawValues

func (_e *MockRows_Expecter) RawValues() *MockRows_RawValues_Call

RawValues is a helper method to define mock.On call

func (*MockRows_Expecter) Scan

func (_e *MockRows_Expecter) Scan(dest ...interface{}) *MockRows_Scan_Call

Scan is a helper method to define mock.On call

  • dest ...interface{}

func (*MockRows_Expecter) Values

Values is a helper method to define mock.On call

type MockRows_FieldDescriptions_Call

type MockRows_FieldDescriptions_Call struct {
	*mock.Call
}

MockRows_FieldDescriptions_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'FieldDescriptions'

func (*MockRows_FieldDescriptions_Call) Return

func (*MockRows_FieldDescriptions_Call) Run

func (*MockRows_FieldDescriptions_Call) RunAndReturn

type MockRows_Next_Call

type MockRows_Next_Call struct {
	*mock.Call
}

MockRows_Next_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Next'

func (*MockRows_Next_Call) Return

func (_c *MockRows_Next_Call) Return(_a0 bool) *MockRows_Next_Call

func (*MockRows_Next_Call) Run

func (_c *MockRows_Next_Call) Run(run func()) *MockRows_Next_Call

func (*MockRows_Next_Call) RunAndReturn

func (_c *MockRows_Next_Call) RunAndReturn(run func() bool) *MockRows_Next_Call

type MockRows_RawValues_Call

type MockRows_RawValues_Call struct {
	*mock.Call
}

MockRows_RawValues_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'RawValues'

func (*MockRows_RawValues_Call) Return

func (*MockRows_RawValues_Call) Run

func (_c *MockRows_RawValues_Call) Run(run func()) *MockRows_RawValues_Call

func (*MockRows_RawValues_Call) RunAndReturn

func (_c *MockRows_RawValues_Call) RunAndReturn(run func() [][]byte) *MockRows_RawValues_Call

type MockRows_Scan_Call

type MockRows_Scan_Call struct {
	*mock.Call
}

MockRows_Scan_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Scan'

func (*MockRows_Scan_Call) Return

func (_c *MockRows_Scan_Call) Return(_a0 error) *MockRows_Scan_Call

func (*MockRows_Scan_Call) Run

func (_c *MockRows_Scan_Call) Run(run func(dest ...interface{})) *MockRows_Scan_Call

func (*MockRows_Scan_Call) RunAndReturn

func (_c *MockRows_Scan_Call) RunAndReturn(run func(...interface{}) error) *MockRows_Scan_Call

type MockRows_Values_Call

type MockRows_Values_Call struct {
	*mock.Call
}

MockRows_Values_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Values'

func (*MockRows_Values_Call) Return

func (_c *MockRows_Values_Call) Return(_a0 []interface{}, _a1 error) *MockRows_Values_Call

func (*MockRows_Values_Call) Run

func (_c *MockRows_Values_Call) Run(run func()) *MockRows_Values_Call

func (*MockRows_Values_Call) RunAndReturn

func (_c *MockRows_Values_Call) RunAndReturn(run func() ([]interface{}, error)) *MockRows_Values_Call

type Option

type Option func(err error) bool

func WithConstraintName

func WithConstraintName(name string) Option

type PgxListener

type PgxListener struct {
	// contains filtered or unexported fields
}

func NewListener

func NewListener(ctx context.Context, db Acquirer, name string) (*PgxListener, error)

func (*PgxListener) Release

func (l *PgxListener) Release()

func (*PgxListener) WaitForNotification

func (l *PgxListener) WaitForNotification(ctx context.Context) (*pgconn.Notification, error)

type PgxPool

type PgxPool struct {
	*pgxpool.Pool
}

PgxPool is a PostgreSQL wrapper that implements the Executor interface.

func NewPgxPool

func NewPgxPool(pool *pgxpool.Pool) *PgxPool

func (*PgxPool) Begin

func (p *PgxPool) Begin(ctx context.Context) (*PgxTx, error)

func (*PgxPool) BeginFunc

func (p *PgxPool) BeginFunc(ctx context.Context, f func(conn Tx) error) error

BeginFunc starts a transaction and executes the given function within that transaction.

func (*PgxPool) BeginTx

func (p *PgxPool) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (*PgxTx, error)

func (*PgxPool) BeginTxFunc

func (p *PgxPool) BeginTxFunc(ctx context.Context, txOptions pgx.TxOptions, f func(conn Tx) error) error

BeginTxFunc starts a transaction and executes the given function within that transaction.

func (*PgxPool) Get

func (p *PgxPool) Get(ctx context.Context, dst any, query string, args ...any) error

Get fetches a single row from the database and stores the result in the given struct.

func (*PgxPool) Select

func (p *PgxPool) Select(ctx context.Context, dest any, query string, args ...any) error

Select fetches multiple rows from the database and stores the results in the given slice of structs.

type PgxTx

type PgxTx struct {
	pgx.Tx
}

PgxTx is a PostgreSQL transaction wrapper that implements the Executor interface.

func NewPgxTx

func NewPgxTx(tx pgx.Tx) *PgxTx

func (*PgxTx) Begin

func (p *PgxTx) Begin(ctx context.Context) (*PgxTx, error)

func (*PgxTx) BeginFunc

func (p *PgxTx) BeginFunc(ctx context.Context, f func(conn Tx) error) error

BeginFunc starts a nested transaction and executes the given function within that transaction.

func (*PgxTx) Get

func (p *PgxTx) Get(ctx context.Context, dst any, query string, args ...any) error

Get fetches a single row from the database within the transaction and stores the result in the given struct.

func (*PgxTx) Select

func (p *PgxTx) Select(ctx context.Context, dest any, query string, args ...any) error

Select fetches multiple rows from the database within the transaction and stores the results in the given slice of structs.

type Pinger

type Pinger interface {
	Ping(ctx context.Context) error
}

type Querier

type Querier interface {
	Query(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)
}

type Result

type Result interface {
	LastInsertId() (int64, error)
	RowsAffected() (int64, error)
}

type Transactor

type Transactor interface {
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

type Tx

type Tx interface {
	Executor
	Transactor
}

Jump to

Keyboard shortcuts

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