sqlite

package
v0.13.17 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package sqlite 提供 SQLite 数据库的 ORM 封装,基于 GORM,包含连接管理、表名处理与分页查询等辅助方法。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FindPageOptions added in v0.11.1

type FindPageOptions struct {
	// 查询配置
	Page        int  `default:"1"`     // 页码,默认 1
	PageSize    int  `default:"10"`    // 分页大小,默认 10,最大 1000
	ShowDeleted bool `default:"false"` // 是否显示软删除数据,默认 false

	// 模型配置
	PkId            string `default:"id"` // 主键字段名,默认 "id"
	ModelTableAlias string `default:""`   // 模型表别名,默认为表名

	// 回调
	ListQuery  func(*gorm.DB) *gorm.DB                   // 可选,自定义查询回调
	ListSelect func(*gorm.DB) *gorm.DB                   // 可选,自定义查询字段回调
	ListOrder  func() interface{}                        // 可选,自定义排序回调
	ListEach   func(interface{}) interface{}             // 可选,自定义遍历回调
	ListReturn func(jcbaseGo.ListData) jcbaseGo.ListData // 可选,自定义返回回调
}

FindPageOptions 定义分页查询选项,语义与 CRUD list 保持一致;

type Instance

type Instance struct {
	Conf jcbaseGo.SqlLiteStruct
	Db   *gorm.DB

	Errors []error
	// contains filtered or unexported fields
}

Instance 表示 SQLite 连接实例,封装数据库配置、连接句柄、调试状态与错误收集。

func New

func New(Conf jcbaseGo.SqlLiteStruct, opts ...string) (*Instance, error)

New 创建一个 SQLite 实例并建立数据库连接; 可通过可选参数 `opts[0]` 指定配置别名以存入环境变量。 返回:

  • *Instance: SQLite实例
  • error: 初始化或连接失败时的错误信息

func NewWithDebugger added in v0.13.8

func NewWithDebugger(conf jcbaseGo.SqlLiteStruct, debuggerLogger debugger.LoggerInterface, opts ...string) (*Instance, error)

NewWithDebugger 创建SQLite实例并集成debugger日志记录 参数:

  • conf: 数据库配置
  • debuggerLogger: debugger日志记录器
  • opts: 可选参数,第一个参数为配置别名

返回:

  • *Instance: SQLite实例

func (*Instance) AddError

func (c *Instance) AddError(err error)

AddError 将错误追加到实例的错误收集切片中(忽略 nil)。

func (*Instance) Debug

func (c *Instance) Debug() *Instance

Debug 开启调试模式,后续通过 `GetDb()` 获取的 *gorm.DB 将启用 Debug。

func (*Instance) EnableSQLLogging added in v0.13.8

func (c *Instance) EnableSQLLogging(debuggerLogger debugger.LoggerInterface, opts ...interface{}) *Instance

EnableSQLLogging 为当前数据库实例启用SQL日志记录 参数:

  • debuggerLogger: debugger日志记录器
  • logLevel: GORM日志级别(可选,默认logger.Info)
  • slowThreshold: 慢查询阈值(可选,默认200ms)

返回:

  • *Instance: 支持SQL日志记录的数据库实例

func (*Instance) Error

func (c *Instance) Error() []error

Error 返回已收集的非 nil 错误列表。

func (*Instance) FindForPage added in v0.11.1

func (c *Instance) FindForPage(model interface{}, options *FindPageOptions) (jcbaseGo.ListData, error)

FindForPage 按分页选项查询并返回列表数据;

func (*Instance) GetAllTableName

func (c *Instance) GetAllTableName() (tableNames []string, err error)

GetAllTableName 查询并返回当前数据库中的所有表名。

func (*Instance) GetConf added in v0.10.2

func (c *Instance) GetConf() interface{}

GetConf 返回当前实例的原始配置结构。

func (*Instance) GetDb

func (c *Instance) GetDb() *gorm.DB

GetDb 返回当前数据库连接;若已开启调试模式返回 Debug 包装的连接。 调试模式优先级:debuggerLogger > debug标志 这是一个只读方法,不会修改实例状态

func (*Instance) GetDebuggerLogger added in v0.13.8

func (c *Instance) GetDebuggerLogger() debugger.LoggerInterface

GetDebuggerLogger 获取debugger日志记录器

func (*Instance) SetDebuggerLogger added in v0.13.8

func (i *Instance) SetDebuggerLogger(debuggerLogger debugger.LoggerInterface)

SetDebuggerLogger 设置debugger日志记录器 参数:

  • debuggerLogger: debugger日志记录器实例

func (*Instance) TableName

func (c *Instance) TableName(tableName *string, quotes ...bool) *Instance

TableName 根据配置的表前缀拼接并可选包裹反引号,返回处理后的表名。 参数:

  • tableName: 传入待处理的表名指针
  • quotes: 可选,是否为表名添加反引号

Jump to

Keyboard shortcuts

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