sqlize

package module
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2022 License: AGPL-3.0 Imports: 13 Imported by: 3

README

sqlize

Go Reference

Experimental tools to help with the use of Go sql.

This module is somewhat chaotic. It is primarily published as a dependency off another FOSS app project. However the sqlite has matured. And, by the way, it should combine quite well with sqlx.

Key features are:

  • Easily handle transactions with a "begin at most once" semantic. See sqlize.Xaction and stmt.Xaction.

  • Query building that hepls to keep queries in line with the DB model. See package bsq.

  • Supports using the above concepts with prepared statements.

Documentation

Overview

Example (SplitPara)
rd := strings.NewReader(`1 one line
1 next line
	
2 another line
   

3 third 1
3 third 2`)
scn := bufio.NewScanner(rd)
scn.Split(splitPara)
for scn.Scan() {
	fmt.Print(scn.Text())
	fmt.Println("¶")
}
Output:

1 one line
1 next line¶
2 another line¶
3 third 1
3 third 2¶

Index

Examples

Constants

View Source
const IDSeqMax32 = 4194303 // 0x3f_ffff
View Source
const IDSeqMax64 = 281474976710655 // 0xffff_ffffffff
View Source
const IDTypeMax32 = 511 // 0x1ff
View Source
const IDTypeMax64 = 32767 // 0x7fff
View Source
const NoID = 0

NoID is an ID that no DB record must have as an ID.

Variables

This section is empty.

Functions

func NotNoRows

func NotNoRows(err error) error

func RunScript

func RunScript(db Querier, rd io.Reader, ignoreErr bool) error

func RunScriptFile

func RunScriptFile(db Querier, file string, ignoreErr bool) error

func Xaction added in v0.2.0

func Xaction(conn Querier, recovr bool, do func(tx Tx) error) error

Xaction supports writing DB code that needs a "begin at most once" logic for transaction. If conn is already a Tx simply do() is called with that transaction and returns its result.

If conn is a DB a new Tx is started from DB and do() is called with the new transaction. If do() returns a non-nil error the new transcation is rolled back and the error will be returned. If do() panics the new transaction will also be rolled back. If recovr is true the panic will be recovered and returned as error. If recovr is false the panic will escape Xaction. If the rollback itself returns an error this will be returned as RollbackError where Cause represents the reason for the rollback. Without error and panic Xaction will commit the new transaction and return the result from Commit().

Sell also XactionContext

func XactionContext added in v0.2.0

func XactionContext(
	ctx context.Context,
	conn Querier,
	opts *sql.TxOptions,
	recovr bool,
	do func(tx Tx) error,
) error

XactionContext supports writing DB code that need a "begin at most once" logic for transaction. It allows to pass a Context and TxOptions. See also Xaction().

Types

type DB

type DB interface {
	Querier
	Close() error
	Begin() (*sql.Tx, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
	Ping() error
}

type DataSource added in v0.2.0

type DataSource struct {
	Name     string
	User     string
	Password string
	Log      bool
}

func (*DataSource) Flags added in v0.2.0

func (ds *DataSource) Flags()

type Entity added in v0.2.0

type Entity[ID EntityID] interface {
	EntityID() ID
	Zero() Entity[ID]
	IsZero() bool
}

type Entity32

type Entity32 = Entity[ID32]

type Entity64

type Entity64 = Entity[ID64]

type EntityBase added in v0.2.0

type EntityBase[E Entity[ID], ID EntityID] struct {
	// contains filtered or unexported fields
}

func (EntityBase[_, ID]) EntityID added in v0.2.0

func (eb EntityBase[_, ID]) EntityID() ID

func (*EntityBase[_, ID]) IDScanner added in v0.2.0

func (eb *EntityBase[_, ID]) IDScanner() sql.Scanner

TODO Find out how to do this with generics, not reflection

func (*EntityBase[E, ID]) SetReader added in v0.2.0

func (eb *EntityBase[E, ID]) SetReader(rd Reader[E, ID])

type EntityID added in v0.2.0

type EntityID interface {
	ID32 | ID64
}

type ID32

type ID32 int32

ID32 is the 32-bit ID type.

func MakeID32

func MakeID32(t TypeID, s uint32) (ID32, error)

MakeID32 creates an ID32 from the TypeID t and the sequence value s.

func MustMakeID32

func MustMakeID32(t TypeID, s uint32) ID32

MustMakeID32 panics if MakeID32 returns an error.

func NoRowsNoErr32

func NoRowsNoErr32(id ID32, err error) (ID32, error)

NoRowsNoErr32 returns NoID, nil when err is sql.ErrNoRows

func (*ID32) Scan

func (id *ID32) Scan(src interface{}) error

func (ID32) Type

func (id ID32) Type() TypeID

Type returns the TypeID from id.

func (ID32) Value

func (id ID32) Value() (driver.Value, error)

type ID64

type ID64 int64

ID64 is the 64-bit ID type.

func MakeID64

func MakeID64(t TypeID, s uint64) (ID64, error)

MakeID64 creates an ID64 from the TypeID t and the sequence value s.

func MustMakeID64

func MustMakeID64(t TypeID, s uint64) ID64

MustMakeID64 panics if MakeID64 returns an error.

func NoRowsNoErr64

func NoRowsNoErr64(id ID64, err error) (ID64, error)

NoRowsNoErr64 returns NoID, nil when err is sql.ErrNoRows

func (*ID64) Scan

func (id *ID64) Scan(src interface{}) error

func (ID64) Type

func (id ID64) Type() TypeID

Type returns the TypeID from id.

func (ID64) Value

func (id ID64) Value() (driver.Value, error)

type Querier added in v0.2.0

type Querier interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
	Prepare(query string) (*sql.Stmt, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

Querier allows to run or execute queries against a database or to prepare statements. It is used to drop the distinction between sql.DB and sql.Tx in application code. See also Xaction and XactionContext.

type Reader added in v0.2.0

type Reader[E Entity[ID], ID EntityID] interface {
	Read(q Querier, id ID, reuse E) (E, error)
}

type Ref added in v0.2.0

type Ref[E Entity[ID], ID EntityID] struct {
	// contains filtered or unexported fields
}

func RefEntity added in v0.2.0

func RefEntity[ID EntityID, E Entity[ID]](entity E) Ref[E, ID]

func RefID added in v0.2.0

func RefID[E Entity[ID], ID EntityID](r Reader[E, ID], id ID) Ref[E, ID]

func (*Ref[E, ID]) Get added in v0.2.0

func (ref *Ref[E, ID]) Get(db Querier) (entity E, err error)

func (Ref[E, ID]) ID added in v0.2.0

func (ref Ref[E, ID]) ID() ID

func (*Ref[E, ID]) Nil added in v0.2.0

func (ref *Ref[E, ID]) Nil() bool

func (*Ref[E, ID]) Resolved added in v0.2.0

func (ref *Ref[E, ID]) Resolved() (is bool, entity E)

func (*Ref[E, ID]) Scan added in v0.2.0

func (ref *Ref[E, ID]) Scan(src interface{}) error

TODO Find out how to do this with generics, not reflection

func (*Ref[E, ID]) Set added in v0.2.0

func (ref *Ref[E, ID]) Set(entity E)

func (*Ref[E, ID]) SetID added in v0.2.0

func (ref *Ref[E, ID]) SetID(r Reader[E, ID], id ID)

func (*Ref[E, ID]) SetReader added in v0.2.0

func (ref *Ref[E, ID]) SetReader(r Reader[E, ID])

func (*Ref[E, ID]) SetRef added in v0.2.0

func (ref *Ref[E, ID]) SetRef(r Ref[E, ID])

func (Ref[E, ID]) Value added in v0.2.0

func (ref Ref[E, ID]) Value() (driver.Value, error)

func (Ref[E, ID]) WithReader added in v0.2.0

func (ref Ref[E, ID]) WithReader(r Reader[E, ID]) (res Ref[E, ID])

type Repo added in v0.3.0

type Repo[E Entity[ID], ID EntityID] interface {
	Reader[E, ID]
	Create(q Querier, entity E) (E, error)
	Update(q Querier, entity E) error
	Delete(q Querier, entity E) error
}

type RepoConfig added in v0.3.2

type RepoConfig struct {
	ReadQuery   any
	CreateQuery any
	UpdateQuery any
	DeleteQuery any
}

type RollbackError

type RollbackError struct {
	Cause    error
	Rollback error
}

func (RollbackError) Error

func (ce RollbackError) Error() string

func (RollbackError) Unwrap

func (ce RollbackError) Unwrap() error

type Tx

type Tx interface {
	Querier
	Commit() error
	Rollback() error
	Stmt(stmt *sql.Stmt) *sql.Stmt
}

type TypeID

type TypeID = uint16

To support ploymorphism, IDs consist of a TypeID part and a sequence part. The number of bits for both parts depend on the number of bits in the ID type. Currently only ID32 and ID64, i.e. int32 and int64, are supported ID types. A valid type id must be > 0.

type TypeTable

type TypeTable struct {
	// contains filtered or unexported fields
}

func NewTypeTable

func NewTypeTable(conn Querier, table string) (res TypeTable, err error)

func (TypeTable) ForEach

func (idt TypeTable) ForEach(do func(tid TypeID, tag string) (done bool)) (hasDone bool)

func (TypeTable) ID

func (idt TypeTable) ID(tag string) TypeID

func (TypeTable) Tag

func (idt TypeTable) Tag(tid TypeID) string

Directories

Path Synopsis
bsq
Package bsq helps to build structured queries for SQL in a maintainable manner.
Package bsq helps to build structured queries for SQL in a maintainable manner.
keywords
Package keywords defines some handy SQL keywords as const strings for use with the bsq query builder.
Package keywords defines some handy SQL keywords as const strings for use with the bsq query builder.
cmd
bsq
cmd
Package null implements typed adapters for Go values to nullable DB columns.
Package null implements typed adapters for Go values to nullable DB columns.
Package postgres implements the PostgreSQL dialect for bsq.
Package postgres implements the PostgreSQL dialect for bsq.
Package qblogz provides a sqlmw driver interceptor that logs to a qblog Logger (see http://github.com/ngrok/sqlmw).
Package qblogz provides a sqlmw driver interceptor that logs to a qblog Logger (see http://github.com/ngrok/sqlmw).
Package sqlite3 implements the SQLite 3 dialect for bsq.
Package sqlite3 implements the SQLite 3 dialect for bsq.
Package stmt builds on top of package bsq to make programming with prepared statements and sqlize more convenient.
Package stmt builds on top of package bsq to make programming with prepared statements and sqlize more convenient.

Jump to

Keyboard shortcuts

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