Esempio n. 1
0
        protected override Expression VisitMember(MemberExpression node)
        {
            PropertyInfo propertyInfo = node.Member as PropertyInfo;

            if (propertyInfo != null)
            {
                // get property with JqlFieldAttribute
                var attributes          = propertyInfo.GetCustomAttributes(typeof(JqlFieldAttribute), true);
                JqlFieldAttribute field = attributes[0] as JqlFieldAttribute;
                this.jql.Append(field.Name);
            }
            else
            {
                LambdaExpression lambda     = Expression.Lambda(node);
                Delegate         fn         = lambda.Compile();
                Expression       expression = Expression.Constant(fn.DynamicInvoke(null));
                Visit(expression);
            }

            return(node);
        }
Esempio n. 2
0
        protected override Expression VisitBinary(BinaryExpression node)
        {
            ConstantExpression rightExpression = null;
            JqlFieldAttribute  field           = null;

            switch (node.NodeType)
            {
            case ExpressionType.And:
            case ExpressionType.AndAlso:
                this.jql.Append("(");
                this.Visit(node.Left);
                this.jql.Append(" AND ");
                this.Visit(node.Right);
                this.jql.Append(")");
                break;

            case ExpressionType.Or:
            case ExpressionType.OrElse:
                this.jql.Append("(");
                this.Visit(node.Left);
                this.jql.Append(" OR");
                this.Visit(node.Right);
                this.jql.Append(")");
                break;

            case ExpressionType.Equal:
                field = GetFieldInfo(node.Left);
                this.jql.Append(field.Name);
                if (node.Right.NodeType == ExpressionType.Convert)
                {
                    UnaryExpression unaryExpression = node.Right as UnaryExpression;
                    rightExpression = unaryExpression.Operand as ConstantExpression;
                }
                else
                {
                    rightExpression = node.Right as ConstantExpression;
                }
                if (rightExpression != null && rightExpression.Value == null && rightExpression.Type == typeof(DateTime?))
                {
                    this.jql.Append(" is empty");
                }
                else if (rightExpression != null && rightExpression.Value == null)
                {
                    this.jql.Append(" is null");
                }
                else if (rightExpression != null && rightExpression.Value.GetType() == typeof(string) && ((string)rightExpression.Value) == "")
                {
                    this.jql.Append(" is empty");
                }
                else
                {
                    if ((field.Compare & JqlFieldCompare.Contains) == JqlFieldCompare.Contains)
                    {
                        this.jql.Append(" ~ ");
                    }
                    else
                    {
                        this.jql.Append(" = ");
                    }
                    this.Visit(node.Right);
                }
                break;

            case ExpressionType.NotEqual:
                field = GetFieldInfo(node.Left);
                this.jql.Append(field.Name);
                if (node.Right.NodeType == ExpressionType.Convert)
                {
                    UnaryExpression unaryExpression = node.Right as UnaryExpression;
                    rightExpression = unaryExpression.Operand as ConstantExpression;
                }
                else
                {
                    rightExpression = node.Right as ConstantExpression;
                }
                if (rightExpression != null && rightExpression.Value == null && rightExpression.Type == typeof(DateTime?))
                {
                    this.jql.Append(" is not empty");
                }
                else if (rightExpression != null && rightExpression.Value == null)
                {
                    this.jql.Append(" is not null");
                }
                else if (rightExpression != null && rightExpression.Value.GetType() == typeof(string) && ((string)rightExpression.Value) == "")
                {
                    this.jql.Append(" is not empty");
                }
                else
                {
                    if ((field.Compare & JqlFieldCompare.Contains) == JqlFieldCompare.Contains)
                    {
                        this.jql.Append(" !~ ");
                    }
                    else
                    {
                        this.jql.Append(" != ");
                    }
                    this.Visit(node.Right);
                }
                break;

            case ExpressionType.LessThan:
                this.Visit(node.Left);
                this.jql.Append(" < ");
                this.Visit(node.Right);
                break;

            case ExpressionType.LessThanOrEqual:
                this.Visit(node.Left);
                this.jql.Append(" <= ");
                this.Visit(node.Right);
                break;

            case ExpressionType.GreaterThan:
                this.Visit(node.Left);
                this.jql.Append(" > ");
                this.Visit(node.Right);
                break;

            case ExpressionType.GreaterThanOrEqual:
                this.Visit(node.Left);
                this.jql.Append(" >= ");
                this.Visit(node.Right);
                break;

            default:
                throw new NotSupportedException(string.Format("The binary operator '{0}' is not supported", node.NodeType));
            }
            return(node);
        }