/// <summary> /// 转换单个查询条件为Sql文本 /// </summary> /// <param name="conditon"></param> /// <param name="parameterDictionary"></param> /// <returns></returns> private static string ToSubSqlText(QueryCondition conditon, Dictionary <string, int> parameterDictionary, string escapeSymbol) { if (conditon != null && ConditionToSql.CheckConditionIsValid(conditon)) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(" "); stringBuilder.Append(ConditionToSql.GetLinkType(conditon.LinkType)); stringBuilder.Append(" "); // 如果有子级查询条件,则遍历构造子级查询条件 if (conditon.SubQuery != null && conditon.SubQuery.Length > 0) { string subSqlText = string.Empty; QueryCondition[] subQuery = conditon.SubQuery; for (int i = 0; i < subQuery.Length; i++) { QueryCondition subConditon = subQuery[i]; subSqlText += ConditionToSql.ToSubSqlText(subConditon, parameterDictionary, escapeSymbol); } if (!string.IsNullOrEmpty(subSqlText)) { stringBuilder.Append("("); stringBuilder.Append(ConditionToSql.FixSQLText(subSqlText)); stringBuilder.Append(")"); } } else { stringBuilder.Append(escapeSymbol[0]); stringBuilder.Append(conditon.Property.ToString()); stringBuilder.Append(escapeSymbol[1]); stringBuilder.Append(" "); stringBuilder.Append(ConditionToSql.GetCompareType(conditon)); stringBuilder.Append(" "); if (conditon.CompareType == CompareType.In || conditon.CompareType == CompareType.NotIn) { stringBuilder.Append("("); } stringBuilder.Append(ConditionToSql.GetParameterName(conditon, parameterDictionary)); if (conditon.CompareType == CompareType.In || conditon.CompareType == CompareType.NotIn) { stringBuilder.Append(")"); } } return(stringBuilder.ToString()); } return(string.Empty); }
/// <summary> /// 获取单个条件对应的参数值对象 /// </summary> /// <param name="conditon"></param> /// <param name="parameterList"></param> /// <param name="parameterDictionary"></param> /// <returns></returns> private static string GetParameterObject(QueryCondition conditon, List <DbParameter> parameterList, Dictionary <string, int> parameterDictionary, DbType dbType) { string parameterName = string.Empty; string conditionName = conditon.ConditionName.ToString(); bool isArray = false; if (conditon.Value != null) { isArray = conditon.Value.GetType().IsArray; } if (isArray) { var conditonValueEnum = (System.Collections.IEnumerable)conditon.Value; foreach (var v in conditonValueEnum) { var tmpParameterName = "@" + conditionName; tmpParameterName = GetUniqueParameterName(tmpParameterName, parameterDictionary); parameterList.Add(ConditionToSql.CreateParameterObject(tmpParameterName, v, dbType)); } } else { if (conditon.CompareType == CompareType.GET || conditon.CompareType == CompareType.GT) { parameterName = "@min" + conditionName; } else { if (conditon.CompareType == CompareType.LT || conditon.CompareType == CompareType.LET) { parameterName = "@max" + conditionName; } else { parameterName = "@" + conditionName; } } parameterName = GetUniqueParameterName(parameterName, parameterDictionary); parameterList.Add(ConditionToSql.CreateParameterObject(parameterName, conditon.Value, dbType)); } return(parameterName); }
/// <summary> /// 转换查询条件数组为基类Db参数 /// </summary> /// <param name="conditionArray"></param> /// <param name="dbType"></param> /// <returns></returns> private static DbParameter[] ToDbSqlParas(QueryCondition[] conditionArray, DbType dbType) { List <DbParameter> parameterList = new List <DbParameter>(); if (conditionArray != null && conditionArray.Length > 0) { Dictionary <string, int> parameterDictionary = new Dictionary <string, int>(); for (int i = 0; i < conditionArray.Length; i++) { QueryCondition conditon = conditionArray[i]; ConditionToSql.ToSubSqlParas(conditon, parameterList, parameterDictionary, dbType); } parameterDictionary.Clear(); } return(parameterList.ToArray()); }
/// <summary> /// 转换查询条件数组为Sql文本 /// </summary> /// <param name="conditionArray"></param> /// <returns></returns> public static string ToDbSqlText(QueryCondition[] conditionArray, string escapeSymbol) { StringBuilder stringBuilder = new StringBuilder(); if (conditionArray != null && conditionArray.Length > 0) { Dictionary <string, int> parameterDictionary = new Dictionary <string, int>(); for (int i = 0; i < conditionArray.Length; i++) { QueryCondition conditon = conditionArray[i]; stringBuilder.Append(ConditionToSql.ToSubSqlText(conditon, parameterDictionary, escapeSymbol)); } parameterDictionary.Clear(); return(ConditionToSql.FixSQLText(stringBuilder.ToString())); } return(string.Empty); }
/// <summary> /// 转换单个查询条件为Sql参数 /// </summary> /// <param name="conditon"></param> /// <param name="parameterList"></param> /// <param name="parameterDictionary"></param> private static void ToSubSqlParas(QueryCondition conditon, List <DbParameter> parameterList, Dictionary <string, int> parameterDictionary, DbType dbType) { if (conditon != null && ConditionToSql.CheckConditionIsValid(conditon)) { if (conditon.SubQuery != null && conditon.SubQuery.Length > 0) { QueryCondition[] subQuery = conditon.SubQuery; for (int i = 0; i < subQuery.Length; i++) { QueryCondition subConditon = subQuery[i]; ConditionToSql.ToSubSqlParas(subConditon, parameterList, parameterDictionary, dbType); } } if (!string.IsNullOrEmpty(conditon.Property.ToString())) { ConditionToSql.GetParameterObject(conditon, parameterList, parameterDictionary, dbType); } } }