对sql查询语句组合查询的通用实现算法(c++版,java版)
  TEZNKK3IfmPf 2023年11月14日 20 0
 现在我们做的大型项目大多要与关系型数据库进行交互,不知道大家有没有遇到这样的问题,组合查询,两个条件A,B,组合情况有A,B,AB,也就三种,写死SQL语句也不麻烦,不就是几个if,else吗!但是要是有三个条件组合呢?A,B,C,这样就会有A,B,C,AB,AC....这要n个if,else吧.
ok,下面我们写个通用算法解决以上问题
首先C++版
 
    1. // 查询条件结构 
    2. struct SelCondition 
    3.     string  m_strValueFirst; 
    4.     string  m_strValueSecond; 
    5.     enum ConditionType {CT_EQUAL = 1, CT_NOT_EQUAL, CT_ABOVE, CT_BELOW, CT_FUZZY,CT_AREA,CT_GROUP} m_conType; 
    6.  
    7. //针对范围
    8.     SelCondition(string valueFirst,string   valueSecond, SelCondition::ConditionType conType) 
    9.     { 
    10.         m_strValueFirst = valueFirst; 
    11.         m_strValueSecond=valueSecond; 
    12.         m_conType = conType; 
    13.     } 
    14. //针对一个条件
    15.     SelCondition(string valueFirst, SelCondition::ConditionType conType) 
    16.     { 
    17.         m_strValueFirst = valueFirst; 
    18.         m_conType = conType; 
    19.     } 
    20.     SelCondition(string valueFirst) 
    21.     { 
    22.         m_strValueFirst = valueFirst; 
    23.     } 
    24.     SelCondition(){}; 
    25. }; 
    26.  
  1. // 返回不带 where 的条件语句,并且是以 and 开头,例如 and id>8 and name=aiht 
  2. string DbOpBase::AssembleCondition(const map<string, SelCondition> &mapConditions) 
  3.     string  strCondition=""
  4.      
  5.     typedef map<string, SelCondition>::const_iterator CI; 
  6.     for(CI p=mapConditions.begin(); p!=mapConditions.end(); ++p) 
  7.     { 
  8.         string strCmd; 
  9.         string colName = p->first; 
  10.         // TODO 判断列名是否是"" 
  11.  
  12. //进行查询类型的判定拼装相应条件
  1.         switch(p->second.m_conType) 
  2.         { 
  3.         case SelCondition::CT_ABOVE: 
  4.             strCmd = _T(" AND ") + colName + _T(" > '") + p->second.m_strValueFirst + _T("'"); 
  5.             break
  6.         case SelCondition::CT_BELOW: 
  7.             strCmd = _T(" AND ") + colName + _T(" < '") + p->second.m_strValueFirst + _T("'"); 
  8.             break
  9.         case SelCondition::CT_EQUAL: 
  10.             strCmd = _T(" AND ") + colName + _T(" = '") + p->second.m_strValueFirst + _T("'"); 
  11.             break
  12.         case SelCondition::CT_NOT_EQUAL: 
  13.             strCmd = _T(" AND ") + colName + _T(" != '") + p->second.m_strValueFirst + _T("'"); 
  14.             break
  15.         case SelCondition::CT_FUZZY: 
  16.             strCmd =_T(" AND ") + colName + _T(" like '%") + p->second.m_strValueFirst +_T("%'"); 
  17.             break
  18.         case SelCondition::CT_AREA: 
  19.             strCmd =_T(" AND ") + colName + _T(" BETWEEN '") + p->second.m_strValueFirst +_T("' AND '")+p->second.m_strValueSecond+_T("'"); 
  20.             break
  21.         case SelCondition::CT_GROUP: 
  22.             strCmd=_T(" group by StartTime "); 
  23.             break
  24.         default
  25.             ; 
  26.  
  27.         } 
  28.         strCondition += strCmd; 
  29.     } 
  30.     return strCondition;     
来看看java版,其中带有自己所做项目的一些字段和逻辑,请见谅
 
  1. public class HostSearchCondition  
  2.     //构造数据库的查询条件(=,%,between) 
  3.     public static final int CT_EQUAL = 0
  4.     public static final int CT_LIKE = 1
  5.     public static final int CT_AREA = 2
  6.  
  7.     // 界面向数据库操作类传值——结构 
  8.     public static class SelCondition 
  9.     { 
  10.             String  m_strValueFirst; 
  11.             String  m_strValueSecond; 
  12.             int m_conType; 
  13.  
  14.             public SelCondition(String valueFirst,String valueSecond, int conType) 
  15.             { 
  16.                     this.m_strValueFirst = valueFirst; 
  17.                     this.m_strValueSecond=valueSecond; 
  18.                     this.m_conType = conType; 
  19.             } 
  20.             public SelCondition(String valueFirst, int conType) 
  21.             { 
  22.                     this.m_strValueFirst = valueFirst; 
  23.                     this.m_conType = conType; 
  24.             } 
  25.             public SelCondition(String valueFirst) 
  26.             { 
  27.                     this.m_strValueFirst = valueFirst; 
  28.             } 
  29.             public SelCondition(){}; 
  30.     }; 
  31.      
  32.      
  33.     static HashMap<String, SelCondition> sqlmap = new HashMap<String, SelCondition>(); 
  34.  
  35.     /** 
  36.      *把组合查询条件传入map 
  37.      * @param 接收的组合查询条件 
  38.      */ 
  39.     public  static HashMap<String, SelCondition> Condition(HostBasicInfo condition) 
  40.     { 
  41.         sqlmap.clear(); 
  42.         SelCondition  selcon; 
  43.         if(condition.getSName().length()!= 0
  44.         { 
  45.             selcon = new SelCondition(condition.getSName(), CT_EQUAL); 
  46.             sqlmap.put("sName", selcon); 
  47.         } 
  48.          
  49.         if(condition.getSCharacterCode().length()!= 0
  50.         { 
  51.             selcon = new SelCondition(condition.getSName(), CT_LIKE); 
  52.             sqlmap.put("sCharacterCode", selcon); 
  53.         } 
  54.          
  55.         if(condition.getNHostType().length()!= 0
  56.         { 
  57.             selcon = new SelCondition(condition.getNHostType(), CT_EQUAL); 
  58.             sqlmap.put("nHostType", selcon); 
  59.         } 
  60.          
  61.         if(condition.getESecLevel().length()!= 0
  62.         { 
  63.             selcon = new SelCondition(condition.getESecLevel(), CT_EQUAL); 
  64.             sqlmap.put("eSecLevel", selcon); 
  65.         } 
  66.         return sqlmap; 
  67.     } 
  68.      
  69.     /** 
  70.      *构造数据库where后的查询条件 
  71.      * @param 组合查询条件map 
  72.      */ 
  73.      @SuppressWarnings("unchecked"
  74.     public static String AssembleCondition(Map<String, SelCondition> mapConditions) 
  75.      { 
  76.          String strCondition=""
  77.          Iterator<?> iter = mapConditions.entrySet().iterator(); 
  78.          while (iter.hasNext()) 
  79.          { 
  80.                 String strCmd = null
  81.                 Map.Entry entry = (Map.Entry) iter.next(); String key = (String)entry.getKey(); 
  82.                 String colName = key;            
  83.                 SelCondition value = (SelCondition)entry.getValue(); 
  84.  
  85.                 switch(value.m_conType) 
  86.                 { 
  87.                     case CT_EQUAL: 
  88.                          strCmd = " AND Tbl_Host_BasicInfo."+ colName + " = '" + value.m_strValueFirst + "'"
  89.                          break
  90.                     case CT_LIKE: 
  91.                          strCmd =" AND Tbl_Host_BasicInfo." + colName + " like '%" + value.m_strValueFirst +"%'"
  92.                          break
  93.                     case CT_AREA: 
  94.                          strCmd ="  " + colName + " BETWEEN '"+ value.m_strValueFirst +"' AND '"+ value.m_strValueSecond+"'"
  95.                          break
  96.                     default
  97.                          ; 
  98.                 } 
  99.                 strCondition = strCondition + strCmd; 
  100.           } 
  101.           return strCondition; 
  102.      } 
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   27   0   0 sqlite数据库
  TEZNKK3IfmPf   2024年05月31日   31   0   0 数据库mysql
  TEZNKK3IfmPf   2024年05月17日   38   0   0 sqlcube
  TEZNKK3IfmPf   2024年05月31日   27   0   0 数据库mysql
TEZNKK3IfmPf