Documentation
¶
Index ¶
- Constants
- func CloseDatabase() error
- func DB(ctx context.Context) *gorm.DB
- func GetDB(ctx context.Context) *gorm.DB
- func GetDatabaseType() string
- func GetGlobalDB() *gorm.DB
- func InitDatabase() error
- func IsDatabaseInitialized() bool
- func OpenDB(dbConfig config.DatabaseConfig) (*gorm.DB, error)
- func SetGlobalDB(database *gorm.DB)
Constants ¶
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 ¶
DB 已废弃,请使用 GetDB(ctx);保留该函数只为兼容现有调用。 DB is deprecated in favor of GetDB(ctx) and remains only for existing callers.
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 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 ¶
SetGlobalDB 设置全局数据库实例(主要用于测试注入与环境重置) SetGlobalDB sets the global database instance (primarily for test injection and environment resets)
Types ¶
This section is empty.