db

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0 Imports: 19 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

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

func (ScriptText) GormDBDataType(db *gorm.DB, _ *schema.Field) string

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.

func (ScriptText) Value added in v1.0.0

func (s ScriptText) Value() (driver.Value, error)

Value 实现 driver.Valuer,写入数据库。 Value implements driver.Valuer for database writes.

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

func (UTCPlugin) Initialize(database *gorm.DB) error

Initialize implements gorm.Plugin.

func (UTCPlugin) Name added in v1.2.0

func (UTCPlugin) Name() string

Name implements gorm.Plugin.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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