开源地址: https://gitee.com/ichub/godbclient.git
通用查询
func Test0010_SelectDptSub(t *testing.T) { var dbRequest = NewPageDbRequest(2) dbRequest.TableName = "department" dbRequest.Eq("id", 1) dbRequest.NewSubTable() dbRequest.SubTable.TableName = "employee" dbRequest.SubTable.JoinKeys["id"] = "department_id" dbRequest.SubTable.FieldsName = "id,name" var result = dbRequest.GeneralQuery() assert.Equal(t, 200, result.Code) ichublog.Log(result.ToPrettyString()) logrus.Info(result.ToPrettyString()) }
查询条件:
func (this *IchubPageRequest) BuildWhere(dbc *gorm.DB) *gorm.DB { this.InitPage() if this.Fields == nil { return dbc } for _, field := range this.Fields { this.TransOpType(field) //if notbetween noin notlike if field.OpType == pagebase.OpSign[pagebase.Between] { dbc = dbc.Where(fmt.Sprintf("%s BETWEEN ? and ?", field.Field), field.Values[0], field.Values[1]) } if field.OpType == pagebase.OpSign[pagebase.NotBetween] { dbc = dbc.Where(fmt.Sprintf("%s Not BETWEEN ? and ?", field.Field), field.Values[0], field.Values[1]) } if field.OpType == pagebase.OpSign[pagebase.Ge] { dbc = dbc.Where(fmt.Sprintf("%s >= ?", field.Field), field.Values[0]) } if field.OpType == pagebase.OpSign[pagebase.Gt] { dbc = dbc.Where(fmt.Sprintf("%s > ?", field.Field), field.Values[0]) } if field.OpType == pagebase.OpSign[pagebase.Le] { dbc = dbc.Where(fmt.Sprintf("%s <= ?", field.Field), field.Values[0]) } if field.OpType == pagebase.OpSign[pagebase.Lt] { dbc = dbc.Where(fmt.Sprintf("%s < ?", field.Field), field.Values[0]) } if field.OpType == pagebase.OpSign[pagebase.Eq] { dbc = dbc.Where(fmt.Sprintf("%s = ?", field.Field), field.Values[0]) } if field.OpType == pagebase.OpSign[pagebase.Ne] { dbc = dbc.Where(fmt.Sprintf("%s != ?", field.Field), field.Values[0]) } if field.OpType == pagebase.OpSign[pagebase.In] { dbc = dbc.Where(fmt.Sprintf("%s in (%s)", field.Field, field.Values2InStr())) } if field.OpType == pagebase.OpSign[pagebase.NotIn] { dbc = dbc.Where(fmt.Sprintf("%s not in (%s)", field.Field, field.Values2InStr())) } if field.OpType == pagebase.OpSign[pagebase.Like] { var sval = baseutils.Any2Str(field.Values[0]) dbc = dbc.Where(fmt.Sprintf("%s like ?", field.Field), "'%"+sval+"%'") } if field.OpType == pagebase.OpSign[pagebase.NotLike] { var sval = baseutils.Any2Str(field.Values[0]) dbc = dbc.Where(fmt.Sprintf("%s not like ?", field.Field), "'%"+sval+"%'") } } return dbc }
使用用例
package pagedbdto import ( "gitee.com/ichub/goconfig/common/ichublog" "gitee.com/ichub/godbclient/dbclient/dbclientdto" "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "testing" "time" ) /* @Title 文件名称: generalquery.go @Description 描述: 通用查询 @Author 作者: [email protected] 时间(2024-02-22 22:38:21) @Update 作者: [email protected] 时间(2024-02-21 22:38:21) */ //http://www.forenose.com/column/content/427118110.html //领域事件建模 func init() { ichublog.InitLogrus() } func Test001_SelectDptGeneral(t *testing.T) { var dbRequest = NewPageDbRequest(2) // dbRequest.Clear() dbRequest.TableName = "department" var result = dbRequest.GeneralQuery() assert.Equal(t, 200, result.Code) ichublog.Log(result.ToPrettyString()) } func Test002_SelectEmployeeGeneral(t *testing.T) { var dbRequest = NewIchubPageDbRequest() dbRequest.PageSize = 2 dbRequest.TableName = "employee" dbRequest.TimeToInt = true dbRequest.Ge("department_id", 1) var result = dbRequest.GeneralQuery() ichublog.Log(result.ToPrettyString()) } // IchubPageDbRequest func Test003_SelectRuleDbRequest(t *testing.T) { var dbRequest = NewIchubPageDbRequest() dbRequest.TableName = "rules" // dbRequest.Ge("rule_id", 3) // dbRequest.OrderBy("rule_id", "asc") var result = dbRequest.GeneralQuery() ichublog.Log(result.ToPrettyString()) assert.Equal(t, 200, result.Code) } func Test0004_GeneralQueryEmpRequest(t *testing.T) { var dbRequest = NewIchubPageDbRequest() dbRequest.PageSize = 2 dbRequest.TableName = "employee" dbRequest.Lt("birthday", time.Now()) var result = dbRequest.GeneralQuery() logrus.Info(result.ToPrettyString()) ichublog.Log(result.ToPrettyString()) assert.Equal(t, 200, result.Code) } func Test0004_GeneralQueryDtoEmpRequest(t *testing.T) { var dbRequest = NewIchubPageDbRequest() dbRequest.PageSize = 2 dbRequest.TableName = "employee" dbRequest.Lt("birthday", time.Now()) var result = dbRequest.GeneralQueryDto() logrus.Info(result.ToPrettyString()) ichublog.Log(result.ToPrettyString()) assert.Equal(t, 200, result.Code) } func Test0006_QueryServicePolicys(t *testing.T) { var dbRequest = NewIchubPageDbRequest() dbRequest.PageSize = 4 dbRequest.TableName = "service_policy_instances" // dbRequest.Source = "dept_id,create_time" var result = dbRequest.GeneralQuery() logrus.Info(result.ToPrettyString()) ichublog.Log(result.ToPrettyString()) assert.Equal(t, 200, result.Code) } func Test0009_SelectEmp(t *testing.T) { //fields var dbRequest = NewPageDbRequest(1) dbRequest.TableName = "employee" dbRequest.TimeToInt = true var pageResult = dbRequest.GeneralQuery() ichublog.Log(pageResult.ToPrettyString()) } func Test0010_SelectDptSub(t *testing.T) { var dbRequest = NewPageDbRequest(2) dbRequest.TableName = "department" dbRequest.Eq("id", 1) dbRequest.NewSubTable() dbRequest.SubTable.TableName = "employee" dbRequest.SubTable.JoinKeys["id"] = "department_id" dbRequest.SubTable.FieldsName = "id,name" var result = dbRequest.GeneralQuery() assert.Equal(t, 200, result.Code) ichublog.Log(result.ToPrettyString()) logrus.Info(result.ToPrettyString()) } func Test0011_QueryMetadata(t *testing.T) { var dbRequest = NewIchubPageDbRequest() dbRequest.TableName = "service_policy_instances" var result = dbRequest.MetadataQuery() ichublog.Log(result.ToPrettyString()) assert.Equal(t, 200, result.Code) } func Test0012_QueryRulePs(t *testing.T) { var dbRequest = NewIchubPageDbRequest() dbRequest.TableName = "rule_params" dbRequest.Eq("policy_id", 1) var result = dbRequest.GeneralQuery() ichublog.Log(result) assert.Equal(t, 200, result.Code) var ps = dbclientdto.IchubParams{} for _, v := range result.Data.([]map[string]any) { logrus.Println(v["code"].(string) + ":" + v["inival"].(string)) ps.SetParam(v["code"].(string), v["inival"].(string)) } logrus.Info(ps.ToJson()) } func FindPs() dbclientdto.IchubParams { var dbRequest = NewIchubPageDbRequest() dbRequest.TableName = "rule_params" dbRequest.Eq("policy_id", 1) var result = dbRequest.GeneralQuery() var ps = dbclientdto.IchubParams{} for _, v := range result.Data.([]map[string]any) { logrus.Println(v["code"].(string) + ":" + v["inival"].(string)) ps.SetParam(v["code"].(string), v["inival"].(string)) } logrus.Info(ps) return ps } func Test0013_QueryRule(t *testing.T) { var dbRequest = NewIchubPageDbRequest() dbRequest.TableName = "rules" dbRequest.Eq("rule_id", 1) var result = dbRequest.GeneralQuery() var rule = result.Data.([]map[string]any)[0]["rule"] var pss = result.Data.([]map[string]any)[0]["param"] //var ps = FindPs() //logrus.Info(ps.String(), pss) //ps.MergeJson(pss.(string)) assert.Equal(t, 200, result.Code) ichublog.Log(rule, pss) }
标签:ToPrettyString,源代码,dbRequest,--,TableName,field,result,var,goDbClient From: https://blog.csdn.net/leijmdas/article/details/137380143