mdb

package
v0.0.0-...-52e5b9a Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: MIT Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NoCache = "no-cache"
)

Variables

View Source
var (
	DB              = NewGormDB()
	ErrRecordExists = errors.New("record exists")
)
View Source
var (
	PCacheStat = new(PluginCacheStat)
)
View Source
var Rdb = new(RedisV9)
View Source
var SeparatorColon = ":"

Functions

func AddModels

func AddModels(v ...interface{})

func AutoDelete

func AutoDelete(db *gorm.DB, bus interface{})

func BatchDeleteFromTop

func BatchDeleteFromTop(db *gorm.DB, model interface{}, count int)

BatchDeleteFromTop eachCount = 1000; count: count of delete rows , order by id asc

func CreateIfNotExists

func CreateIfNotExists(db *gorm.DB, bean interface{}, query interface{}, args ...interface{}) error

func FindRecords

func FindRecords[T any](val T, call func(tx *gorm.DB) *gorm.DB, args ...interface{}) (sliceResult []T, err error)

func FindRecordsWithDB

func FindRecordsWithDB[T any](db *gorm.DB, val T, call func(tx *gorm.DB) *gorm.DB, args ...interface{}) (sliceResult []T, err error)

FindRecordsWithDB ...

func HDeleter

func HDeleter(c *redis.Client, key string, args ...interface{}) func(fields ...string)

func HScanCallback

func HScanCallback(c *redis.Client, key, match string, fn func(k, v string))

func Md5

func Md5(b []byte) string

Md5 returns the MD5 checksum string of the data.

func ModelTableName

func ModelTableName(model interface{}) string

func NewDBLoggerSilent

func NewDBLoggerSilent() logger.Interface

func NewDBLoggerWithLevel

func NewDBLoggerWithLevel(level logger.LogLevel) logger.Interface

func NewDBLoggerWithLevelOut

func NewDBLoggerWithLevelOut(level logger.LogLevel, logName string) logger.Interface

func NewPlugin

func NewPlugin(c Config) gorm.Plugin

func ParseModel

func ParseModel(model interface{}) (s *schema.Schema, err error)

func SetModelBus

func SetModelBus(bus interface{})

func StructHasIDField

func StructHasIDField(v reflect.Value) bool

func ValueIDUint64

func ValueIDUint64(val util.Map) (id uint64, ok bool)

Types

type BaseModel

type BaseModel struct {
	ID        int      `json:"id,omitempty" gorm:"primaryKey;autoIncrement:true;autoIncrementIncrement:1;"`
	CreatedAt *Time    `json:"created_at,omitempty" gorm:"notnull;default:CURRENT_TIMESTAMP;"`
	UpdatedAt *Time    `json:"updated_at,omitempty" gorm:"notnull;"`
	Version   *Version `json:"version,omitempty" gorm:"notnull;default:1;comment:版本号;"`
}

type CCStore

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

func GetCCacheStore

func GetCCacheStore() *CCStore

func NewCCacheStore

func NewCCacheStore() *CCStore

func (*CCStore) ClearAll

func (c *CCStore) ClearAll()

func (*CCStore) Del

func (c *CCStore) Del(key string)

func (*CCStore) DropPrefix

func (c *CCStore) DropPrefix(prefix ...string)

func (*CCStore) Get

func (c *CCStore) Get(key string) (data []byte, ok bool)

func (*CCStore) GetCCache

func (c *CCStore) GetCCache() *ccache.Cache[[]byte]

GetCCache ...

func (*CCStore) Set

func (c *CCStore) Set(key string, data []byte, ttl ...int64)

type CacheStore

type CacheStore interface {
	Get(key string) (data []byte, ok bool)
	Set(key string, data []byte, ttl ...int64)
	Del(key string)
	ClearAll()
	DropPrefix(prefix ...string)
}

type Config

type Config struct {
	Skip bool
	// TTL is the time to live in seconds, default is 0, means no expiration
	TTL    int64
	Prefix string
	Store  CacheStore
}

type CreateClause

type CreateClause struct {
	Field *schema.Field
}

func (CreateClause) Build

func (v CreateClause) Build(clause.Builder)

func (CreateClause) MergeClause

func (v CreateClause) MergeClause(*clause.Clause)

func (CreateClause) ModifyStatement

func (v CreateClause) ModifyStatement(stmt *gorm.Statement)

func (CreateClause) Name

func (v CreateClause) Name() string

type CurdParams

type CurdParams struct {
	TableName         string      `json:"table_name,omitempty"`
	Values            util.Map    `json:"values,omitempty"`
	Where             string      `json:"where,omitempty"`
	Model             interface{} `json:"-"`
	BeforeCall        func(bean interface{})
	Check             func(bean interface{}) (err error)
	CheckBeforeUpdate func(oldVal, newVal interface{}) (err error)
}

func (*CurdParams) AddUserIDCondition

func (c *CurdParams) AddUserIDCondition(bean interface{}, userID uint64)

func (*CurdParams) AddWhereCondition

func (c *CurdParams) AddWhereCondition(where string)

AddWhereCondition ... 添加where条件

func (*CurdParams) Create

func (c *CurdParams) Create() (err error)

Create ...添加数据

func (*CurdParams) Delete

func (c *CurdParams) Delete() (err error)

Delete ...删除数据

func (*CurdParams) Update

func (c *CurdParams) Update() (err error)

Update ...更新数据

type DBOption

type DBOption struct {
	Type string `json:"type"`
	Host string `json:"host"`
	Port string `json:"port"`
	User string `json:"user"`
	Pwd  string `json:"pwd"`
	DB   string `json:"db"`

	SkipCache bool   `json:"skip_cache"`
	CacheType string `json:"cache_type"`

	Logger logger.Interface `json:"-"`

	MaxIdleConns       int   `json:"max_idle_conns"`
	MaxOpenConns       int   `json:"max_open_conns"`
	MaxLifetimeSeconds int64 `json:"max_lifetime_seconds"`
	MaxIdleTimeSeconds int64 `json:"max_idle_time_seconds"`

	SkipCreateDB bool `json:"skipCreateDb,omitempty"`
}

func (*DBOption) DSN

func (d *DBOption) DSN() string

DSN ...

func (*DBOption) DSNMd5

func (d *DBOption) DSNMd5() string

DSNMd5 ...

func (*DBOption) GetLogger

func (d *DBOption) GetLogger() logger.Interface

GetLogger ...

type FindByID

type FindByID struct {
	TableName string `json:"table_name,omitempty"`
	ID        uint64 `json:"id,omitempty"`
	Condition string `json:"condition,omitempty"`

	Dest interface{} `json:"-"`
}

func (*FindByID) AppendEqCondition

func (f *FindByID) AppendEqCondition(str string)

AppendEqCondition ...

func (*FindByID) FindByID

func (f *FindByID) FindByID() (result interface{}, err error)

FindByID ...

type FindParams

type FindParams struct {
	Table     string `json:"table,omitempty"`
	Condition string `json:"condition,omitempty"`
	Order     string `json:"order,omitempty"`
	PageIndex int    `json:"page_index,omitempty"`
	PageSize  int    `json:"page_size,omitempty"`

	Dest interface{} `json:"-"`
}

func (*FindParams) AppendEqCondition

func (f *FindParams) AppendEqCondition(str string)

AppendEqCondition ...

func (*FindParams) FindResultWithModel

func (f *FindParams) FindResultWithModel(tx *gorm.DB) (result *FindResult, err error)

FindResultWithModel get data

type FindResult

type FindResult struct {
	// Data
	Data interface{} `json:"data,omitempty"`
	// Pagination
	Pagination *Pagination `json:"pagination,omitempty"`
	// Extra
	Extra interface{} `json:"extra,omitempty"`
}

type FreeStore

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

func GetFreeCacheStore

func GetFreeCacheStore() *FreeStore

func NewFreeCacheStore

func NewFreeCacheStore() *FreeStore

func NewFreeCacheStoreWithSize

func NewFreeCacheStoreWithSize(size int) *FreeStore

func (*FreeStore) ClearAll

func (f *FreeStore) ClearAll()

func (*FreeStore) Del

func (f *FreeStore) Del(key string)

func (*FreeStore) DropPrefix

func (f *FreeStore) DropPrefix(prefix ...string)

func (*FreeStore) Get

func (f *FreeStore) Get(key string) (data []byte, ok bool)

func (*FreeStore) GetFC

func (f *FreeStore) GetFC() *freecache.Cache

func (*FreeStore) Set

func (f *FreeStore) Set(key string, data []byte, ttl ...int64)

type GormDB

type GormDB struct {
	*gorm.DB
	// contains filtered or unexported fields
}

func NewGormDB

func NewGormDB() *GormDB

func (*GormDB) CheckDBNil

func (g *GormDB) CheckDBNil() (err error)

func (*GormDB) Close

func (g *GormDB) Close() (err error)

Close ...

func (*GormDB) CreateDB

func (g *GormDB) CreateDB()

func (*GormDB) DropDB

func (g *GormDB) DropDB()

DropDB ...

func (*GormDB) GetFindModel

func (g *GormDB) GetFindModel(table string) (interface{}, error)

GetFindModel ...

func (*GormDB) GetModel

func (g *GormDB) GetModel(table string) (interface{}, error)

GetModel ...

func (*GormDB) Initialize

func (g *GormDB) Initialize(opt *DBOption) *GormDB

Initialize GormDB

func (*GormDB) MigrateModels

func (g *GormDB) MigrateModels(v ...interface{})

MigrateModels ...

func (*GormDB) ModelByTableName

func (g *GormDB) ModelByTableName(tableName string) (model interface{}, err error)

ModelByTableName ...

func (*GormDB) ModelTableName

func (g *GormDB) ModelTableName(model interface{}) (name string)

ModelTableName ...

func (*GormDB) Models

func (g *GormDB) Models() map[string]interface{}

func (*GormDB) Opt

func (g *GormDB) Opt() *DBOption

func (*GormDB) RegModel

func (g *GormDB) RegModel(model interface{})

RegModel ...

func (*GormDB) RegModelBus

func (g *GormDB) RegModelBus(bus interface{})

RegModelBus ...

func (*GormDB) RegViewModel

func (g *GormDB) RegViewModel(model interface{})

RegViewModel ...

func (*GormDB) SetOpt

func (g *GormDB) SetOpt(opt *DBOption)

func (*GormDB) ViewModels

func (g *GormDB) ViewModels() map[string]interface{}

type ImplResultAfterFind

type ImplResultAfterFind interface {
	ResultAfterFind(tx *gorm.DB) error
}

type ItfModelInitializer

type ItfModelInitializer interface {
	InitData(db *gorm.DB) (interface{}, error)
}

type Pagination

type Pagination struct {
	// Total number of records
	Total int64 `json:"total,omitempty"`
	// Size of each page
	Size int `json:"size,omitempty"`
	// Index of pages
	Index int `json:"index,omitempty"`
	// Pages Number of pages
	Pages int `json:"pages,omitempty"`
}

type PluginCacheStat

type PluginCacheStat struct {
	SearchHit   uint64
	SearchMiss  uint64
	PrimaryHit  uint64
	PrimaryMiss uint64
}

type RedisOptions

type RedisOptions struct {
	Addr     string
	Username string
	Password string
	DB       int
}

func (*RedisOptions) Copy

func (r *RedisOptions) Copy() *RedisOptions

Copy ...

type RedisStore

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

func GetRedisStore

func GetRedisStore(c *redis.Client, opts ...*RedisStoreOption) *RedisStore

func NewRedisStore

func NewRedisStore(c *redis.Client, opts ...*RedisStoreOption) *RedisStore

func (*RedisStore) ClearAll

func (r *RedisStore) ClearAll()

func (*RedisStore) Del

func (r *RedisStore) Del(key string)

func (*RedisStore) DropPrefix

func (r *RedisStore) DropPrefix(prefix ...string)

func (*RedisStore) Get

func (r *RedisStore) Get(key string) (data []byte, ok bool)

func (*RedisStore) GetKeyFieldOrNot

func (r *RedisStore) GetKeyFieldOrNot(key string) (k, f string, y bool)

func (*RedisStore) ScanKeys

func (r *RedisStore) ScanKeys(matchParam string) []string

func (*RedisStore) Set

func (r *RedisStore) Set(key string, data []byte, ttl ...int64)

func (*RedisStore) StoreGC

func (r *RedisStore) StoreGC(prefix string)

type RedisStoreOption

type RedisStoreOption struct {
	Separator string
}

type RedisV9

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

func (*RedisV9) Close

func (r *RedisV9) Close()

Close ...

func (*RedisV9) GetClient

func (r *RedisV9) GetClient(dbs ...int) *redis.Client

func (*RedisV9) Initialize

func (r *RedisV9) Initialize(opt *RedisOptions) *RedisV9

func (*RedisV9) Opt

func (r *RedisV9) Opt() *RedisOptions

type RedisValue

type RedisValue struct {
	Val      util.RawMessage `json:"val"`
	ExpireAt int64           `json:"expire_at"`
}

type StoreGC

type StoreGC interface {
	StoreGC(prefix string)
}

type Time

type Time = util.JSONTime

type UpdateClause

type UpdateClause struct {
	Field *schema.Field
}

func (UpdateClause) Build

func (v UpdateClause) Build(clause.Builder)

func (UpdateClause) MergeClause

func (v UpdateClause) MergeClause(*clause.Clause)

func (UpdateClause) ModifyStatement

func (v UpdateClause) ModifyStatement(stmt *gorm.Statement)

func (UpdateClause) Name

func (v UpdateClause) Name() string

type Version

type Version sql.NullInt64

func (*Version) CreateClauses

func (v *Version) CreateClauses(field *schema.Field) []clause.Interface

func (*Version) MarshalJSON

func (v *Version) MarshalJSON() ([]byte, error)

func (*Version) Scan

func (v *Version) Scan(value interface{}) error

func (*Version) UnmarshalJSON

func (v *Version) UnmarshalJSON(bytes []byte) error

func (*Version) UpdateClauses

func (v *Version) UpdateClauses(field *schema.Field) []clause.Interface

func (*Version) Value

func (v *Version) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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