增加测试用例

This commit is contained in:
springrain
2023-06-27 17:34:04 +08:00
parent 0307df1576
commit 6fe77d33f9
3 changed files with 125 additions and 16 deletions

View File

@@ -19,6 +19,7 @@
package zorm
import (
"encoding/json"
"errors"
"strings"
)
@@ -34,13 +35,13 @@ type Finder struct {
values []interface{}
// 注入检查,默认true 不允许SQL注入的 ' 单引号
// Injection check, default true does not allow SQL injection single quote
InjectionCheck bool
InjectionCheck bool `json:"injectionCheck,omitempty"`
// CountFinder 自定义的查询总条数'Finder',使用指针默认为nil.主要是为了在'group by'等复杂情况下,为了性能,手动编写总条数语句
// CountFinder The total number of custom queries is'Finder', and the pointer is nil by default. It is mainly used to manually write the total number of statements for performance in complex situations such as'group by'
CountFinder *Finder
CountFinder *Finder `json:"countFinder,omitempty"`
// 是否自动查询总条数,默认true.同时需要Page不为nil,才查询总条数
// Whether to automatically query the total number of entries, the default is true. At the same time, the Page is not nil to query the total number of entries
SelectTotalCount bool
SelectTotalCount bool `json:"selectTotalCount,omitempty"`
// SQL语句
// SQL statement
sqlstr string
@@ -182,3 +183,60 @@ func (finder *Finder) GetSQL() (string, error) {
finder.sqlstr = sqlstr
return sqlstr, nil
}
// GetValues 返回Finder封装的values值
func (finder *Finder) GetValues() ([]interface{}, error) {
// 不要自己构建finder,使用NewFinder方法
// Don't build finder by yourself, use NewFinder method
if finder == nil || finder.values == nil {
return nil, errors.New("->finder-->GetValues()不要自己构建finder,使用NewFinder()方法")
}
return finder.values, nil
}
func (finder *Finder) MarshalJSON() ([]byte, error) {
if finder == nil {
return nil, errors.New("->finder-->MarshalJSON()finder对象为nil")
}
sqlstr, err := finder.GetSQL()
if err != nil {
return nil, err
}
values, err := finder.GetValues()
if err != nil {
return nil, err
}
type FinderJSON Finder
data := struct {
*FinderJSON
SQLStr string `json:"sqlstr,omitempty"`
Values []interface{} `json:"values,omitempty"`
}{
FinderJSON: (*FinderJSON)(finder),
SQLStr: sqlstr,
Values: values,
}
return json.Marshal(data)
}
func (finder *Finder) UnmarshalJSON(data []byte) error {
if finder == nil {
return errors.New("->finder-->UnmarshalJSON()finder对象为nil")
}
type FinderJSON Finder
aux := &struct {
*FinderJSON
SQLStr string `json:"sqlstr,omitempty"`
Values []interface{} `json:"values,omitempty"`
}{
FinderJSON: (*FinderJSON)(finder),
Values: make([]interface{}, 0),
}
if err := json.Unmarshal(data, aux); err != nil {
return err
}
finder.Append(aux.SQLStr, aux.Values...)
return nil
}

View File

@@ -18,6 +18,11 @@
package zorm
import (
"encoding/json"
"errors"
)
// IEntityStruct "struct"实体类的接口,所有的struct实体类都要实现这个接口
// IEntityStruct The interface of the "struct" entity class, all struct entity classes must implement this interface
type IEntityStruct interface {
@@ -77,9 +82,9 @@ const defaultPkName = "id"
//GetTableName 获取表名称,必须有具体的Struct实现,类似java的抽象方法,避免手误忘记写表名.如果有扩展需求,建议使用接口进行扩展,不要默认实现GetTableName
/*
func (entity *EntityStruct) GetTableName() string {
return ""
}
func (entity *EntityStruct) GetTableName() string {
return ""
}
*/
// GetPKColumnName 获取数据库表的主键字段名称.因为要兼容Map,只能是数据库的字段名称
@@ -110,9 +115,9 @@ type EntityMap struct {
// 表名
tableName string
// 主键列名
PkColumnName string
PkColumnName string `json:"pkColumnName,omitempty"`
// 主键序列,如果有值,优先级最高
PkSequence string
PkSequence string `json:"pkSequence,omitempty"`
// 数据库字段,不暴露外部
dbFieldMap map[string]interface{}
// 列名,记录顺序
@@ -168,3 +173,49 @@ func (entity *EntityMap) Set(key string, value interface{}) map[string]interface
return entity.dbFieldMap
}
func (entityMap *EntityMap) MarshalJSON() ([]byte, error) {
if entityMap == nil {
return nil, errors.New("->entityMap-->MarshalJSON()entityMap对象为nil")
}
type EntityMapJSON EntityMap
data := struct {
*EntityMapJSON
TableName string `json:"tableName,omitempty"`
DBFieldMap map[string]interface{} `json:"dbFieldMap,omitempty"`
DBFieldMapKey []string `json:"dbFieldMapKey,omitempty"`
}{
EntityMapJSON: (*EntityMapJSON)(entityMap),
TableName: entityMap.tableName,
DBFieldMap: entityMap.dbFieldMap,
DBFieldMapKey: entityMap.dbFieldMapKey,
}
return json.Marshal(data)
}
func (entityMap *EntityMap) UnmarshalJSON(data []byte) error {
if entityMap == nil {
return errors.New("->entityMap-->UnmarshalJSON()entityMap对象为nil")
}
type EntityMapJSON EntityMap
aux := &struct {
*EntityMapJSON
TableName string `json:"tableName,omitempty"`
DBFieldMap map[string]interface{} `json:"dbFieldMap,omitempty"`
DBFieldMapKey []string `json:"dbFieldMapKey,omitempty"`
}{
EntityMapJSON: (*EntityMapJSON)(entityMap),
DBFieldMap: make(map[string]interface{}, 0),
DBFieldMapKey: make([]string, 0),
}
if err := json.Unmarshal(data, aux); err != nil {
return err
}
entityMap.tableName = aux.TableName
entityMap.dbFieldMap = aux.DBFieldMap
entityMap.dbFieldMapKey = aux.DBFieldMapKey
return nil
}

16
Page.go
View File

@@ -23,35 +23,35 @@ package zorm
type Page struct {
// 当前页码,从1开始
// Current page number, starting from 1
PageNo int
PageNo int `json:"pageNo,omitempty"`
// 每页多少条,默认20条
// How many items per page, 20 items by default
PageSize int
PageSize int `json:"pageSize,omitempty"`
// 数据总条数
// Total number of data
TotalCount int
TotalCount int `json:"totalCount,omitempty"`
// 共多少页
// How many pages
PageCount int
PageCount int `json:"pageCount,omitempty"`
// 是否是第一页
// Is it the first page
FirstPage bool
FirstPage bool `json:"firstPage,omitempty"`
// 是否有上一页
// Whether there is a previous page
HasPrev bool
HasPrev bool `json:"hasPrev,omitempty"`
// 是否有下一页
// Is there a next page
HasNext bool
HasNext bool `json:"hasNext,omitempty"`
// 是否是最后一页
// Is it the last page
LastPage bool
LastPage bool `json:"lastPage,omitempty"`
}
// NewPage 创建Page对象