pgxs

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyConfig = fmt.Errorf("pxgs: PSQL Config is required")

ErrEmptyConfig is returned when the PSQL Config is empty.

Functions

func QuoteString

func QuoteString(str string) string

QuoteString removes single quotes (') and percentage signs (%) from the given string. It replaces single quotes and percentage signs with an empty string to prevent SQL injection issues. Returns the modified string.

Types

type Config

type Config struct {
	DataDir  string `json:"data_dir" yaml:"data_dir"`
	DbUri    string `json:"db_uri" yaml:"db_uri"`
	Host     string `json:"host" yaml:"host"`
	Port     string `json:"port" yaml:"port"`
	Name     string `json:"name" yaml:"name"`
	User     string `json:"user" yaml:"user"`
	Password string `json:"password" yaml:"password"`
}

Config is structure that stores data for connecting to a database

func (*Config) GetConnString

func (c *Config) GetConnString() string

GetConnString generates and returns a string based on the data in the config

type PickfighterRepo

type PickfighterRepo interface {
	GetPoolConfig() (*pgxpool.Config, error)
	GracefulShutdown()
	DeleteRecords(ctx context.Context, scheme string, tableName string) error
	ConnectDBPool(ctx context.Context) (*pgxpool.Pool, error)
	DebugLogSqlErr(q string, err error) error
	SanitizeString(s string) string
	GetPool() *pgxpool.Pool
	BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
}

type Repo

type Repo struct {
	Pool   *pgxpool.Pool `json:"-" yaml:"-"`
	Config *Config       `json:"-" yaml:"-"`
}

Repo is structure for interacting with the database, storing a pool, logger and config

func NewPool

func NewPool(ctx context.Context, conf *Config) (*Repo, error)

NewPool creates a new Repo with a configured PostgreSQL connection pool. It requires a context, a SugaredLogger, and a database configuration (Config). If the configuration is nil, it returns an error.

func (*Repo) BeginTx

func (db *Repo) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)

BeginTx starts a new transaction with the given options.

func (*Repo) ConnectDBPool

func (db *Repo) ConnectDBPool(ctx context.Context) (*pgxpool.Pool, error)

ConnectDBPool creates and returns a new pgxpool.Pool using the configured context and pgxpool.Config. It uses the GetPoolConfig method to obtain the configuration. Returns the created pgxpool.Pool and an error if there is any issue.

func (*Repo) DebugLogSqlErr

func (db *Repo) DebugLogSqlErr(q string, err error) error

DebugLogSqlErr logs debug information for SQL queries and errors. It takes the SQL query string 'q' and the error 'err' as parameters. If the error is a PostgreSQL "23505" violation, it sets the 'deuce' flag. It logs a debug message with the SQL query if the error is not of type pgx.ErrNoRows and not a "23505" violation.

func (*Repo) DeleteRecords

func (db *Repo) DeleteRecords(ctx context.Context, scheme string, tableName string) error

DeleteRecords deletes records from the table whose name is passed as an argument

func (*Repo) GetPool

func (db *Repo) GetPool() *pgxpool.Pool

GetPool returns the pgxpool.Pool stored in the Repo.

func (*Repo) GetPoolConfig

func (db *Repo) GetPoolConfig() (*pgxpool.Config, error)

GetPoolConfig retrieves the pgxpool.Config for the PostgreSQL connection pool. It parses the connection string from the Repo's configuration (Config) using pgxpool.ParseConfig. Returns the pgxpool.Config and an error if parsing fails.

func (*Repo) GracefulShutdown

func (db *Repo) GracefulShutdown()

GracefulShutdown checks whether the value is in the pool and if so, closes it and logs the message

func (*Repo) SanitizeString

func (db *Repo) SanitizeString(s string) string

SanitizeString removes single quotes (') and percentage signs (%) from the given string.

Directories

Path Synopsis
Package mock_pgxs is a generated GoMock package.
Package mock_pgxs is a generated GoMock package.

Jump to

Keyboard shortcuts

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