mysql

package
v0.9.23 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Adapter 实现读写分离的gorm适配器 通过实现gorm里的SQLCommon接口实现读写分离的适配 TODO 读库可能存在多个,可以通过基于权重的副在均衡算法完成连接工作的分配

Index

Constants

View Source
const (
	ColumnCreatedAt = "created_at"
	ColumnUpdatedAt = "updated_at"
)

Variables

This section is empty.

Functions

func Resolver

func Resolver() *dbresolver.DBResolver

func ResolverConfig

func ResolverConfig(item ResolverItem) dbresolver.Config

Types

type Columns

type Columns map[string]interface{}

type Config

type Config struct {
	// 最大空闲连接数
	MaxIdleConnections int

	// 最大打开连接数
	MaxOpenConnections int

	// 最长活跃时间
	MaxLifeTime int

	// dsn
	Dsn string
}

type Manager

type Manager struct {
	*gorm.DB
	// contains filtered or unexported fields
}

func NewManager

func NewManager(conf *Config) *Manager

func (*Manager) Connect

func (db *Manager) Connect() error

type Model

type Model struct {
	Id        int `gorm:"primary_key;AUTO_INCREMENT;column:id" json:"id"`
	CreatedAt int `gorm:"column:created_at" json:"created_at"`
	UpdatedAt int `gorm:"column:updated_at" json:"updated_at"`
}

基础模型

type ReadWriteAdapter

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

ReadWriteAdapter 读写分离适配器

func NewReadWriteAdapter

func NewReadWriteAdapter(dialectType string, dataSourceNames []string) (*ReadWriteAdapter, error)

NewReadWriteAdapter 通过多个dsn打开多个实例的连接 Open concurrently opens each underlying physical db. dataSourceNames must be a semi-comma separated list of DSNs with the first one being used as the master and the rest as slaves.

func (ReadWriteAdapter) Begin

func (adapter ReadWriteAdapter) Begin() (*sql.Tx, error)

Begin starts a transaction on the master. The isolation level is dependent on the driver.

func (ReadWriteAdapter) BeginTx

func (adapter ReadWriteAdapter) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a transaction with the provided context on the master.

The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.

func (ReadWriteAdapter) Exec

func (adapter ReadWriteAdapter) Exec(query string, args ...interface{}) (sql.Result, error)

Exec

func (ReadWriteAdapter) Master

func (adapter ReadWriteAdapter) Master() *sql.DB

Master 获取master连接

func (ReadWriteAdapter) Ping

func (adapter ReadWriteAdapter) Ping() error

Ping verifies if a connection to each physical database is still alive, establishing a connection if necessary.

func (ReadWriteAdapter) Prepare

func (adapter ReadWriteAdapter) Prepare(query string) (*sql.Stmt, error)

Prepare

func (ReadWriteAdapter) Query

func (adapter ReadWriteAdapter) Query(query string, args ...interface{}) (*sql.Rows, error)

Query

func (ReadWriteAdapter) QueryRow

func (adapter ReadWriteAdapter) QueryRow(query string, args ...interface{}) *sql.Row

QueryRow

func (ReadWriteAdapter) SetConnMaxLifetime

func (adapter ReadWriteAdapter) SetConnMaxLifetime(d time.Duration)

SetConnMaxLifetime sets the maximum amount of time a connection may be reused. Expired connections may be closed lazily before reuse. If d <= 0, connections are reused forever.

func (ReadWriteAdapter) SetMaxIdleConns

func (adapter ReadWriteAdapter) SetMaxIdleConns(n int)

SetMaxIdleConns sets the maximum number of connections in the idle connection pool for each underlying physical db. If MaxOpenConns is greater than 0 but less than the new MaxIdleConns then the new MaxIdleConns will be reduced to match the MaxOpenConns limit If n <= 0, no idle connections are retained.

func (ReadWriteAdapter) SetMaxOpenConns

func (adapter ReadWriteAdapter) SetMaxOpenConns(n int)

SetMaxOpenConns sets the maximum number of open connections to each physical database. If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than MaxIdleConns, then MaxIdleConns will be reduced to match the new MaxOpenConns limit. If n <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited).

func (ReadWriteAdapter) Slave

func (adapter ReadWriteAdapter) Slave() *sql.DB

Slave 获取slave链接

type ResolverItem

type ResolverItem struct {
	Sources  []string //
	Replicas []string //
	Tables   []string //
}

type Timestamp

type Timestamp struct {
	time.Time
}

Timestamp 自定义gorm的时间戳格式

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

MarshalJSON 解析

func (*Timestamp) Scan

func (t *Timestamp) Scan(v interface{}) error

Scan 转换时间戳

func (Timestamp) Value

func (t Timestamp) Value() (driver.Value, error)

Value

Jump to

Keyboard shortcuts

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