specifications

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: Apache-2.0 Imports: 23 Imported by: 18

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SELECT    = []byte("SELECT")
	FROM      = []byte("FROM")
	WHERE     = []byte("WHERE")
	INSERT    = []byte("INSERT")
	UPDATE    = []byte("UPDATE")
	DELETE    = []byte("DELETE")
	SPACE     = []byte(" ")
	INTO      = []byte("INTO")
	AND       = []byte("AND")
	SET       = []byte("SET")
	EQ        = []byte("=")
	PLUS      = []byte("+")
	AT        = []byte("@")
	LB        = []byte("(")
	RB        = []byte(")")
	COMMA     = []byte(", ")
	DOT       = []byte(".")
	STAR      = []byte("*")
	AS        = []byte("AS")
	VALUES    = []byte("VALUES")
	EXISTS    = []byte("EXISTS")
	COUNT     = []byte("COUNT")
	NOT       = []byte("NOT")
	CONFLICT  = []byte("CONFLICT")
	ON        = []byte("ON")
	RETURNING = []byte("RETURNING")
	DO        = []byte("DO")
	NOTHING   = []byte("NOTHING")
	ORDER     = []byte("ORDER")
	BY        = []byte("BY")
	DESC      = []byte("DESC")
	GROUP     = []byte("GROUP")
	HAVING    = []byte("HAVING")
	OFFSET    = []byte("OFFSET")
	LIMIT     = []byte("LIMIT")
)

Functions

func AsFloat

func AsFloat(e any) (n float64, ok bool)

func AsInt

func AsInt(e any) (n int64, ok bool)

func DictSet

func DictSet(key string, value ...string)

func Instance

func Instance[T any]() (v T)

func MaybeTable

func MaybeTable(e any) (ok bool)

func MaybeView

func MaybeView(e any) (ok bool)

func NewBasicValueWriter

func NewBasicValueWriter(rt reflect.Type) (vw ValueWriter, ct ColumnTypeName, err error)

func RegisterDialect

func RegisterDialect(dialect Dialect)

func ScanRows

func ScanRows[T any](ctx context.Context, rows sql.Rows, fields []string) (entries []T, err error)

func TrySetupAuditCreation

func TrySetupAuditCreation[T any](ctx context.Context, spec *Specification, entries []T) (err error)

func TrySetupAuditDeletion

func TrySetupAuditDeletion[T any](ctx context.Context, spec *Specification, entries []T) (err error)

func TrySetupAuditModification

func TrySetupAuditModification[T any](ctx context.Context, spec *Specification, entries []T) (err error)

func TrySetupAuditVersion

func TrySetupAuditVersion[T any](ctx context.Context, entries []T) (err error)

func TrySetupLastInsertId

func TrySetupLastInsertId[T any](ctx context.Context, entry T, id int64) (v T, err error)

func WriteInsertReturning

func WriteInsertReturning[T any](ctx context.Context, rows sql.Rows, returning []string, entries []T) (affected int64, err error)

Types

type BoolValue

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

func (*BoolValue) Write

func (w *BoolValue) Write(value any, rv reflect.Value) (err error)

type ByteValue

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

func (*ByteValue) Write

func (w *ByteValue) Write(value any, rv reflect.Value) (err error)

type BytesValue

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

func (*BytesValue) Write

func (w *BytesValue) Write(value any, rv reflect.Value) (err error)

type Column

type Column struct {
	FieldIdx    []int
	Field       string
	Name        string
	JsonIdent   string
	Kind        ColumnKind
	Type        ColumnType
	ValueWriter ValueWriter
}

Column 'column:"{name},{kind},{options}"'

func (*Column) Incr

func (column *Column) Incr() bool
func (column *Column) Link() (hostField string, awayField string, mapping *Specification, ok bool)
func (column *Column) Links() (hostField string, awayField string, mapping *Specification, order orders.Orders, length int, ok bool)

func (*Column) ReadValue added in v1.2.41

func (column *Column) ReadValue(sv reflect.Value) (fv reflect.Value)

func (*Column) Reference

func (column *Column) Reference() (awayField string, mapping *Specification, ok bool)

func (*Column) String

func (column *Column) String() (s string)

func (*Column) Valid

func (column *Column) Valid() bool

func (*Column) Virtual

func (column *Column) Virtual() (kind VirtualQueryKind, query string, ok bool)

func (*Column) WriteValue

func (column *Column) WriteValue(field reflect.Value, value any) (err error)

type ColumnKind

type ColumnKind int
const (
	Normal    ColumnKind = iota // column
	Pk                          // column,pk{,incr}
	Acb                         // column,acb
	Act                         // column,act
	Amb                         // column,amb
	Amt                         // column,amt
	Adb                         // column,adb
	Adt                         // column,adt
	Aol                         // column,aol
	Json                        // column,json
	Virtual                     // ident,vc,basic|object|array|aggregate,query|agg_func
	Reference                   // column,ref,target_field
	Link                        // ident,link,field+target_field
	Links                       // column,links,field+target_field,orders:field@desc+field,length:10
)

func (ColumnKind) String

func (kind ColumnKind) String() string

type ColumnType

type ColumnType struct {
	Name    ColumnTypeName
	Value   reflect.Type
	Mapping *Specification
	Options []string
}

func (*ColumnType) String

func (ct *ColumnType) String() string

type ColumnTypeName

type ColumnTypeName int
const (
	UnknownType ColumnTypeName = iota
	StringType
	BoolType
	IntType
	FloatType
	DatetimeType
	DateType
	TimeType
	BytesType
	ByteType
	JsonType
	ScanType
	MappingType
)

type Condition

type Condition struct {
	conditions.Condition
}

func (Condition) Render

func (cond Condition) Render(ctx Context, w io.Writer) (arguments []any, err error)

type Context

type Context interface {
	context.Context
	FormatIdent(ident string) string
	NextQueryPlaceholder() (v string)
	SkipNextQueryPlaceholderCursor(n int)
	// Localization
	// key can be struct field, struct value and [struct value, struct value]
	// when field then return column name
	// when value then return table name
	// when [struct value, struct value] then return column name of table name
	Localization(key any) (content []string, has bool)
}

func Fork

func Fork(ctx Context) Context

func SwitchKey

func SwitchKey(ctx Context, key any) Context

func Todo

func Todo(ctx context.Context, key any, dialect Dialect) Context

type DateValue

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

func (*DateValue) Write

func (w *DateValue) Write(value any, rv reflect.Value) (err error)

type DatetimeValue

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

func (*DatetimeValue) Write

func (w *DatetimeValue) Write(value any, rv reflect.Value) (err error)

type Dialect

type Dialect interface {
	Name() string
	FormatIdent(ident string) string
	QueryPlaceholder() QueryPlaceholder
	Insert(ctx Context, spec *Specification, values int) (method Method, query []byte, fields []string, returning []string, err error)
	InsertOrUpdate(ctx Context, spec *Specification) (method Method, query []byte, fields []string, returning []string, err error)
	InsertWhenExist(ctx Context, spec *Specification, src QueryExpr) (method Method, query []byte, fields []string, arguments []any, returning []string, err error)
	InsertWhenNotExist(ctx Context, spec *Specification, src QueryExpr) (method Method, query []byte, fields []string, arguments []any, returning []string, err error)
	Update(ctx Context, spec *Specification) (method Method, query []byte, fields []string, err error)
	UpdateFields(ctx Context, spec *Specification, fields []FieldValue, cond Condition) (method Method, query []byte, arguments []any, err error)
	Delete(ctx Context, spec *Specification) (method Method, query []byte, fields []string, err error)
	DeleteByConditions(ctx Context, spec *Specification, cond Condition) (method Method, query []byte, audits []string, arguments []any, err error)
	Exist(ctx Context, spec *Specification, cond Condition) (method Method, query []byte, arguments []any, err error)
	Count(ctx Context, spec *Specification, cond Condition) (method Method, query []byte, arguments []any, err error)
	Query(ctx Context, spec *Specification, cond Condition, orders Orders, offset int, length int) (method Method, query []byte, arguments []any, fields []string, err error)
	View(ctx Context, spec *Specification, cond Condition, orders Orders, groupBy GroupBy, offset int, length int) (method Method, query []byte, arguments []any, fields []string, err error)
}

func LoadDialect

func LoadDialect(ctx context.Context) (dialect Dialect, err error)

type Dict

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

func NewDict

func NewDict() *Dict

func (*Dict) Get

func (dict *Dict) Get(key ...any) (value []string, has bool)

func (*Dict) Set

func (dict *Dict) Set(key string, value ...string)

Set table: key is {path}.{name}, value is table name column: key is {path}.{name}:{field}, value is column name

type FieldValue

type FieldValue struct {
	Name  string
	Value any
}

type FloatValue

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

func (*FloatValue) Write

func (w *FloatValue) Write(value any, rv reflect.Value) (err error)

type Generic

type Generic struct {
	Valid bool
	Value any
}

func (*Generic) Reset

func (v *Generic) Reset()

func (*Generic) Scan

func (v *Generic) Scan(src any) (err error)

type Generics

type Generics []any

func (Generics) WriteTo

func (generics Generics) WriteTo(spec *Specification, fieldNames []string, entryPtr any) (err error)

type GroupBy

type GroupBy struct {
	groups.GroupBy
}

func (GroupBy) Exist

func (group GroupBy) Exist() bool

func (GroupBy) Render

func (group GroupBy) Render(ctx Context, w io.Writer) (argument []any, err error)

type IntValue

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

func (*IntValue) Write

func (w *IntValue) Write(value any, rv reflect.Value) (err error)

type JsonValue

type JsonValue struct {
	ValueType reflect.Type
}

func (*JsonValue) Write

func (w *JsonValue) Write(value any, rv reflect.Value) (err error)

type MappingValue

type MappingValue struct {
	ValueType reflect.Type
}

func (*MappingValue) Write

func (w *MappingValue) Write(value any, rv reflect.Value) (err error)

type Method

type Method int
const (
	QueryMethod Method = iota + 1
	ExecuteMethod
)

func BuildCount

func BuildCount[T any](ctx context.Context, cond Condition) (method Method, query []byte, arguments []any, err error)

func BuildDelete

func BuildDelete[T any](ctx context.Context, entries []T) (method Method, query []byte, arguments []any, err error)

func BuildDeleteAnyByCondition

func BuildDeleteAnyByCondition(ctx context.Context, entry any, cond Condition) (method Method, query []byte, arguments []any, err error)

func BuildDeleteByCondition

func BuildDeleteByCondition[T any](ctx context.Context, cond Condition) (method Method, query []byte, arguments []any, err error)

func BuildExist

func BuildExist[T any](ctx context.Context, cond Condition) (method Method, query []byte, arguments []any, err error)

func BuildInsert

func BuildInsert[T any](ctx context.Context, entries []T) (method Method, query []byte, arguments []any, returning []string, err error)

func BuildInsertOrUpdate

func BuildInsertOrUpdate[T any](ctx context.Context, entries []T) (method Method, query []byte, arguments []any, returning []string, err error)

func BuildInsertWhenExist

func BuildInsertWhenExist[T any](ctx context.Context, entries []T, src QueryExpr) (method Method, query []byte, arguments []any, returning []string, err error)

func BuildInsertWhenNotExist

func BuildInsertWhenNotExist[T any](ctx context.Context, entries []T, src QueryExpr) (method Method, query []byte, arguments []any, returning []string, err error)

func BuildQuery

func BuildQuery[T any](ctx context.Context, cond Condition, orders Orders, offset int, length int) (method Method, query []byte, arguments []any, columns []string, err error)

func BuildUpdate

func BuildUpdate[T any](ctx context.Context, entries []T) (method Method, query []byte, arguments []any, err error)

func BuildUpdateFields

func BuildUpdateFields[T any](ctx context.Context, fields []FieldValue, cond Condition) (method Method, query []byte, arguments []any, err error)

func BuildView

func BuildView[T any](ctx context.Context, cond Condition, orders Orders, groupBy GroupBy, offset int, length int) (method Method, query []byte, arguments []any, columns []string, err error)

type Orders

type Orders orders.Orders

func (Orders) Render

func (o Orders) Render(ctx Context, w io.Writer) (argument []any, err error)

type Page

type Page struct {
	No   int
	Size int
}

func PG

func PG(no int, size int) Page

func (Page) Range

func (p Page) Range() (rng Range)

type Predicate

type Predicate struct {
	conditions.Predicate
}

func (Predicate) Render

func (p Predicate) Render(ctx Context, w io.Writer) (argument []any, err error)

type QueryExpr

type QueryExpr struct {
	conditions.QueryExpr
}

func (QueryExpr) Render

func (expr QueryExpr) Render(ctx Context, w io.Writer) (argument []any, err error)

type QueryPlaceholder

type QueryPlaceholder interface {
	Next() (v string)
	SkipCursor(n int)
	Current() (v string)
}

type Range

type Range struct {
	Offset int
	Length int
}

func RNG

func RNG(offset int, length int) Range

func (Range) Page

func (rng Range) Page() (page Page)

type Render

type Render interface {
	Render(ctx Context, w io.Writer) (argument []any, err error)
}

type ScanValue

type ScanValue struct {
}

func (*ScanValue) Write

func (w *ScanValue) Write(value any, rv reflect.Value) (err error)

type Specification

type Specification struct {
	Key       string
	Schema    string
	Name      string
	View      bool
	ViewBase  *Specification
	Type      reflect.Type
	Columns   []*Column
	Conflicts []string
}

func GetSpecification

func GetSpecification(ctx context.Context, e any) (spec *Specification, err error)

func ScanTable

func ScanTable(ctx context.Context, table any) (spec *Specification, err error)

func ScanView

func ScanView(ctx context.Context, view any) (spec *Specification, err error)

func (*Specification) ArgumentByField

func (spec *Specification) ArgumentByField(instance any, field string) (argument any, err error)

func (*Specification) Arguments

func (spec *Specification) Arguments(instance any, fieldNames []string) (arguments []any, err error)

func (*Specification) AuditCreation

func (spec *Specification) AuditCreation() (by *Column, at *Column, has bool)

func (*Specification) AuditDeletion

func (spec *Specification) AuditDeletion() (by *Column, at *Column, has bool)

func (*Specification) AuditModification

func (spec *Specification) AuditModification() (by *Column, at *Column, has bool)

func (*Specification) AuditVersion

func (spec *Specification) AuditVersion() (v *Column, has bool)

func (*Specification) ColumnByField

func (spec *Specification) ColumnByField(fieldName string) (column *Column, has bool)

func (*Specification) ConflictColumns

func (spec *Specification) ConflictColumns() (columns []*Column, err error)

func (*Specification) Instance

func (spec *Specification) Instance() (v any)

func (*Specification) Pk

func (spec *Specification) Pk() (v *Column, has bool)

func (*Specification) String

func (spec *Specification) String() (s string)

type StringValue

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

func (*StringValue) Write

func (w *StringValue) Write(value any, rv reflect.Value) (err error)

type TableInfo

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

func GetTableInfo

func GetTableInfo(e any) (info TableInfo, err error)

type TimeValue

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

func (*TimeValue) Write

func (w *TimeValue) Write(value any, rv reflect.Value) (err error)

type ValueWriter

type ValueWriter interface {
	Write(value any, field reflect.Value) (err error)
}

type ViewInfo

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

func GetViewInfo

func GetViewInfo(e any) (info ViewInfo, err error)

type VirtualQueryKind

type VirtualQueryKind int
const (
	UnknownVirtualQueryKind VirtualQueryKind = iota
	BasicVirtualQuery
	ObjectVirtualQuery
	ArrayVirtualQuery
	AggregateVirtualQuery
)

Jump to

Keyboard shortcuts

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