zd

package module
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2022 License: Apache-2.0 Imports: 19 Imported by: 1

README

zorm-md

介绍

后端项目封装。将应用与数据库、前端的逻辑进行封装,在此封装的基础上进行后端项目开发,只需关注在业务逻辑。

软件架构

以 struct 为基础,实现数据库的各类操作封装(Create/Drop/Truncate/Select/Insert/Update/Delete),为 apiFox 提供数据结构所需的 json 代码(camel 格式),生成或更新为前端 vue 所用的 interface。

安装教程

  • Dockerfile 镜像制作
  • Makefile 部署脚本

使用说明

安装达梦(docker)
  1. 下载镜像 https://eco.dameng.com/download/
  2. 安装
    docker load -i dm8_20220822_rev166351_x86_rh6_64_ctm.tar
    docker run -d -p 5236:5236 --restart=always --name dm8 --privileged=true -e PAGE_SIZE=16 -e LD_LIBRARY_PATH=/opt/dmdbms/bin -e INSTANCE_NAME=dm8_01 -v /data/dm8_01:/opt/dmdbms/data dm8_single:v8.1.2.128_ent_x86_64_ctm_pack4
    
数据库操作使用 zorm 框架
  • 初始化 InitDao(dmDSN)

  • 数据表函数(create/drop/truncate)统一为 func[type]()

  • GetTableName 默认取 struct 类别名作为表名(无 schema), 如需改名或增加 schema 则需实现 GetTableName 自定义函数.

  • GetDataType 自定义类型

    • 以 string 的别名, 实现 GetDataType()string 返回 "varchar(nn)", 以实现特定类型的定义, 并在多处重复使用.
工具

不支持数组/切片

  • MapToUpper map 中的 key 全大写,以适应数据库字段匹配
  • ToMap 类型转换 map[string]any,处理自定义类型
  • ToUpperMap 类型转 map 并 key 全大写
  • ToLittleCamelMap 类型转 map key 采用 camel 格式
  • ToLittleCamelJson 类型转 json key 采用 camel 格式
数据库
  • Create Table
    • GetTableName
    • GetPrimaryKey
    • GetColumns
    • struct 组合
      • 重名字段的定义以父 struct 为准
      • zorm.Select 遇到重名字段会报错
    • tag: primaryKey/not null/size/default/index/comment
    • tag: unique/check
    • 自定义类型
      • type xxx string
      • type xxx rune
      • DM 读取 char(1)转为 rune 的自定义类型
    • 基础类型: int float64 bool
  • Drop Table
  • Truncate Table
  • select/find
  • insert
  • update
开启事务
gin
  • Get
  • Post
  • Put
  • Delete
自定义类型转换

func init() {
	//RegisterCustomDriverValueConver 注册自定义的字段处理逻辑,用于驱动无法直接转换的场景,例如达梦的 TEXT 无法直接转化成 string
	//一般是放到init方法里进行注册
	zorm.RegisterCustomDriverValueConver("CHAR", EnumType{})
}

type EnumType struct{}

//GetDriverValue 根据数据库列类型,返回driver.Value的实例,struct属性类型
//非struct类型接收,无法获取到structFieldType,会传入nil
func (e EnumType) GetDriverValue(ctx context.Context, columnType *sql.ColumnType, structFieldType *reflect.Type) (driver.Value, error) {
	// fmt.Println(columnType.Name()) // Status
	// fmt.Println(columnType.DatabaseTypeName()) // CHAR
	// if len, _ := columnType.Length(); len > 1 { // char(1) 时 length == 0
	// 	return nil, nil
	// }
	return new(string), nil
}

//ConverDriverValue 数据库列类型,GetDriverValue返回的driver.Value的临时接收值,struct属性类型
//非struct类型接收,无法获取到structFieldType,会传入nil
//返回符合接收类型值的指针,指针,指针!!!!
func (e EnumType) ConverDriverValue(ctx context.Context, columnType *sql.ColumnType, tempDriverValue driver.Value, structFieldType *reflect.Type) (interface{}, error) {
	val := tempDriverValue.(*string)
	c := rune((*val)[0])
	// 变量类型为 structFieldType 否则提示类型不匹配
	v := reflect.NewAt(*structFieldType, unsafe.Pointer(&c))
	return v.Interface(), nil
}

请求方式与参数类型

请示方式 query 参数 param 参数 body 参数
GET ✔X
POST ✔X ✔X
PUT ✔X ✔X
DELETE ✔X ✔X
状态码
状态码 英文名称 描述
400 Bad Request 客户端请求的语法错误,服务器无法理解
401 Unauthorized 表示用户没有权限(令牌、用户名、密码错误)
403 Forbidden 没有权限访问该请求,服务器收到请求但拒绝提供服务
404 Not Found 服务器无法根据客户端的请求找到资源(如路径不存在)
405 Method Not Allowed 客户端请求的方法服务端不支持,例如使用 POST 方法请求只支持 GET 方法的接口
406 Not Acceptable 用户 GET 请求的格式不可得(比如用户请求 JSON 格式,但是只有 XML 格式)
408 Request Time-out 客户端请求超时
410 Gone 客户端 GET 请求的资源已经不存在。410 不同于 404,如果资源以前有现在被永久删除了可使用 410 代码,网站设计人员可通过 301 代码指定资源的新位置
415 Unsupported Media Type 通常表示服务器不支持客户端请求首部 Content-Type 指定的数据格式。如在只接受 JSON 格式的 API 中放入 XML 类型的数据并向服务器发送
429 Too Many Requests 客户端的请求次数超过限额

Documentation

Overview

自定义类型 type XXX byte const AAA XXX = '0'

自定义类型 type XXX byte const AAA XXX = '0'

Index

Constants

This section is empty.

Variables

View Source
var SchemaName = "SYSDBA" // 模式

Functions

func CreateSchema

func CreateSchema(schemaName string) error

func CreateTable

func CreateTable[T IEntity]() error

func Delete

func Delete[T IEntity](params map[string]any) (int, error)

Delete 删除

func DeleteByName added in v1.0.12

func DeleteByName(tblName string, params map[string]any) (int, error)

DeleteByName 根据表名删除

func DeleteObject

func DeleteObject[T IEntity](c *gin.Context)

DeleteObject delete

func DropTable

func DropTable[T IEntity]() error

func GetColumnName

func GetColumnName(tblName string) []string

GetColumnName 依表名取列名(从dm中取)

func GetObjects

func GetObjects[T IEntity](c *gin.Context)

GetObjects find

func GetPrimaryKey

func GetPrimaryKey[T IEntity]() []string

GetPrimaryKey 主键(全大写)

func GetPrimaryKeyByName added in v1.0.13

func GetPrimaryKeyByName(tblName string) ([]string, error)

GetPrimaryKeyByName 取指定表的主键(从库中查询)

func GetTableName

func GetTableName[T IEntity]() string

GetTableName 根据struct类型取表名(SchemaName+tblName) 大写

func InitDao

func InitDao(dmDSN string) (context.Context, error)

func InitDaoOracle added in v1.0.16

func InitDaoOracle(crmsDSN string) (context.Context, error)

InitDaoOracle 初始化 oracle 应用需要 import _ "github.com/godror/godror" 基于[email protected] 需修改 dbdao.go 995行 valus[i]=new(float64), 以返回 NUMBER 类型

func Insert

func Insert[T IEntity](entities ...T) error

Insert 插入数据

func InsertMap added in v1.0.12

func InsertMap[T IEntity](entities ...map[string]any) error

Insert 插入数据

func InsertMapByName added in v1.0.12

func InsertMapByName(tblName string, entities ...map[string]any) error

InsertMapByName 根据表名插入数据 Insert 基于此

func InsertNotZero added in v1.0.16

func InsertNotZero[T IEntity](entities ...T) error

InsertNotZero 插入数据, 过滤0值(bool:false, int:0, string:"")

func MapToSetFinder

func MapToSetFinder(params map[string]any) *zorm.Finder

MapToSetFinder 用于 update

func MapToWhereFinder

func MapToWhereFinder(params map[string]any) *zorm.Finder

MapToWhereFinder 用于 select update

func PostObject

func PostObject[T IEntity](c *gin.Context)

PostObject insert

func PutObject

func PutObject[T IEntity](c *gin.Context)

PutObject update

func QueryMap added in v1.0.16

func QueryMap(sqlStr string, values ...any) ([]map[string]any, error)

QueryMap 执行sql指令进行查询 用于多表联查,或其他复杂的情况

func ResponseErr

func ResponseErr(c *gin.Context, err error, datas ...any)

func ResponseOK

func ResponseOK(c *gin.Context, datas ...any)

ResponseOK datas... 数据 PageInfo

func Select

func Select[T IEntity](p *PageInfo, params map[string]any, sqlStrAppend ...string) ([]T, error)

Select 查询, 返回 struct p==nil 不分页; params==nil 查所有; fndAppend 附加查询(如Order Limit Group 等)

func SelectByStruct added in v1.0.16

func SelectByStruct[T IEntity](p *PageInfo, t T, sqlStrAppend ...string) ([]T, error)

SelectByStruct 查询,条件用 IEntity 实例, 返回 struct p==nil 不分页; t==T{} 查所有; fndAppend 附加查询(如Order Limit Group 等) t 接受非0值, 若查查询 bool:false int:0 string:"" 的条件,可在 sqlStrAppend 拼接

func SelectCount

func SelectCount[T IEntity](where map[string]any) (int, error)

SelectCount 查询 count(1)

func SelectCountByName added in v1.0.9

func SelectCountByName(tblName string, where map[string]any) (int, error)

SelectCount 根据表名查询 count(1)

func SelectMap

func SelectMap[T IEntity](p *PageInfo, params map[string]any, sqlStrAppend ...string) ([]map[string]any, error)

SelectMap 查询返回 map p==nil 不分页; params==nil 查所有; fndAppend 附加查询(如Order Limit Group 等)

func SelectMapByName

func SelectMapByName(tblName string, p *PageInfo, params map[string]any, sqlStrAppend ...string) ([]map[string]any, error)

SelectMapByName 根据表名查询, 返回 map p==nil 不分页; params==nil 查所有; fndAppend 附加查询(如Order Limit Group 等)

func SelectMapColumns added in v1.0.9

func SelectMapColumns[T IEntity](p *PageInfo, params map[string]any, columns []string, sqlStrAppend ...string) ([]map[string]any, error)

SelectMapColumns 返回 map, 指定字段名 p==nil 不分页; params==nil 查所有; fndAppend 附加查询(如Order Limit Group 等) columns 指定查询列名

func SelectMapColumnsByName added in v1.0.9

func SelectMapColumnsByName(tblName string, p *PageInfo, params map[string]any, columns []string, sqlStrAppend ...string) ([]map[string]any, error)

SelectMapColumnsByName 根据表名查询, 返回 map 指定列名 p==nil 不分页; params==nil 查所有; fndAppend 附加查询(如Order Limit Group 等) columns 指定查询列名 所有 SelectMap 基于此

func SelectRow added in v1.0.9

func SelectRow[T IEntity](columnName string, params map[string]any, sqlStrAppend ...string) (any, error)

SelectRow 返回首行指定列的值, 无效查询返回 nil, nil

func SelectRowByName added in v1.0.9

func SelectRowByName(tblName string, columnName string, params map[string]any, sqlStrAppend ...string) (any, error)

SelectRowByName 根据表名查询, 返回首行指定列的值, 无效查询返回 nil, nil

func SetDefaultCtx added in v1.0.16

func SetDefaultCtx(ctx context.Context)

func StructToJson added in v1.0.12

func StructToJson[T IEntity]() string

StructToJson 转为json 为apiFox使用

func TableExists

func TableExists(tblName string) bool

TableExists 表是否创建

func ToLittleCamelJson

func ToLittleCamelJson(t any) string

ToLittleCamelJson

func ToLittleCamelMap

func ToLittleCamelMap(t any) map[string]any

ToLittleCamelMap

func ToMap

func ToMap(t any) map[string]any

ToMap

func ToUpperMap

func ToUpperMap(t any) map[string]any

ToUpperMap

func Transaction

func Transaction(fun func() error) error

Transaction 使用事务

func TrunTable

func TrunTable[T IEntity]() error

func Update

func Update[T IEntity](entities ...T) (int, error)

Update 更新 Struct

func UpdateMap added in v1.0.12

func UpdateMap[T IEntity](where map[string]any, set map[string]any) (int, error)

UpdateOne 更新(单个条件)

func UpdateMapByName added in v1.0.12

func UpdateMapByName(tblName string, where map[string]any, set map[string]any) (int, error)

UpdateMapByName 根据表名更新(单个条件)

func UpdateMapConditions added in v1.0.12

func UpdateMapConditions[T IEntity](wheres []map[string]any, set map[string]any) (int, error)

UpdateMapConditions 更新(多条件)

func UpdateMapConditionsByName added in v1.0.12

func UpdateMapConditionsByName(tblName string, wheres []map[string]any, set map[string]any) (int, error)

UpdateMapConditionsByName 根据表名更新 更新数据量为 0,会返回 error 所有 Update 基于此

Types

type Entity

type Entity struct{}

Entity 实现 IEntity

func (Entity) GetPKColumnName

func (Entity) GetPKColumnName() string

用 GetPrimaryKey[T] 替代

func (Entity) GetPkSequence

func (Entity) GetPkSequence() string

func (Entity) GetPrimaryKey

func (Entity) GetPrimaryKey() []string

GetPrimaryKey 表的主键, 返回 nil 时忽略此函数; 否则以此函数为准忽略 tag中的 primaryKey 配置

func (Entity) GetTableName

func (Entity) GetTableName() string

GetTableName 返回struct类型名;需要struct自行实现

type EnumType

type EnumType struct{}

func (EnumType) ConverDriverValue

func (e EnumType) ConverDriverValue(ctx context.Context, columnType *sql.ColumnType, tempDriverValue driver.Value, structFieldType *reflect.Type) (interface{}, error)

ConverDriverValue 数据库列类型,GetDriverValue返回的driver.Value的临时接收值,struct属性类型 非struct类型接收,无法获取到structFieldType,会传入nil 返回符合接收类型值的指针,指针,指针!!!!

func (EnumType) GetDriverValue

func (e EnumType) GetDriverValue(ctx context.Context, columnType *sql.ColumnType, structFieldType *reflect.Type) (driver.Value, error)

GetDriverValue 根据数据库列类型,返回driver.Value的实例,struct属性类型 非struct类型接收,无法获取到structFieldType,会传入nil

type Error

type Error struct {
	Code int    `json:"code,omitempty"` // 错误码
	Msg  string `json:"msg,omitempty"`  // 说明
}

type IEntity

type IEntity interface {
	zorm.IEntityStruct
	GetPrimaryKey() []string // 主键
}

type NumericType added in v1.0.14

type NumericType struct{}

func (NumericType) ConverDriverValue added in v1.0.14

func (e NumericType) ConverDriverValue(ctx context.Context, columnType *sql.ColumnType, tempDriverValue driver.Value, structFieldType *reflect.Type) (interface{}, error)

ConverDriverValue 数据库列类型,GetDriverValue返回的driver.Value的临时接收值,struct属性类型 非struct类型接收,无法获取到structFieldType,会传入nil 返回符合接收类型值的指针,指针,指针!!!!

func (NumericType) GetDriverValue added in v1.0.14

func (e NumericType) GetDriverValue(ctx context.Context, columnType *sql.ColumnType, structFieldType *reflect.Type) (driver.Value, error)

GetDriverValue 根据数据库列类型,返回driver.Value的实例,struct属性类型 非struct类型接收,无法获取到structFieldType,会传入nil

type PageInfo

type PageInfo struct {
	PageNo     int `json:"pageNo,omitempty"`
	PageSize   int `json:"pageSize,omitempty"`
	TotalCount int `json:"totalCount,omitempty"`
}

func GetQryParams

func GetQryParams(c *gin.Context) (*PageInfo, map[string]any)

type Response

type Response struct {
	Status string      `json:"status"`
	Msg    string      `json:"msg"`
	Data   interface{} `json:"data"`
	Data2  interface{} `json:"data2"`
}

type TableColumn

type TableColumn struct {
	ColumnName      string // 数据表字段名(字段名or由columns定义)
	StructFieldName string // 结构体字段名称
	ColumnType      string // 数据表中的类型定义
	Comment         string
	PrimaryKey      bool
	NotNull         bool
	Unique          bool
	Index           bool
	AutoIncrement   bool // 自增
	Default         string
}

func GetColumns

func GetColumns[T IEntity]() []TableColumn

GetColumns 列定义(列名大写)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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