database

package
v1.0.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const KindStruct = reflect.Struct

Variables

This section is empty.

Functions

func Drivers

func Drivers() []string

Drivers returns the sorted list of registered driver names.

func NewStruct

func NewStruct(typ reflect.Type) reflect.Value

func Register

func Register(name string, dbType DbType)

Register associates a driver name with a DbType. It is intended to be called from a backend package's init function. Registering the same name twice is a no-op; registering a name that maps to a different DbType panics, since that indicates a programming error rather than a runtime condition.

func ScanRows

func ScanRows[T any](rows *sql.Rows, schema *CachedSchema) ([]T, error)

ScanRows reads every row into a T and returns them.

It is shared by the SQL backends so that column mapping and value materialisation live in exactly one place.

T may be either the struct type (User) or a pointer to it (*User); both are handled. Columns returned by the query that are absent from the schema are discarded.

func TypeOf

func TypeOf(v any) reflect.Type

func TypeOfT

func TypeOfT[T any]() reflect.Type

TypeOfT returns the struct type of T, dereferencing pointer types. It is the type-safe counterpart of TypeOf for generic code paths.

func UnwrapValueOf

func UnwrapValueOf(v any) reflect.Value

Types

type CachedSchema

type CachedSchema struct {
	Type       reflect.Type
	Fields     []FieldInfo
	Insertable []FieldInfo
	PKIndex    int
	FieldMap   map[string]FieldInfo

	InsertColsMysql  string
	InsertPlaceMysql string
	InsertColsPG     string
	InsertPlacePG    string
}

func GetOrBuildSchema

func GetOrBuildSchema(typ reflect.Type) *CachedSchema

func SchemaOfT

func SchemaOfT[T any]() *CachedSchema

SchemaOfT returns the cached schema for T, building it on first use.

func (*CachedSchema) BuildInsertQueryMysql

func (s *CachedSchema) BuildInsertQueryMysql(tableName string) string

func (*CachedSchema) BuildInsertQueryPG

func (s *CachedSchema) BuildInsertQueryPG(tableName string) string

func (*CachedSchema) BuildWhereClause

func (s *CachedSchema) BuildWhereClause(val reflect.Value, whereFields []string, paramStyle string) (string, []any, error)

func (*CachedSchema) ExtractValues

func (s *CachedSchema) ExtractValues(val reflect.Value) []any

type Config

type Config struct {
	Type       string
	NullAble   bool
	Identity   bool
	PrimaryKey bool
	Unique     bool
}

type Database

type Database[T any] interface {
	Create(string, map[string]Config) error
	GetTable(string, T) (Table[T], error)
	DeleteTable(string) error
}

type DbType

type DbType int
const (
	MySQL DbType = iota
	PostgreSQL
	SQLite
	Redis
	Memory
	Bloom
	Mixture
)

func Lookup

func Lookup(name string) (DbType, bool)

Lookup resolves a driver name to its DbType.

type FieldInfo

type FieldInfo struct {
	Index       int
	ColumnName  string
	IsAutoInc   bool
	IsNullable  bool
	IsPrimary   bool
	IsUnique    bool
	GoFieldName string
	GoKind      reflect.Kind
}

type FieldNotFoundError

type FieldNotFoundError struct {
	Name string
}

func (*FieldNotFoundError) Error

func (e *FieldNotFoundError) Error() string

type ScanTypeError

type ScanTypeError struct {
	Got reflect.Type
}

ScanTypeError reports that rows could not be materialised into T.

func (*ScanTypeError) Error

func (e *ScanTypeError) Error() string

type Table

type Table[T any] interface {
	Ins(val T, ttl time.Duration) error
	Get(val T, whereFields []string, ttl time.Duration) ([]T, error)
	Set(val T, whereFields []string, ttl time.Duration) error
	Del(val T, whereFields []string, ttl time.Duration) error
}

Directories

Path Synopsis
Package factory constructs a database backend by name or by type.
Package factory constructs a database backend by name or by type.

Jump to

Keyboard shortcuts

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