dbtyp

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 3 Imported by: 0

README

dbtyp

CI Go Reference GitHub tag (latest by date) Go Report Card

dbtyp is a library that associates types with *sql.DB.

*dbtyp.DB[T] and *dbtyp.Tx[T] have the same methods as *sql.DB and *sql.Tx, but it can define different types with the same interface using generics.

Additionally, it can generate instances of restricted types *dbtyp.ExecQueryer[T], *dbtyp.Execer[T], and *dbtyp.Queryer[T].

Installation

go get github.com/winebarrel/dbtyp

Usage

package main

import (
	"database/sql"
	"fmt"

	"github.com/winebarrel/dbtyp"
	_ "modernc.org/sqlite"
)

type AliceDB struct{}
type BobDB struct{}

type MyDB interface {
	AliceDB | BobDB
}

func main() {
	aliceDB := openDB[AliceDB]()
	bobDB := openDB[BobDB]()

	// bob = alice // COMPILE ERROR!

	createTable(aliceDB, "foo")
	createTable(bobDB, "bar")

	procForAlice(aliceDB.ExecQueryer())
	// procForAlice(bob.ExecQueryer()) // COMPILE ERROR!

	procForBob(bobDB.Queryer())
	// procForBob(alice.Queryer()) // COMPILE ERROR!
}

func openDB[T MyDB]() *dbtyp.DB[T] {
	db, err := dbtyp.New2[T](sql.Open("sqlite", "file::memory:"))

	if err != nil {
		panic(err)
	}

	return db
}

func createTable[T MyDB](db *dbtyp.DB[T], name string) {
	_, err := db.Exec("create table " + name + " (id int)")

	if err != nil {
		panic(err)
	}
}

func procForAlice(eq *dbtyp.ExecQueryer[AliceDB]) {
	_, err := eq.Exec("insert into foo values (1)")
	if err != nil {
		panic(err)
	}

	var n int
	err = eq.QueryRow("select count(*) from foo").Scan(&n)
	if err != nil {
		panic(err)
	}
	fmt.Println("foo rows count:", n)
}

func procForBob(q *dbtyp.Queryer[BobDB]) {
	var n int
	err := q.QueryRow("select count(*) from bar").Scan(&n)
	if err != nil {
		panic(err)
	}
	fmt.Println("bar rows count:", n)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB added in v0.3.0

type DB[T any] struct {
	*sql.DB
}

func New

func New[T any](v *sql.DB) *DB[T]

func New2

func New2[T any](v *sql.DB, err error) (*DB[T], error)

func (*DB[T]) BeginT added in v0.3.0

func (v *DB[T]) BeginT() (*Tx[T], error)

func (*DB[T]) BeginTxT added in v0.3.0

func (v *DB[T]) BeginTxT(ctx context.Context, opts *sql.TxOptions) (*Tx[T], error)

func (*DB[T]) ExecQueryer added in v0.3.0

func (v *DB[T]) ExecQueryer() *ExecQueryer[T]

func (*DB[T]) Execer added in v0.3.0

func (v *DB[T]) Execer() *Execer[T]

func (*DB[T]) PrepareContextT added in v0.3.0

func (v *DB[T]) PrepareContextT(ctx context.Context, query string) (*Stmt[T], error)

func (*DB[T]) PrepareT added in v0.3.0

func (v *DB[T]) PrepareT(query string) (*Stmt[T], error)

func (*DB[T]) Queryer added in v0.3.0

func (v *DB[T]) Queryer() *Queryer[T]

type ExecQueryer added in v0.3.0

type ExecQueryer[T any] struct {
	// contains filtered or unexported fields
}

func (*ExecQueryer[T]) Exec added in v0.3.0

func (v *ExecQueryer[T]) Exec(query string, args ...any) (sql.Result, error)

func (*ExecQueryer[T]) ExecContext added in v0.3.0

func (v *ExecQueryer[T]) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

func (*ExecQueryer[T]) Execer added in v0.3.0

func (v *ExecQueryer[T]) Execer() *Execer[T]

func (*ExecQueryer[T]) Query added in v0.3.0

func (v *ExecQueryer[T]) Query(query string, args ...any) (*sql.Rows, error)

func (*ExecQueryer[T]) QueryContext added in v0.3.0

func (v *ExecQueryer[T]) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*ExecQueryer[T]) QueryRow added in v0.3.0

func (v *ExecQueryer[T]) QueryRow(query string, args ...any) *sql.Row

func (*ExecQueryer[T]) QueryRowContext added in v0.3.0

func (v *ExecQueryer[T]) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

func (*ExecQueryer[T]) Queryer added in v0.3.0

func (v *ExecQueryer[T]) Queryer() *Queryer[T]

type Execer added in v0.3.0

type Execer[T any] struct {
	// contains filtered or unexported fields
}

func (*Execer[T]) Exec added in v0.3.0

func (v *Execer[T]) Exec(query string, args ...any) (sql.Result, error)

func (*Execer[T]) ExecContext added in v0.3.0

func (v *Execer[T]) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

type Queryer added in v0.3.0

type Queryer[T any] struct {
	// contains filtered or unexported fields
}

func (*Queryer[T]) Query added in v0.3.0

func (v *Queryer[T]) Query(query string, args ...any) (*sql.Rows, error)

func (*Queryer[T]) QueryContext added in v0.3.0

func (v *Queryer[T]) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*Queryer[T]) QueryRow added in v0.3.0

func (v *Queryer[T]) QueryRow(query string, args ...any) *sql.Row

func (*Queryer[T]) QueryRowContext added in v0.3.0

func (v *Queryer[T]) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

type Stmt added in v0.3.0

type Stmt[T any] struct {
	*sql.Stmt
}

type Tx added in v0.3.0

type Tx[T any] struct {
	*sql.Tx
}

func (*Tx[T]) ExecQueryer added in v0.3.0

func (v *Tx[T]) ExecQueryer() *ExecQueryer[T]

func (*Tx[T]) Execer added in v0.3.0

func (v *Tx[T]) Execer() *Execer[T]

func (*Tx[T]) PrepareContextT added in v0.3.0

func (v *Tx[T]) PrepareContextT(ctx context.Context, query string) (*Stmt[T], error)

func (*Tx[T]) PrepareT added in v0.3.0

func (v *Tx[T]) PrepareT(query string) (*Stmt[T], error)

func (*Tx[T]) Queryer added in v0.3.0

func (v *Tx[T]) Queryer() *Queryer[T]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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