Documentation ¶
Index ¶
- func SetConfig(cfg *config.Config) error
- type Connection
- type DBConnection
- func (c *DBConnection) Begin(ctx context.Context, opts *sql.TxOptions) *TxConnection
- func (c *DBConnection) Conn() *sql.DB
- func (c *DBConnection) DSN() string
- func (c *DBConnection) EqualDSN(conn *DBConnection) bool
- func (c *DBConnection) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (c *DBConnection) IsEqualShardColumnToShardKeyColumn() bool
- func (c *DBConnection) NextSequenceID(tableName string) (int64, error)
- func (c *DBConnection) Prepare(ctx context.Context, query string) (*sql.Stmt, error)
- func (c *DBConnection) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (c *DBConnection) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (c *DBConnection) ShardConnectionByID(id int64) (*DBShardConnection, error)
- type DBConnectionManager
- func (cm *DBConnectionManager) Close() error
- func (cm *DBConnectionManager) ConnectionByTableName(tableName string) (*DBConnection, error)
- func (cm *DBConnectionManager) CurrentSequenceID(tableName string) (int64, error)
- func (cm *DBConnectionManager) IsEqualShardColumnToShardKeyColumn(tableName string) bool
- func (cm *DBConnectionManager) IsShardTable(tableName string) bool
- func (cm *DBConnectionManager) NextSequenceID(tableName string) (int64, error)
- func (cm *DBConnectionManager) SequencerConnectionByTableName(tableName string) (*sql.DB, error)
- func (cm *DBConnectionManager) SetConnMaxLifetime(d time.Duration)
- func (cm *DBConnectionManager) SetMaxIdleConns(n int)
- func (cm *DBConnectionManager) SetMaxOpenConns(n int)
- func (cm *DBConnectionManager) SetQueryString(s string) error
- func (cm *DBConnectionManager) ShardColumnName(tableName string) string
- func (cm *DBConnectionManager) ShardKeyColumnName(tableName string) string
- type DBConnectionMap
- type DBShardConnection
- type DBShardConnections
- func (c *DBShardConnections) AllShard() []*DBShardConnection
- func (c *DBShardConnections) Close() error
- func (c *DBShardConnections) ShardConnectionByIndex(shardIndex int) *DBShardConnection
- func (c *DBShardConnections) ShardConnectionByName(shardName string) *DBShardConnection
- func (c *DBShardConnections) ShardNum() int
- type QueryLog
- type TxConnection
- func (c *TxConnection) AddReadQuery(query string, args ...interface{})
- func (c *TxConnection) AddWriteQuery(conn Connection, result sql.Result, query string, args ...interface{}) error
- func (c *TxConnection) Commit() (e error)
- func (c *TxConnection) Exec(ctx context.Context, conn Connection, query string, args ...interface{}) (sql.Result, error)
- func (c *TxConnection) Prepare(ctx context.Context, conn Connection, query string) (*sql.Stmt, error)
- func (c *TxConnection) Query(ctx context.Context, conn Connection, query string, args ...interface{}) (*sql.Rows, error)
- func (c *TxConnection) QueryRow(ctx context.Context, conn Connection, query string, args ...interface{}) (*sql.Row, error)
- func (c *TxConnection) Rollback() error
- func (c *TxConnection) Stmt(ctx context.Context, conn Connection, stmt *sql.Stmt) (*sql.Stmt, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Connection ¶
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) Query ¶
func (c *DBConnection) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Query executes `Query` (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 ¶
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
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.