connection

package
v1.1.11 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetConfig

func SetConfig(cfg *config.Config) error

SetConfig set config.Config instance to internal global variable

Types

type Connection

type Connection interface {
	DSN() string
	Conn() *sql.DB
}

Connection common interface for DBConnection and DBShardConnection

type DBConnection

type DBConnection struct {
	Config             *config.TableConfig
	Algorithm          algorithm.ShardingAlgorithm
	Adapter            adap.DBAdapter
	IsShard            bool
	IsUsedSequencer    bool
	Connection         *sql.DB
	Sequencer          *sql.DB
	ShardKeyColumnName string
	ShardColumnName    string
	ShardConnections   *DBShardConnections
}

DBConnection has connection to sequencer or master server or all shards

func (*DBConnection) Begin

func (c *DBConnection) Begin(ctx context.Context, opts *sql.TxOptions) *TxConnection

Begin creates TxConnection instance for transaction.

func (*DBConnection) Conn

func (c *DBConnection) Conn() *sql.DB

Conn returns *sql.DB for not sharded database

func (*DBConnection) DSN

func (c *DBConnection) DSN() string

DSN returns DSN for not sharded database

func (*DBConnection) EqualDSN

func (c *DBConnection) EqualDSN(conn *DBConnection) bool

EqualDSN returns whether connection is same DSN connection that executed SQL previously or not.

func (*DBConnection) Exec

func (c *DBConnection) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

Exec executes `Exec` (not shards).

func (*DBConnection) IsEqualShardColumnToShardKeyColumn

func (c *DBConnection) IsEqualShardColumnToShardKeyColumn() bool

IsEqualShardColumnToShardKeyColumn returns whether shard_column value equals to shard_key value or not.

func (*DBConnection) NextSequenceID

func (c *DBConnection) NextSequenceID(tableName string) (int64, error)

NextSequenceID returns next unique id by sequencer table name.

func (*DBConnection) Prepare

func (c *DBConnection) Prepare(ctx context.Context, query string) (*sql.Stmt, error)

Prepare executes `Prepare` (not shards).

func (*DBConnection) Query

func (c *DBConnection) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

Query executes `Query` (not shards).

func (*DBConnection) QueryRow

func (c *DBConnection) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRow executes `QueryRow` (not shards).

func (*DBConnection) ShardConnectionByID

func (c *DBConnection) ShardConnectionByID(id int64) (*DBShardConnection, error)

ShardConnectionByID returns connection to shard by unique id.

type DBConnectionManager

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

DBConnectionManager has DBConnectionMap and settings to connection of database

func NewConnectionManager

func NewConnectionManager() (*DBConnectionManager, error)

NewConnectionManager creates instance of DBConnectionManager, If call this before loads configuration file, it returns error.

func (*DBConnectionManager) Close

func (cm *DBConnectionManager) Close() error

Close close all connections

func (*DBConnectionManager) ConnectionByTableName

func (cm *DBConnectionManager) ConnectionByTableName(tableName string) (*DBConnection, error)

ConnectionByTableName returns DBConnection instance by table name

func (*DBConnectionManager) CurrentSequenceID

func (cm *DBConnectionManager) CurrentSequenceID(tableName string) (int64, error)

CurrentSequenceID returns current unique id by table name of sequencer

func (*DBConnectionManager) IsEqualShardColumnToShardKeyColumn

func (cm *DBConnectionManager) IsEqualShardColumnToShardKeyColumn(tableName string) bool

IsEqualShardColumnToShardKeyColumn returns whether shard_column value equals to shard_key value or not.

func (*DBConnectionManager) IsShardTable

func (cm *DBConnectionManager) IsShardTable(tableName string) bool

IsShardTable whether sharding table or not.

func (*DBConnectionManager) NextSequenceID

func (cm *DBConnectionManager) NextSequenceID(tableName string) (int64, error)

NextSequenceID returns next unique id by table name of sequencer

func (*DBConnectionManager) SequencerConnectionByTableName

func (cm *DBConnectionManager) SequencerConnectionByTableName(tableName string) (*sql.DB, error)

SequencerConnectionByTableName returns `*sql.DB` instance by table name

func (*DBConnectionManager) SetConnMaxLifetime

func (cm *DBConnectionManager) SetConnMaxLifetime(d time.Duration)

SetConnMaxLifetime compatible interface of SetConnMaxLifetime in 'database/sql' package

func (*DBConnectionManager) SetMaxIdleConns

func (cm *DBConnectionManager) SetMaxIdleConns(n int)

SetMaxIdleConns compatible interface of SetMaxIdleConns in 'database/sql' package

func (*DBConnectionManager) SetMaxOpenConns

func (cm *DBConnectionManager) SetMaxOpenConns(n int)

SetMaxOpenConns compatible interface of SetMaxOpenConns in 'database/sql' package

func (*DBConnectionManager) SetQueryString

func (cm *DBConnectionManager) SetQueryString(s string) error

SetQueryString set up query string like `?parseTime=true`

func (*DBConnectionManager) ShardColumnName

func (cm *DBConnectionManager) ShardColumnName(tableName string) string

ShardColumnName returns shard_column value by table name

func (*DBConnectionManager) ShardKeyColumnName

func (cm *DBConnectionManager) ShardKeyColumnName(tableName string) string

ShardKeyColumnName returns shard_key value by table name

type DBConnectionMap

type DBConnectionMap struct {
	*sync.Map
}

DBConnectionMap has all DBConnection.

func (DBConnectionMap) Each

func (m DBConnectionMap) Each(f func(string, *DBConnection) bool)

Each iterate all DBConnections.

func (DBConnectionMap) Get

func (m DBConnectionMap) Get(tableName string) *DBConnection

Get get DBConnection instance by table name.

func (DBConnectionMap) Set

func (m DBConnectionMap) Set(tableName string, conn *DBConnection)

Set set DBConnection instance with table name.

type DBShardConnection

type DBShardConnection struct {
	ShardName  string
	Connection *sql.DB
	Masters    []*sql.DB
	Slaves     []*sql.DB
	// contains filtered or unexported fields
}

DBShardConnection has connection to sharded database.

func (*DBShardConnection) Conn

func (c *DBShardConnection) Conn() *sql.DB

Conn returns *sql.DB instance for shard

func (*DBShardConnection) DSN

func (c *DBShardConnection) DSN() string

DSN returns DSN for shard

type DBShardConnections

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

DBShardConnections has all DBShardConnection instances.

func (*DBShardConnections) AllShard

func (c *DBShardConnections) AllShard() []*DBShardConnection

AllShard returns slice of DBShardConnection structure

func (*DBShardConnections) Close

func (c *DBShardConnections) Close() error

Close close all database connections for shards

func (*DBShardConnections) ShardConnectionByIndex

func (c *DBShardConnections) ShardConnectionByIndex(shardIndex int) *DBShardConnection

ShardConnectionByIndex returns DBShardConnection structure by index of shards

func (*DBShardConnections) ShardConnectionByName

func (c *DBShardConnections) ShardConnectionByName(shardName string) *DBShardConnection

ShardConnectionByName returns DBShardConnection structure by database name

func (*DBShardConnections) ShardNum

func (c *DBShardConnections) ShardNum() int

ShardNum returns number of shards

type QueryLog

type QueryLog struct {
	Query        string        `json:"query"`
	Args         []interface{} `json:"args"`
	LastInsertID int64         `json:"lastInsertId"`
}

QueryLog type for storing information of executed query

type TxConnection

type TxConnection struct {
	WriteQueries               []*QueryLog
	ReadQueries                []*QueryLog
	BeforeCommitCallback       func() error
	AfterCommitSuccessCallback func() error
	AfterCommitFailureCallback func(bool, []*QueryLog) error
	// contains filtered or unexported fields
}

TxConnection manage transaction

func (*TxConnection) AddReadQuery

func (c *TxConnection) AddReadQuery(query string, args ...interface{})

func (*TxConnection) AddWriteQuery

func (c *TxConnection) AddWriteQuery(conn Connection, result sql.Result, query string, args ...interface{}) error

func (*TxConnection) Commit

func (c *TxConnection) Commit() (e error)

Commit executes `Commit` with transaction.

func (*TxConnection) Exec

func (c *TxConnection) Exec(ctx context.Context, conn Connection, query string, args ...interface{}) (sql.Result, error)

Exec executes `Exec` with transaction.

func (*TxConnection) Prepare

func (c *TxConnection) Prepare(ctx context.Context, conn Connection, query string) (*sql.Stmt, error)

Prepare executes `Prepare` with transaction.

func (*TxConnection) Query

func (c *TxConnection) Query(ctx context.Context, conn Connection, query string, args ...interface{}) (*sql.Rows, error)

Query executes `Query` with transaction.

func (*TxConnection) QueryRow

func (c *TxConnection) QueryRow(ctx context.Context, conn Connection, query string, args ...interface{}) (*sql.Row, error)

QueryRow executes `QueryRow` with transaction.

func (*TxConnection) Rollback

func (c *TxConnection) Rollback() error

Rollback executes `Rollback` with transaction.

func (*TxConnection) Stmt

func (c *TxConnection) Stmt(ctx context.Context, conn Connection, stmt *sql.Stmt) (*sql.Stmt, error)

Stmt executes `Stmt` with transaction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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