db

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DatabaseTypeSQLite   = "sqlite"
	DatabaseTypeMySQL    = "mysql"
	DatabaseTypePostgres = "postgres"
)

DatabaseType 数据库类型常量

Variables

This section is empty.

Functions

func CloseDatabase

func CloseDatabase() error

CloseDatabase 关闭数据库连接并重置全局状态 CloseDatabase closes the database connection and resets global state

func DB

func DB(ctx context.Context) *gorm.DB

DB 已废弃,请使用 GetDB(ctx);保留该函数只为兼容现有调用。 DB is deprecated in favor of GetDB(ctx) and remains only for existing callers.

func GetDB

func GetDB(ctx context.Context) *gorm.DB

GetDB 获取带上下文的数据库实例

func GetDatabaseType

func GetDatabaseType() string

GetDatabaseType 获取当前数据库类型

func GetGlobalDB

func GetGlobalDB() *gorm.DB

GetGlobalDB 获取全局数据库实例(不带上下文)

func InitDatabase

func InitDatabase() error

InitDatabase 根据配置初始化数据库连接 InitDatabase initializes the database connection according to configuration 支持 SQLite、MySQL、PostgreSQL 三种数据库类型,默认使用 SQLite Supports SQLite, MySQL, and PostgreSQL; defaults to SQLite 此函数是幂等的,重复调用会跳过已初始化的数据库 This function is idempotent; repeated calls skip already initialized database

func IsDatabaseInitialized

func IsDatabaseInitialized() bool

IsDatabaseInitialized 检查数据库是否已初始化

func OpenDB

func OpenDB(dbConfig config.DatabaseConfig) (*gorm.DB, error)

OpenDB 根据指定配置创建独立的数据库连接实例(不污染全局变量,便于多数据库测试) OpenDB creates an independent database connection instance based on specified config (without polluting global variable, useful for multi-DB testing)

func SetGlobalDB

func SetGlobalDB(database *gorm.DB)

SetGlobalDB 设置全局数据库实例(主要用于测试注入与环境重置) SetGlobalDB sets the global database instance (primarily for test injection and environment resets)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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