xsql

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2021 License: Apache-2.0 Imports: 10 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cluster

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

func OpenCluster

func OpenCluster(name string, selector Selector, masterOption ConnectionOptions, slaveOptions []ConnectionOptions) (*Cluster, error)

OpenCluster init new gorm client cluster with mysql database. name is instance name of current mysql instance. selector to select which slave node to use. masterOption is master node options slaveOptions are slave nodes options

func (*Cluster) Exec

func (c *Cluster) Exec(sql string, args ...interface{}) error

Exec will do update/insert/delete operation with master node. 在主节点上执行 更新/插入/删除 操作。

func (*Cluster) Master

func (c *Cluster) Master() *sql.DB

Master return master node to do more operation. 返回主节点。

func (*Cluster) Run

func (c *Cluster) Run(fn func(master *sql.DB, slave *sql.DB) error) error

Run will do operation on master or slave node with user choose. 用户可选的在何种节点上执行操作。

func (*Cluster) RunShard

func (c *Cluster) RunShard(key string, fn func(master *sql.DB, slave *sql.DB) error) error

Run will do operation on master or slave node with user choose, with key to select which slave node. 用户可选的在何种节点上执行操作,key 用来选择从节点。

func (*Cluster) Slave

func (c *Cluster) Slave() *sql.DB

Slave get slave db use default `Selector`. 使用默认的选择器选择从节点。

func (*Cluster) SlaveByKey

func (c *Cluster) SlaveByKey(key string) *sql.DB

SlaveByKey get slave db use shard `Selector` with shard key. 使用key来选择从节点。

func (*Cluster) SlaveBySelector

func (c *Cluster) SlaveBySelector(selector Selector) *sql.DB

SlaveBySelector get slave db use user `Selector`. 使用用户指定的选择器选择从节点。

func (*Cluster) Transaction

func (c *Cluster) Transaction(fn func(tx *sql.Tx) error) error

Transaction will do transaction on master node, with commit/rollback automatically. 在主节点上执行事务,可以自动执行commit/rollback。

type ConnectionOption

type ConnectionOption func(options *ConnectionOptions)

ConnectionOption is helper function to modify database pool options

func WithConnMaxIdleTime

func WithConnMaxIdleTime(d time.Duration) ConnectionOption

func WithDriverName

func WithDriverName(name string) ConnectionOption

func WithDsn

func WithDsn(dsn string) ConnectionOption

func WithMaxIdleConn

func WithMaxIdleConn(n int) ConnectionOption

func WithMaxOpenConn

func WithMaxOpenConn(n int) ConnectionOption

type ConnectionOptions

type ConnectionOptions struct {
	DriverName string
	Dsn        string

	MaxIdleConn     int
	MaxOpenConn     int
	ConnMaxLifeTime time.Duration
}

func NewDefaultConnectionOptions

func NewDefaultConnectionOptions() ConnectionOptions

type Enum

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

Enum present a selectable of value which store in database in int mode, but in application is string mode, this will translate automatically.

func NewEnum

func NewEnum(valueMap map[string]int, defs ...string) *Enum

func (*Enum) Scan

func (a *Enum) Scan(src interface{}) error

func (*Enum) String

func (a *Enum) String() string

func (Enum) Value

func (a Enum) Value() (driver.Value, error)

type IPV4

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

IPV4 present a selectable of ipv4 address which store in database in int mode, but in application is ipv4 mode, this will translate automatically.

func NewIPV4

func NewIPV4(ip string) (*IPV4, error)

func (IPV4) IP

func (a IPV4) IP() net.IP

func (*IPV4) Scan

func (a *IPV4) Scan(src interface{}) error

func (IPV4) String

func (a IPV4) String() string

func (IPV4) Value

func (a IPV4) Value() (driver.Value, error)

type Selector

type Selector interface {
	SelectDB(n int) int
}

type SelectorFunc

type SelectorFunc func(n int) int

func RandomSelector

func RandomSelector() SelectorFunc

func RoundRobinSelector

func RoundRobinSelector() SelectorFunc

func ShardSelector

func ShardSelector(key string) SelectorFunc

func (SelectorFunc) SelectDB

func (fn SelectorFunc) SelectDB(n int) int

type StringArray

type StringArray []string

StringArray present a slice of array which store in database in string mode, but in application is string slice mode, this will translate automatically.

func (*StringArray) Scan

func (a *StringArray) Scan(src interface{}) error

func (StringArray) String

func (a StringArray) String() string

func (StringArray) Value

func (a StringArray) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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