db

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: Apache-2.0 Imports: 12 Imported by: 1

README

db

A small collection of database utilities that make life easier for database use and testing.

import (
	"database/sql"

    "github.com/harrybrwn/db"
)

func main() {
    pool, err := sql.Open("postgres", "host=localhost password=testlab")
    if err != nil {
        panic(err)
    }
    db := db.New(pool, db.WithLogger(slog.Default()))
    // ...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDBTimeout = errors.New("database ping timeout")
)

Functions

func New

func New(pool *sql.DB, opts ...Option) *database

func ScanOne

func ScanOne(r Rows, dest ...any) (err error)

Types

type Config

type Config struct {
	Type     Type
	Host     string
	Port     string
	User     string
	Password string
	DBName   string
	// Query options
	SSLMode        string
	SSLCA          string
	SSLCert        string
	SSLKey         string
	SSLSNI         string
	ConnectTimeout uint64
}

Config holds database connection config info.

func (*Config) Init

func (db *Config) Init()

func (*Config) URI

func (db *Config) URI() *url.URL

type DB

type DB interface {
	io.Closer
	QueryContext(context.Context, string, ...any) (Rows, error)
	ExecContext(context.Context, string, ...any) (sql.Result, error)
}

type Option

type Option func(*dbOptions)

func WithLogger

func WithLogger(l *slog.Logger) Option

type Pingable

type Pingable interface {
	Ping() error
	PingContext(context.Context) error
}

type Rows

type Rows interface {
	Scanner
	io.Closer
	Next() bool
	Err() error
}

type Scanner

type Scanner interface {
	Scan(...any) error
}

type StmtPreparor

type StmtPreparor interface {
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

StmtPreparor is an abstract type that should be able to prepare database statements.

type TxBeginor

type TxBeginor interface {
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

TxBeginor is an abstract type that should be able to begin database transactions.

type Type

type Type string
const (
	PostgresDBType Type = "postgres"
	MySQLDBType    Type = "mysql"
)

Directories

Path Synopsis
Package mockdb is a generated GoMock package.
Package mockdb is a generated GoMock package.
Package mockrows is a generated GoMock package.
Package mockrows is a generated GoMock package.
Package mocktx is a generated GoMock package.
Package mocktx is a generated GoMock package.

Jump to

Keyboard shortcuts

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