Go SQL database utilities leveraging generics.
This uses genfuncs for some Go generics and functional code.
import "gendb"
- func Exec(db *sql.DB, query string, args ...any) *genfuncs.Result[sql.Result]
- func ExecContext(db *sql.DB, ctx context.Context, query string, args ...any) *genfuncs.Result[sql.Result]
- func Query[T any](db *sql.DB, binder func(*T) []any, query string, args ...any) *genfuncs.Result[[]T]
- func QueryContext[T any](db *sql.DB, ctx context.Context, binder func(*T) []any, query string, args ...any) *genfuncs.Result[[]T]
- func QueryRow[T any](db *sql.DB, binder func(*T) []any, query string, args ...any) *genfuncs.Result[T]
- func QueryRowContext[T any](db *sql.DB, ctx context.Context, binder func(*T) []any, query string, args ...any) *genfuncs.Result[T]
- func SingleBinder[T any](t *T) []any
- type Nullable
- type SerDes
func Exec
func Exec(db *sql.DB, query string, args ...any) *genfuncs.Result[sql.Result]
Exec calls ExecContext with the default background context.
Example
package main
import (
"fmt"
"gendb"
"gendb/internal/sql_test"
)
func main() {
db := sql_test.CreateDB()
exec := gendb.Exec(db.OrEmpty(), "UPDATE student set program = ? WHERE code = '0001'", "CS")
count, _ := exec.OrEmpty().RowsAffected()
fmt.Println("Updated:", count)
}
Updated: 1
func ExecContext
func ExecContext(db *sql.DB, ctx context.Context, query string, args ...any) *genfuncs.Result[sql.Result]
ExecContext executes a query with arguments and returns a sql.Result summarizing the effect of the statement.
func Query
func Query[T any](db *sql.DB, binder func(*T) []any, query string, args ...any) *genfuncs.Result[[]T]
Query calls QueryContext with the default background context.
Example
package main
import (
"fmt"
"gendb"
"gendb/internal/sql_test"
)
func main() {
db := sql_test.CreateDB()
type studentProgram struct {
name string
program string
}
binder := func(s *studentProgram) []any { return []any{&s.name, &s.program} }
results := gendb.Query[studentProgram](
db.OrEmpty(),
binder,
"SELECT name, program FROM student")
for _, student := range results.OrEmpty() {
fmt.Println(student.name, student.program)
}
}
fred masters
barney PHD
func QueryContext
func QueryContext[T any](db *sql.DB, ctx context.Context, binder func(*T) []any, query string, args ...any) *genfuncs.Result[[]T]
QueryContext performs a query with arguments using a binder to assign the rows returned to a slice of results.
func QueryRow
func QueryRow[T any](db *sql.DB, binder func(*T) []any, query string, args ...any) *genfuncs.Result[T]
QueryRow calls QueryRowContext with the default Background context.
Example
package main
import (
"fmt"
"gendb"
"gendb/internal/sql_test"
)
func main() {
db := sql_test.CreateDB()
results := gendb.QueryRow[int](
db.OrEmpty(),
gendb.SingleBinder[int],
"SELECT count(*) FROM student")
fmt.Println("Count:", results.OrEmpty())
}
Count: 2
func QueryRowContext
func QueryRowContext[T any](db *sql.DB, ctx context.Context, binder func(*T) []any, query string, args ...any) *genfuncs.Result[T]
QueryRowContext performs a query with arguments using a binder to assign the first row returned to a single result. If multiple rows are returned the first one is used. If no rows are returned the error sql.ErrNoRows is returned.
func SingleBinder
func SingleBinder[T any](t *T) []any
SingleBinder is a binder for base types.
type Nullable
Nullable indicates type supports the database nullable concept.
type Nullable interface {
IsNull() bool
}
type SerDes
SerDes combines the interfaces needed to serialize and deserialize custom types to and from database.
type SerDes interface {
sql.Scanner
driver.Valuer
}
Generated by gomarkdoc