base

package
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: MIT Imports: 14 Imported by: 5

Documentation

Index

Constants

View Source
const (
	// FromQueryTag tag标记
	FromQueryTag = "query"
	// Mysql 数据库标识
	Mysql = "mysql"
	// Postgres 数据库标识
	Postgres = "pgsql"
)
View Source
const (
	OK      = 200
	FAILURE = 500
)

Variables

This section is empty.

Functions

func CameCaseToUnderscore added in v1.0.9

func CameCaseToUnderscore(s string) string

func FmtReqId

func FmtReqId(reqId string) string

func GetAcceptLanguage

func GetAcceptLanguage(c *gin.Context) string

func ResolveSearchQuery

func ResolveSearchQuery(driver string, q any, condition Condition, pTName string)

ResolveSearchQuery 解析 *

  • eq 等于(默认不填都可以)
  • like 包含
  • gt / gte 大于 / 大于等于
  • lt / lte 小于 / 小于等于
  • left / ileft :like xxx%
  • right / iright : like %xxx
  • in
  • isnull
  • order 排序 e.g. order[key]=desc order[key]=asc

Types

type BaseDao added in v1.0.3

type BaseDao struct {
	DbName string
}

func NewDao added in v1.0.3

func NewDao(dbname string) *BaseDao

func (*BaseDao) Cache added in v1.0.3

func (s *BaseDao) Cache() cache.ICache

* 获取缓存

func (*BaseDao) Chunk added in v1.0.3

func (s *BaseDao) Chunk(db *gorm.DB, size int, callback func(records []map[string]interface{}) error) error

* * chunk 查询

func (*BaseDao) Count added in v1.0.3

func (s *BaseDao) Count(model any, count *int64) error

* * 条数查询 * model: 查询条件 * count: 查询条数

func (*BaseDao) CountByMap added in v1.0.3

func (s *BaseDao) CountByMap(where map[string]any, model any, count *int64) error

* * 条数查询 * model: 查询条件 * count: 查询条数

func (*BaseDao) Create added in v1.0.3

func (s *BaseDao) Create(model any) error

* 创建 结构体model

func (*BaseDao) CreateTx added in v1.0.3

func (s *BaseDao) CreateTx(tx *gorm.DB, model any) error

func (*BaseDao) DB added in v1.0.3

func (s *BaseDao) DB() *gorm.DB

* 获取数据库

func (*BaseDao) DelIds added in v1.0.3

func (s *BaseDao) DelIds(model any, ids any) error

func (*BaseDao) DelIdsTx added in v1.0.3

func (s *BaseDao) DelIdsTx(tx *gorm.DB, model any, ids any) error

func (*BaseDao) DelModel added in v1.0.6

func (s *BaseDao) DelModel(model any) error

DelModel model id 不能为空

func (*BaseDao) DelWhereMap added in v1.0.3

func (s *BaseDao) DelWhereMap(model any, where map[string]any) error

* 条件删除,模型 where 为map

func (*BaseDao) DelWhereMapTx added in v1.0.3

func (s *BaseDao) DelWhereMapTx(tx *gorm.DB, model any, where map[string]any) error

func (*BaseDao) DelWhereTx added in v1.0.3

func (s *BaseDao) DelWhereTx(tx *gorm.DB, model any) error

func (*BaseDao) Get added in v1.0.3

func (s *BaseDao) Get(id any, model any) error

* 根据id获取模型

func (*BaseDao) GetByMap added in v1.0.3

func (s *BaseDao) GetByMap(where map[string]any, models any) error

* * 列表条件查询 * where: 条件查询 * models: 代表查询返回的model数组

func (*BaseDao) GetByWhere added in v1.0.3

func (s *BaseDao) GetByWhere(where any, models any) error

* * 条件查询 * where: where 查询条件model * models: 代表查询返回的model数组

func (*BaseDao) MakeCondition added in v1.0.3

func (s *BaseDao) MakeCondition(q Query) func(db *gorm.DB) *gorm.DB

* * 查询条件组装

func (*BaseDao) Page added in v1.0.3

func (s *BaseDao) Page(where Query, models any, limit, offset int) error

Page 分页查询

func (*BaseDao) Paginate added in v1.0.3

func (s *BaseDao) Paginate(pageSize, pageIndex int) func(db *gorm.DB) *gorm.DB

* 分页组装

func (*BaseDao) QPage added in v1.0.8

func (s *BaseDao) QPage(where any, data any, total *int64, limit, offset int) error

* 分页获取

func (*BaseDao) Query added in v1.0.3

func (s *BaseDao) Query(where Query, models any) error

* * 查询 * where 实现Query接口

func (*BaseDao) QueryPage added in v1.0.3

func (s *BaseDao) QueryPage(where Query, models any, total *int64, limit, offset int) error

* 分页获取

func (*BaseDao) Save added in v1.0.3

func (s *BaseDao) Save(model any) error

* 更新整个模型 结构体model 注意空值

func (*BaseDao) SaveTx added in v1.0.3

func (s *BaseDao) SaveTx(tx *gorm.DB, model any) error

func (*BaseDao) UpdateById added in v1.0.3

func (s *BaseDao) UpdateById(model any) error

* 根据模型id更新

func (*BaseDao) UpdateByIdTx added in v1.0.3

func (s *BaseDao) UpdateByIdTx(tx *gorm.DB, model any) error

func (*BaseDao) UpdateWhere added in v1.0.3

func (s *BaseDao) UpdateWhere(model any, where any, updates map[string]any) error

* 条件跟新

func (*BaseDao) UpdateWhereModel added in v1.0.3

func (s *BaseDao) UpdateWhereModel(where any, updates any) error

* 模型更新

func (*BaseDao) UpdateWhereModelTx added in v1.0.3

func (s *BaseDao) UpdateWhereModelTx(tx *gorm.DB, where any, updates any) error

func (*BaseDao) UpdateWhereTx added in v1.0.3

func (s *BaseDao) UpdateWhereTx(tx *gorm.DB, model any, where any, updates map[string]any) error

type Condition

type Condition interface {
	SetWhere(k string, v []interface{})
	SetOr(k string, v []interface{})
	SetOrder(k string)
	SetJoinOn(t, on string) Condition
}

type ControlBy

type ControlBy struct {
	CreateBy uint64 `json:"createBy" gorm:"type:bigint unsigned;index;comment:创建者"` //创建者id
	UpdateBy uint64 `json:"updateBy" gorm:"type:bigint unsigned;index;comment:更新者"` //更新者id
}

type GormCondition

type GormCondition struct {
	GormPublic
	Join []*GormJoin
}

func (*GormCondition) SetJoinOn

func (e *GormCondition) SetJoinOn(t, on string) Condition

type GormJoin

type GormJoin struct {
	Type   string
	JoinOn string
	GormPublic
}

func (*GormJoin) SetJoinOn

func (e *GormJoin) SetJoinOn(t, on string) Condition

type GormPublic

type GormPublic struct {
	Where map[string][]interface{}
	Order []string
	Or    map[string][]interface{}
}

func (*GormPublic) SetOr

func (e *GormPublic) SetOr(k string, v []interface{})

func (*GormPublic) SetOrder

func (e *GormPublic) SetOrder(k string)

func (*GormPublic) SetWhere

func (e *GormPublic) SetWhere(k string, v []interface{})

type Model

type Model struct {
	Id int `json:"id" gorm:"type:bigint unsigned;primaryKey;autoIncrement;comment:主键编码"` //主键
}

type ModelIntTime

type ModelIntTime struct {
	CreatedAt time.Time  `json:"createdAt" gorm:"type:datetime;comment:创建时间"`   //创建时间戳
	UpdatedAt *time.Time `json:"updatedAt" gorm:"type:datetime;comment:最后更新时间"` //更新时间戳
}

type ModelTime

type ModelTime struct {
	CreatedAt time.Time      `json:"createdAt" gorm:"comment:创建时间"`   //创建时间
	UpdatedAt *time.Time     `json:"updatedAt" gorm:"comment:最后更新时间"` //更新时间
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index;comment:删除时间"`     //删除时间
}

type Option

type Option func(resp *Resp)

func WithCode

func WithCode(code int) Option

func WithData

func WithData(data any) Option

func WithMsg

func WithMsg(msg string) Option

type PageResp

type PageResp struct {
	List  any   `json:"list"`  //数据列表
	Total int64 `json:"total"` //总条数
}

type Query

type Query interface {
	TableName() string
}

* 条件查询结构体,结果体非零值字段将查询 * @Param type * eq 等于(默认不填都可以) * like 包含 * gt / gte 大于 / 大于等于 * lt / lte 小于 / 小于等于 * left / ileft :like xxx% * right / iright : like %xxx * in * isnull * order 排序 e.g. order[key]=desc order[key]=asc * "-" 忽略该字段 * @Param table * table 不填默认取 TableName值 * @Param column * column 不填以结构体字段 * eg: * type ExampleQuery struct{ * Name string `json:"name" query:"type:like;column:name;table:exampale"` * Status int `json:"status" query:"type:gt"` * } * func (ExampleQuery) TableName() string { * return "ExampleQuery" * }

type QueryTag

type QueryTag string
const (
	EQ        QueryTag = "eq"
	LIKE      QueryTag = "like"
	ILIKE     QueryTag = "ilike"
	LEFT      QueryTag = "left"
	ILEFT     QueryTag = "ileft"
	RIGHT     QueryTag = "right"
	IRIGHT    QueryTag = "iright"
	GT        QueryTag = "gt"
	GTE       QueryTag = "gte"
	LT        QueryTag = "lt"
	LTE       QueryTag = "lte"
	IN        QueryTag = "in"
	ISNULL    QueryTag = "isnull"
	ISNOTNULL QueryTag = "isnotnull"
	ORDER     QueryTag = "order"
	JOIN      QueryTag = "join"
)

type ReqBase

type ReqBase struct {
	ReqId string `json:"reqId" form:"reqId"` // 请求id 链路跟踪
}

type ReqId

type ReqId struct {
	Id uint64 `json:"id" form:"id"` // 主键ID
}

type ReqIds

type ReqIds struct {
	Ids []uint64 `json:"ids" form:"ids[]"` //多id
}

func (*ReqIds) Valid added in v1.0.1

func (x *ReqIds) Valid() error

type ReqPage

type ReqPage struct {
	Page     int `json:"page" form:"page" query:"-"`         // 页码
	PageSize int `json:"pageSize" form:"pageSize" query:"-"` // 每页大小
}

func (*ReqPage) GetOffset

func (e *ReqPage) GetOffset() int

func (*ReqPage) GetPage

func (e *ReqPage) GetPage() int

func (*ReqPage) GetSize

func (e *ReqPage) GetSize() int

type ReqStrId

type ReqStrId struct {
	Id string `json:"id" form:"id"` // 主键ID
}

type Resp

type Resp struct {
	Code int    `json:"code"`           //返回码
	Msg  string `json:"msg,omitempty"`  //消息
	Data any    `json:"data,omitempty"` //数据
}

type StatusModel

type StatusModel struct {
	Status int `json:"status" gorm:"type:tinyint unsigned;comment:状态 1 默认状态 2 成功 3 失败"`
}

Jump to

Keyboard shortcuts

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