rdb

package
v0.0.0-...-b3367d5 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: Apache-2.0 Imports: 15 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LogLevelMap = map[string]logger.LogLevel{
	"warn":   logger.Warn,
	"info":   logger.Info,
	"error":  logger.Error,
	"silent": logger.Silent,
}

LogLevelMap log level for config string.

Functions

func IsErrDuplicatedKey

func IsErrDuplicatedKey(err error) bool

IsErrDuplicatedKey return true if the error is a duplicate key error @Description: 判断err是否是"主键冲突"

func IsErrRecordNotFound

func IsErrRecordNotFound(err error) bool

IsRecordNotFound ... @Description: 判断err是否是"没有该记录"

func NewTxMaker

func NewTxMaker(ori Tx, factory TxFactory) (Tx, *TxMaker)

NewTxMaker new transaction maker.

if ori is nil, create a new transaction by factory if ori is not nil, use the ori transaction function return the new transaction and the txmaker

txmaker is used to manage the transaction lifecycle when the function ends, txmaker.Close(*errp) will be called, the transaction will be committed or rolled back if errp is nil, the transaction will be committed if errp is not nil, the transaction will be rolled back

you can use TxMaker to pass the transaction between functions.

func Open

func Open(config *Config) (conn *gorm.DB, err error)

Open Open database connection.

Types

type ClickhouseTable

type ClickhouseTable interface {
	ClickhouseTableOption(option DataBaseOption) TableOption
}

ClickhouseTable clickhouse table interface.

type Config

type Config struct {
	DSN          string `json:"dsn" yaml:"dsn"` // 数据库连接字符串
	MaxOpenConns int    `json:"max_open_conns"  yaml:"max_open_conns"`
	MaxIdleConns int    `json:"max_idle_conns" yaml:"max_idle_conns"`
	MaxIdleTime  string `json:"max_idle_time" yaml:"max_idle_time"` // 连接池最大空闲时间
	LogLevel     string `json:"log_level" yaml:"log_level"`         // 日志级别
	Cluster      string `json:"cluster" yaml:"cluster"`             // 集群名称
}

Config DBClient配置.

func NewConfig

func NewConfig() *Config

func ParseConfig

func ParseConfig(conf config.AccessPoint) (config *Config, err error)

ParseConfig Parse config from accesspoint.

type DBClient

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

DBClient Gorm的数据库连接.

func NewDBClient

func NewDBClient(config *Config) (*DBClient, error)

NewDBClient Create DBEngine instance.

func (*DBClient) DB

func (client *DBClient) DB() *gorm.DB

DB DB.

func (*DBClient) DropTables

func (client *DBClient) DropTables(tables []any) error

DropTables drop tables from db.

func (*DBClient) GetConfig

func (client *DBClient) GetConfig() *Config

func (*DBClient) NewTx

func (client *DBClient) NewTx() *gorm.DB

NewTx create a new transaction.

func (*DBClient) NewTxMaker

func (client *DBClient) NewTxMaker(tx Tx) (Tx, *TxMaker)

NewTxMaker create a tx maker.

func (*DBClient) Session

func (client *DBClient) Session() *gorm.DB

Session create a new session.

func (*DBClient) Stats

func (client *DBClient) Stats() (sql.DBStats, error)

StartMonitor Monitor DBState.

func (*DBClient) SyncTables

func (client *DBClient) SyncTables(tables []any) error

SyncTables sync tables defined in table object.

func (*DBClient) TruncateTables

func (client *DBClient) TruncateTables(tables []any) error

func (*DBClient) WithDB

func (client *DBClient) WithDB(db *gorm.DB) *DBClient

WithDB set client db connection

type DBClientOptions

type DBClientOptions struct {
	MaxOpenConns int
	MaxIdleConns int
	MaxIdleTime  string
	LogLevel     string
	Cluster      string
}

DBClientOptions DBClient 配置选项.

type DataBaseOption

type DataBaseOption struct {
	Cluster      string
	DatabaseName string
}

type DefaultClickHouseDistributedTable

type DefaultClickHouseDistributedTable struct {
	// 采集时间
	Time time.Time `json:"time"  gorm:"comment:时序时间"`
}

DefaultClickHouseDistributedTable default clickhouse table field.

func (DefaultClickHouseDistributedTable) ClickhouseTableOption

func (table DefaultClickHouseDistributedTable) ClickhouseTableOption(dbop DataBaseOption) TableOption

ClickhouseTableOption 配置Clickhouse的创建options.

type DefaultClickHouseTable

type DefaultClickHouseTable struct {
	// 采集时间
	Time time.Time `json:"time" gorm:"comment:时序时间"`
}

DefaultClickHouseTable 默认clickhouse表.

func (DefaultClickHouseTable) ClickhouseTableOption

func (table DefaultClickHouseTable) ClickhouseTableOption(dbop DataBaseOption) TableOption

ClickhouseTableOption gorm.

type DefaultMySQLTable

type DefaultMySQLTable struct{}

DefaultMySQLTable default mysql table field.

func (DefaultMySQLTable) MySQLTableOption

func (table DefaultMySQLTable) MySQLTableOption(_ string) TableOption

MySQLTableOption gorm mysql option.

type MySQLTable

type MySQLTable interface {
	MySQLTableOption(option DataBaseOption) TableOption
}

MySQLTable mysql table interface.

type TableOption

type TableOption struct {
	TableOptions string
	// TableClusterOptions for clickhouse distributed table.
	TableClusterOptions string
}

TableOption table option.

type Tx

type Tx = *gorm.DB

Tx session rename.

func ForUpdate

func ForUpdate(tx Tx) Tx

ForUpdate create session with for update lock.

type TxFactory

type TxFactory interface {
	// NewTx create a new transaction
	NewTx() Tx
}

TxFactory a factory can create new transaction

type TxMaker

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

TxMaker ....

func (*TxMaker) Close

func (maker *TxMaker) Close(errp *error) error

Close close transaction

if origin tx != nil, it means the tx is used by upper function, so we don't need to do anything if origin tx is nil, it means the tx is a new tx created by factory, we need to handle it. if errp is nil, the transaction will be committed if errp is not nil, the transaction will be rolled back

Jump to

Keyboard shortcuts

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