예제 #1
0
        /// <summary>
        /// 转换表达式
        /// </summary>
        /// <param name="expression">表达式</param>
        private void convertCall(MethodCallExpression expression)
        {
            MethodInfo method = expression.Method;

            if (method.ReflectedType == typeof(ExpressionCall))
            {
                switch (method.Name)
                {
                case "In": convertCall(expression, true); break;

                case "NotIn": convertCall(expression, false); break;

                case "Like":
                    System.Collections.ObjectModel.ReadOnlyCollection <System.Linq.Expressions.Expression> arguments = expression.Arguments;
                    convertIsSimple(arguments[0]);
                    SqlStream.SimpleWriteNotNull(" like ");
                    convertIsSimple(expression.Object);
                    ConstantConverter.ConvertLike(SqlStream, arguments[1].GetConstant(), true, true);
                    break;

                default:
                    SqlStream.SimpleWriteNotNull(method.Name);
                    SqlStream.Write('(');
                    if (expression.Arguments != null)
                    {
                        bool isNext = false;
                        foreach (Expression argumentExpression in expression.Arguments)
                        {
                            if (isNext)
                            {
                                SqlStream.Write(',');
                            }
                            Convert(argumentExpression);
                            isNext = true;
                        }
                    }
                    SqlStream.Write(')');
                    break;
                }
            }
            else if (IsStringContains(method))
            {
                convertIsSimple(expression.Object);
                SqlStream.SimpleWriteNotNull(" like ");
                ConstantConverter.ConvertLike(SqlStream, expression.Arguments[0].GetConstant(), true, true);
            }
            else
            {
                convertConstant(expression.GetConstant());
            }
        }
예제 #2
0
 /// <summary>
 /// 转换 LIKE 表达式
 /// </summary>
 /// <param name="expression">表达式</param>
 /// <param name="isNot"></param>
 /// <param name="isStart"></param>
 /// <param name="isEnd"></param>
 private void convertCallLike(MethodCallExpression expression, bool isNot, bool isStart, bool isEnd)
 {
     System.Collections.ObjectModel.ReadOnlyCollection <System.Linq.Expressions.Expression> arguments = expression.Arguments;
     whereExpression.Convert(arguments[1]);
     if (whereExpression.Type != WhereExpression.ConvertType.Unknown)
     {
         if (whereExpression.Expression.NodeType == ExpressionType.Constant)
         {
             Expression like = whereExpression.Expression;
             convertIsSimple(arguments[0]);
             SqlStream.SimpleWrite(isNot ? " not like " : " like ");
             ConstantConverter.ConvertLike(SqlStream, like.GetConstantValue(), isStart, isEnd);
         }
         else
         {
             throw new InvalidCastException("未知 Like 函数表达式值类型 " + whereExpression.Expression.NodeType.ToString());
         }
     }
     else
     {
         throw new InvalidCastException("未知 Like 函数表达式值 " + whereExpression.UnknownType.ToString());
     }
 }
예제 #3
0
        /// <summary>
        /// 转换表达式
        /// </summary>
        /// <param name="expression">表达式</param>
        private void convertCall(MethodCallExpression expression)
        {
            MethodInfo method = expression.Method;

            if (method.ReflectedType == typeof(ExpressionCall))
            {
                switch (method.Name)
                {
                case "In": convertCallIn(expression, true); break;

                case "NotIn": convertCallIn(expression, false); break;

                case "Like":
                {
                    System.Collections.ObjectModel.ReadOnlyCollection <System.Linq.Expressions.Expression> arguments = expression.Arguments;
                    whereExpression.Convert(arguments[1]);
                    if (whereExpression.Type != WhereExpression.ConvertType.Unknown)
                    {
                        if (whereExpression.Expression.NodeType == ExpressionType.Constant)
                        {
                            Expression like = whereExpression.Expression;
                            convertIsSimple(arguments[0]);
                            SqlStream.SimpleWriteNotNull(" like ");
                            ConstantConverter.ConvertLike(SqlStream, like.GetConstantValue(), true, true);
                        }
                        else
                        {
                            throw new InvalidCastException("未知 Like 函数表达式值类型 " + whereExpression.Expression.NodeType.ToString());
                        }
                    }
                    else
                    {
                        throw new InvalidCastException("未知 Like 函数表达式值 " + whereExpression.UnknownType.ToString());
                    }
                }
                break;

                case "Count":
                    SqlStream.SimpleWriteNotNull("count(");
                    convert(expression.Arguments[0]);
                    SqlStream.Write(')');
                    break;

                case "Sum":
                    SqlStream.SimpleWriteNotNull("sum(");
                    convert(expression.Arguments[0]);
                    SqlStream.Write(')');
                    break;

                case "GetDate": SqlStream.SimpleWriteNotNull("getdate()"); break;

                case "SysDateTime": SqlStream.SimpleWriteNotNull("sysdatetime()"); break;

                case "IsNull":
                    SqlStream.SimpleWriteNotNull("isnull(");
                    convert(expression.Arguments[0]);
                    SqlStream.Write(',');
                    convert(expression.Arguments[1]);
                    SqlStream.Write(')');
                    break;

                case "Call":
                    int parameterIndex = 0;
                    foreach (Expression argumentExpression in expression.Arguments)
                    {
                        switch (parameterIndex)
                        {
                        case 0:
                            whereExpression.Convert(argumentExpression);
                            if (whereExpression.Type == WhereExpression.ConvertType.Unknown || whereExpression.Expression.NodeType != ExpressionType.Constant)
                            {
                                throw new InvalidCastException("未知 SQL 函数名称 " + whereExpression.Expression.NodeType.ToString());
                            }
                            string functionName = (string)whereExpression.Expression.GetConstantValue();
                            if (string.IsNullOrEmpty(functionName))
                            {
                                throw new InvalidCastException("SQL 函数名称不能为空 " + whereExpression.Expression.NodeType.ToString());
                            }
                            SqlStream.SimpleWriteNotNull(functionName);
                            SqlStream.Write('(');
                            break;

                        case 1: convert(argumentExpression); break;

                        default:
                            SqlStream.Write(',');
                            convert(argumentExpression);
                            break;
                        }
                        ++parameterIndex;
                    }
                    SqlStream.Write(')');
                    break;
                }
            }
            //else if (IsStringContains(method))
            //{
            //    convertIsSimple(expression.Object);
            //    SqlStream.SimpleWriteNotNull(" like ");
            //    ConstantConverter.ConvertLike(SqlStream, expression.Arguments[0].GetConstant(), true, true);
            //}
            else
            {
                whereExpression.ConvertCall(expression, method);
                if (whereExpression.Type != WhereExpression.ConvertType.Unknown)
                {
                    convertConstant(whereExpression.Expression.GetConstantValue());
                }
                else
                {
                    throwWhereExpressionError();
                }
            }
        }