sqlhelper

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2021 License: BSD-2-Clause Imports: 15 Imported by: 0

README

sqlhelper

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

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

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

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

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

This section is empty.

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 WithMaxIdleConns

func WithMaxIdleConns(MaxIdleConns int) Option

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

func WithMaxOpenConns

func WithMaxOpenConns(MaxOpenConns int) Option

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

func WithParallelCallback

func WithParallelCallback() Option

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

func WithQueryTimeout

func WithQueryTimeout(QueryTimeout time.Duration) Option

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

func WithURL

func WithURL(URL string) Option

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

type Options

type Options struct {
	URL                   string
	Parallelcallback      bool
	QueryTimeout          time.Duration
	MaxOpenConns          int
	ConnMaxLifetime       time.Duration
	MaxIdleConns          int
	ConnMaxIdleTime       time.Duration
	DiscardUnknownColumns bool
}

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

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) error

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

func (*Proxy) SetPool

func (proxy *Proxy) SetPool(sqldb *sql.DB)

Jump to

Keyboard shortcuts

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