Esempio n. 1
0
        public override ExecuteResult Execute(ExecuteContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            var group = context.GroupResolver;

            if (group == null)
                throw new Exception(String.Format("'{0}' can only be used as an aggregate function.", FunctionName));

            DataObject result = null;
            // All aggregates functions return 'null' if group size is 0
            int size = group.Count;
            if (size == 0) {
                // Return a NULL of the return type
                return context.Result(DataObject.Null(ReturnType(context)));
            }

            DataObject val;
            ObjectName v = context.Arguments[0].AsReferenceName();
            // If the aggregate parameter is a simple variable, then use optimal
            // routine,
            if (v != null) {
                for (int i = 0; i < size; ++i) {
                    val = group.Resolve(v, i);
                    result = Evaluate(result, val, context.QueryContext, group);
                }
            } else {
                // Otherwise we must resolve the expression for each entry in group,
                // This allows for expressions such as 'sum(quantity * price)' to
                // work for a group.
                var exp = context.Arguments[0];
                for (int i = 0; i < size; ++i) {
                    val = exp.EvaluateToConstant(context.QueryContext, group.GetVariableResolver(i));
                    result = Evaluate(result, val, context.QueryContext, group);
                }
            }

            // Post method.
            result = PostEvaluate(result, context.QueryContext, group);

            return context.Result(result);
        }
Esempio n. 2
0
        public override ExecuteResult Execute(ExecuteContext context)
        {
            if (method == null)
                method = DiscoverMethod();

            if (method == null)
                throw new InvalidOperationException();

            var args = context.EvaluatedArguments;

            try {
                var methodArgs = ConvertArguments(method, args);
                var result = method.Invoke(null, methodArgs);

                return  context.Result(ConvertValue(result, ReturnType()));
            } catch (Exception ex) {
                throw;
            }
        }
 private static ExecuteResult Simple(ExecuteContext context, Func<DataObject[], DataObject> func)
 {
     var args = context.EvaluatedArguments;
     var funcResult = func(args);
     return context.Result(funcResult);
 }
            public static ExecuteResult Execute(ExecuteContext context)
            {
                var value = context.EvaluatedArguments[0];
                var typeArg = context.EvaluatedArguments[1];
                var typeString = typeArg.AsVarChar().Value.ToString();
                var type = SqlType.Parse(context.QueryContext, typeString);

                return context.Result(SystemFunctions.Cast(value, type));
            }
            public static ExecuteResult Execute(ExecuteContext context)
            {
                if (context.GroupResolver == null)
                    throw new Exception("'count' can only be used as an aggregate function.");

                int size = context.GroupResolver.Count;
                DataObject result;
                // if, count(*)
                if (size == 0 || context.Invoke.IsGlobArgument) {
                    result = DataObject.Integer(size);
                } else {
                    // Otherwise we need to count the number of non-null entries in the
                    // columns list(s).

                    int totalCount = size;

                    var exp = context.Arguments[0];
                    for (int i = 0; i < size; ++i) {
                        var val = exp.EvaluateToConstant(context.QueryContext, context.GroupResolver.GetVariableResolver(i));
                        if (val.IsNull) {
                            --totalCount;
                        }
                    }

                    result = DataObject.Integer(totalCount);
                }

                return context.Result(result);
            }
 private ExecuteResult Simple(ExecuteContext context, Func<DataObject[], DataObject> func)
 {
     var evaluated = context.EvaluatedArguments;
     var value = func(evaluated);
     return context.Result(value);
 }
 private ExecuteResult Binary(ExecuteContext context, Func<DataObject, DataObject, DataObject> func)
 {
     var evaluated = context.EvaluatedArguments;
     var value = func(evaluated[0], evaluated[1]);
     return context.Result(value);
 }