database

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateRandomID

func GenerateRandomID() int64

Types

type Batch

type Batch interface {
	GetBatch() any
	Len() int
	Queue(query string, arguments ...any)
}

Batch - transaction interface.

type ConnConfig

type ConnConfig struct {
	VpcDirectConnection bool
	Host                string
	Port                int32
	DBName              string
	User                string
	Password            string
	MaxConn             int32
	IsLocalEnv          bool
}

ConnConfig - InstanceManager configuration.

type DbShard

type DbShard string

DbShard - represent the shard instance key.

type InstanceManager

type InstanceManager interface {
	GetDbConnPool() (any, error)
	GetConnFromPool(ctx context.Context) (any, error)
	CloseDbConnPool()
	GetConnectionConfig() ConnConfig
	Query(ctx context.Context, lockId int64, query string, args ...any) (*ResultSet, error)
	QueryAndProcess(ctx context.Context, lockId int64, processCallback func(row Row) error, query string, args ...any) error
	Exec(ctx context.Context, lockId int64, execQuery string, args ...any) (int64, error)
	TxBegin(ctx context.Context, lockId int64) (Transaction, error)
	ExecTransactionalTask(ctx context.Context, lockId int64, task func(ctx context.Context, tx Transaction) error) error
	AcquireDistributedLock(ctx context.Context, lockId int64) (int64, error)
	ReleaseDistributedLock(ctx context.Context, lockId int64) error
	ExecTaskWithDistributedLock(ctx context.Context, lockId int64, task func(ctx context.Context) error) error
}

InstanceManager - InstanceManager interface.

type MasterReplicaManager

type MasterReplicaManager struct {
	DbMaster  InstanceManager
	DbReplica InstanceManager
}

MasterReplicaManager - it manage master and read replica instance.

type PreparedStatement

type PreparedStatement struct {
	Name  string
	Query string
}

PreparedStatement - Prepared statement query.

func NewPreparedStatement

func NewPreparedStatement(name, query string) PreparedStatement

NewPreparedStatement - Create new Prepared Statement.

func (PreparedStatement) GetName

func (p PreparedStatement) GetName() string

GetName - name of the prepared statement.

func (PreparedStatement) GetQuery

func (p PreparedStatement) GetQuery() string

GetQuery - query of the prepared statement.

type PreparedStatementsMap

type PreparedStatementsMap struct {
	DbPrepStmMap map[DbShard][]PreparedStatement
}

PreparedStatementsMap - map of DbShard and relative PreparedStatements.

type ResultSet

type ResultSet struct {
	Rows     []Row
	RowsScan []RowScan
}

ResultSet represents the query result set.

func (*ResultSet) GetRow

func (r *ResultSet) GetRow(rowIdx int) (Row, error)

GetRow - get row by index.

func (*ResultSet) GetRowScan

func (r *ResultSet) GetRowScan(rowIdx int) (RowScan, error)

GetRowScan - Get row scan.

type Row

type Row []any

Row represents a database row returned as a result.

type RowScan

type RowScan interface {
	Scan(dest ...any) error
}

RowScan represents a row that can be mapped to dest fields trough Scan function.

type ShardManager

type ShardManager struct {
	DbShardMap map[DbShard]MasterReplicaManager
}

ShardManager - it manage a pool of db shards, keeping them in a map. Each map value is a MasterReplicaManager.

func NewDbShardManager

func NewDbShardManager() *ShardManager

NewDbShardManager is a constructor that ensures the inner map is always initialized.

func (*ShardManager) AddInstanceManager

func (dsm *ShardManager) AddInstanceManager(dbShard DbShard, manager MasterReplicaManager)

AddInstanceManager adds a new MasterReplicaManager to the given DbShard.

type Transaction

type Transaction interface {
	TxCommit(ctx context.Context, lockId int64) error
	TxRollback(ctx context.Context, lockId int64)
	TxQuery(ctx context.Context, query string, args ...any) (*ResultSet, error)
	TxExec(ctx context.Context, query string, args ...any) (int64, error)
	TxExecBatch(ctx context.Context, batch Batch) (int64, error)
}

Transaction - transaction interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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