mysql

package module
Version: v3.0.4 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

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