Esempio n. 1
0
        public override Argument VisitAdditiveExpression(CalcParser.AdditiveExpressionContext context)
        {
            Argument arg1 = Visit(context.expression(0));
            Argument arg2 = Visit(context.expression(1));

            if (arg1.IsNull || arg2.IsNull)
            {
                return(Argument.Null);
            }

            if (context.Plus() != null)
            {
                return(Sum(arg1, arg2));
            }

            if (context.Minus() != null)
            {
                return(Difference(arg1, arg2));
            }

            return(Argument.Null);
        }
Esempio n. 2
0
        public override Query VisitAdditiveExpression(CalcParser.AdditiveExpressionContext context)
        {
            Query q1 = Visit(context.expression(0));
            Query q2 = Visit(context.expression(1));

            if (context.Plus() != null)
            {
                ResultType type = ResultType.Undefined;
                if (q1.ExpectedResult == ResultType.Number)
                {
                    if (q2.ExpectedResult == ResultType.Number)
                    {
                        type = ResultType.Number;
                    }
                    else if (q2.ExpectedResult == ResultType.Date)
                    {
                        type = ResultType.Date;
                    }
                }

                if (q1.ExpectedResult == ResultType.Date)
                {
                    if (q2.ExpectedResult == ResultType.Number)
                    {
                        type = ResultType.Date;
                    }
                }

                return(new Query($"({q1}+{q2})", type));
            }

            if (context.Minus() != null)
            {
                ResultType type = ResultType.Undefined;
                if (q1.ExpectedResult == ResultType.Number && q2.ExpectedResult == ResultType.Number)
                {
                    type = ResultType.Number;
                }

                if (q1.ExpectedResult == ResultType.Date)
                {
                    if (q2.ExpectedResult == ResultType.Date)
                    {
                        type = ResultType.Number;
                    }
                    else if (q2.ExpectedResult == ResultType.Number)
                    {
                        type = ResultType.Date;
                    }
                }

                string queryText = $"({q1}-{q2})";

                if (q1.ExpectedResult == ResultType.Date && q2.ExpectedResult == ResultType.Date)
                {
                    queryText = this._dbTranslator.CastToInt(queryText);
                }

                return(new Query(queryText, type));
            }

            throw new ExpressionException("Unknown additive operation: " + context.GetText());
        }