首页 > 其他分享 >goDbClient开源代码--通用查询

goDbClient开源代码--通用查询

时间:2024-04-05 09:04:55浏览次数:16  
标签:ToPrettyString 源代码 dbRequest -- TableName field result var goDbClient

开源地址: https://gitee.com/ichub/godbclient.git

8e79e0a55fb34b739842ab712222ae8f.jpeg

通用查询

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())

}

720fe78f111443ba9b4709df1cecbad2.png查询条件:

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

相关文章

  • 专题:数学
    欧拉函数定义\(\phi(n)\)表示\([1,n-1]\)内与\(n\)互质的数的个数。公式对于所有质数,显然有\(\phi(n)=n-1\)。若\(n=p_1^{\alpha_1}\timesp_2^{\alpha_2}\ldots\timesp_k^{\alpha_k}\),则\[\phi(n)=(p_1-1)\timesp_1^{\alpha_1-1}\times(p_1......
  • Java项目:基于Springboot+vue实现的医院住院管理系统设计与实现(源码+数据库+开题报告+
    一、项目简介本项目是一套基于Springboot+vue实现的医院住院管理系统设包含:项目源码、数据库脚本等,该项目附带全部源码可作为毕设使用。项目都经过严格调试,eclipse或者idea确保可以运行!该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值......
  • MLIR中间表示和编译器框架
    MLIR中间表示和编译器框架背景随着深度学习技术的发展,深度学习技术也逐渐从学术研究的方向转向了实践应用的方向,这不仅对深度模型的准确率有了较高的需求,也对深度模型的推理速度有了越来越高的需求。目前深度模型的推理引擎按照实现方式大体分为两类:解释型推理引擎:一般包含......
  • 原创c++小游戏《扫雷但是地狱难度》1.0.1版本
    这个扫雷非常难,2500个格子,500个雷#include<bits/stdc++.h>usingnamespacestd;charm[51][51],rm[51][51];intbombs=500;//intbxy[501][501];intd[8][2]={{0,1},{0,-1},{1,1},{-1,1},{1,0},{-1,0},{-1,-1},{1,-1}};voidtancha(intx,inty){ intt=0; if(rm[x+1......
  • 中间件 ZK分布式专题与Dubbo微服务入门 7-2 搭建maven工程,建立curator与zkserver的连
    0课程地址https://coding.imooc.com/lesson/201.html#mid=12839 1重点关注1.1本节内容使用curator建立和zkServer的连接java用法STS搭建maven工程 1.2关键代码/***同步创建zk示例,原生api是异步的,推荐第1,2种写法......
  • 基于SSM+Jsp+Mysql的超市管理系统
    开发语言:Java框架:ssm技术:JSPJDK版本:JDK1.8服务器:tomcat7数据库:mysql5.7(一定要5.7版本)数据库工具:Navicat11开发软件:eclipse/myeclipse/ideaMaven包:Maven3.3.9系统展示管理员登录员工管理商品类型管理商品库存管理商品进货管理上班打卡管理请假信息管理交流信息......
  • [转帖]shell编程-流程控制-if语句
    1.单分支if条件语句单分支if条件语句有三种写法:if[条件判断式];then操作fi或者if[条件判断式]then操作fi或者if[条件判断式];then操作;fi说明:只有条件成立,才执行相应的操作。示例:if[aa==aa];thenechoyes;fi1注意要点:if语句使用......
  • P1776宝物筛选
    宝物筛选题目描述终于,破解了千年的难题。小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物。这下小FF可发财了,嘎嘎。但是这里的宝物实在是太多了,小FF的采集车似乎装不下那么多宝物。看来小FF只能含泪舍弃其中的一部分宝物了。小FF对洞穴里的宝物进行了整......
  • [转帖]Linux内存–零拷贝
    https://plantegg.github.io/2020/11/15/Linux%E5%86%85%E5%AD%98--%E9%9B%B6%E6%8B%B7%E8%B4%9D/ 本系列有如下几篇Linux内存问题汇总Linux内存–PageCacheLinux内存–管理和碎片Linux内存–HugePageLinux内存–零拷贝零拷贝“Zero-copy“describescomputeroper......
  • 如何使用AI生成短视频脚本
    短视频作为目前最火热的创作方式,很多人都想拍短视频来表达自己的想法,但是却不知道如何下手,今天教大家如何快速生成短视频文案。打开智游剪辑(官网:zyjj.cc),搜索抖音文案生成然后我们就只需要输入视频主题即可帮你自动生成视频文案了主题是可以任意的,比如我们可以生成......