dbtool

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2019 License: MIT Imports: 8 Imported by: 0

README

dbtool

Build Status Latest Tag

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

安装
go get -u github.com/cuckoopark/dbtool
GORM-MySQL

可以通过接口,来直接获取GORM的MySQL数据库连接:

func GetGormMySQLConnection(config MySqlConfig) (db *gorm.DB, err error)

这里MySqlConfig是MySQL的配置信息,格式如下:

  • Host:服务器主机地址。
  • Port:服务器端口号。
  • User:服务器登陆用户。
  • Password:调试时数据库密码。
  • Scheme:数据库名称。
  • MaxIdleConnection:最大闲置连接数。
  • MaxOpenConnection:最大打开连接数。
  • Params:连接参数。
通用插入语句

可以调用如下接口,来实现将map数据插入到一个指定数据库表中:

func GeneralInsert(db basic.SQLCommon, tableName string, data map[string]interface{}) (rsp sql.Result, err error)
通用列表语句

通过传递ListByPageParams类型参数,来实现分页查询列表和排序功能,该类型包含字段如下:

  • Fields:查询的字段,若为空数组,则查询全部字段。
  • IgnoreFields:查询全部字段时,忽略掉哪些字段。
  • PageNumber:从1开始的页码。
  • PageSize:大于1为有效的页大小。
  • SortBy:按哪个字段名称排序。
  • SortDirection:排序方向,true为ASC,false为DESC。

使用SQL来查询分页列表的接口:

func GeneralListSQL(db basic.SQLCommon, tableName string, params ListByPageParams, whereSql string, whereArgs ...interface{}) (rsp []interface{}, err error)

使用GORM来查询分页列表的接口:

func GeneralListGORM(database *gorm.DB, params ListByPageParams) (db *gorm.DB)
通用事务处理

使用如下接口,进行GORM事务处理:

func TransactionGORM(db *gorm.DB, f TransactionFuncGORM) (data interface{}, err error)

里面的f参数是实际事务操作的回调函数,类型是:

type TransactionFuncGORM func(*gorm.DB) (interface{}, error)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GeneralInsert

func GeneralInsert(db basic.SQLCommon, tableName string, data map[string]interface{}) (rsp sql.Result, err error)

插入数据到数据库表的通用方法

func GeneralListGORM

func GeneralListGORM(database *gorm.DB, params ListByPageParams) (db *gorm.DB)

func GeneralListSQL

func GeneralListSQL(db basic.SQLCommon, tableName string, params ListByPageParams, whereSql string, whereArgs ...interface{}) (rsp []interface{}, err error)

分页查询的通用查询接口

func GetGormMySQLConnection

func GetGormMySQLConnection(config MySqlConfig) (db *gorm.DB, err error)

获取GORM的MySQL数据库连接

func TransactionGORM

func TransactionGORM(db *gorm.DB, f TransactionFuncGORM) (data interface{}, err error)

GORM事务处理

Types

type ListByPageParams

type ListByPageParams struct {
	Fields        []string `json:"fields"`         // 查询的字段,若为空数组,则查询全部字段
	IgnoreFields  []string `json:"ignore_fields"`  // 忽略掉哪些字段
	PageNumber    uint64   `json:"page_number"`    // 从1开始的页码
	PageSize      uint64   `json:"page_size"`      // 大于1为有效的页大小
	SortBy        string   `json:"sort_by"`        // 按哪个字段名称排序
	SortDirection bool     `json:"sort_direction"` // true为ASC,false为DESC
}

分页查询列表的公共参数

type MySqlConfig

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

MySQL的配置信息格式

type TransactionFuncGORM

type TransactionFuncGORM func(*gorm.DB) (interface{}, error)

事务实际操作函数

Jump to

Keyboard shortcuts

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