sqldb

package
v0.52.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package sqldb is used to interact with a datastore. It has functions to help set up a sql.DB as well as helpers for working with the sql.DB once it's initialized.

Index

Constants

View Source
const (
	// DBHostEnv is the database host environment variable name
	DBHostEnv string = "DB_HOST"
	// DBPortEnv is the database port environment variable name
	DBPortEnv string = "DB_PORT"
	// DBNameEnv is the database name environment variable name
	DBNameEnv string = "DB_NAME"
	// DBUserEnv is the database user environment variable name
	DBUserEnv string = "DB_USER"
	// DBPasswordEnv is the database user password environment variable name
	DBPasswordEnv string = "DB_PASSWORD"
	// DBSearchPathEnv is the database search path environment variable name
	DBSearchPathEnv string = "DB_SEARCH_PATH"
)

Variables

This section is empty.

Functions

func NewPostgreSQLPool

func NewPostgreSQLPool(ctx context.Context, lgr zerolog.Logger, dsn PostgreSQLDSN) (pool *pgxpool.Pool, close func(), err error)

NewPostgreSQLPool creates a new db pool and establishes a connection. In addition, returns a Close function to defer closing the pool.

Types

type DB

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

DB is a concrete implementation for a PostgreSQL database

func NewDB

func NewDB(pool *pgxpool.Pool) *DB

NewDB is an initializer for the DB struct

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context) (pgx.Tx, error)

BeginTx returns an acquired transaction from the db pool and adds app specific error handling

func (*DB) CommitTx

func (db *DB) CommitTx(ctx context.Context, tx pgx.Tx) error

CommitTx is a wrapper for sql.Tx.Commit in order to expose from the Datastore interface. Proper error handling is also considered.

func (*DB) Ping

func (db *DB) Ping(ctx context.Context) error

Ping pings the DB pool.

From pgx: "Ping acquires a connection from the Pool and executes an empty sql statement against it. If the sql returns without error, the database Ping is considered successful, otherwise, the error is returned."

func (*DB) RollbackTx

func (db *DB) RollbackTx(ctx context.Context, tx pgx.Tx, err error) error

RollbackTx is a wrapper for sql.Tx.Rollback in order to expose from the Datastore interface. Proper error handling is also considered.

func (*DB) ValidatePool

func (db *DB) ValidatePool(ctx context.Context, log zerolog.Logger) error

ValidatePool pings the database and logs the current user and database. ValidatePool is used for logging db status on startup.

type PostgreSQLDSN

type PostgreSQLDSN struct {
	Host       string
	Port       int
	DBName     string
	SearchPath string
	User       string
	Password   string
}

PostgreSQLDSN is a PostgreSQL datasource name

func (PostgreSQLDSN) ConnectionURI

func (dsn PostgreSQLDSN) ConnectionURI() string

ConnectionURI returns a formatted PostgreSQL datasource "Keyword/Value Connection String" The general form for a connection URI is: postgresql://[userspec@][hostspec][/dbname][?paramspec] where userspec is

user[:password]

and hostspec is:

[host][:port][,...]

and paramspec is:

name=value[&...]

The URI scheme designator can be either postgresql:// or postgres://. Each of the remaining URI parts is optional. The following examples illustrate valid URI syntax:

postgresql://
postgresql://localhost
postgresql://localhost:5433
postgresql://localhost/mydb
postgresql://user@localhost
postgresql://user:secret@localhost
postgresql://other@localhost/otherdb?connect_timeout=10&application_name=myapp
postgresql://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp

func (PostgreSQLDSN) KeywordValueConnectionString

func (dsn PostgreSQLDSN) KeywordValueConnectionString() string

KeywordValueConnectionString returns a formatted PostgreSQL datasource "Keyword/Value Connection String"

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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