zinc

package module
v0.0.0-...-a17aa27 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 13 Imported by: 0

README

ZINC - SQL based toolkit for Golang

Documentation

Index

Constants

View Source
const (
	StructPtr    = 1
	Map          = 2
	PrimitivePtr = 3
	SlicePtr     = 4
)

Variables

View Source
var (
	ErrInvalidDriver  = errors.New("invalid driver")
	ErrInvalidKind    = errors.New("invalid kind")
	ErrInvalidDest    = errors.New("invalid dest")
	ErrInvalidQuery   = errors.New("invalid query")
	ErrInvalidArg     = errors.New("invalid arg")
	ErrBind           = errors.New("bind error")
	ErrNoRows         = sql.ErrNoRows
	ErrCoerceDest     = errors.New("coerce dest error")
	ErrSetStructField = errors.New("set struct field error")
	ErrPostProcess    = errors.New("post process error")
)

Functions

func Exec

func Exec[R any](db *DB, q any, args ...any) (R, error)

func ForeachResult

func ForeachResult[E any](resultVal reflect.Value, f func(elem E) error) error

func QueryAll

func QueryAll[ROW any](db *DB, q any, args ...any) ([]ROW, error)

func QueryAssociated

func QueryAssociated[K comparable, ROW any](db *DB, aggregator func(ROW) (K, bool), q any, args ...any) (map[K]ROW, error)

func QueryGrouped

func QueryGrouped[K comparable, ROW any](db *DB, aggregator func(ROW) (K, bool), q any, args ...any) (map[K][]ROW, error)

func QueryOne

func QueryOne[ROW any](db *DB, q any, args ...any) (ROW, error)

func ToSql

func ToSql(q any, opts *Options) (string, error)

func Trans

func Trans[R any](db *DB, txOpts *sql.TxOptions, f func(tx *DB) (R, error)) (R, error)

Types

type Args

type Args []any

type ArgsProvider

type ArgsProvider interface {
	ProvideArgs(opts *Options) ([]any, error)
}

func StructArgs

func StructArgs(p any) ArgsProvider

type DB

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

func New

func New(driverName string, db *sql.DB, opts *Options) (*DB, error)

func Open

func Open(driverName string, dsn string, opts *Options) (*DB, error)

func (*DB) Begin

func (db *DB) Begin(txOpts *sql.TxOptions) (*DB, error)

func (*DB) Bind

func (db *DB) Bind(q string, unnamedArgs Args, namedArgs NamedArgs) (string, []any, error)

func (*DB) Close

func (db *DB) Close() error

func (*DB) Commit

func (db *DB) Commit() error

func (*DB) DB

func (db *DB) DB() *sql.DB

func (*DB) Dialect

func (db *DB) Dialect() Dialect

func (*DB) Exec

func (db *DB) Exec(dest any, q any, args ...any) error

func (*DB) ExecBatch

func (db *DB) ExecBatch(q any, batchedArgs any, prepare bool, optionalArgs ...any) error

func (*DB) Prepare

func (db *DB) Prepare(q string) (*DB, error)

func (*DB) QueryAll

func (db *DB) QueryAll(dest any, q any, args ...any) error

func (*DB) QueryOne

func (db *DB) QueryOne(dest any, q any, args ...any) error

func (*DB) Rollback

func (db *DB) Rollback() error

func (*DB) Trans

func (db *DB) Trans(txOpts *sql.TxOptions, f func(tx *DB) error) error

func (*DB) WithContext

func (db *DB) WithContext(ctx context.Context) *DB

type Dest

type Dest struct {
	Target any
	Kind   int
	Type   reflect.Type
}

type Dialect

type Dialect interface {
	DriverName() string
	QuoteString(s string, opts *Options) string
	NamingString(s string, opts *Options) string
	CompileNamedQuery(q string, opts *Options) (string, []string, error)
	NewDest(ci *sql.ColumnType, opts *Options) any
	CoerceDest(ci *sql.ColumnType, scannedVal reflect.Value, toType reflect.Type, opts *Options) (reflect.Value, error)
}

func DialectOf

func DialectOf(driverName string) Dialect

type Insert

type Insert struct {
	Options    string
	Into       any
	Partitions []string
	Columns    []string
	Values     []string
}

func (Insert) ToSql

func (node Insert) ToSql(p SqlPrinter, opts *Options)

type LogFormatter

type LogFormatter func(q string, args Args, namedArgs NamedArgs, err error, elapsed int64) string

type Logger

type Logger interface {
	LogQuery(ctx context.Context, msg string)
	LogQueryErr(ctx context.Context, msg string)
}
var StdLogger Logger = stdLogger{}

type Mapper

type Mapper func(src *Src, dest *Dest, opts *Options) error

type NameResolver

type NameResolver interface {
	ResolveTableName(structName string) string
	ResolveFieldName(structName, structFieldName string) string
}
var DefaultNameResolver NameResolver = defaultNameResolver{}

type NamedArg

type NamedArg = sql.NamedArg

func Named

func Named(name string, value any) NamedArg

type NamedArgs

type NamedArgs map[string]any

func StructToNamed

func StructToNamed(v any, resolver NameResolver) (NamedArgs, error)

type Options

type Options struct {
	// dialect
	Dialect      Dialect
	Mapper       Mapper
	NameResolver NameResolver
	TextCharset  string
	TimeFormat   string

	// log
	Logger           Logger
	LogFormatter     LogFormatter
	LogBound         bool
	LogSuccess       bool
	LogSlowThreshold int64
}

type PostCompleter

type PostCompleter[E any, FID comparable, FE any] struct {
	Disabled bool
	Collect  func(elem E, foreignIds []FID) []FID
	Query    func(foreignIds []FID) (map[FID]FE, error)
	Complete func(elem E, m map[FID]FE) error
}

func (PostCompleter[E, FID, FE]) If

func (c PostCompleter[E, FID, FE]) If(enabled bool) PostCompleter[E, FID, FE]

func (PostCompleter[E, FID, FE]) PostProcess

func (c PostCompleter[E, FID, FE]) PostProcess(resultVal reflect.Value) error

type PostModifier

type PostModifier[E any] struct {
	Disabled bool
	Modifier func(elem E) error
}

func PostModify

func PostModify[E any](f func(elem E) error) PostModifier[E]

func (PostModifier[E]) If

func (m PostModifier[E]) If(enabled bool) PostModifier[E]

func (PostModifier[E]) PostProcess

func (m PostModifier[E]) PostProcess(resultVal reflect.Value) error

type PostProcessor

type PostProcessor interface {
	PostProcess(resultVal reflect.Value) error
}

type PostProcessorFunc

type PostProcessorFunc func(resultVal reflect.Value) error

func (PostProcessorFunc) PostProcess

func (f PostProcessorFunc) PostProcess(resultVal reflect.Value) error

type Result

type Result struct {
	LastInsertId int64
	RowsAffected int64
}

type SliceHolder

type SliceHolder interface {
	SlicePtr() any
	Aggregate()
}

func AssociatedDest

func AssociatedDest[K comparable, V any](dest *map[K]V, aggregator func(V) (K, bool)) SliceHolder

func GroupedDest

func GroupedDest[K comparable, V any](dest *map[K][]V, aggregator func(V) (K, bool)) SliceHolder

type SqlPrinter

type SqlPrinter interface {
	EnableLeadingSpace() SqlPrinter
	DisableLeadingSpace() SqlPrinter
	WithLeadingSpace(enable bool, f func()) SqlPrinter

	Str(s string) SqlPrinter
	Strs(ss []string, sep string) SqlPrinter
	Quote(s string, opts *Options) SqlPrinter
	Naming(s string, opts *Options) SqlPrinter
	Quotes(ss []string, sep string, opts *Options) SqlPrinter
	Namings(ss []string, sep string, opts *Options) SqlPrinter
	Value(v any, opts *Options) SqlPrinter
	Values(values []any, sep string, opts *Options) SqlPrinter

	If(b bool, f func()) SqlPrinter
	Bracket(left, right string, f func()) SqlPrinter
}

type Sqlable

type Sqlable interface {
	ToSql(p SqlPrinter, opts *Options)
}

type Src

type Src struct {
	Rows        *sql.Rows
	Columns     []string
	ColumnTypes []*sql.ColumnType
}

func (*Src) NextResultSet

func (src *Src) NextResultSet() (bool, error)

type Struct

type Struct struct {
	Name   string         `json:"name"`
	Fields []*StructField `json:"fields"`
}

func ParseStruct

func ParseStruct(t reflect.Type) (*Struct, bool)

func (*Struct) ToTable

func (s *Struct) ToTable(resolver NameResolver) Table

type StructField

type StructField struct {
	Paths    []*StructFieldPath `json:"paths"`
	TypeName string             `json:"type"`
	Type     reflect.Type       `json:"-"`
	Tag      StructFieldTag     `json:"tag"`
}

type StructFieldPath

type StructFieldPath struct {
	Name         string `json:"name"`
	Index        int    `json:"index"`
	ColumnPrefix string `json:"column_prefix"`
}

type StructFieldTag

type StructFieldTag struct {
	DBTable string
	DBField string
	DBAuto  bool
	Columns []string
}

type Table

type Table interface {
	Name() string
	Fields() []TableField
	FieldNames(withoutAuto bool) []string
}

func ParseTable

func ParseTable(v any, opts *Options) Table

type TableField

type TableField interface {
	Name() string
	Auto() bool
}

type Tabular

type Tabular interface {
	ToTable(resolver NameResolver) Table
}

Jump to

Keyboard shortcuts

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