mysql

package
v6.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2019 License: Apache-2.0 Imports: 17 Imported by: 49

README

mysql

A mysql ORM(Object Role Modeling) package with redis cache.

Example

import (
    "encoding/json"
    "testing"
    "time"

	"github.com/xiaoenai/tp-micro/model/mysql"
    "github.com/xiaoenai/tp-micro/model/redis"
)

type testTable struct {
    TestId      int64
    TestContent string
    Deleted     bool `db:"test_deleted"`
}

func (t *testTable) TableName() string {
    return "test_table"
}

func TestCacheDb(t *testing.T) {
    dbConf, err := mysql.ReadConfig("test_db")
    if err != nil {
        t.Fatal(err)
    }
    dbConf.Database = "test"
    redisConf, err := redis.ReadConfig("test_redis")
    if err != nil {
        t.Fatal(err)
    }
    db, err := mysql.Connect(dbConf, redisConf)
    if err != nil {
        t.Fatal(err)
    }
    _, err = db.Exec("CREATE TABLE IF NOT EXISTS `dbtest` (`test_id` INT(10) AUTO_INCREMENT, `test_content` VARCHAR(20), `test_deleted` TINYINT(2),  PRIMARY KEY(`test_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='测试表'")
    if err != nil {
        t.Fatal(err)
    }
    c, err := db.RegCacheableDB(new(testTable), time.Second)
    if err != nil {
        t.Fatal(err)
    }
    obj := &testTable{
        TestId:      1,
        TestContent: "abc",
        Deleted:     false,
    }
    _, err = c.NamedExec("INSERT INTO dbtest (test_id,test_content,test_deleted)VALUES(:test_id,:test_content,:test_deleted) ON DUPLICATE KEY UPDATE test_id=:test_id", obj)
    if err != nil {
        t.Fatal(err)
    }

    // query and cache
    dest := &testTable{TestId: 1}
    err = c.CacheGet(dest)
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("first:%#v", dest)

    // verify the cache
    cacheKey, _, err := c.CreateCacheKey(dest)
    t.Logf("cacheKey:%#v", cacheKey)
    key := cacheKey.Key
    if err != nil {
        t.Fatal(err)
    }
    b, err := c.Cache.Get(key).Bytes()
    if err != nil {
        t.Fatal(err)
    }
    var v1 = new(testTable)
    err = json.Unmarshal(b, v1)
    if err != nil {
        t.Fatal(err)
    }
    t.Logf("cache of before expiring: %+v", v1)

    time.Sleep(2 * time.Second)

    b, err = c.Cache.Get(key).Bytes()
    if err == nil {
        var v2 = new(testTable)
        err = json.Unmarshal(b, v2)
        if err != nil {
            t.Fatal(err)
        }
        t.Fatalf("expired but not deleted: %#v", v2)
    }
    t.Logf("expired cache error: %v", err)
}
go test -v -run=TestCacheDb

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheNil = errors.New("*DB.Cache (redis) is nil")

ErrCacheNil error: *DB.Cache (redis) is nil

View Source
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

func IsNoRows

func IsNoRows(err error) bool

IsNoRows is the data exist or not.

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.

func (*CacheableDB) PutCache

func (c *CacheableDB) PutCache(srcStructPtr Cacheable, fields ...string) error

PutCache caches one row 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 NewConfig

func NewConfig() *Config

NewConfig creates a default config.

func ReadConfig

func ReadConfig(configSection string) (*Config, error)

ReadConfig gets a mysql db config form yaml.

func (*Config) Reload

func (cfg *Config) Reload(bind cfgo.BindFunc) error

Reload sync automatically config from config file.

func (*Config) Source

func (cfg *Config) Source() string

Source returns the mysql connection string.

type DB

type DB struct {
	*sqlx.DB
	Cache *redis.Client
	// contains filtered or unexported fields
}

DB is a wrapper around sqlx.DB and redis.Client.

func Connect

func Connect(dbConfig *Config, redisConfig *redis.Config) (*DB, error)

Connect to a database and verify with a ping.

func (*DB) Callback

func (d *DB) Callback(fn func(sqlx.DbOrTx) error, tx ...*sqlx.Tx) error

Callback non-transactional operations.

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

func (d *DB) TransactCallback(fn func(*sqlx.Tx) error, tx ...*sqlx.Tx) (err error)

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.

func (*Int32s) Scan

func (i *Int32s) Scan(value interface{}) error

Scan implements the sql.Scanner interface.

func (Int32s) Value

func (i Int32s) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type Int64s

type Int64s []int64

Int64s db column value type.

func (*Int64s) Scan

func (i *Int64s) Scan(value interface{}) error

Scan implements the sql.Scanner interface.

func (Int64s) Value

func (i Int64s) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

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.

func (Int8IFaceMap) Value

func (i Int8IFaceMap) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type IntStringMap

type IntStringMap map[int]string

IntStringMap db column value type.

func (*IntStringMap) Scan

func (i *IntStringMap) Scan(value interface{}) error

Scan implements the sql.Scanner interface.

func (IntStringMap) Value

func (i IntStringMap) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type PreDB

type PreDB struct {
	*DB
	// contains filtered or unexported fields
}

PreDB preset *DB

func NewPreDB

func NewPreDB() *PreDB

NewPreDB creates a unconnected *DB

func (*PreDB) Init

func (p *PreDB) Init(dbConfig *Config, redisConfig *redis.Config) (err error)

Init initialize *DB.

func (*PreDB) Init2

func (p *PreDB) Init2(dbConfig *Config, redisClient *redis.Client) (err error)

Init2 initialize *DB.

func (*PreDB) RegCacheableDB

func (p *PreDB) RegCacheableDB(ormStructPtr Cacheable, cacheExpiration time.Duration, initQuery string, args ...interface{}) (*CacheableDB, error)

RegCacheableDB registers a cacheable table.

type Strings

type Strings []string

Strings db column value type.

func (*Strings) Scan

func (s *Strings) Scan(value interface{}) error

Scan implements the sql.Scanner interface.

func (Strings) Value

func (s Strings) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

Jump to

Keyboard shortcuts

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