Exemplo n.º 1
0
        /// <summary>
        /// Gets the expression resulting value.
        /// </summary>
        /// <returns></returns>
        public object GetValue()
        {
            switch (_type)
            {
            case ExpressionType.Value:
                return(_data);

            case ExpressionType.DatabaseColumn:
                try
                {
                    return(tFilter.oCurrentData[iColumn]);
                }
                catch
                {
                    throw Trace.Error(Trace.COLUMN_NOT_FOUND, sColumn);
                }

            case ExpressionType.Function:
                return(fFunction.GetValue());

            case ExpressionType.Query:
                return(sSelect.GetValue(_columnType));

            case ExpressionType.Negate:
                return(Column.Negate(eArg.GetValue(_columnType), _columnType));

            case ExpressionType.Count:

                // count(*): sum(1); count(col): sum(col<>null)
                if (eArg.Type == ExpressionType.Asterix || eArg.GetValue() != null)
                {
                    return(1);
                }

                return(0);

            case ExpressionType.Maximum:
            case ExpressionType.Minimum:
            case ExpressionType.Sum:
            case ExpressionType.Average:
                return(eArg.GetValue());

            case ExpressionType.Exists:
                return(Test());

            case ExpressionType.Convert:
                return(eArg.GetValue(_columnType));

            case ExpressionType.CaseWhen:
                if (eArg.Test())
                {
                    return(eArg2.eArg.GetValue());
                }
                else
                {
                    return(eArg2.eArg2.GetValue());
                }

            case ExpressionType.Variable:
                return(eArg.GetValue());
            }

            // todo: simplify this
            object a = null, b = null;

            if (eArg != null)
            {
                a = eArg.GetValue(_columnType);
            }

            if (eArg2 != null)
            {
                b = eArg2.GetValue(_columnType);
            }

            switch (_type)
            {
            case ExpressionType.Add:
                return(Column.Add(a, b, _columnType));

            case ExpressionType.Subtract:
                return(Column.Subtract(a, b, _columnType));

            case ExpressionType.Multiply:
                return(Column.Multiply(a, b, _columnType));

            case ExpressionType.Divide:
                return(Column.Divide(a, b, _columnType));

            case ExpressionType.Concat:
                return(Column.Concat(a, b, _columnType));

            case ExpressionType.IfNull:
                return(a == null ? b : a);

            case ExpressionType.Equal:
                if (eArg.Type == ExpressionType.Variable)
                {
                    Trace.Check(eArg2 != null, Trace.GENERAL_ERROR);

                    return(eArg2.GetValue(eArg._columnType));
                }
                else
                {
                    return(Test());
                }

            default:

                // must be comparisation
                // todo: make sure it is
                return(Test());
            }
        }