mysql

package module
Version: v0.0.0-...-428a524 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2020 License: MIT Imports: 12 Imported by: 0

README

mysql-go

a simple mysql package for golang projects base on xorm,support connection pools and master&slave operation

install

go get github.com/scofieldpeng/mysql-go/v3

Usage

First, create a ini file under your $ProjectPath/config(or any dir that you like) directory like below:

# node_* sections are the mysql nodes configurations,the node name is the `*` part,ie node_default,the node name is default
[mysql_node_default]
# mysql dsn driver configuration,more detail see here https://github.com/go-sql-driver/mysql#dsn-data-source-name
dsn=root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4

# if the node have slaves nodes,add the node name,each nodes devide by `,`
# note make every slave have configuration in the ini file!!!
slave=slave1,slave2,slave3

# connection pool max connect number, only affect to this node
maxConn=100
# max idle connect number, only affect to this node
maxIdle=5

slave config

[mysql_node_slave1] dsn=root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4

Second, initialize the mysql engine

// initialize the mysql ini file,more document see the package https://github.com/scofieldpeng/config
connConfig := config.Data("mysql")
mysqlConfig := mysql.Config{
    Debug: false,
    MaxIdle:5,
    MaxConn:10,
}
if err := mysql.Init(mysqlConfig,mysqlFileConfig);err != nil {
    fmt.Println(err)
}

Now, you can use as you like

// return a xorm engine object,now you can use mysql operation freely
xormEngine := mysql.Select("default").Engine()

// or you can use the master node
xormMasterEngine := mysql.Select("default").Master()

// most of time we always choose the master node, but you want to use slave,you can use the Slave method,
// when you set the slave name param, you can operation the specific slave node, or the system will return a random slave node
xormSlaveEngine := mysql.Select("default").Slave("slave1")

if you are not familiar with xorm, you can see the xorm document http://xorm.io

Licence

MIT Licence

Thanks

https://github.com/go-xorm/xorm

Documentation

Overview

mysql-go is a mysql library based on xorm[http://xorm.io]

config

“`ini [mysql_node_default] dsn= slave=abc,abc1,abc2,abc3 # 最大空闲数量 maxIdle=10 # 最大连接数量 maxConn=100 “`

usage:

“`go // 获取主 engine := mysql.Select().Master()

// 获取主 engine := mysql.Select().Engine()

// 获取slave节点 engine := mysql.Select("default").Slave() “`

Index

Constants

View Source
const (
	DefaultIdleNum     = 5                               // 默认的连接池空闲数大小
	DefaultMaxOpenConn = 10                              // 默认的最大打开连接数
	DefaultTimeout     = time.Duration(30) * time.Second // 默认超时时间

	DEFAULT_CONN_PREFIX = "mysql_node_"
)

Variables

View Source
var (

	// Engine没有找到
	ErrEngineNotFound = errors.New("not found engine")
	// mysql操作影响了0行
	ErrAffectedZeroRow = errors.New("affect 0 rows")
)

Functions

func Init

func Init(mysqlConfig Config, connConfig ini.File) error

初始化

func Select

func Select(node ...string) (e *engine)

选择某个节点,如果没有选择节点名称,默认选择default节点 BUG: 当用户没有设置default节点配置时如果又通过default来获取,可能会获取不到结果

Types

type Config

type Config struct {
	Debug bool
	// 最大空闲数量
	MaxIdle int
	// 最大连接数量
	MaxConn int
	// 超时时间
	Timeout time.Duration
	// log writer
	LogWriter io.Writer
	// log prefix
	LogPrefix string
	// log flag
	LogFlag int
	// log level
	LogLevel core.LogLevel
}

engine mysql的连接对象

func (*Config) Set

func (c *Config) Set(config Config)

设置config,如果传入的值存在,则覆盖默认配置项目

type Table

type Table interface {
	SelfObj() *xorm.TableName
}

type TableFactory

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

func (*TableFactory) Delete

func (tf *TableFactory) Delete() (int64, error)

删除表结构

func (*TableFactory) Find

func (tf *TableFactory) Find(whereBuilder *WhereBuilder, fields string, orderBy string, listResult interface{}, fromMaster ...bool) (err error)

查询列表

func (*TableFactory) Get

func (tf *TableFactory) Get(fromMaster ...bool) (bool, error)

查询一条数据

func (*TableFactory) Insert

func (tf *TableFactory) Insert() (affectRows int64, err error)

func (TableFactory) Myself

func (tf TableFactory) Myself() interface{}

func (*TableFactory) SetMyself

func (tf *TableFactory) SetMyself(self func() interface{})

func (*TableFactory) SetTableNode

func (tf *TableFactory) SetTableNode(node string)

func (TableFactory) TableNode

func (tf TableFactory) TableNode() string

func (*TableFactory) Update

func (tf *TableFactory) Update(whereBuilder *WhereBuilder, updateFields ...string) (int64, error)

更新表

type WhereBuilder

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

WhereBuilder用来构建xorm的engine所需的where模块 使用方法很easy mysql.NewWhereBuilder() mysql.Add("id",1) 然后使用的时候直接mysql.Select()

func NewWhereBuilder

func NewWhereBuilder(args ...map[string]interface{}) *WhereBuilder

func (*WhereBuilder) Add

func (wb *WhereBuilder) Add(condition string, args interface{}) *WhereBuilder

添加参数,需要注意的是,原来的xorm中where条件是id>? AND id

func (*WhereBuilder) Encode

func (wb *WhereBuilder) Encode() (whereStr string, beans []interface{})

Encode用来生成xorm的engine.Where()条件的两个参数

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL