dbtool

package
v0.0.0-...-3377335 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MulanPSL-2.0 Imports: 5 Imported by: 0

README

dbtool

这里使用GORM框架,数据库支持MySQL等,包含了一些常用的工具,包括数据库连接、分页查询列表等。

连接MySQL:

import _ "github.com/go-sql-driver/mysql"
db, err := dbtool.ConnectMysql(...)

连接SQLite:

import _ "github.com/xeodou/go-sqlcipher"
db, err := dbtool.ConnectSqlite(...)

其他接口:

  • Transaction:GORM事务处理。
  • IsTransaction:判断是否是GORM事务句柄。

复杂查询条件,包括原子级条件项和复合条件项,其中:

  • 原子级条件项:只有左值、右值、操作符,拼接成一个原子级查询语句。
  • 复合条件项:包括多个子项,子项可以是原子级条件项,也可以是复合条件项,用连接符连接,可以是and或or。

其中QuerySql接口可以获取生成的查询语句和条件项,Fields接口可以获取查询条件中的所有字段列表,用于判断是否有字段不符合后台要求。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectMysql

func ConnectMysql(config MysqlConfig) (db *gorm.DB, err error)

获取MySQL数据库连接

func ConnectSqlite

func ConnectSqlite(config SqliteConfig) (db *gorm.DB, err error)

获取SQLite数据库连接

func InOperationUint8

func InOperationUint8(
	fieldName string,
	values []uint8,
	isIn bool,
) string

由于xxx in/not in (?)时,如果值数组是uint8数组,则GORM会按照字符串处理

func IsTransaction

func IsTransaction(db *gorm.DB) error

判断是否是事务句柄

func ListByPage

func ListByPage(db *gorm.DB, params *ListByPageParams) *gorm.DB

分页查询

func Transaction

func Transaction[T any](db *gorm.DB, f TransactionFunc[T]) (data T, err error)

GORM事务处理

Types

type ConditionItem

type ConditionItem struct {
	WhereSql  string
	WhereArgs []interface{}
}

查询的拼接结果

func ConnAnd

func ConnAnd(items ...*ConditionItem) *ConditionItem

AND连接符

func ConnOr

func ConnOr(items ...*ConditionItem) *ConditionItem

OR连接符

func FieldTextLike

func FieldTextLike(name string, value string) *ConditionItem

文本字段(模糊搜索)

type ListByPageParams

type ListByPageParams struct {
	PageNumber uint64            `json:"page_number"` // 从1开始的页码
	PageSize   uint64            `json:"page_size"`   // 大于1为有效的页大小
	Sort       map[string]string `json:"sort"`        // 排序信息(仅支持一个),[字段名:ascend/descend]
}

分页查询列表的公共参数

type MysqlConfig

type MysqlConfig struct {
	Host              string            // 服务器主机地址
	Port              int               // 服务器端口号
	User              string            // 服务器登陆用户
	Password          string            // 调试时数据库密码
	Scheme            string            // 数据库名称
	MaxIdleConnection int               // 最大闲置连接数
	MaxOpenConnection int               // 最大打开连接数
	DebugMode         bool              // 开启SQL调试模式
	Params            map[string]string // 连接参数
}

MySQL的配置信息

type SqliteConfig

type SqliteConfig struct {
	Filepath  string // 文件地址
	Password  string // 调试时数据库密码
	DebugMode bool   // 开启SQL调试模式
}

SQLite的配置信息

type TransactionFunc

type TransactionFunc[T any] func(*gorm.DB) (T, error)

事务实际操作函数

Jump to

Keyboard shortcuts

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