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)
- type ScriptText
- func (ScriptText) GormDBDataType(db *gorm.DB, _ *schema.Field) string
- func (ScriptText) GormDataType() string
- func (s ScriptText) MarshalJSON() ([]byte, error)
- func (s *ScriptText) Scan(value interface{}) error
- func (s ScriptText) String() string
- func (s *ScriptText) UnmarshalJSON(data []byte) error
- func (s ScriptText) Value() (driver.Value, error)
- type UTCPlugin
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 ¶
type ScriptText ¶ added in v1.0.0
type ScriptText string
ScriptText 是脚本/大配置内容的方言感知文本类型。 ScriptText is a dialect-aware text type for scripts and large configuration payloads.
MySQL → MEDIUMTEXT(约 16MB)/ MySQL → MEDIUMTEXT (~16MB) PostgreSQL / SQLite → TEXT(足够大)/ PostgreSQL / SQLite → TEXT (effectively unbounded)
注意:不要再写 gorm:"type:text",否则会覆盖方言分流。 Note: do not set gorm:"type:text"; that tag overrides dialect selection.
func (ScriptText) GormDBDataType ¶ added in v1.0.0
GormDBDataType 按当前数据库方言返回真实列类型。 GormDBDataType returns the concrete column type for the active dialect.
func (ScriptText) GormDataType ¶ added in v1.0.0
func (ScriptText) GormDataType() string
GormDataType 返回通用逻辑类型名。 GormDataType returns the generic logical data type name.
func (ScriptText) MarshalJSON ¶ added in v1.0.0
func (s ScriptText) MarshalJSON() ([]byte, error)
MarshalJSON 将 ScriptText 序列化为 JSON 字符串。 MarshalJSON serializes ScriptText as a JSON string.
func (*ScriptText) Scan ¶ added in v1.0.0
func (s *ScriptText) Scan(value interface{}) error
Scan 实现 sql.Scanner,从数据库读出。 Scan implements sql.Scanner for database reads.
func (ScriptText) String ¶ added in v1.0.0
func (s ScriptText) String() string
String 返回底层字符串。 String returns the underlying string value.
func (*ScriptText) UnmarshalJSON ¶ added in v1.0.0
func (s *ScriptText) UnmarshalJSON(data []byte) error
UnmarshalJSON 从 JSON 字符串反序列化。 UnmarshalJSON deserializes from a JSON string.
type UTCPlugin ¶ added in v1.2.0
type UTCPlugin struct{}
UTCPlugin 是全局时区统一的兜底层:在写入前把模型/Map 中的 time.Time 统一为 UTC, 并在执行前把 WHERE 条件里的 time.Time 参数统一为 UTC。这样 SQLite(DATETIME 以文本比较) 与 MySQL/PostgreSQL 的存储与比较语义一致,不再依赖进程 time.Local。 UTCPlugin is the safety net of the unified timezone design: it normalizes time.Time values in models/maps to UTC before writes and normalizes time.Time WHERE arguments to UTC before execution, so SQLite (text-compared DATETIME) and MySQL/PostgreSQL behave identically regardless of the process time.Local.
func (UTCPlugin) Initialize ¶ added in v1.2.0
Initialize implements gorm.Plugin.