db

package
v0.0.0-...-a69402d Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: MIT Imports: 13 Imported by: 0

README

db

mysql 基础库,开放有限接口,支持日志、opentracing 和 prometheus 监控。

配置

DB 配置,格式为 DB_${NAME}_DSN,内容参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name

必须设置 parseTime 选项。设置好了就可以通过 ${NAME} 获取 DB 连接池。

时区问题参考 https://www.jianshu.com/p/3f7fc9093db4

示例

import "context"
import "github.com/busyfree/leaf-go/util/db"

ctx := context.Background()
c := db.Get(ctx, "default")

sql := "insert into foo(id) values(1)"
q := SQLUpdate("foo", sql)
result, err := c.ExecContext(ctx, q)

// 执行 db 事务
err := c.ExecTx(ctx, func(ctx context.Context, tx db.Conn) error {
	sql := "insert into foo(id) values(1)"
	q := SQLUpdate("foo", sql)
	result, err := c.ExecContext(ctx, q)
	if err != nil {
		return err
	}

	sql := "insert into foo(id) values(2)"
	q := SQLUpdate("foo", sql)
	result, err = tx.ExecContext(ctx, q)
	if err != nil {
		return err
	}

	return nil
})

Documentation

Overview

Package db 提供 mysql 封装

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GatherMetrics

func GatherMetrics()

GatherMetrics 连接池状态指标

func GetXORM

func GetXORM(ctx context.Context, name string) *xorm.Engine

func IsDuplicateEntryErr

func IsDuplicateEntryErr(err error) bool

IsDuplicateEntryErr 判断是否为唯一键冲突错误

func IsNoRowsErr

func IsNoRowsErr(err error) bool

IsNoRowsErr 判断是否为 ErrNoRows 错误

func Reset

func Reset()

Reset 关闭所有 DB 连接 新调用 Get 方法时会使用最新 DB 配置创建连接

如果在配置中开启 HOT_LOAD_DB 开关,则每次下发配置都会重置 DB 连接!

func ResetXORM

func ResetXORM()

ResetXORM 关闭所有 DB 连接 新调用 GetXORM 方法时会使用最新 DB 配置创建连接

Types

type Conn

type Conn interface {
	ExecContext(ctx context.Context, query Query, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query Query, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query Query, args ...interface{}) *sql.Row
}

Conn 简单 DB 接口。用于统一非事务和事务业务逻辑

type DB

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

DB 对象,有限开放 sql.DB 功能,支持上报 metrics

func Get

func Get(ctx context.Context, name string) *DB

Get 根据配置名字创建并返回 DB 连接池对象

DB 配置名字格式为 DB_{$name}_DSN DB 配置内容格式请参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name Get 是并发安全的,可以在多协程下使用

func (*DB) ExecContext

func (db *DB) ExecContext(ctx context.Context, query Query, args ...interface{}) (sql.Result, error)

ExecContext 执行查询,无返回数据

func (*DB) ExecTx

func (db *DB) ExecTx(ctx context.Context, f TxFunc) error

ExecTx 执行一次事务,回调函数返回 err 或者 panic 或者 ctx 取消都会回滚事务。 返回的 err 为 Commit 或者 Rollback 的错误

func (*DB) QueryContext

func (db *DB) QueryContext(ctx context.Context, query Query, args ...interface{}) (*sql.Rows, error)

QueryContext 执行查询,返回多行数据

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(ctx context.Context, query Query, args ...interface{}) *sql.Row

QueryRowContext 执行查询,至多返回一行数据

type Query

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

Query sql 查询对象

func SQLDelete

func SQLDelete(table string, sql string) Query

SQLDelete 构造 delete 查询

func SQLInsert

func SQLInsert(table string, sql string) Query

SQLInsert 构造 insert 查询

func SQLSelect

func SQLSelect(table string, sql string) Query

SQLSelect 构造 select 查询

func SQLUpdate

func SQLUpdate(table string, sql string) Query

SQLUpdate 构造 update 查询

type Tx

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

Tx 事务对象简单封装

func (*Tx) ExecContext

func (tx *Tx) ExecContext(ctx context.Context, query Query, args ...interface{}) (sql.Result, error)

ExecContext 执行写查询 不鼓励在事务中使用读查询,所以只提供 ExecContext 方法 框架会根据返回错误自动提交或者回滚,所以不提供相应方法

func (*Tx) QueryContext

func (tx *Tx) QueryContext(ctx context.Context, query Query, args ...interface{}) (*sql.Rows, error)

QueryContext 在事务中查询多行数据

func (*Tx) QueryRowContext

func (tx *Tx) QueryRowContext(ctx context.Context, query Query, args ...interface{}) *sql.Row

QueryRowContext 在事务中查询单行数据

type TxFunc

type TxFunc func(ctx context.Context, tx Conn) error

TxFunc 事务函数,返回 err 会回滚未提交事务,否则自动提交事务

Jump to

Keyboard shortcuts

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