mdb

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2023 License: MIT Imports: 34 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 added in v1.0.0

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

func FindRecordsWithDB added in v1.0.0

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) 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 added in v1.0.0

func NewDBLoggerSilent() logger.Interface

func NewDBLoggerWithLevel added in v0.0.2

func NewDBLoggerWithLevel(level logger.LogLevel) logger.Interface

func NewDBLoggerWithLevelOut added in v1.0.0

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 ValueIDUint64

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

Types

type BaseModel added in v1.0.0

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 added in v1.0.0

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

func GetCCacheStore

func GetCCacheStore() *CCStore

func NewCCacheStore added in v1.0.0

func NewCCacheStore() *CCStore

func (*CCStore) ClearAll added in v1.0.0

func (c *CCStore) ClearAll()

func (*CCStore) Del added in v1.0.0

func (c *CCStore) Del(key string)

func (*CCStore) DropPrefix added in v1.0.0

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

func (*CCStore) Get added in v1.0.0

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

func (*CCStore) GetCCache added in v1.0.0

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

GetCCache ...

func (*CCStore) Set added in v1.0.0

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 added in v1.0.0

type CreateClause struct {
	Field *schema.Field
}

func (CreateClause) Build added in v1.0.0

func (v CreateClause) Build(clause.Builder)

func (CreateClause) MergeClause added in v1.0.0

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

func (CreateClause) ModifyStatement added in v1.0.0

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

func (CreateClause) Name added in v1.0.0

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 added in v0.0.5

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 added in v1.0.0

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

func GetFreeCacheStore added in v1.0.0

func GetFreeCacheStore() *FreeStore

func NewFreeCacheStore added in v1.0.0

func NewFreeCacheStore() *FreeStore

func (*FreeStore) ClearAll added in v1.0.0

func (f *FreeStore) ClearAll()

func (*FreeStore) Del added in v1.0.0

func (f *FreeStore) Del(key string)

func (*FreeStore) DropPrefix added in v1.0.0

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

func (*FreeStore) Get added in v1.0.0

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

func (*FreeStore) GetFC added in v1.0.0

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

func (*FreeStore) Set added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

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

func GetRedisStore

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

func NewRedisStore added in v1.0.0

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

func (*RedisStore) ClearAll added in v1.0.0

func (r *RedisStore) ClearAll()

func (*RedisStore) Del added in v1.0.0

func (r *RedisStore) Del(key string)

func (*RedisStore) DropPrefix added in v1.0.0

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

func (*RedisStore) Get added in v1.0.0

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

func (*RedisStore) GetKeyFieldOrNot added in v1.0.0

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

func (*RedisStore) ScanKeys added in v1.0.0

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

func (*RedisStore) Set added in v1.0.0

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

func (*RedisStore) StoreGC added in v1.0.0

func (r *RedisStore) StoreGC(prefix string)

type RedisStoreOption added in v1.0.0

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 added in v1.0.0

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

type StoreGC

type StoreGC interface {
	StoreGC(prefix string)
}

type Time added in v1.0.0

type Time = util.JSONTime

type UpdateClause added in v1.0.0

type UpdateClause struct {
	Field *schema.Field
}

func (UpdateClause) Build added in v1.0.0

func (v UpdateClause) Build(clause.Builder)

func (UpdateClause) MergeClause added in v1.0.0

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

func (UpdateClause) ModifyStatement added in v1.0.0

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

func (UpdateClause) Name added in v1.0.0

func (v UpdateClause) Name() string

type Version added in v1.0.0

type Version sql.NullInt64

func (*Version) CreateClauses added in v1.0.0

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

func (*Version) MarshalJSON added in v1.0.0

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

func (*Version) Scan added in v1.0.0

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

func (*Version) UnmarshalJSON added in v1.0.0

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

func (*Version) UpdateClauses added in v1.0.0

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

func (*Version) Value added in v1.0.0

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