gmysql

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DEFAULT_MAX_CONN      int = 10   //默认最大连接数
	DEFAULT_IDLE_CONN     int = 5    //默认闲置连接数
	DEFAULT_READ_TIMEOUT  int = 1000 // 默认读超时
	DEFAULT_WRITE_TIMEOUT int = 1000 // 默认读超时
	DEFAULT_TIMEOUT       int = 1000 // 默认超时

)

Variables

View Source
var (
	ERR_DB_GROUP_NOT_FOUND   = errors.New("此DB不存在")
	ERR_DB_CONN_NOT_FOUND    = errors.New("没有可用DB连接")
	ERR_QUERY_RESULT_INVALID = errors.New("result传参类型必须是*[]*ElemType")
	ERR_RECURSION_TX         = errors.New("嵌套开启了事务")
	ERR_INVALID_TX           = errors.New("非事务不能提交或回滚")
	ERR_INVALID_PARAM        = errors.New("错误的参数")
	ERR_BEGIN_TX_FAILED      = errors.New("开启事务失败")
)

错误码

View Source
var DEFAULT_DBOPTION = &DBOptions{}
View Source
var DEFAULT_OTHER_CONDS = &DBOtherConds{
	Fields:  "*",
	GroupBy: "",
	OrderBy: "",
	Limit:   "",
}

Functions

This section is empty.

Types

type DBConf

type DBConf struct {
	GroupConfList []DBGroupConf `toml:"Group"`
}

type DBConn

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

单个Mysql连接

func (*DBConn) Begin

func (dbConn *DBConn) Begin(context context.Context) (txConn *DBConn, err error)

开启事务

func (*DBConn) Commit

func (dbConn *DBConn) Commit(context context.Context) (err error)

提交事务

func (*DBConn) Exec

func (dbConn *DBConn) Exec(context context.Context, sql string, values ...interface{}) (result int64, err error)

SQL写入

func (*DBConn) JDBC

func (dbConn *DBConn) JDBC() (dsn string)

生成JDBC风格的DSN

func (*DBConn) Query

func (dbConn *DBConn) Query(context context.Context, result interface{}, sql string, values ...interface{}) (err error)

SQL查询

func (*DBConn) Rollback

func (dbConn *DBConn) Rollback(context context.Context) (err error)

回滚事务

type DBConnConf

type DBConnConf struct {
	Host string `toml:"host"` // host
	Port int    `toml:"port"` // 端口
}

单实例配置

type DBGroup

type DBGroup struct {
	Master []*DBConn

	Slave []*DBConn
	// contains filtered or unexported fields
}

func (*DBGroup) Begin

func (dbGroup *DBGroup) Begin(context context.Context) (tx *DBConn, err error)

开启事务

func (*DBGroup) Exec

func (dbGroup *DBGroup) Exec(context context.Context, sql string, values ...interface{}) (result int64, err error)

SQL写入

func (*DBGroup) ORM

func (DBGroup *DBGroup) ORM() *DBOrm

func (*DBGroup) Query

func (dbGroup *DBGroup) Query(context context.Context, result interface{}, sql string, values ...interface{}) (err error)

SQL查询

func (*DBGroup) QueryWithOptions

func (dbGroup *DBGroup) QueryWithOptions(context context.Context, dbOptions *DBOptions, result interface{}, sql string, values ...interface{}) (err error)

type DBGroupConf

type DBGroupConf struct {
	Name     string `toml:"name"`
	Database string `toml:"database"` // 数据库名称
	Username string `toml:"username"` // 用户名
	Password string `toml:"password"` // 密码

	Master *DBSubGroupConf `toml:"Master"` // 主库配置
	Slaves *DBSubGroupConf `toml:"Slave"`  // 从库配置
}

Mysql主从配置

type DBManager

type DBManager struct {
	GroupMap map[string]*DBGroup
}
var GEDB *DBManager

单例管理器

func NewMysql

func NewMysql(config *DBConf) (dbMgr *DBManager, err error)

func (*DBManager) Instance

func (dbMgr *DBManager) Instance(name string) (dbGroup *DBGroup, err error)

根据name获取mysql组

type DBOptions

type DBOptions struct {
	ForceMaster bool
}

type DBOrm

type DBOrm struct {
	Parent *DBGroup
}

func (*DBOrm) Delete

func (dbOrm *DBOrm) Delete(context context.Context, table string, whereConds map[string]interface{}, dbOptions *DBOptions) (result int64, err error)

删除

func (*DBOrm) Escape

func (dbOrm *DBOrm) Escape(sql string) string

func (*DBOrm) Insert

func (dbOrm *DBOrm) Insert(context context.Context, table string, insertMap map[string]interface{}, dbOptions *DBOptions) (result int64, err error)

插入

func (*DBOrm) Select

func (dbOrm *DBOrm) Select(context context.Context, result interface{}, table string, whereConds map[string]interface{}, otherConds *DBOtherConds, dbOptions *DBOptions) (err error)

查询

func (*DBOrm) Update

func (dbOrm *DBOrm) Update(context context.Context, table string, updateMap map[string]interface{}, whereConds map[string]interface{}, dbOptions *DBOptions) (result int64, err error)

更新

type DBOtherConds

type DBOtherConds struct {
	Fields  string
	GroupBy string
	OrderBy string
	Limit   string
}

type DBSubGroupConf

type DBSubGroupConf struct {
	MaxConn      int          `toml:"maxConn"`      // 最大连接数
	IdleConn     int          `toml:"idleConn"`     // 最大保持连接数
	IdleTime     int          `toml:"idleTime"`     // 空闲回收时间
	ReadTimeout  int          `toml:"readTimeout"`  // 读超时时间
	WriteTimeout int          `toml:"writeTimeout"` // 写超时时间
	Timeout      int          `toml:"Timeout"`      // 超时时间
	Instances    []DBConnConf `toml:"Instance"`     // 实例列表
}

Master 或者 Slave配置

Jump to

Keyboard shortcuts

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