/// <summary>
        ///     Contains方法解析
        /// </summary>
        /// <param name="fieldType"></param>
        /// <param name="fieldName"></param>
        /// <param name="paramType"></param>
        /// <param name="paramName"></param>
        protected virtual void VisitMethodContains(MethodCallExpression m, Type fieldType, string fieldName, Type paramType, string paramName)
        {
            // 非List<>形式
            if (paramType != null && !IsGenericOrArray(paramType))
            {
                #region 搜索值串的处理
                var param = ParamList.Find(o => o.ParameterName == paramName);
                if (param != null && Regex.IsMatch(param.Value.ToString(), @"[\d]+") && (Type.GetTypeCode(fieldType) == TypeCode.Int16 || Type.GetTypeCode(fieldType) == TypeCode.Int32 || Type.GetTypeCode(fieldType) == TypeCode.Decimal || Type.GetTypeCode(fieldType) == TypeCode.Double || Type.GetTypeCode(fieldType) == TypeCode.Int64 || Type.GetTypeCode(fieldType) == TypeCode.UInt16 || Type.GetTypeCode(fieldType) == TypeCode.UInt32 || Type.GetTypeCode(fieldType) == TypeCode.UInt64))
                {
                    param.Value  = "," + param.Value + ",";
                    param.DbType = DbType.String;
                    if (DbProvider.KeywordAegis("").Length > 0)
                    {
                        fieldName = "','+" + fieldName.Substring(1, fieldName.Length - 2) + "+','";
                    }
                    else
                    {
                        fieldName = "','+" + fieldName + "+','";
                    }
                }
                #endregion

                // 判断是不是字段调用Contains
                var isFieldCall = m.Object != null && m.Object.NodeType == ExpressionType.MemberAccess && ((MemberExpression)m.Object).Expression != null && ((MemberExpression)m.Object).Expression.NodeType == ExpressionType.Parameter;
                SqlList.Push(isFieldCall ? FunctionProvider.CharIndex(fieldName, paramName, IsNot) : FunctionProvider.CharIndex(paramName, fieldName, IsNot));
            }
            else
            {
                // 删除参数化,后面需要改成多参数
                var paramValue = CurrentDbParameter.Value.ToString();
                ParamList.RemoveAt(ParamList.Count - 1);

                // 参数类型,转换成多参数
                var lstParamName = new List <string>();
                var index        = 0;
                foreach (var val in paramValue.Split(','))
                {
                    var param = DbProvider.CreateDbParam(CurrentDbParameter.ParameterName.Substring(1) + "_" + index++, val, fieldType);
                    lstParamName.Add(param.ParameterName);
                    ParamList.Add(param);
                }

                SqlList.Push(FunctionProvider.In(fieldName, lstParamName, IsNot));
            }
        }
        protected override void VisitMethodContains(MethodCallExpression m, Type fieldType, string fieldName, Type paramType, string paramName)
        {
            // 非List<>形式
            if (paramType != null && (!paramType.IsGenericType || paramType.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                #region 搜索值串的处理
                var param = ParamList.Find(o => o.ParameterName == paramName);
                if (param != null && Regex.IsMatch(param.Value.ToString(), @"[\d]+") && (Type.GetTypeCode(fieldType) == TypeCode.Int16 || Type.GetTypeCode(fieldType) == TypeCode.Int32 || Type.GetTypeCode(fieldType) == TypeCode.Decimal || Type.GetTypeCode(fieldType) == TypeCode.Double || Type.GetTypeCode(fieldType) == TypeCode.Int64 || Type.GetTypeCode(fieldType) == TypeCode.UInt16 || Type.GetTypeCode(fieldType) == TypeCode.UInt32 || Type.GetTypeCode(fieldType) == TypeCode.UInt64))
                {
                    param.Value  = "," + param.Value + ",";
                    param.DbType = DbType.String;
                    if (DbProvider.KeywordAegis("").Length > 0)
                    {
                        fieldName = "','+" + fieldName.Substring(1, fieldName.Length - 2) + "+','";
                    }
                    else
                    {
                        fieldName = "','+" + fieldName + "+','";
                    }
                }
                #endregion

                // 判断是不是字段调用Contains
                var isFieldCall = m.Object != null && m.Object.NodeType == ExpressionType.MemberAccess && ((MemberExpression)m.Object).Expression != null && ((MemberExpression)m.Object).Expression.NodeType == ExpressionType.Parameter;
                SqlList.Push(isFieldCall ? $"INSTR({fieldName},{paramName}) {(IsNot ? "<=" : ">")} 0" : $"INSTR({paramName},{fieldName}) {(IsNot ? "<=" : ">")} 0");
            }
            else
            {
                // 不使用参数化形式,同时移除参数
                var paramValue = CurrentDbParameter.Value;
                ParamList.RemoveAt(ParamList.Count - 1);

                // 字段是字符类型的,需要加入''符号
                if (Type.GetTypeCode(fieldType) == TypeCode.String)
                {
                    paramValue = "'" + paramValue.ToString().Replace(",", "','") + "'";
                }

                SqlList.Push($"{fieldName} {(IsNot ? "Not" : "")} IN ({paramValue})");
            }
        }
示例#3
0
        /// <summary>
        /// Contains方法解析
        /// </summary>
        /// <param name="fieldType"></param>
        /// <param name="fieldName"></param>
        /// <param name="paramType"></param>
        /// <param name="paramName"></param>
        private void VisitMethodContains(Type fieldType, string fieldName, Type paramType, string paramName)
        {
            // 非List<>形式
            if (paramType != null && (!paramType.IsGenericType || paramType.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                #region 搜索值串的处理
                var param = ParamList.Find(o => o.ParameterName == paramName);
                if (param != null && Regex.IsMatch(param.Value.ToString(), @"[\d]+") && (Type.GetTypeCode(fieldType) == TypeCode.Int16 || Type.GetTypeCode(fieldType) == TypeCode.Int32 || Type.GetTypeCode(fieldType) == TypeCode.Decimal || Type.GetTypeCode(fieldType) == TypeCode.Double || Type.GetTypeCode(fieldType) == TypeCode.Int64 || Type.GetTypeCode(fieldType) == TypeCode.UInt16 || Type.GetTypeCode(fieldType) == TypeCode.UInt32 || Type.GetTypeCode(fieldType) == TypeCode.UInt64))
                {
                    param.Value  = "," + param.Value + ",";
                    param.DbType = DbType.String;
                    if (DbProvider.KeywordAegis("").Length > 0)
                    {
                        fieldName = "','+" + fieldName.Substring(1, fieldName.Length - 2) + "+','";
                    }
                    else
                    {
                        fieldName = "','+" + fieldName + "+','";
                    }
                }
                #endregion

                SqlList.Push(String.Format("CHARINDEX({0},{1}) {2} 0", paramName, fieldName, IsNot ? "<=" : ">"));
            }
            else
            {
                // 不使用参数化形式,同时移除参数
                var paramValue = CurrentDbParameter.Value;
                ParamList.RemoveAt(ParamList.Count - 1);

                // 字段是字符类型的,需要加入''符号
                if (Type.GetTypeCode(fieldType) == TypeCode.String)
                {
                    paramValue = "'" + paramValue.ToString().Replace(",", "','") + "'";
                }

                SqlList.Push(String.Format("{0} {1} IN ({2})", fieldName, IsNot ? "Not" : "", paramValue));
            }
        }