liteMng

package module
v0.0.65 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2025 License: MIT Imports: 9 Imported by: 0

README

liteMng

Documentation

Index

Constants

View Source
const (
	BaseType   = "Base"
	Unknowned  = "__unknowned__"
	DateLayout = "2006-01-02"
	TimeLayout = "2006-01-02 15:04:05.999999999 -0700 MST"
)

Variables

View Source
var (
	Fmt         *SqliteFmt      = &SqliteFmt{",", "`", "."}
	Registry    *entityRegistry = &entityRegistry{}
	SqlDebug    bool            = false
	EntityDebug bool            = false
)

Functions

func CamelToUnderscore

func CamelToUnderscore(camelCase string) string

Transform entity field in db field

func DbField

func DbField(ent Entity, fieldName string) string

Give db field name related to specified entity and enitity field name

func FindAll

func FindAll[T any](call Rs) ([]T, error)

func GetStructFieldNames

func GetStructFieldNames(v any) []string

func GetStructFieldNamesWithExclude

func GetStructFieldNamesWithExclude(v any, excludeNames []string) []string

func GetStructFieldValues

func GetStructFieldValues(v any, fieldList []string) []any

func HasBasedType

func HasBasedType(t reflect.Type, targetType reflect.Type) bool

Check if specified type embed specified target type

func IsNilPointer

func IsNilPointer(v any) bool

check if specified pointer is nil

func IsPointer

func IsPointer(v any) bool

check if specified value is a pointer

func ListTo

func ListTo[T any](values []Entity) ([]T, error)

Convert specified Entities list to specified Type Entity Embedder

func NewEntity

func NewEntity[T Entity](ent T) T

func PointerReadVal added in v0.0.37

func PointerReadVal[T any](p Pointer, val *T) (bool, error)

Fill specified value with specified Pointer value

func PointerVal

func PointerVal[T any](p Pointer) (T, error)

Dereference specified Pointer as specified T <any> type value

func To

func To[T any](value Entity) (T, error)

Convert specified entity to specified Type Entity Embedder

Types

type Base

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

Base Entity structure defining an abstract entity

func (*Base) Compound

func (e *Base) Compound(name string) Entity

Give compound entity for specified entity field name

func (*Base) Compounds

func (e *Base) Compounds() []Entity

Give compounds entity (sub entities mapped to entity fields)

func (*Base) Entity

func (e *Base) Entity() Entity

Give related entity as Entity interface

func (*Base) Field

func (e *Base) Field(fieldName string) (string, bool)

Give db field name related to specified entity field name

func (*Base) Fields

func (e *Base) Fields() []string

Give db fields name list

func (*Base) GetBase

func (e *Base) GetBase() *Base

Give Base Entity

func (*Base) Id

func (e *Base) Id() int64

Give id

func (*Base) IdAddr

func (e *Base) IdAddr() any

Give id Pointer

func (*Base) Init

func (e *Base) Init()

Initialize entity

func (*Base) InitialId

func (e *Base) InitialId() int64

Give initial id

func (*Base) Name added in v0.0.9

func (e *Base) Name(fieldName string) string

retiew Entity name by Db FieldName

func (*Base) Names

func (e *Base) Names() []string

Give all entity fields name

func (*Base) New

func (e *Base) New() Entity

Return new empty entity

func (*Base) Pointer

func (e *Base) Pointer(fieldName string) (Pointer, bool)

Give related field pointer from specified entity field name

func (*Base) Pointers

func (e *Base) Pointers() []Pointer

Give all fields pointer

func (*Base) SetId

func (e *Base) SetId(id int64)

Set id value

func (*Base) Type

func (e *Base) Type(fieldName string) (*reflect.Type, bool)

Give entity related field type from specified entity field name

func (*Base) Types

func (e *Base) Types() []reflect.Type

Give all entity fields type

func (*Base) Value

func (e *Base) Value(fieldName string) (any, bool)

Give entity field value from specified entity field name

func (*Base) Values

func (e *Base) Values() []any

Give all entity fields value

type BaseModel

type BaseModel struct {
	Db        *SqliteDb
	Prefix    *Prefix
	ListOpDef []FieldOpDef
}

func NewBaseModel

func NewBaseModel(db *SqliteDb) *BaseModel

func (*BaseModel) ApplyOnRow

func (m *BaseModel) ApplyOnRow(e *Base) bool

func (*BaseModel) ApplyOnRowCompound added in v0.0.17

func (m *BaseModel) ApplyOnRowCompound(e *Base)

func (*BaseModel) ApplyOnRows

func (m *BaseModel) ApplyOnRows(rows *sql.Rows, ent Entity) ([]Entity, error)

func (*BaseModel) CheckValidPointer

func (m *BaseModel) CheckValidPointer(ent Entity, ptr any) int

func (*BaseModel) DbGetListFields

func (m *BaseModel) DbGetListFields(e *Base) []string

func (*BaseModel) DbInsert

func (m *BaseModel) DbInsert(ent Entity) (bool, error)

func (*BaseModel) DbOnConflictUpdateFields added in v0.0.32

func (m *BaseModel) DbOnConflictUpdateFields(e *Base) string

func (*BaseModel) DbOnInsert

func (m *BaseModel) DbOnInsert(rs sql.Result, e *Base) (bool, error)

func (*BaseModel) DbPrepareAllFields

func (m *BaseModel) DbPrepareAllFields(e *Base) string

func (*BaseModel) DbPrepareFields

func (m *BaseModel) DbPrepareFields(e *Base) string

func (*BaseModel) DbPrepareInsert

func (m *BaseModel) DbPrepareInsert(e *Base, tableName string) (*sql.Stmt, error)

func (*BaseModel) DbPrepareInsertFields

func (m *BaseModel) DbPrepareInsertFields(e *Base) string

func (*BaseModel) DbPrepareValues

func (m *BaseModel) DbPrepareValues(e *Base) string

func (*BaseModel) DbReceivValues

func (m *BaseModel) DbReceivValues(e *Base) []any

func (*BaseModel) DbResolvValues

func (m *BaseModel) DbResolvValues(e *Base) []any

func (*BaseModel) FilterFields

func (m *BaseModel) FilterFields(e *Base, list []string) []string

func (*BaseModel) FindAll

func (m *BaseModel) FindAll(ent Entity, limit int, index int) ([]Entity, error)

func (*BaseModel) FindAllById

func (m *BaseModel) FindAllById(ent Entity, dir OrderDir, limit int, index int) ([]Entity, error)

func (*BaseModel) FindById

func (m *BaseModel) FindById(ent Entity) bool

func (*BaseModel) FindOneBy

func (m *BaseModel) FindOneBy(ent Entity, ptr any) bool

func (*BaseModel) GetFieldOps added in v0.0.27

func (m *BaseModel) GetFieldOps(ent Entity) []FieldOpDef

func (*BaseModel) RegisterFieldOp added in v0.0.20

func (m *BaseModel) RegisterFieldOp(typ reflect.Type, name string, isPreOp bool, op PointerOp)

type Entity

type Entity interface {
	Compounds() []Entity
	Compound(name string) Entity
	Entity() Entity
	Field(fieldName string) (string, bool)
	Fields() []string
	GetBase() *Base
	Id() int64
	IdAddr() any
	Init()
	InitialId() int64
	Names() []string
	New() Entity
	Pointer(fieldName string) (Pointer, bool)
	Pointers() []Pointer
	SetId(id int64)
	Type(fieldName string) (*reflect.Type, bool)
	Types() []reflect.Type
	Value(fieldName string) (any, bool)
	Values() []any
}

Entity interface. The Base Struct implements this interface. In order to use a custom entity struct, you should declare a struct type that embed the *liteMng.Base type, as is, without naming a field. Then you should declare your fields as exportable variables (Beginning with UpperCase)

example :

type Session struct {
	*liteMng.Base
	Uuid    string
	Data    string
	Updated liteMng.StrTime
}

Before using your entity you should register it to the liteMng.Registry and specify the related table name :

liteMng.Registry.RegisterTable(&Session{}, "session")

Then you can use the liteMng.NewEntity function to get a new instance. New instances should be a pointer of your struct :

s := lm.NewEntity(&Session{})

type FieldOp added in v0.0.20

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

Struct representing nature of Field & operation

type FieldOpDef added in v0.0.20

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

Struct representing a FieldOp definition

func (*FieldOpDef) Call added in v0.0.27

func (f *FieldOpDef) Call(ent Entity) (bool, error)

Call registered FieldOp on specified entity

type FuncDef

type FuncDef struct {
	Type reflect.Type
	Fn   func() Entity
}

type OrderDir

type OrderDir int
const (
	Asc OrderDir = iota
	Desc
)

func (OrderDir) String

func (o OrderDir) String() string

type OrderEntry

type OrderEntry struct {
	Prefix    string
	FieldName string
	Dir       OrderDir
}

func (*OrderEntry) Resolve

func (e *OrderEntry) Resolve() string

type Pointer

type Pointer struct {
	Ptr   any          // ptr on any type
	Ptype reflect.Type // ptr type
}

Convenient struct to manage arbitrary type via pointer and reflection

func NewPointer

func NewPointer(ptr any) Pointer

Pointer constructor with type checking

func (*Pointer) Addr

func (p *Pointer) Addr() string

Give the pointer address as string

func (*Pointer) Is

func (p *Pointer) Is(ptr any) bool

check if specified ptr <any> match address of current Pointer

func (*Pointer) UpdateVia

func (p *Pointer) UpdateVia(val any) (bool, error)

update Pointer value via specified val <any>

type PointerOp added in v0.0.18

type PointerOp func(ptr Pointer) (bool, error)

Convenient type related to operation on Pointer

type Prefix

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

Structure managing Prefix Fields

func NewPrefix

func NewPrefix() *Prefix

Prefix constructor

func (*Prefix) Get

func (p *Prefix) Get(ent Entity) string

Give current prefix for specified entity

type QueryOrder

type QueryOrder struct {
	Entries []OrderEntry
	Limit   int
	Index   int
}

func NewQueryOrder

func NewQueryOrder(limit int, index int) *QueryOrder

func (*QueryOrder) NewEntry

func (o *QueryOrder) NewEntry(dir OrderDir, fieldName string)

func (*QueryOrder) NewEntryPfx

func (o *QueryOrder) NewEntryPfx(dir OrderDir, fieldName string, prefix string)

func (*QueryOrder) Resolve

func (o *QueryOrder) Resolve() string

type Rs

type Rs func() ([]Entity, error)

type SqliteDb

type SqliteDb struct {
	Debug bool
	// contains filtered or unexported fields
}

struct to manage sqlite connection

func NewSqliteDb

func NewSqliteDb(path string) *SqliteDb

SqliteDb constructor

func (*SqliteDb) Exec

func (s *SqliteDb) Exec(query string, args ...any) (sql.Result, error)

execute a query and give a sql.Result

type SqliteFmt

type SqliteFmt struct {
	FieldListSep string // field list separator
	FieldQuote   string // field quote
	FieldSep     string // field & table alias separator
}

SqliteFmt is an utility struct that help managing sqlite formating

func (*SqliteFmt) OnConflictUpdateFields added in v0.0.32

func (f *SqliteFmt) OnConflictUpdateFields(fields []string) string

Give query fields update list on conflict update [sqlite UPSERT]

func (*SqliteFmt) PrefixField

func (f *SqliteFmt) PrefixField(fieldName string, alias string) string

Prefix specified fieldName with given table alias prefix

func (*SqliteFmt) PrefixFields

func (f *SqliteFmt) PrefixFields(fields []string, alias string) []string

prefix specified fields list with given table alias prefix as a string array

func (*SqliteFmt) PrefixList

func (f *SqliteFmt) PrefixList(fields []string, alias string) string

prefix specified fields list with given table alias prefix as a string

func (*SqliteFmt) Quote

func (f *SqliteFmt) Quote(val string) string

Quote specified string

func (*SqliteFmt) QuoteList

func (f *SqliteFmt) QuoteList(fields []string) string

quote specified fields list as a string

func (*SqliteFmt) TableAlias

func (f *SqliteFmt) TableAlias(tableName string, alias string) string

Give Table alias definition

type StrDate

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

Convenient Struct to manage Sqlite Date field

func NewStrDate

func NewStrDate(date string) StrDate

StrDate constructor initialized with specified date <string>

func (*StrDate) GetStrPointer

func (sd *StrDate) GetStrPointer() any

Give a pointer on val field as any

func (*StrDate) GetTime

func (sd *StrDate) GetTime() *time.Time

Give current time.Time pointer

func (*StrDate) Synch added in v0.0.36

func (sd *StrDate) Synch() (bool, error)

synchronize the StrDate time Pointer from value field

func (*StrDate) Update

func (sd *StrDate) Update(val any) (bool, error)

Update StrDate value by specified val <any>. val should be string or *string, or time.Time or *time.Time

func (*StrDate) Value

func (sd *StrDate) Value() string

Give value

type StrTime

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

Convenient Struct to manage Sqlite DateTime field

func NewStrTime

func NewStrTime(t time.Time) StrTime

StrTime constructor initialized with specified time.Time

func (*StrTime) GetStrPointer

func (st *StrTime) GetStrPointer() any

Give a pointer on value field as any

func (*StrTime) GetTime

func (st *StrTime) GetTime() *time.Time

Give current time.Time pointer

func (*StrTime) Synch added in v0.0.36

func (st *StrTime) Synch() (bool, error)

Synchronize the StrTime Pointer field from value field

func (*StrTime) Update

func (sd *StrTime) Update(val any) (bool, error)

Update StrTime value by specified val <any>. val should be string or *string, or time.Time or *time.Time

func (*StrTime) Value

func (st *StrTime) Value() string

Give value

Jump to

Keyboard shortcuts

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