dbutil

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2023 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

Docs: https://pkg.go.dev/github.com/minetest-go/dbutil

License

Code: MIT

Documentation

Index

Examples

Constants

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

Variables

This section is empty.

Functions

This section is empty.

Types

type DBTx added in v1.0.4

type DBTx interface {
	Exec(query string, args ...any) (sql.Result, error)
	Prepare(query string) (*sql.Stmt, error)
	QueryRow(query string, args ...any) *sql.Row
	Query(query string, args ...any) (*sql.Rows, error)
}

type DBUtil added in v1.1.0

type DBUtil[E Selectable] struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"database/sql"
	"fmt"

	"github.com/minetest-go/dbutil"

	_ "github.com/mattn/go-sqlite3"
)

// database struct
type MyTable struct {
	PK *int64
	F1 int
}

// 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}
}

func main() {
	// setup
	db, err := sql.Open("sqlite3", "mydb")
	if err != nil {
		panic(err)
	}

	provider := func() *MyTable { return &MyTable{} }
	dbu := dbutil.New(db, dbutil.DialectSQLite, provider)

	// insert
	err = dbu.Insert(&MyTable{F1: 1})
	if err != nil {
		panic(err)
	}

	// select single
	// NOTE: %s is getting replaced by the db-native bind placeholder ($1 for postgres or ?1 for sqlite)
	res, err := dbu.Select("where f1 = %s", 1)
	if err != nil {
		panic(err)
	}
	fmt.Println(res)

	// count all
	count, err := dbu.Count("where true=true")
	if err != nil {
		panic(err)
	}
	fmt.Println(count)

	// update
	res.F1 = 3
	err = dbu.Update(res, "where f1 = %s", 2)
	if err != nil {
		panic(err)
	}

	// delete
	err = dbu.Delete("where f1 = %s", 3)
	if err != nil {
		panic(err)
	}
}

func New added in v1.1.0

func New[E Selectable](db DBTx, dialect SQLDialect, provider EntityProvider[E]) *DBUtil[E]

func (*DBUtil[E]) BindParam added in v1.1.0

func (dbu *DBUtil[E]) BindParam(i int) string

func (*DBUtil[E]) Count added in v1.1.0

func (dbu *DBUtil[E]) Count(constraints string, params ...any) (int, error)

func (*DBUtil[E]) Delete added in v1.1.0

func (dbu *DBUtil[E]) Delete(constraints string, params ...any) error

func (*DBUtil[E]) FormatBindParams added in v1.1.0

func (dbu *DBUtil[E]) FormatBindParams(str string, param_count int) string

func (*DBUtil[E]) Insert added in v1.1.0

func (dbu *DBUtil[E]) Insert(entity Insertable, additionalStmts ...string) error

func (*DBUtil[E]) InsertOrReplace added in v1.1.0

func (dbu *DBUtil[E]) InsertOrReplace(entity Insertable, additionalStmts ...string) error

func (*DBUtil[E]) InsertReturning added in v1.1.0

func (dbu *DBUtil[E]) InsertReturning(entity Insertable, retField string, retValue any) error

func (*DBUtil[E]) Select added in v1.1.0

func (dbu *DBUtil[E]) Select(constraints string, params ...any) (E, error)

func (*DBUtil[E]) SelectMulti added in v1.1.0

func (dbu *DBUtil[E]) SelectMulti(constraints string, params ...any) ([]E, error)

func (*DBUtil[E]) Update added in v1.1.0

func (dbu *DBUtil[E]) Update(entity Insertable, constraints string, params ...any) error

type Entity

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

type EntityProvider added in v1.1.0

type EntityProvider[E Selectable] func() E

type Insertable

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

type SQLDialect added in v1.1.0

type SQLDialect int
const (
	DialectSQLite SQLDialect = iota
	DialectPostgres
)

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