mysql

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: BSD-3-Clause Imports: 16 Imported by: 0 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ginny-mysql

mysql provider for ginny.

Documentation

Index

Constants

This section is empty.

Variables

Provider

Functions

func ConvertEntityToMap

func ConvertEntityToMap(entity interface{}) (map[string]interface{}, error)

ConvertEntityToMap

func GetTrans

func GetTrans(ctx context.Context) (context.Context, *sql.Tx)

从ctx获取事务上下文

func NewTrans

func NewTrans(ctx context.Context, tx *sql.Tx) context.Context

创建事务上下文

func Transaction

func Transaction(c context.Context, m *MysqlDB, fn func(ctx context.Context) error) error

执行事务

Types

type Config

type Config struct {
	WDB         Source   `json:"wdb" mapstructure:"wdb"`
	RDBs        []Source `json:"rdbs" mapstructure:"rdbs"`
	DBName      string   `json:"db_name" mapstructure:"db_name"`
	MaxOpenConn int      `json:"max_open_conn" mapstructure:"max_open_conn"`
	MaxIdleConn int      `json:"max_idle_conn" mapstructure:"max_idle_conn"`
	MaxLifetime int      `json:"max_lifetime" mapstructure:"max_lifetime"`
}

Config DB基础配置

func NewConfig

func NewConfig(v *viper.Viper) (*Config, error)

NewConfig

func (*Config) String

func (c *Config) String() string

String 打印可输出的配置

type MysqlDB

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

MysqlDB 数据库管理器 读写分离 仅对同一业务库

func NewMysqlDB

func NewMysqlDB(ctx context.Context, config *Config, logger *zap.Logger) (*MysqlDB, error)

NewMysqlDB 根据基础配置 初始化数据库

func (*MysqlDB) Close

func (m *MysqlDB) Close() error

Close 关闭所有读写连接池,停止keepalive保活协程。该函数应当很少使用到

func (*MysqlDB) RDB

func (m *MysqlDB) RDB() *sql.DB

RDB 随机返回一个读库

func (*MysqlDB) WDB

func (m *MysqlDB) WDB() *sql.DB

WDB 返回唯一写库

type Source

type Source struct {
	Host string `json:"host" mapstructure:"host"`
	User string `json:"user" mapstructure:"user"`
	Pass string `json:"pass" mapstructure:"pass"`
}

Source DB部署实例数据源配置

func (*Source) String

func (s *Source) String() string

String 打印可输出的配置

type SqlBuilder

type SqlBuilder struct {
	DB    *MysqlDB
	Query *query
	// contains filtered or unexported fields
}

SqlBuilder

func NewSqlBuilder

func NewSqlBuilder(ctx context.Context, config *Config, logger *zap.Logger) (*SqlBuilder, error)

NewSqlBuilder

func (*SqlBuilder) Delete

func (s *SqlBuilder) Delete(ctx context.Context, table string,
	where map[string]interface{}) (int64, error)

Delete deletes matched records in COLUMNS

func (*SqlBuilder) ExecuteSql

func (s *SqlBuilder) ExecuteSql(ctx context.Context, sqlStr string,
	bindMap map[string]interface{}) (int64, error)

ExecuteSql by native sql

func (*SqlBuilder) Find

func (s *SqlBuilder) Find(ctx context.Context, entity interface{},
	table string, where map[string]interface{}, selectFields ...[]string) error

Find gets one record from table by condition "where"

func (*SqlBuilder) FindAll

func (s *SqlBuilder) FindAll(ctx context.Context, entity interface{},
	table string, where map[string]interface{}, selectFields ...[]string) error

FindAll gets multiple records from table by condition "where"

func (*SqlBuilder) Insert

func (s *SqlBuilder) Insert(ctx context.Context, table string, entity interface{}) (int64, error)

Insert inserts data into table

func (*SqlBuilder) QuerySql

func (s *SqlBuilder) QuerySql(ctx context.Context, sqlStr string,
	bindMap map[string]interface{}, entity interface{}) error

QuerySql by native sql

func (*SqlBuilder) Update

func (s *SqlBuilder) Update(ctx context.Context, table string,
	where, update map[string]interface{}) (int64, error)

Update updates the table COLUMNS

Jump to

Keyboard shortcuts

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