gormc

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is an alias of gorm.ErrRecordNotFound.
	ErrNotFound = gorm.ErrRecordNotFound
)

Functions

func ConnectMysql

func ConnectMysql(m Mysql) (*gorm.DB, error)

func ConnectPgSql

func ConnectPgSql(m PgSql) (*gorm.DB, error)

Types

type CachedConn

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

func NewConn

func NewConn(db *gorm.DB, c cache.CacheConf, opts ...cache.Option) CachedConn

NewConn returns a CachedConn with a redis cluster cache.

func NewConnWithCache

func NewConnWithCache(db *gorm.DB, c cache.Cache) CachedConn

NewConnWithCache returns a CachedConn with a custom cache.

func NewNodeConn

func NewNodeConn(db *gorm.DB, rds *redis.Redis, opts ...cache.Option) CachedConn

NewNodeConn returns a CachedConn with a redis node cache.

func (CachedConn) DelCache

func (cc CachedConn) DelCache(keys ...string) error

DelCache deletes cache with keys.

func (CachedConn) DelCacheCtx

func (cc CachedConn) DelCacheCtx(ctx context.Context, keys ...string) error

DelCacheCtx deletes cache with keys.

func (CachedConn) Exec

func (cc CachedConn) Exec(exec ExecCtxFn, keys ...string) error

Exec runs given exec on given keys, and returns execution result.

func (CachedConn) ExecCtx

func (cc CachedConn) ExecCtx(ctx context.Context, execCtx ExecCtxFn, keys ...string) error

ExecCtx runs given exec on given keys, and returns execution result.

func (CachedConn) ExecNoCache

func (cc CachedConn) ExecNoCache(exec ExecCtxFn) error

ExecNoCache runs exec with given sql statement, without affecting cache.

func (CachedConn) ExecNoCacheCtx

func (cc CachedConn) ExecNoCacheCtx(ctx context.Context, execCtx ExecCtxFn) (err error)

ExecNoCacheCtx runs exec with given sql statement, without affecting cache.

func (CachedConn) GetCache

func (cc CachedConn) GetCache(key string, v interface{}) error

GetCache unmarshals cache with given key into v.

func (CachedConn) GetCacheCtx

func (cc CachedConn) GetCacheCtx(ctx context.Context, key string, v interface{}) error

GetCacheCtx unmarshals cache with given key into v.

func (CachedConn) QueryCtx

func (cc CachedConn) QueryCtx(ctx context.Context, v interface{}, key string, query QueryCtxFn) (err error)

func (CachedConn) QueryNoCacheCtx

func (cc CachedConn) QueryNoCacheCtx(ctx context.Context, v interface{}, query QueryCtxFn) (err error)

func (CachedConn) QueryRowIndex

func (cc CachedConn) QueryRowIndex(v interface{}, key string, keyer func(primary interface{}) string,
	indexQuery IndexQueryCtxFn, primaryQuery PrimaryQueryCtxFn) error

QueryRowIndex unmarshals into v with given key.

func (CachedConn) QueryRowIndexCtx

func (cc CachedConn) QueryRowIndexCtx(ctx context.Context, v interface{}, key string, keyer func(primary interface{}) string, indexQuery IndexQueryCtxFn, primaryQuery PrimaryQueryCtxFn) (err error)

QueryRowIndexCtx unmarshals into v with given key.

func (CachedConn) QueryWithExpireCtx

func (cc CachedConn) QueryWithExpireCtx(ctx context.Context, v interface{}, key string, expire time.Duration, query QueryCtxFn) (err error)

QueryWithExpireCtx unmarshals into v with given key, set expire duration and query func.

func (CachedConn) SetCache

func (cc CachedConn) SetCache(key string, v interface{}) error

SetCache sets v into cache with given key.

func (CachedConn) SetCacheCtx

func (cc CachedConn) SetCacheCtx(ctx context.Context, key string, val interface{}) error

SetCacheCtx sets v into cache with given key.

func (CachedConn) SetCacheWithExpireCtx

func (cc CachedConn) SetCacheWithExpireCtx(ctx context.Context, key string, val interface{}, expire time.Duration) error

SetCacheWithExpireCtx sets v into cache with given key.

func (CachedConn) Transact

func (cc CachedConn) Transact(fn func(db *gorm.DB) error, opts ...*sql.TxOptions) error

Transact runs given fn in transaction mode.

func (CachedConn) TransactCtx

func (cc CachedConn) TransactCtx(ctx context.Context, fn func(db *gorm.DB) error, opts ...*sql.TxOptions) error

TransactCtx runs given fn in transaction mode.

type ExecCtxFn

type ExecCtxFn func(conn *gorm.DB) error

ExecCtxFn defines the sql exec method.

type GormLogConfigI

type GormLogConfigI interface {
	GetGormLogMode() logger.LogLevel
	GetSlowThreshold() time.Duration
	GetColorful() bool
}

type IndexQueryCtxFn

type IndexQueryCtxFn func(conn *gorm.DB, v interface{}) (interface{}, error)

IndexQueryCtxFn defines the query method that based on unique indexes.

type Mysql

type Mysql struct {
	Path          string // 服务器地址
	Port          int    `json:",default=3306"`                                               // 端口
	Config        string `json:",default=charset%3Dutf8mb4%26parseTime%3Dtrue%26loc%3DLocal"` // 高级配置
	Dbname        string // 数据库名
	Username      string // 数据库用户名
	Password      string // 数据库密码
	MaxIdleConns  int    `json:",default=10"`                               // 空闲中的最大连接数
	MaxOpenConns  int    `json:",default=10"`                               // 打开到数据库的最大连接数
	LogMode       string `json:",default=dev,options=dev|test|prod|silent"` // 是否开启Gorm全局日志
	LogZap        bool   // 是否通过zap写入日志文件
	SlowThreshold int64  `json:",default=1000"`
}

func (*Mysql) Dsn

func (m *Mysql) Dsn() string

func (*Mysql) GetColorful

func (m *Mysql) GetColorful() bool

func (*Mysql) GetGormLogMode

func (m *Mysql) GetGormLogMode() logger.LogLevel

func (*Mysql) GetSlowThreshold

func (m *Mysql) GetSlowThreshold() time.Duration

type PgSql

type PgSql struct {
	Username      string
	Password      string
	Path          string
	Port          int    `json:",default=5432"`
	SslMode       string `json:",default=disable,options=disable|enable"`
	TimeZone      string `json:",default=Asia/Shanghai"`
	Dbname        string
	MaxIdleConns  int    `json:",default=10"`                               // 空闲中的最大连接数
	MaxOpenConns  int    `json:",default=10"`                               // 打开到数据库的最大连接数
	LogMode       string `json:",default=dev,options=dev|test|prod|silent"` // 是否开启Gorm全局日志
	LogZap        bool   // 是否通过zap写入日志文件
	SlowThreshold int64  `json:",default=1000"`
}

func (*PgSql) Dsn

func (m *PgSql) Dsn() string

func (*PgSql) GetColorful

func (m *PgSql) GetColorful() bool

func (*PgSql) GetGormLogMode

func (m *PgSql) GetGormLogMode() logger.LogLevel

func (*PgSql) GetSlowThreshold

func (m *PgSql) GetSlowThreshold() time.Duration

type PrimaryQueryCtxFn

type PrimaryQueryCtxFn func(conn *gorm.DB, v, primary interface{}) error

PrimaryQueryCtxFn defines the query method that based on primary keys.

type QueryCtxFn

type QueryCtxFn func(conn *gorm.DB, v interface{}) error

QueryCtxFn defines the query method.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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