sqlhelper

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2021 License: BSD-2-Clause Imports: 17 Imported by: 0

README

sqlhelper

uptrace/bun的代理对象,用于解决对pg,mysql和sqlite3的连接问题

本项目使用bun而不是xorm或者gorm这些老牌的项目主要是因为:

  1. bun对postgresql有更好的支持
  2. 使用标准库database/sql的接口定义
  3. 文档更好些

注意:请使用v0.0.5以上的版本

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DB = New()

DB 默认的数据库代理对象

View Source
var DefaultOpts = Options{
	URL: "sqlite://:memory:?cache=shared",
}
View Source
var ErrProxyAllreadySettedUniversalClient = errors.New("代理不能重复设置客户端对象")

ErrProxyAllreadySettedUniversalClient 代理已经设置过redis客户端对象

View Source
var ErrProxyNotYetSettedUniversalClient = errors.New("代理还未设置客户端对象")

ErrProxyNotYetSettedUniversalClient 代理还未设置客户端对象

View Source
var ErrUnSupportSchema = errors.New("未支持的数据库管理服务类型")

ErrUnSupportSchema 未支持的数据库管理服务类型

View Source
var ErrUnknownClientType = errors.New("未知的redis客户端类型")

ErrUnknownClientType 未知的redis客户端类型

Functions

func NewDB added in v0.0.7

func NewDB(URL string, dopts *Options) (*bun.DB, error)

func SetPool added in v0.0.7

func SetPool(sqldb *sql.DB, opts *Options)

SetPool 设置连接池信息

Types

type Callback

type Callback func(cli *bun.DB) error

Callback redis操作的回调函数

type Option

type Option interface {
	Apply(*Options)
}

Option configures how we set up the connection.

func WithConnMaxIdleTimeMS

func WithConnMaxIdleTimeMS(ConnMaxIdleTimeMS int) Option

WithConnMaxIdleTimeMS 设置连接池的最大空闲连接超时时间,单位ms

func WithConnMaxLifetimeMS

func WithConnMaxLifetimeMS(ConnMaxLifetimeMS int) Option

WithConnMaxLifetimeMS 设置连接池的最大连接超时时间,单位ms

func WithDiscardUnknownColumns

func WithDiscardUnknownColumns() Option

WithDiscardUnknownColumns 设置当有未知列时不报错

func WithInstance added in v0.0.7

func WithInstance(cli *bun.DB) Option

func WithLogger added in v0.0.5

func WithLogger(logger logrus.FieldLogger) Option

WithLogger 用于添加logger方便debug

func WithMaxIdleConns

func WithMaxIdleConns(MaxIdleConns int) Option

WithMaxIdleConns 设置连接池的最大空闲连接数

func WithMaxOpenConns

func WithMaxOpenConns(MaxOpenConns int) Option

WithMaxOpenConns 设置连接池的最大连接数

func WithParallelCallback

func WithParallelCallback() Option

WithParallelCallback 设置初始化后回调并行执行而非串行执行

func WithQueryTimeoutMS added in v0.0.5

func WithQueryTimeoutMS(QueryTimeout int) Option

WithQueryTimeoutMS 设置最大请求超时,单位ms

func WithURL

func WithURL(URL string) Option

WithURL 使用要连接的数据库管理系统的url

type Options

type Options struct {
	URL              string        // 只在Init方法中生效
	Cli              *bun.DB       // 只在Init方法中生效
	Parallelcallback bool          // 只在Init方法中生效
	QueryTimeout     time.Duration // 只在Init方法中生效

	MaxOpenConns          int
	ConnMaxLifetime       time.Duration
	MaxIdleConns          int
	ConnMaxIdleTime       time.Duration
	DiscardUnknownColumns bool
	Logger                logrus.FieldLogger
}

Option 设置key行为的选项 @attribute MaxTTL time.Duration 为0则不设置过期 @attribute AutoRefresh string 需要为crontab格式的字符串,否则不会自动定时刷新

type Proxy

type Proxy struct {
	*bun.DB
	// contains filtered or unexported fields
}

Proxy bun客户端的代理

func New

func New() *Proxy

New 创建一个新的数据库客户端代理

func (*Proxy) Init

func (proxy *Proxy) Init(opts ...Option) error

Init 初始化代理对象

func (*Proxy) IsOk

func (proxy *Proxy) IsOk() bool

IsOk 检查代理是否已经可用

func (*Proxy) NewCtx

func (proxy *Proxy) NewCtx() (ctx context.Context, cancel context.CancelFunc)

NewCtx 根据注册的超时时间构造一个上下文

func (*Proxy) Regist

func (proxy *Proxy) Regist(cb Callback) error

Regist 注册回调函数,在init执行后执行回调函数 如果对象已经设置了被代理客户端则无法再注册回调函数

func (*Proxy) SetConnect

func (proxy *Proxy) SetConnect(cli *bun.DB, parallelcallback bool) error

SetConnect 设置连接的客户端 @params cli *bun.DB bun的DB对象

func (*Proxy) SetQueryTimeout added in v0.0.6

func (proxy *Proxy) SetQueryTimeout(timeout time.Duration)

SetQueryTimeout 设置连接的请求超时 @params timeout time.Duration

Jump to

Keyboard shortcuts

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