Example #1
0
        public bool Evaluate(out IJsonValue value, IJSONDocument document)
        {
            //ArithmeticOperation consideration...
            value = null;
            object functionResult = null;

            if (_executionType.Equals(FunctionExecutionType.Scalar))
            {
                if (_funcImpl == null)
                {
                    return(false);
                }

                var values = new List <IJsonValue>();
                foreach (var argument in _arguments)
                {
                    IJsonValue result;
                    if (!argument.Evaluate(out result, document))
                    {
                        return(false);
                    }
                    values.Add(result);
                }

                if (_arguments.Count != values.Count)
                {
                    return(false);
                }

                var arguments = new object[values.Count];
                for (int i = 0; i < values.Count; i++)
                {
                    arguments[i] = JsonWrapper.UnWrap(values[i]);
                }
                try
                {
                    functionResult = _funcImpl.Execute(FunctionName, arguments);
                }
                catch (System.Reflection.TargetParameterCountException)
                {
                    throw;
                }
                catch (System.ArgumentException)
                {
                    throw;
                }
                catch (Exception exception)
                {
                    if (LoggerManager.Instance.QueryLogger != null && LoggerManager.Instance.QueryLogger.IsErrorEnabled)
                    {
                        LoggerManager.Instance.QueryLogger.Error("UDFs", exception.Message);
                    }
                    throw;
                }
                value = JsonWrapper.Wrap(functionResult);
                return(true);
            }

            if (_executionType.Equals(FunctionExecutionType.Aggregate))
            {
                string evaluation = "$" + ToString();
                object outValue;
                value = JsonWrapper.Wrap(!document.TryGet(evaluation, out outValue) ? 0 : outValue);
                return(true);
            }

            return(false);
        }