Exemplo n.º 1
0
        private void VisitBinaryCriterionExpression(BinaryExpression expr)
        {
            ComparePropToValue     comparePropToValue     = null;
            ComparePropToProp      comparePropToProp      = null;
            CompareValueToCriteria compareValueToCriteria = null;
            ComparePropToCriteria  comparePropToCriteria  = null;

            switch (expr.NodeType)
            {
            case ExpressionType.Equal:
                comparePropToValue     = (n, v) => (v != null) ? Restrictions.Eq(n, v) : Restrictions.IsNull(n);
                comparePropToProp      = Restrictions.EqProperty;
                compareValueToCriteria = Subqueries.Eq;
                comparePropToCriteria  = Subqueries.PropertyEq;
                break;

            case ExpressionType.GreaterThan:
                comparePropToValue     = Restrictions.Gt;
                comparePropToProp      = Restrictions.GtProperty;
                compareValueToCriteria = Subqueries.Gt;
                comparePropToCriteria  = Subqueries.PropertyGt;
                break;

            case ExpressionType.GreaterThanOrEqual:
                comparePropToValue     = Restrictions.Ge;
                comparePropToProp      = Restrictions.GeProperty;
                compareValueToCriteria = Subqueries.Ge;
                comparePropToCriteria  = Subqueries.PropertyGe;
                break;

            case ExpressionType.LessThan:
                comparePropToValue     = Restrictions.Lt;
                comparePropToProp      = Restrictions.LtProperty;
                compareValueToCriteria = Subqueries.Lt;
                comparePropToCriteria  = Subqueries.PropertyLt;
                break;

            case ExpressionType.LessThanOrEqual:
                comparePropToValue     = Restrictions.Le;
                comparePropToProp      = Restrictions.LeProperty;
                compareValueToCriteria = Subqueries.Le;
                comparePropToCriteria  = Subqueries.PropertyLe;
                break;

            case ExpressionType.NotEqual:
                comparePropToValue     = (n, v) => (v != null) ? Restrictions.Not(Restrictions.Eq(n, v)) : Restrictions.IsNotNull(n);
                comparePropToProp      = Restrictions.NotEqProperty;
                compareValueToCriteria = Subqueries.Ne;
                comparePropToCriteria  = Subqueries.PropertyNe;
                break;
            }

            CurrentCriterions.Add(
                BinaryCriterionVisitor.GetBinaryCriteria(rootCriteria, session,
                                                         expr, comparePropToValue, comparePropToProp,
                                                         compareValueToCriteria, comparePropToCriteria));
        }
        public static ICriterion GetBinaryCriteria(
            ICriteria rootCriteria,
            ISession session,
            BinaryExpression expr,
            ComparePropToValue comparePropToValue,
            ComparePropToProp comparePropToProp,
            CompareValueToCriteria compareValueToCriteria,
            ComparePropToCriteria comparePropToCriteria)
        {
            var left  = new BinaryCriterionVisitor(rootCriteria, session);
            var right = new BinaryCriterionVisitor(rootCriteria, session);

            left.Visit(expr.Left);
            right.Visit(expr.Right);

            //the query should have been preprocessed so that
            //only the following combinations are possible:
            // LEFT           RIGHT
            // ========================
            // property       value
            // property       property
            // property       criteria
            // value          criteria
            // criteria       criteria   <== not supported yet

            switch (left.Type)
            {
            case BinaryCriterionType.Property:
                switch (right.Type)
                {
                case BinaryCriterionType.Value:
                    object val = right.Value;
                    if (left.ConvertTo != null)
                    {
                        val = LinqUtil.ChangeType(val, left.ConvertTo);
                    }
                    return(comparePropToValue(left.Name, val));

                case BinaryCriterionType.Property:
                    return(comparePropToProp(left.Name, right.Name));

                case BinaryCriterionType.Criteria:
                    return(comparePropToCriteria(left.Name, right.Criteria));
                }
                break;

            case BinaryCriterionType.Value:
                return(compareValueToCriteria(left.Value, right.Criteria));
            }

            throw new NotSupportedException("Could not understand: " + expr);
        }
        public static ICriterion GetBinaryCriteria(
            ICriteria rootCriteria,
            ISession session,
            BinaryExpression expr,
            ComparePropToValue comparePropToValue,
            ComparePropToProp comparePropToProp,
            CompareValueToCriteria compareValueToCriteria,
            ComparePropToCriteria comparePropToCriteria)
        {
            var left = new BinaryCriterionVisitor(rootCriteria, session);
            var right = new BinaryCriterionVisitor(rootCriteria, session);

            left.Visit(expr.Left);
            right.Visit(expr.Right);

            //the query should have been preprocessed so that
            //only the following combinations are possible:
            // LEFT           RIGHT
            // ========================
            // property       value
            // property       property
            // property       criteria
            // value          criteria
            // criteria       criteria   <== not supported yet

            switch (left.Type)
            {
                case BinaryCriterionType.Property:
                    switch (right.Type)
                    {
                        case BinaryCriterionType.Value:
                            object val = right.Value;
                            if (left.ConvertTo != null)
                                val = LinqUtil.ChangeType(val, left.ConvertTo);
                            return comparePropToValue(left.Name, val);

                        case BinaryCriterionType.Property:
                            return comparePropToProp(left.Name, right.Name);

                        case BinaryCriterionType.Criteria:
                            return comparePropToCriteria(left.Name, right.Criteria);
                    }
                    break;

                case BinaryCriterionType.Value:
                    return compareValueToCriteria(left.Value, right.Criteria);
            }

            throw new NotSupportedException("Could not understand: " + expr);
        }