/// <summary> /// 获得查询 /// </summary> /// <param name="item"></param> /// <returns></returns> private static IMongoQuery GetQuery(DataFilter.QueryConditionInputItem item) { IMongoQuery query; var queryvalue = item.Value.GetBsonValue(); switch (item.Compare) { case DataFilter.CompareEnum.Eq: query = Query.EQ(item.ColName, queryvalue); break; case DataFilter.CompareEnum.Gt: query = Query.GT(item.ColName, queryvalue); break; case DataFilter.CompareEnum.Gte: query = Query.GTE(item.ColName, queryvalue); break; case DataFilter.CompareEnum.Lt: query = Query.LT(item.ColName, queryvalue); break; case DataFilter.CompareEnum.Lte: query = Query.LTE(item.ColName, queryvalue); break; case DataFilter.CompareEnum.Ne: query = Query.NE(item.ColName, queryvalue); break; default: query = Query.EQ(item.ColName, queryvalue); break; } return(query); }
/// <summary> /// 通过Sql文的Where条件和列名称来获取Query条件 /// </summary> /// <param name="sqlContent">Where条件</param> /// <param name="columnNameLst">列名称</param> /// <returns></returns> private static List <DataFilter.QueryConditionInputItem> SetQueryCondition(string sqlContent, List <string> columnNameLst) { var conditionlst = new List <DataFilter.QueryConditionInputItem>(); // (a=1 or b="A") AND c="3" => ( a = 1 or b = "A" ) and c = "3" //1. 除了引号里面的文字,全部小写 string[] keyWord = { "(", ")", "=", "or", "and", ">", ">=", "<", "<=", "<>" }; foreach (var keyitem in keyWord) { sqlContent = sqlContent.Replace(keyitem, " " + keyitem + " "); } while (sqlContent.Contains(" ")) { sqlContent = sqlContent.Replace(" ", " "); } sqlContent = sqlContent.Trim(); //从左到右 ( a = 1 or // b = "A" ) and // c = "3" var token = sqlContent.Split(" ".ToCharArray()); var mQueryConditionInputItem = new DataFilter.QueryConditionInputItem { StartMark = string.Empty, EndMark = string.Empty }; for (var i = 0; i < token.Length; i++) { var strToken = token[i].Replace(" ", " "); switch (strToken) { case "(": mQueryConditionInputItem.StartMark = "("; break; case "=": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Eq; break; case ">": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Gt; break; case "<": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Lt; break; case ">=": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Gte; break; case "<=": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Lte; break; case "<>": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Ne; break; case "or": mQueryConditionInputItem.EndMark = ConstMgr.EndMarkOr; conditionlst.Add(mQueryConditionInputItem); mQueryConditionInputItem = new DataFilter.QueryConditionInputItem { StartMark = string.Empty, EndMark = string.Empty }; break; case "and": mQueryConditionInputItem.EndMark = ConstMgr.EndMarkAnd; conditionlst.Add(mQueryConditionInputItem); mQueryConditionInputItem = new DataFilter.QueryConditionInputItem { StartMark = string.Empty, EndMark = string.Empty }; break; case ")": mQueryConditionInputItem.EndMark = ")"; if (i == token.Length - 1) { mQueryConditionInputItem.EndMark = ConstMgr.EndMarkT; } else { if (token[i + 1] == "or") { mQueryConditionInputItem.EndMark = ConstMgr.EndMarkOrT; i++; } else { if (token[i + 1] == "and") { mQueryConditionInputItem.EndMark = ConstMgr.EndMarkAndT; i++; } } } conditionlst.Add(mQueryConditionInputItem); mQueryConditionInputItem = new DataFilter.QueryConditionInputItem { StartMark = string.Empty, EndMark = string.Empty }; break; default: if (mQueryConditionInputItem.ColName == null) { foreach (var colName in columnNameLst) { if (colName.ToLower() == strToken.ToLower()) { //小写的复原 mQueryConditionInputItem.ColName = colName; break; } } } else { //类型设置 if (strToken.StartsWith("\"") & strToken.EndsWith("\"")) { mQueryConditionInputItem.Value = new BsonValueEx(new BsonString(strToken.Replace("\"", ""))); } else { mQueryConditionInputItem.Value = new BsonValueEx(new BsonInt32(Convert.ToInt16(strToken))); } } break; } } if (token[token.Length - 1] != ")") { conditionlst.Add(mQueryConditionInputItem); } return(conditionlst); }
/// <summary> /// 通过Sql文的Where条件和列名称来获取Query条件 /// </summary> /// <param name="sqlContent">Where条件</param> /// <param name="columnNameLst">列名称</param> /// <returns></returns> private static List<DataFilter.QueryConditionInputItem> SetQueryCondition(string sqlContent, List<string> columnNameLst) { var conditionlst = new List<DataFilter.QueryConditionInputItem>(); // (a=1 or b="A") AND c="3" => ( a = 1 or b = "A" ) and c = "3" //1. 除了引号里面的文字,全部小写 string[] keyWord = {"(", ")", "=", "or", "and", ">", ">=", "<", "<=", "<>"}; foreach (var keyitem in keyWord) { sqlContent = sqlContent.Replace(keyitem, " " + keyitem + " "); } while (sqlContent.Contains(" ")) { sqlContent = sqlContent.Replace(" ", " "); } sqlContent = sqlContent.Trim(); //从左到右 ( a = 1 or // b = "A" ) and // c = "3" var token = sqlContent.Split(" ".ToCharArray()); var mQueryConditionInputItem = new DataFilter.QueryConditionInputItem { StartMark = string.Empty, EndMark = string.Empty }; for (var i = 0; i < token.Length; i++) { var strToken = token[i].Replace(" ", " "); switch (strToken) { case "(": mQueryConditionInputItem.StartMark = "("; break; case "=": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Eq; break; case ">": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Gt; break; case "<": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Lt; break; case ">=": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Gte; break; case "<=": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Lte; break; case "<>": mQueryConditionInputItem.Compare = DataFilter.CompareEnum.Ne; break; case "or": mQueryConditionInputItem.EndMark = ConstMgr.EndMarkOr; conditionlst.Add(mQueryConditionInputItem); mQueryConditionInputItem = new DataFilter.QueryConditionInputItem { StartMark = string.Empty, EndMark = string.Empty }; break; case "and": mQueryConditionInputItem.EndMark = ConstMgr.EndMarkAnd; conditionlst.Add(mQueryConditionInputItem); mQueryConditionInputItem = new DataFilter.QueryConditionInputItem { StartMark = string.Empty, EndMark = string.Empty }; break; case ")": mQueryConditionInputItem.EndMark = ")"; if (i == token.Length - 1) { mQueryConditionInputItem.EndMark = ConstMgr.EndMarkT; } else { if (token[i + 1] == "or") { mQueryConditionInputItem.EndMark = ConstMgr.EndMarkOrT; i++; } else { if (token[i + 1] == "and") { mQueryConditionInputItem.EndMark = ConstMgr.EndMarkAndT; i++; } } } conditionlst.Add(mQueryConditionInputItem); mQueryConditionInputItem = new DataFilter.QueryConditionInputItem { StartMark = string.Empty, EndMark = string.Empty }; break; default: if (mQueryConditionInputItem.ColName == null) { foreach (var colName in columnNameLst) { if (colName.ToLower() == strToken.ToLower()) { //小写的复原 mQueryConditionInputItem.ColName = colName; break; } } } else { //类型设置 if (strToken.StartsWith("\"") & strToken.EndsWith("\"")) { mQueryConditionInputItem.Value = new BsonValueEx(new BsonString(strToken.Replace("\"", ""))); } else { mQueryConditionInputItem.Value = new BsonValueEx(new BsonInt32(Convert.ToInt16(strToken))); } } break; } } if (token[token.Length - 1] != ")") { conditionlst.Add(mQueryConditionInputItem); } return conditionlst; }