Documentation
¶
Index ¶
- Constants
- type DBTx
- type DBUtil
- func (dbu *DBUtil[E]) BindParam(i int) string
- func (dbu *DBUtil[E]) Count(constraints string, params ...any) (int, error)
- func (dbu *DBUtil[E]) Delete(constraints string, params ...any) error
- func (dbu *DBUtil[E]) FormatBindParams(str string, param_count int) string
- func (dbu *DBUtil[E]) Insert(entity Insertable, additionalStmts ...string) error
- func (dbu *DBUtil[E]) InsertOrReplace(entity Insertable, additionalStmts ...string) error
- func (dbu *DBUtil[E]) InsertReturning(entity Insertable, retField string, retValue any) error
- func (dbu *DBUtil[E]) PrepareInsert(additionalStmts ...string) (func(Insertable) error, error)
- func (dbu *DBUtil[E]) Select(constraints string, params ...any) (E, error)
- func (dbu *DBUtil[E]) SelectMulti(constraints string, params ...any) ([]E, error)
- func (dbu *DBUtil[E]) Update(entity Insertable, constraints string, params ...any) error
- type Entity
- type EntityProvider
- type Insertable
- type SQLDialect
- type Selectable
Examples ¶
Constants ¶
View Source
const ( InsertAction = "insert" UpdateAction = "update" SelectAction = "select" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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)
}
}
Output:
func New ¶ added in v1.1.0
func New[E Selectable](db DBTx, dialect SQLDialect, provider EntityProvider[E]) *DBUtil[E]
func (*DBUtil[E]) FormatBindParams ¶ added in v1.1.0
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]) PrepareInsert ¶ added in v1.2.0
func (dbu *DBUtil[E]) PrepareInsert(additionalStmts ...string) (func(Insertable) error, error)
func (*DBUtil[E]) SelectMulti ¶ added in v1.1.0
type EntityProvider ¶ added in v1.1.0
type EntityProvider[E Selectable] func() E
type Insertable ¶
type SQLDialect ¶ added in v1.1.0
type SQLDialect int
const ( DialectSQLite SQLDialect = iota DialectPostgres )
Click to show internal directories.
Click to hide internal directories.