mysql

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

README

MySql

基于xorm封装的MySQL库

License GoDoc Go Report Card

特性

  • 简化MySQL实例初始化流程,基于配置自动对MySQL进行初始化
  • 支持连接池、多实例等场景
  • 使用该库,建议再看看ant-coder项目,可以基于数据表的字段和索引生成响应的model代码

安装

go get github.com/ant-libs-go/mysql

快速开始

  • toml 配置文件

    [mysql.default]
        user = "root"
        pawd = "123456"
        host = "127.0.0.1"
        port = "3306"
        name = "business"
    [mysql.stats]
        user = "root"
        pawd = "123456"
        host = "127.0.0.1"
        port = "3306"
        name = "business"
    
  • 使用方法

    // 初始化config包,参考config模块
    code...
    
    // 验证mysql实例的配置正确性与连通性。非必须
    if err = mysql.Valid(); err != nil {
        fmt.Printf("mysql error: %s\n", err)
        os.Exit(-1)
    }
    
    // 如下方式可以直接使用MySQL实例
    mysql.DefaultClient().SQL("SELECT * FROM t").Find(&rows)
    mysql.Client("default").SQL("SELECT * FROM t").Find(&rows)
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Client

func Client(name string) (r *gorm.DB)

func DefaultClient

func DefaultClient() (r *gorm.DB)

func NewMysqlPool

func NewMysqlPool(cfg *Cfg) *gorm.DB

func Pool

func Pool(name string) (r *gorm.DB)

func SafeClient

func SafeClient(name string) (r *gorm.DB, err error)

func SafePool

func SafePool(name string) (r *gorm.DB, err error)

func Valid

func Valid(names ...string) (err error)

验证mysql实例的配置正确性与连通性。 参数names是实例的名称列表,如果为空则检测所有配置的实例

Types

type Cfg

type Cfg struct {
	// dial
	Engine   string `toml:"engine"` // postgressql/mysql
	DialUser string `toml:"user"`
	DialPawd string `toml:"pawd"`
	DialHost string `toml:"host"`
	DialPort string `toml:"port"`
	DialName string `toml:"name"`

	Debug bool `toml:"debug"` // 是否显示sql语句

	// pool
	PoolMaxOpenConn     int           `toml:"max_open_conn"`     // 最大连接数大小
	PoolMaxIdleConn     int           `toml:"max_idle_conn"`     // 最大空闲的连接的个数
	PoolConnMaxLifetime time.Duration `toml:"conn_max_lifetime"` // 连接的生命时间,超过此时间,连接将关闭后重新建立新的,0代表忽略相关判断,单位:second
}

Jump to

Keyboard shortcuts

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