db

package
v1.32.69 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: Zlib Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultDnsFunc = func(cnf *config.Database, tls, serverPubKey bool) string {
	if cnf == nil {
		return ""
	}
	if cnf.DriverType == "mysql" {
		if cnf.Charset == "" {
			cnf.Charset = "utf8mb4"
		}
		dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local", cnf.User, cnf.Pwd, cnf.Host, cnf.Port, cnf.Name, cnf.Charset)
		if tls {
			dsn += "&tls=" + url.QueryEscape(cnf.Tls.Key)
		}
		if serverPubKey {
			dsn += "&serverPubKey=" + url.QueryEscape(cnf.ServerPubKey.Key)
		}
		return dsn
	} else if cnf.DriverType == "postgres" {
		return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s", cnf.Host, cnf.Port, cnf.User, cnf.Pwd, cnf.Name)
	} else if cnf.DriverType == "sqlite3" {
		return cnf.Name
	} else if cnf.DriverType == "sql" {
		return fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s", cnf.User, cnf.Pwd, cnf.Host, cnf.Port, cnf.Name)
	}
	return ""
}
View Source
var DefaultTlsFunc = func(cnf *commontls.Tls) (*tls.Config, error) {
	if strings.EqualFold(cnf.Key, "true") ||
		strings.EqualFold(cnf.Key, "false") ||
		strings.EqualFold(cnf.Key, "skip-verify") ||
		strings.EqualFold(cnf.Key, "preferred") {
		return nil, nil
	}
	return NewTlsConfig(cnf)
}

Functions

func AddDriver added in v1.6.1

func AddDriver(driverName string, dial DialFunc)

func NewTlsConfig added in v1.32.56

func NewTlsConfig(cnf *commontls.Tls) (*tls.Config, error)

func UseEsClientOption added in v1.30.3

func UseEsClientOption(cnf *config.Elastic, opts ...commonoption.Option) []elastic.ClientOptionFunc

func WithDnsOption added in v1.9.15

func WithDnsOption(f func(cnf *config.Database, tls, pubKey bool) string) commonoption.Option

func WithEsClientOption added in v1.30.3

func WithEsClientOption(f func(cfg *config.Elastic) []elastic.ClientOptionFunc) commonoption.Option

func WithServerPubKeyOption added in v1.9.15

func WithServerPubKeyOption(f func(cnf *commontls.ServerPubKey) (*rsa.PublicKey, error)) commonoption.Option

func WithTlsOption added in v1.9.15

func WithTlsOption(f func(cnf *commontls.Tls) (*tls.Config, error)) commonoption.Option

Types

type Adapter

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

Adapter represents the Gorm adapter for policy storage.

func NewAdapter

func NewAdapter(cfg *config.Sql, opts ...commonoption.Option) (*Adapter, error)

NewAdapter is the constructor for Adapter.

func (*Adapter) Close

func (a *Adapter) Close()

func (*Adapter) CollectData added in v1.28.0

func (a *Adapter) CollectData() (data.Model, error)

func (*Adapter) Db

func (a *Adapter) Db() *gorm.DB

func (*Adapter) Name

func (a *Adapter) Name() string

func (*Adapter) Version added in v1.32.10

func (a *Adapter) Version() string

type ConditionCallback

type ConditionCallback func(ctx context.Context, data interface{}, db *gorm.DB) *gorm.DB

type DialFunc added in v1.6.1

type DialFunc func(string) gorm.Dialector

type EsDatabase

type EsDatabase interface {
	// Get return client,version,error
	Get(name string) (*elastic.Client, string, error)
	Do(name string, call func(es *elastic.Client, version string) error) error
	Close(name string) error
	CloseAll()
	Select(name string)
	GetSelector() string
}

func NewES

func NewES(conf []config.Elastic, opts ...commonoption.Option) (EsDatabase, error)

type ScopesFunc

type ScopesFunc func(db *gorm.DB) *gorm.DB

type ServerPubKeyFunc added in v1.9.15

type ServerPubKeyFunc func(cnf *commontls.ServerPubKey) (*rsa.PublicKey, error)

func (ServerPubKeyFunc) String added in v1.9.15

func (t ServerPubKeyFunc) String() string

func (ServerPubKeyFunc) Type added in v1.9.15

func (t ServerPubKeyFunc) Type() int

func (ServerPubKeyFunc) Value added in v1.9.15

func (t ServerPubKeyFunc) Value() interface{}

type Sql

type Sql interface {
	Db() *gorm.DB
	Name() string
	Version() string
	// CollectData 主要统计 Stats
	CollectData() (data.Model, error)
}

type SqlDatabase

type SqlDatabase interface {
	Get(name string) (Sql, error)
	Do(name string, c func(db Sql) error) error
	Close(name string)
	CloseAll()
	Select(name string)
	GetSelector() string
}

func NewDB

func NewDB(cfgs []config.Sql, opts ...commonoption.Option) (SqlDatabase, error)

type Stats added in v1.26.6

type Stats struct {
	MaxOpenConnections int           `json:"maxOpenConnections"` //最大打开连接数
	OpenConnections    int           `json:"openConnections"`    // 连接数,The number of established connections both in use and idle.
	InUse              int           `json:"inUse"`              // 正在使用数,The number of connections currently in use.
	Idle               int           `json:"idle"`               // 当前空闲连接数
	WaitCount          int64         `json:"waitCount"`          // The total number of connections waited for.
	WaitDuration       time.Duration `json:"waitDuration"`       // The total time blocked waiting for a new connection.
	MaxIdleClosed      int64         `json:"maxIdleClosed"`      // The total number of connections closed due to SetMaxIdleConns.
	MaxIdleTimeClosed  int64         `json:"maxIdleTimeClosed"`  // The total number of connections closed due to SetConnMaxIdleTime.
	MaxLifetimeClosed  int64         `json:"maxLifetimeClosed"`  // 连接被关闭的次数(因达到最大存活时间)The total number of connections closed due to SetConnMaxLifetime.
}

func (Stats) DataType added in v1.28.0

func (s Stats) DataType() string

Jump to

Keyboard shortcuts

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