dbutil

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2022 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Golang database utilities and helpers

Coverage Status

Example

Type definition

// database struct
type MyTable struct {
	PK *int64 // optional primary key (autoincremented)
	F1 int    // simple field
}

// entity
func (t *MyTable) Table() string {
	return "mytable"
}
func (t *MyTable) Columns(action string) []string {
	return []string{"pk", "f1"}
}

// selectable (implies deletable)
func (t *MyTable) Scan(action string, r func(dest ...any) error) error {
	return r(&t.PK, &t.F1)
}

// insertable
func (t *MyTable) Values(action string) []any {
	return []any{t.PK, t.F1}
}

Queries

// insert
dbutil.Insert(db, &MyTable{F1: 1})

// insert with return value
var retVal int64 = -1
dbutil.InsertReturning(db, &MyTable{F1: 2}, "pk", &retVal)

// select single
res, err := dbutil.Select(db, &MyTable{}, "where f1 = $1", 1)
//NOTE: err may be of type "sql.ErrNoRows" if no rows were found

// select multi
list, err := dbutil.SelectMulti(db, func() *MyTable { return &MyTable{} }, "where true=true")

// count
count, err := dbutil.Count(db, &MyTable{}, "where true=true")

// update (where f1 = 2)
err := dbutil.Update(db, tbl, "where f1 = $1", 2)

// delete
err := dbutil.Delete(db, &MyTable{}, "where f1 = $1", 3)

License

Code: MIT

Documentation

Index

Constants

View Source
const (
	InsertAction = "insert"
	UpdateAction = "update"
	SelectAction = "select"
)

Variables

This section is empty.

Functions

func Count

func Count[E Selectable](d *sql.DB, entity E, constraints string, params ...any) (int, error)

func Delete

func Delete[E Selectable](d *sql.DB, entity E, constraints string, params ...any) error

func Insert

func Insert(d *sql.DB, entity Insertable, additionalStmts ...string) error

func InsertReturning

func InsertReturning(d *sql.DB, entity Insertable, retField string, retValue any) error

func Select

func Select[E Selectable](d *sql.DB, entity E, constraints string, params ...any) (E, error)

func SelectMulti

func SelectMulti[E Selectable](d *sql.DB, p func() E, constraints string, params ...any) ([]E, error)

func Update

func Update(d *sql.DB, entity Insertable, constraints string, params ...any) error

Types

type Entity

type Entity interface {
	Columns(action string) []string
	Table() string
}

type Insertable

type Insertable interface {
	Entity
	Values(action string) []any
}

type Selectable

type Selectable interface {
	Entity
	Scan(action string, r func(dest ...any) error) error
}

Jump to

Keyboard shortcuts

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