// 根据nodeItem得到检索的相关信息 // parameter: // nodeItem 配置节点 // strTarget out参数,返回检索目标,会是多库的多途径 // strWord out参数,返回检索词 // strMatch out参数,返回匹配方式 // strRelation out参数,返回关系符 // strDataType out参数,返回检索的数据类型 // strIdOrder out参数,返回id的排序规则 // strKeyOrder out参数,返回key的排序规则 // strOrderBy out参数,返回由order与originOrder组合的排序规则,例如" keystring ASC,idstring DESC " // nMax out参数,返回最大条数 // strError out参数,返回出错信息 // return: // -1 出错 // 0 成功 public static int GetSearchInfo(XmlNode nodeItem, out string strTarget, out string strWord, out string strMatch, out string strRelation, out string strDataType, out string strIdOrder, out string strKeyOrder, out string strOrderBy, out int nMaxCount, out string strError) { strTarget = ""; strWord = ""; strMatch = ""; strRelation = ""; strDataType = ""; strIdOrder = ""; strKeyOrder = ""; strOrderBy = ""; nMaxCount = 0; strError = ""; //-------------------------------------- //调GetTarget函数,得到检索目标target节点 XmlNode nodeTarget = QueryUtil.GetTarget(nodeItem); if (nodeTarget == null) { strError = "检索式的target元素未定义"; return(-1); } strTarget = DomUtil.GetAttrDiff(nodeTarget, "list"); if (strTarget == null) { strError = "target元素的list属性未定义"; } strTarget = strTarget.Trim(); if (strTarget == "") { strError = "target元素的list属性值不能为空字符串"; return(-1); } //------------------------------------------- //检索文本 可以为空字符串 XmlNode nodeWord = nodeItem.SelectSingleNode("word"); if (nodeWord == null) { strError = "检索式的word元素未定义"; return(-1); } strWord = DomUtil.GetNodeText(nodeWord); strWord = strWord.Trim(); //------------------------------------ //匹配方式 XmlNode nodeMatch = nodeItem.SelectSingleNode("match"); if (nodeMatch == null) { strError = "检索式的match元素未定义"; return(-1); } strMatch = DomUtil.GetNodeText(nodeMatch); strMatch = strMatch.Trim(); if (strMatch == "") { strError = "检索式的match元素内容不能为空字符串"; return(-1); } if (QueryUtil.CheckMatch(strMatch) == false) { strError = "检索式的match元素内容'" + strMatch + "'不合法,必须为left,middle,right,exact"; return(-1); } //-------------------------------------------- //关系操作符 XmlNode nodeRelation = nodeItem.SelectSingleNode("relation"); if (nodeRelation == null) { strError = "检索式的relation元素未定义"; return(-1); } strRelation = DomUtil.GetNodeText(nodeRelation); strRelation = strRelation.Trim(); if (strRelation == "") { strError = "检索式的relation元素内容不能为空字符串"; return(-1); } strRelation = QueryUtil.ConvertLetterToOperator(strRelation); if (QueryUtil.CheckRelation(strRelation) == false) { strError = "检索式的relation元素内容'" + strRelation + "'不合法."; return(-1); } //------------------------------------------- //数据类型 XmlNode nodeDataType = nodeItem.SelectSingleNode("dataType"); if (nodeDataType == null) { strError = "检索式的dataType元素未定义"; return(-1); } strDataType = DomUtil.GetNodeText(nodeDataType); strDataType = strDataType.Trim(); if (strDataType == "") { strError = "检索式的dataType元素内容不能为空字符串"; return(-1); } if (QueryUtil.CheckDataType(strDataType) == false) { strError = "检索式的dataType元素内容'" + strDataType + "'不合法,必须为string,number"; return(-1); } // ----------order可以不存在---------- int nOrderIndex = -1; string strOrder = null; int nOriginOrderIndex = -1; string strOriginOrder = null; //id的序 //ASC:升序 //DESC:降序 XmlNode nodeOrder = nodeItem.SelectSingleNode("order"); // 当定义了order元素时,才会id进行排序 if (nodeOrder != null) { string strOrderText = DomUtil.GetNodeText(nodeOrder); strOrderText = strOrderText.Trim(); if (strOrderText != "") { strOrder = "idstring " + strOrderText; nOrderIndex = DomUtil.GetIndex(nodeOrder); strIdOrder = strOrderText; } } //key的序 //ASC:升序 //DESC:降序 XmlNode nodeOriginOrder = nodeItem.SelectSingleNode("originOrder"); // 当定义了order元素时,才会id进行排序 if (nodeOriginOrder != null) { string strOriginOrderText = DomUtil.GetNodeText(nodeOriginOrder); strOriginOrderText = strOriginOrderText.Trim(); if (strOriginOrderText != "") { strOriginOrder = "keystring " + strOriginOrderText; nOriginOrderIndex = DomUtil.GetIndex(nodeOriginOrder); strKeyOrder = strOriginOrderText; } } if (strOrder != null && strOriginOrder != null) { if (nOrderIndex == -1 || nOriginOrderIndex == -1) { strError = "此时nOrderIndex和nOriginOrderIndex都不可能为-1"; return(-1); } if (nOrderIndex == nOriginOrderIndex) { strError = "nOrderIndex 与 nOriginOrderIndex不可能相等"; return(-1); } if (nOrderIndex > nOriginOrderIndex) { strOrderBy = strOrder + "," + strOriginOrder; } else { strOrderBy = strOriginOrder + "," + strOrder; } } else { if (strOrder != null) { strOrderBy = strOrder; } if (strOriginOrder != null) { strOrderBy = strOriginOrder; } } //------------------------------------------- //最大个数 XmlNode nodeMaxCount = nodeItem.SelectSingleNode("maxCount"); /* * if (nodeMaxCount == null) * { * strError = "检索式的maxCount元素未定义"; * return -1; * } */ string strMaxCount = ""; if (nodeMaxCount != null) { strMaxCount = DomUtil.GetNodeText(nodeMaxCount).Trim(); } if (strMaxCount == "") { strMaxCount = "-1"; } /* * if (strMaxCount == "") * { * strError = "检索式的maxCount元素的值为空字符串"; * return -1; * } */ try { nMaxCount = Convert.ToInt32(strMaxCount); if (nMaxCount < -1) { strError = "xml检索式的maxCount的值'" + strMaxCount + "'不合法,必须是数值型"; return(-1); } } catch { strError = "xml检索式的maxCount的值'" + strMaxCount + "'不合法,必须是数值型"; return(-1); } return(0); }
// 检索单元item的信息,对库进行检索 // parameter: // nodeItem item节点 // resultSet 传进结果集,????????每次清空,既然每次清空,那还不如返回一个结果集呢 // isConnected 是否连接 // strError out参数,返回出错信息 // return: // -1 出错 // -6 无足够的权限 // 0 成功 public int doItem(XmlNode nodeItem, DpResultSet resultSet, Delegate_isConnected isConnected, out string strError) { strError = ""; if (nodeItem == null) { strError = "doItem() nodeItem参数为null."; return(-1); } if (resultSet == null) { strError = "doItem() oResult参数为null."; return(-1); } //先清空一下 resultSet.Clear(); int nRet; //调processRelation对检索单元的成员检查是否存在矛盾 //如果返回0,则可能对item的成员进行了修改,所以后面重新提取内容 nRet = ProcessRelation(nodeItem); if (nRet == -1) { strError = "doItem()里调processRelation出错"; return(-1); } // 根据nodeItem得到检索信息 string strTarget; string strWord; string strMatch; string strRelation; string strDataType; string strIdOrder; string strKeyOrder; string strOrderBy; int nMaxCount; nRet = QueryUtil.GetSearchInfo(nodeItem, out strTarget, out strWord, out strMatch, out strRelation, out strDataType, out strIdOrder, out strKeyOrder, out strOrderBy, out nMaxCount, out strError); if (nRet == -1) { return(-1); } //将target以;号分成多个库 string[] aDatabase = strTarget.Split(new Char[] { ';' }); foreach (string strOneDatabase in aDatabase) { if (strOneDatabase == "") { continue; } string strDbName; string strTableList; // 拆分库名与途径 nRet = DatabaseUtil.SplitToDbNameAndForm(strOneDatabase, out strDbName, out strTableList, out strError); if (nRet == -1) { return(-1); } // 得到库 Database db = m_dbColl.GetDatabase(strDbName); if (db == null) { strError = "未找到'" + strDbName + "'库"; return(-1); } string strTempRecordPath = db.GetCaption("zh-cn") + "/" + "record"; string strExistRights = ""; bool bHasRight = this.m_oUser.HasRights(strTempRecordPath, ResType.Record, "read", out strExistRights); if (bHasRight == false) { strError = "您的帐户名为'" + m_oUser.Name + "'" + ",对'" + strDbName + "'" + "数据库中的记录没有'读(read)'权限,目前的权限为'" + strExistRights + "'。"; return(-6); } SearchItem searchItem = new SearchItem(); searchItem.TargetTables = strTableList; searchItem.Word = strWord; searchItem.Match = strMatch; searchItem.Relation = strRelation; searchItem.DataType = strDataType; searchItem.IdOrder = strIdOrder; searchItem.KeyOrder = strKeyOrder; searchItem.OrderBy = strOrderBy; searchItem.MaxCount = nMaxCount; // 注: SearchByUnion不清空resultSet,从而使多个库的检索结结果放在一起 string strWarningInfo = ""; nRet = db.SearchByUnion(searchItem, isConnected, resultSet, this.m_nWarningLevel, out strError, out strWarningInfo); if (nRet == -1) { return(-1); } // 多个库的排序去重功能暂不做 //resultSet.Sort(); //按具体的风格排序 //resultSet.RemoveDup(); } return(0); }
// 根据nodeItem得到检索的相关信息 // parameter: // nodeItem 配置节点 // strTarget out参数,返回检索目标,会是多库的多途径 // strWord out参数,返回检索词 // strMatch out参数,返回匹配方式 // strRelation out参数,返回关系符 // strDataType out参数,返回检索的数据类型 // strIdOrder out参数,返回id的排序规则 // strKeyOrder out参数,返回key的排序规则 // strOrderBy out参数,返回由order与originOrder组合的排序规则,例如" keystring ASC,idstring DESC " // nMax out参数,返回最大条数 // strError out参数,返回出错信息 // return: // -1 出错 // 0 成功 public static int GetSearchInfo(XmlNode nodeItem, string strOutputStyle, out string strTarget, out string strWord, out string strMatch, out string strRelation, out string strDataType, out string strIdOrder, out string strKeyOrder, out string strOrderBy, out int nMaxCount, out string strHint, out string strError) { strTarget = ""; strWord = ""; strMatch = ""; strRelation = ""; strDataType = ""; strIdOrder = ""; strKeyOrder = ""; strOrderBy = ""; nMaxCount = 0; strHint = ""; strError = ""; bool bOutputKeyCount = StringUtil.IsInList("keycount", strOutputStyle); bool bOutputKeyID = StringUtil.IsInList("keyid", strOutputStyle); //-------------------------------------- //调GetTarget函数,得到检索目标target节点 XmlElement nodeTarget = QueryUtil.GetTarget(nodeItem); if (nodeTarget == null) { strError = "检索式的target元素未定义"; return(-1); } strTarget = DomUtil.GetAttrDiff(nodeTarget, "list"); if (strTarget == null) { strError = "target元素的list属性未定义"; } strTarget = strTarget.Trim(); if (strTarget == "") { strError = "target元素的list属性值不能为空字符串"; return(-1); } // 2015/11/8 strHint = nodeTarget.GetAttribute("hint"); //------------------------------------------- //检索文本 可以为空字符串 XmlNode nodeWord = nodeItem.SelectSingleNode("word"); if (nodeWord == null) { strError = "检索式的word元素未定义"; return(-1); } strWord = nodeWord.InnerText.Trim(); // // 2012/2/16 strWord = strWord.Trim(); //------------------------------------ //匹配方式 XmlNode nodeMatch = nodeItem.SelectSingleNode("match"); if (nodeMatch == null) { strError = "检索式的match元素未定义"; return(-1); } strMatch = nodeMatch.InnerText.Trim(); // 2012/2/16 if (string.IsNullOrEmpty(strMatch) == true) { strError = "检索式的match元素内容不能为空字符串"; return(-1); } if (QueryUtil.CheckMatch(strMatch) == false) { strError = "检索式的match元素内容'" + strMatch + "'不合法,必须为left,middle,right,exact之一"; return(-1); } //-------------------------------------------- //关系操作符 XmlNode nodeRelation = nodeItem.SelectSingleNode("relation"); if (nodeRelation == null) { strError = "检索式的relation元素未定义"; return(-1); } strRelation = nodeRelation.InnerText.Trim(); // 2012/2/16 if (string.IsNullOrEmpty(strRelation) == true) { strError = "检索式的relation元素内容不能为空字符串"; return(-1); } strRelation = QueryUtil.ConvertLetterToOperator(strRelation); if (QueryUtil.CheckRelation(strRelation) == false) { strError = "检索式的relation元素内容 '" + strRelation + "' 不合法."; return(-1); } //------------------------------------------- //数据类型 XmlNode nodeDataType = nodeItem.SelectSingleNode("dataType"); if (nodeDataType == null) { strError = "检索式的dataType元素未定义"; return(-1); } strDataType = nodeDataType.InnerText.Trim(); // 2012/2/16 if (string.IsNullOrEmpty(strDataType) == true) { strError = "检索式的dataType元素内容不能为空字符串"; return(-1); } if (QueryUtil.CheckDataType(strDataType) == false) { strError = "检索式的dataType元素内容'" + strDataType + "'不合法,必须为string,number"; return(-1); } // ----------order可以不存在---------- int nOrderIndex = -1; string strOrder = null; int nOriginOrderIndex = -1; string strOriginOrder = null; //id的序 //ASC:升序 //DESC:降序 XmlNode nodeOrder = nodeItem.SelectSingleNode("order"); // 当定义了order元素时,才会id进行排序 if (nodeOrder != null) { string strOrderText = nodeOrder.InnerText; // 2012/2/16 strOrderText = strOrderText.Trim().ToUpper(); if (strOrderText != "ASC" && strOrderText != "DESC") { strError = "<order>元素值应为 ASC DESC 之一"; return(-1); } if (String.IsNullOrEmpty(strOrderText) == false) { // 2010/5/10 if (bOutputKeyCount == true) { strOrder = "keystring " + strOrderText; nOrderIndex = DomUtil.GetIndex(nodeOrder); strIdOrder = strOrderText; } else if (bOutputKeyID == true) { strOrder = "keystring " + strOrderText; nOrderIndex = DomUtil.GetIndex(nodeOrder); strIdOrder = strOrderText; } else { strOrder = "idstring " + strOrderText; nOrderIndex = DomUtil.GetIndex(nodeOrder); strIdOrder = strOrderText; } } } //key的序 //ASC:升序 //DESC:降序 XmlNode nodeOriginOrder = nodeItem.SelectSingleNode("originOrder"); // 当定义了order元素时,才会id进行排序 if (nodeOriginOrder != null) { string strOriginOrderText = nodeOriginOrder.InnerText; // 2012/2/16 strOriginOrderText = strOriginOrderText.Trim().ToUpper(); if (strOriginOrderText != "ASC" && strOriginOrderText != "DESC") { strError = "<originOrder>元素值应为 ASC DESC 之一"; return(-1); } if (string.IsNullOrEmpty(strOriginOrderText) == false) { strOriginOrder = "keystring " + strOriginOrderText; nOriginOrderIndex = DomUtil.GetIndex(nodeOriginOrder); strKeyOrder = strOriginOrderText; } } if (strOrder != null && strOriginOrder != null) { if (nOrderIndex == -1 || nOriginOrderIndex == -1) { strError = "此时nOrderIndex和nOriginOrderIndex都不可能为-1"; return(-1); } if (nOrderIndex == nOriginOrderIndex) { strError = "nOrderIndex 与 nOriginOrderIndex不可能相等"; return(-1); } if (nOrderIndex > nOriginOrderIndex) { strOrderBy = strOrder + "," + strOriginOrder; } else { strOrderBy = strOriginOrder + "," + strOrder; } } else { if (strOrder != null) { strOrderBy = strOrder; } if (strOriginOrder != null) { strOrderBy = strOriginOrder; } } //------------------------------------------- //最大个数 XmlNode nodeMaxCount = nodeItem.SelectSingleNode("maxCount"); /* * if (nodeMaxCount == null) * { * strError = "检索式的maxCount元素未定义"; * return -1; * } */ string strMaxCount = ""; if (nodeMaxCount != null) { strMaxCount = nodeMaxCount.InnerText.Trim(); // 2012/2/16 } if (string.IsNullOrEmpty(strMaxCount) == true) { strMaxCount = "-1"; } /* * if (strMaxCount == "") * { * strError = "检索式的maxCount元素的值为空字符串"; * return -1; * } */ try { nMaxCount = Convert.ToInt32(strMaxCount); if (nMaxCount < -1) { strError = "xml检索式的maxCount的值'" + strMaxCount + "'不合法,必须是数值型"; return(-1); } } catch { strError = "xml检索式的maxCount的值'" + strMaxCount + "'不合法,必须是数值型"; return(-1); } return(0); }
// 功能: 检查检索单元match,relation,dataType三项的关系 // 如果存在矛盾: // 如果处理警告的级别为0,会给检索单元node加warning信息,不自动更正,返回-1, // 如果级别为非0(应该固定为1),则系统自动更加,并在更正的地方加comment信息,返回0 // 不存在矛盾:返回0 // parameter: // nodeItem 检索单元节点 // return: // 0 正常(如果有矛盾,但由于处理警告级别为非0,自动更正,也返回0) // -1 存在矛盾,且警告级别为0 public int ProcessRelation(XmlNode nodeItem) { //匹配方式 XmlNode nodeMatch = nodeItem.SelectSingleNode("match"); string strMatch = ""; if (nodeMatch != null) { strMatch = DomUtil.GetNodeText(nodeMatch).Trim(); } if (strMatch == "") { DomUtil.SetNodeText(nodeMatch, "left"); DomUtil.SetAttr(nodeMatch, "comment", "原为" + strMatch + ",修改为缺省的left"); } //关系操作符 XmlNode nodeRelation = nodeItem.SelectSingleNode("relation"); string strRelation = ""; if (nodeRelation != null) { strRelation = DomUtil.GetNodeText(nodeRelation).Trim(); } strRelation = QueryUtil.ConvertLetterToOperator(strRelation); //数据类型 XmlNode nodeDataType = nodeItem.SelectSingleNode("dataType"); string strDataType = ""; if (nodeDataType != null) { strDataType = DomUtil.GetNodeText(nodeDataType).Trim(); } if (strDataType == "number") { if (strMatch == "left" || strMatch == "right") { //当dataType值为number时,match值为left或right或 //修改可以自动有两种: //1.将left换成exact; //2.将dataType设为string, //我们先按dataType优先,将match改为exact if (m_nWarningLevel == 0) { DomUtil.SetAttr(nodeItem, "warningInfo", "匹配方式为值‘" + strMatch + "'与数据类型的值'" + strDataType + "'矛盾,且处理警告级别为0,自动不进行自动更正"); return(-1); } else { DomUtil.SetNodeText(nodeMatch, "exact"); DomUtil.SetAttr(nodeMatch, "comment", "原为" + strMatch + ",由于与数据类型'" + strDataType + "'矛盾,修改为exact"); } } } if (strDataType == "string") { //如果dataType值为string, //match值为left或right,且relation值不等于"=" //出现矛盾,(我们认为match的left或rgith值,只与relation的"="值匹配) //有两种裁决办法: //1.将relation值改为"="号; //2.将match值由left或right改为exact //目前按1进行修改 if ((strMatch == "left" || strMatch == "right") && strRelation != "=") { //根据处理警告级别做不同的处理 if (m_nWarningLevel == 0) { DomUtil.SetAttr(nodeItem, "warningInfo", "关系操作符'" + strRelation + "'与数据类型" + strDataType + "和匹配方式'" + strMatch + "'不匹配"); return(-1); } else { DomUtil.SetNodeText(nodeRelation, "="); DomUtil.SetAttr(nodeRelation, "comment", "原为" + strRelation + ",由于与数据类型'" + strDataType + "和匹配方式'" + strMatch + "'不匹配,修改为'='"); } } } return(0); }