private void Visit(BetweenOperator op, bool not)
        {
            if (!(op.Left is PropertyReference prop &&
                  TryGetLiteralValue(op.Min, out var min) &&
                  TryGetLiteralValue(op.Max, out var max)))
            {
                throw new NotSupportedException();
            }

            var condition = not ? Condition.NotBetween : Condition.Between;

            if (min is long minLng && max is long maxLng)
            {
                if (minLng <= int.MaxValue && minLng >= int.MinValue &&
                    maxLng <= int.MaxValue && maxLng >= int.MinValue)
                {
                    var minInt = (int)minLng;
                    var maxInt = (int)maxLng;
                    AddCriteria(prop, condition, new Range <int>(minInt, maxInt));
                }
                else
                {
                    AddCriteria(prop, condition, new Range <long>(minLng, maxLng));
                }
            }
 public virtual IExpression Clone(BetweenOperator op)
 {
     return(new BetweenOperator()
     {
         Left = CloneAndReturn(op.Left),
         Min = CloneValue(op.Left, op.Min),
         Max = CloneValue(op.Left, op.Max)
     }.Normalize());
 }
Beispiel #3
0
 protected virtual void Visit(BetweenOperator op, bool not)
 {
     AddParenthesesIfNeeded(op, () =>
     {
         op.Left.Visit(this);
         Writer.Write((not ? " not" : "") + " between ");
         op.Min.Visit(this);
         Writer.Write(" and ");
         op.Max.Visit(this);
     });
 }
Beispiel #4
0
        public void Visit(BetweenOperator op)
        {
            if (!(op.Left is PropertyReference prop))
            {
                throw new NotSupportedException();
            }

            EnterContext(op, () =>
            {
                _writer.WriteStartElement(prop.Name);
                _writer.WriteAttributeString("condition", "between");
                if (op.Min is DateOffsetLiteral minOffset && op.Max is DateOffsetLiteral maxOffset)
                {
                    _writer.WriteAttributeString(ParameterSubstitution.DateRangeAttribute
                                                 , ParameterSubstitution.SerializeDateRange(minOffset.Offset, maxOffset.Offset));
                }
                op.Min.Visit(op.Min is DateTimeLiteral ? (IExpressionVisitor)this : _sqlVisitor);
                _writer.WriteString(" and ");
                op.Max.Visit(op.Max is DateTimeLiteral ? (IExpressionVisitor)this : _sqlVisitor);
                _writer.WriteEndElement();
            });
Beispiel #5
0
 public void Visit(BetweenOperator op)
 {
     Visit(op, false);
 }
 public virtual void Visit(BetweenOperator op)
 {
     op.Left.Visit(this);
     op.Min.Visit(this);
     op.Max.Visit(this);
 }
 public void Visit(BetweenOperator op)
 {
     op.ToConditional().Visit(this);
 }
 void IExpressionVisitor.Visit(BetweenOperator op)
 {
     _clone = Clone(op);
 }
        /// <summary>Closes one element and pops the corresponding namespace scope.</summary>
        /// <exception cref="InvalidOperationException">This results in an invalid XML document.</exception>
        /// <exception cref="InvalidOperationException">An <see cref="XmlWriter" /> method was called before a previous asynchronous operation finished. In this case, <see cref="InvalidOperationException" /> is thrown with the message “An asynchronous operation is already in progress.”</exception>
        public override void WriteEndElement()
        {
            if (_stack.Count > 0)
            {
                var value = _buffer.ToString() ?? "";
                _buffer.Length = 0;

                var last = _stack.Pop();
                if (_attrBuffer.TryGetValue(ParameterSubstitution.DateRangeAttribute, out var dateRange) &&
                    ParameterSubstitution.TryDeserializeDateRange(dateRange, out var dateStart, out var dateEnd) &&
                    (dateStart.HasValue || dateEnd.HasValue))
                {
                    var property = (last as BinaryOperator)?.Left ?? (last as BetweenOperator)?.Left;
                    if (property == null)
                    {
                        throw new NotSupportedException();
                    }

                    if (dateStart.HasValue && dateEnd.HasValue)
                    {
                        last = new BetweenOperator()
                        {
                            Left = property,
                            Min  = new DateOffsetLiteral(_context, dateStart.Value, false),
                            Max  = new DateOffsetLiteral(_context, dateEnd.Value, true),
                        }.Normalize();
                    }
                    else if (dateStart.HasValue)
                    {
                        last = new GreaterThanOrEqualsOperator()
                        {
                            Left  = property,
                            Right = new DateOffsetLiteral(_context, dateStart.Value, false)
                        }.Normalize();
                    }
                    else
                    {
                        last = new LessThanOrEqualsOperator()
                        {
                            Left  = property,
                            Right = new DateOffsetLiteral(_context, dateEnd.Value, true)
                        }.Normalize();
                    }
                }
                else
                {
                    if (last is IsOperator isOp)
                    {
                        switch (value)
                        {
                        case "defined":
                            isOp.Right = IsOperand.Defined;
                            break;

                        case "not defined":
                            isOp.Right = IsOperand.NotDefined;
                            break;

                        case "not null":
                            isOp.Right = IsOperand.NotNull;
                            break;

                        case "null":
                            isOp.Right = IsOperand.Null;
                            break;

                        case "":
                            break;

                        default:
                            throw new NotSupportedException();
                        }
                    }
                    else if (last is InOperator inOp)
                    {
                        inOp.Right = ListExpression.FromSqlInClause(value);
                    }
                    else if (last is BetweenOperator betweenOp)
                    {
                        betweenOp.SetMinMaxFromSql(value);
                    }
                    else if (last is PropertyReference property)
                    {
                        if (_stack.OfType <Join>().Last().Right.Joins.Any(j => j.Right.TypeProvider == property))
                        {
                            last = null;
                        }
                        else
                        {
                            last = new EqualsOperator()
                            {
                                Left = property
                            };
                        }
                    }

                    if (!(last is ILogical) && last is BinaryOperator binOp)
                    {
                        if (value == "__now()")
                        {
                            binOp.Right = new Functions.CurrentDateTime();
                        }
                        else if (binOp is LikeOperator)
                        {
                            binOp.Right = AmlLikeParser.Instance.Parse(value);
                        }
                        else
                        {
                            binOp.Right = NormalizeLiteral((PropertyReference)binOp.Left, value, _context);
                        }
                    }
                }

                if (last is BinaryOperator genOp &&
                    genOp.Left is PropertyReference prop &&
                    (prop.Name == "generation" || prop.Name == "id"))
                {
                    Query.Version = new VersionCriteria()
                    {
                        Condition = genOp
                    };
                    if (prop.Name == "id")
                    {
                        AddToCondition(genOp);
                    }
                }