Documentation ¶
Index ¶
- Variables
- func IsNoRows(err error) bool
- type CacheKey
- type Cacheable
- type CacheableDB
- func (c *CacheableDB) CacheGet(destStructPtr Cacheable, fields ...string) error
- func (c *CacheableDB) CacheGetByWhere(destStructPtr Cacheable, whereNamedCond string) error
- func (c *CacheableDB) CreateCacheKey(structPtr Cacheable, fields ...string) (CacheKey, reflect.Value, error)
- func (c *CacheableDB) CreateCacheKeyByFields(fields []string, values []interface{}) (string, error)
- func (c *CacheableDB) CreateGetQuery(whereFields ...string) string
- func (c *CacheableDB) DeleteCache(srcStructPtr Cacheable, fields ...string) error
- func (c *CacheableDB) PutCache(srcStructPtr Cacheable, fields ...string) error
- type Config
- type DB
- func (d *DB) Callback(fn func(sqlx.DbOrTx) error, tx ...*sqlx.Tx) error
- func (d *DB) CallbackInSession(fn func(context.Context, *sqlx.Conn) error, ctx ...context.Context) error
- func (d *DB) GetCacheableDB(tableName string) (*CacheableDB, error)
- func (d *DB) RegCacheableDB(ormStructPtr Cacheable, cacheExpiration time.Duration) (*CacheableDB, error)
- func (d *DB) TransactCallback(fn func(*sqlx.Tx) error, tx ...*sqlx.Tx) (err error)
- func (d *DB) TransactCallbackInSession(fn func(context.Context, *sqlx.Tx) error, ctx ...context.Context) (err error)
- type Int32s
- type Int64s
- type Int8IFaceMap
- type IntStringMap
- type PreDB
- type Strings
Constants ¶
This section is empty.
Variables ¶
var ErrCacheNil = errors.New("*DB.Cache (redis) is nil")
ErrCacheNil error: *DB.Cache (redis) is nil
var ErrNoRows = sql.ErrNoRows
ErrNoRows is returned by Scan when QueryRow doesn't return a row. In such a case, QueryRow returns a placeholder *Row value that defers this error until a Scan.
Functions ¶
Types ¶
type CacheKey ¶
type CacheKey struct { Key string FieldValues []interface{} // contains filtered or unexported fields }
CacheKey cache key and values corresponding to primary keys
type Cacheable ¶
type Cacheable interface {
TableName() string
}
Cacheable the interface that can use cache. It must be orm-struct.
type CacheableDB ¶
type CacheableDB struct { *DB // contains filtered or unexported fields }
CacheableDB cacheable DB handle
func (*CacheableDB) CacheGet ¶
func (c *CacheableDB) CacheGet(destStructPtr Cacheable, fields ...string) error
CacheGet selects one row by primary key. Priority from the read cache. NOTE:
If the cache does not exist, then write the cache; destStructPtr must be a *struct type; If fields is empty, auto-use primary fields.
func (*CacheableDB) CacheGetByWhere ¶
func (c *CacheableDB) CacheGetByWhere(destStructPtr Cacheable, whereNamedCond string) error
CacheGetByWhere selects one row by the whereNamedCond. Priority from the read cache. NOTE:
If the cache does not exist, then write the cache; destStructPtr must be a *struct type; whereNamedCond e.g. 'id=:id AND created_at>1520000000'.
func (*CacheableDB) CreateCacheKey ¶
func (c *CacheableDB) CreateCacheKey(structPtr Cacheable, fields ...string) (CacheKey, reflect.Value, error)
CreateCacheKey creates cache key and fields' values. NOTE:
If fields is empty, auto-use primary fields.
func (*CacheableDB) CreateCacheKeyByFields ¶
func (c *CacheableDB) CreateCacheKeyByFields(fields []string, values []interface{}) (string, error)
CreateCacheKeyByFields creates the cache key string by specified fields and values.
func (*CacheableDB) CreateGetQuery ¶
func (c *CacheableDB) CreateGetQuery(whereFields ...string) string
CreateGetQuery creates query string of selecting one row data. NOTE:
If whereFields is empty, auto-use primary fields.
func (*CacheableDB) DeleteCache ¶
func (c *CacheableDB) DeleteCache(srcStructPtr Cacheable, fields ...string) error
DeleteCache deletes one row form cache by primary key. NOTE:
destStructPtr must be a *struct type; If fields is empty, auto-use primary fields.
type Config ¶
type Config struct { Database string Username string Password string Host string Port int // the maximum number of connections in the idle connection pool. // // If MaxOpenConns is greater than 0 but less than the new MaxIdleConns // then the new MaxIdleConns will be reduced to match the MaxOpenConns limit // // If n <= 0, no idle connections are retained. MaxIdleConns int `yaml:"max_idle_conns"` // the maximum number of open connections to the database. // If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than // MaxIdleConns, then MaxIdleConns will be reduced to match the new // MaxOpenConns limit // // If n <= 0, then there is no limit on the number of open connections. // The default is 0 (unlimited). MaxOpenConns int `yaml:"max_open_conns"` // maximum amount of second a connection may be reused. // If d <= 0, connections are reused forever. ConnMaxLifetime int64 `yaml:"conn_max_lifetime"` // NoCache whether to disable cache NoCache bool `yaml:"no_cache"` // contains filtered or unexported fields }
Config db config
func ReadConfig ¶
ReadConfig gets a mysql db config form yaml.
type DB ¶
DB is a wrapper around sqlx.DB and redis.Client.
func (*DB) CallbackInSession ¶
func (d *DB) CallbackInSession(fn func(context.Context, *sqlx.Conn) error, ctx ...context.Context) error
CallbackInSession non-transactional operations in one session.
func (*DB) GetCacheableDB ¶
func (d *DB) GetCacheableDB(tableName string) (*CacheableDB, error)
GetCacheableDB returns the specified *CacheableDB
func (*DB) RegCacheableDB ¶
func (d *DB) RegCacheableDB(ormStructPtr Cacheable, cacheExpiration time.Duration) (*CacheableDB, error)
RegCacheableDB registers a cacheable table.
func (*DB) TransactCallback ¶
TransactCallback transactional operations. nOTE: if an error is returned, the rollback method should be invoked outside the function.
func (*DB) TransactCallbackInSession ¶
func (d *DB) TransactCallbackInSession(fn func(context.Context, *sqlx.Tx) error, ctx ...context.Context) (err error)
TransactCallbackInSession transactional operations in one session. nOTE: if an error is returned, the rollback method should be invoked outside the function.
type Int32s ¶
type Int32s []int32
Int32s db column value type.
type Int64s ¶
type Int64s []int64
Int64s db column value type.
type Int8IFaceMap ¶
type Int8IFaceMap map[int8]interface{}
Int8IFaceMap db column value type.
func (*Int8IFaceMap) Scan ¶
func (i *Int8IFaceMap) Scan(value interface{}) error
Scan implements the sql.Scanner interface.
type IntStringMap ¶
IntStringMap db column value type.
func (*IntStringMap) Scan ¶
func (i *IntStringMap) Scan(value interface{}) error
Scan implements the sql.Scanner interface.
type PreDB ¶
type PreDB struct { *DB // contains filtered or unexported fields }
PreDB preset *DB
func (*PreDB) RegCacheableDB ¶
func (p *PreDB) RegCacheableDB(ormStructPtr Cacheable, cacheExpiration time.Duration, initQuery string, args ...interface{}) (*CacheableDB, error)
RegCacheableDB registers a cacheable table.