adb

package
v0.0.0-...-1894dca Latest Latest
Warning

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

Go to latest
Published: May 20, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Master = make(map[string]*gorm.DB)
)

Global Master map 存储所有数据库连接对象(全局唯一)

Functions

func Close

func Close()

Close 关闭所有数据库连接(除了名称为 "default" 的连接) Close closes all database connections except for the one named "default".

func CreateConnection

func CreateConnection(config DatabaseConfig) (*gorm.DB, error)

CreateConnection 根据配置创建单个数据库连接 CreateConnection creates a single database connection based on the provided configuration.

func GetDatabase

func GetDatabase(name string) *gorm.DB

GetDatabase 根据名称获取数据库连接对象 GetDatabase retrieves a database connection by its name.

func InitDb

func InitDb(connections []map[string]any)

InitDb 初始化数据库连接 InitDb initializes database connections based on the provided configuration. connections is a slice of maps representing the configuration for each database.

func Mysql

func Mysql(dsn string) gorm.Dialector

Mysql 返回 MySQL 的 Dialector 配置 Mysql returns the GORM Dialector for MySQL.

func Postgres

func Postgres(dsn string) gorm.Dialector

Postgres 返回 PostgreSQL 的 Dialector 配置 Postgres returns the GORM Dialector for PostgreSQL.

func Sqlserver

func Sqlserver(dsn string) gorm.Dialector

Sqlserver 返回 SQL Server 的 Dialector 配置 Sqlserver returns the GORM Dialector for SQL Server.

Types

type DatabaseConfig

type DatabaseConfig struct {
	Name            string              `json:"name"`
	Type            string              `json:"type"`
	Hostname        string              `json:"hostname"`
	Port            string              `json:"port"`
	Username        string              `json:"username"`
	Password        string              `json:"password"`
	DatabaseName    string              `json:"database"`
	Params          string              `json:"params"`
	LogEnabled      bool                `json:"log"`
	DSN             string              `json:"dsn"`
	MaxIdleConns    int                 `json:"max_idle_conns"`
	MaxOpenConns    int                 `json:"max_open_conns"`
	ConnMaxLifetime int                 `json:"conn_max_lifetime"`
	ConnMaxIdleTime int                 `json:"conn_max_idle_time"` // 注意:修改为统一格式
	LogLevel        gormlogger.LogLevel `json:"level"`
}

DatabaseConfig 数据库配置结构体 DatabaseConfig represents the configuration for a database connection.

func (*DatabaseConfig) Distributed

func (d *DatabaseConfig) Distributed(config dbresolver.Config, datas ...interface{}) *dbresolver.DBResolver

Distributed 设置分布式数据库,返回 dbresolver 插件实例 Distributed configures a distributed database using GORM's dbresolver plugin. 'config' specifies the resolver configuration, and 'datas' represents the data sources.

func (*DatabaseConfig) Open

func (d *DatabaseConfig) Open(dialector gorm.Dialector, opts *gorm.Config) (*gorm.DB, error)

Open 使用指定的 Dialector 和选项打开数据库连接 Open opens a database connection using the provided Dialector and GORM options.

func (*DatabaseConfig) Use

func (d *DatabaseConfig) Use(name string, plugin gorm.Plugin)

Use 为指定数据库连接添加插件 Use applies a GORM plugin to the database connection identified by name.

type Logger

type Logger struct {
	ZapLogger                 *zap.Logger
	LogLevel                  gormlogger.LogLevel
	SlowThreshold             time.Duration
	SkipCallerLookup          bool
	IgnoreRecordNotFoundError bool
}

func New

func New(zapLogger *zap.Logger) Logger

func (Logger) Error

func (l Logger) Error(ctx context.Context, str string, args ...interface{})

func (Logger) Info

func (l Logger) Info(ctx context.Context, str string, args ...interface{})

func (Logger) LogMode

func (l Logger) LogMode(level gormlogger.LogLevel) gormlogger.Interface

func (Logger) SetAsDefault

func (l Logger) SetAsDefault()

func (Logger) Trace

func (l Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

func (Logger) Warn

func (l Logger) Warn(ctx context.Context, str string, args ...interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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