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
- func NewPostgreSQLPool(ctx context.Context, lgr zerolog.Logger, dsn PostgreSQLDSN) (pool *pgxpool.Pool, close func(), err error)
- type DB
- func (db *DB) BeginTx(ctx context.Context) (pgx.Tx, error)
- func (db *DB) CommitTx(ctx context.Context, tx pgx.Tx) error
- func (db *DB) Ping(ctx context.Context) error
- func (db *DB) RollbackTx(ctx context.Context, tx pgx.Tx, err error) error
- func (db *DB) ValidatePool(ctx context.Context, log zerolog.Logger) error
- type PostgreSQLDSN
Constants ¶
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 (*DB) BeginTx ¶
BeginTx returns an acquired transaction from the db pool and adds app specific error handling
func (*DB) CommitTx ¶
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 ¶
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 ¶
RollbackTx is a wrapper for sql.Tx.Rollback in order to expose from the Datastore interface. Proper error handling is also considered.
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"