gxorm

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2021 License: MIT Imports: 9 Imported by: 0

README

gxorm

基于xorm.io/xorm v0.8.2 封装而成的dao操作库
如果要升级到v1.0.3版本(需要mysql8.0才可以),需要注意xorm api的兼容性

参考手册

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// EngineNotExist engine not found
	EngineNotExist = errors.New("current db engine not exist")
	// EngineNameEmpty engine name is empty
	EngineNameEmpty = errors.New("current engine name is empty")
)

Functions

func CloseAllDb

func CloseAllDb()

CloseAllDb 由于xorm db.Close()是关闭当前连接,一般建议如下函数放在main/init关闭连接就可以

func CloseAllEngineGroup added in v1.1.12

func CloseAllEngineGroup()

CloseAllEngineGroup 关闭当前引擎组连接,一般建议如下函数放在main/init关闭连接就可以

func CloseDbByName

func CloseDbByName(name string) error

CloseDbByName 关闭指定name的db engine

func CloseEngineGroupByName added in v1.1.12

func CloseEngineGroupByName(name string) error

CloseEngineGroupByName 关闭指定name的db engine group

func GetEngineByName

func GetEngineByName(name string) (*xorm.Engine, error)

GetEngineByName 从db engine中获取一个数据库连接句柄 根据数据库连接句柄name获取指定的连接句柄

func GetEngineGroupName added in v1.1.12

func GetEngineGroupName(name string) (*xorm.EngineGroup, error)

GetEngineGroupName 从引擎组中获得一个db engine group

Types

type DbBaseConf

type DbBaseConf struct {
	Ip        string
	Port      int
	User      string
	Password  string
	Database  string
	Charset   string // 字符集 utf8mb4 支持表情符号
	Collation string // 整理字符集 utf8mb4_unicode_ci
	ParseTime bool   // 是否格式化时间
	Loc       string // 时区字符串 Local,PRC

	Timeout      time.Duration // Dial timeout
	ReadTimeout  time.Duration // I/O read timeout
	WriteTimeout time.Duration // I/O write timeout
}

DbBaseConf 数据库基本配置

func (*DbBaseConf) InitDbEngine

func (conf *DbBaseConf) InitDbEngine() (*xorm.Engine, error)

InitDbEngine new a db engine mysql charset查看 mysql> show character set where charset="utf8mb4"; +---------+---------------+--------------------+--------+ | Charset | Description | Default collation | Maxlen | +---------+---------------+--------------------+--------+ | utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 | +---------+---------------+--------------------+--------+ 1 row in set (0.00 sec)

type DbConf

type DbConf struct {
	DbBaseConf

	UsePool      bool // 当前db实例是否采用db连接池,默认不采用,如采用请求配置该参数
	MaxIdleConns int  // 设置连接池的空闲数大小
	MaxOpenConns int  // 最大open connection个数

	// sets the maximum amount of time a connection may be reused.
	// 设置连接可以重用的最大时间
	// 给db设置一个超时时间,时间小于数据库的超时时间
	MaxLifetime time.Duration

	ShowSql bool      // 是否输出sql,输出句柄是logger
	Logger  io.Writer // sql日志输出interface
}

DbConf mysql连接信息 parseTime=true changes the output type of DATE and DATETIME values to time.Time instead of []byte / string The date or datetime like 0000-00-00 00:00:00 is converted into zero value of time.Time.

func (*DbConf) NewEngine

func (conf *DbConf) NewEngine() (*xorm.Engine, error)

NewEngine create a db engine 如果配置上有显示sql执行时间和采用pool机制,就会建立db连接池

func (*DbConf) SetEngineName

func (conf *DbConf) SetEngineName(name string) error

SetEngineName 给当前数据库指定engineName 一般用在多个db 数据库连接引擎的时候,可以给当前的db engine设置一个name 这样业务上游层,就可以通过 GetEngine(name)获得当前db engine

func (*DbConf) ShortConnect

func (conf *DbConf) ShortConnect() (*xorm.Engine, error)

ShortConnect 短连接设置,一般用于短连接服务的数据库句柄

type EngineGroupConf

type EngineGroupConf struct {
	Master DbBaseConf
	Slaves []DbBaseConf

	UsePool bool // 是否采用db连接池,默认不采用,如采用请求配置该参数
	// the following configuration is for the configuration on each instance of master and slave
	// not the overall configuration of the engine group.
	// 下面的配置对于每个实例的配置,并非整个引擎组的配置
	MaxIdleConns int // 设置连接池的空闲数大小
	MaxOpenConns int // 最大open connection个数
	// sets the maximum amount of time a connection may be reused.
	// Expired connections may be closed lazily before reuse.
	// If d <= 0, connections are reused forever.
	MaxLifetime time.Duration

	ShowSql bool      // 是否输出sql,输出句柄是logger
	Logger  io.Writer // sql日志输出interface
}

EngineGroupConf 读写分离引擎配置

func (*EngineGroupConf) NewEngineGroup

func (conf *EngineGroupConf) NewEngineGroup(policies ...xorm.GroupPolicy) (*xorm.EngineGroup, error)

NewEngineGroup 创建读写分离的引擎组,附带一些拓展配置 这里可以采用功能模式,方便后面对引擎组句柄进行拓展 默认采用连接池方式建立连接

func (*EngineGroupConf) SetEngineGroupName added in v1.1.12

func (conf *EngineGroupConf) SetEngineGroupName(name string, policies ...xorm.GroupPolicy) error

SetEngineGroupName 给db engine group设置名字

Jump to

Keyboard shortcuts

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