예제 #1
0
        private void RegisterAggregate(string name,
                                       SqlParameterInfo param1, SqlParameterInfo param2,
                                       SqlType returnType,
                                       Action <IterateContext> iterate,
                                       Func <InitializeContext, Task> initialize = null,
                                       Func <MergeContext, Task> merge           = null)
        {
            var methodInfo = new SqlFunctionInfo(new ObjectName(name), returnType);

            if (param1 != null)
            {
                methodInfo.Parameters.Add(param1);
            }
            if (param2 != null)
            {
                methodInfo.Parameters.Add(param2);
            }

            var aggregate = new SqlAggregateFunctionDelegate(methodInfo, iterate);

            aggregate.Initialize(initialize);
            aggregate.Merge(merge);

            Register(aggregate);
        }
예제 #2
0
        public async Task ExecuteWithNamedArgsAndDefaultValue()
        {
            var name = ObjectName.Parse("a.func");
            var info = new SqlFunctionInfo(name, PrimitiveTypes.Integer());

            info.Parameters.Add(new SqlParameterInfo("a", PrimitiveTypes.Integer()));
            info.Parameters.Add(new SqlParameterInfo("b",
                                                     PrimitiveTypes.String(),
                                                     SqlExpression.Constant(SqlObject.String(new SqlString("test")))));

            var function = new SqlFunctionDelegate(info, ctx => {
                var a = ctx.Value("a");
                var b = ctx.Value("b");
                Assert.NotNull(b);
                return(Task.FromResult(a.Multiply(SqlObject.BigInt(2))));
            });

            Assert.Equal(name, info.MethodName);
            Assert.Equal(FunctionType.Scalar, function.FunctionType);
            Assert.NotNull(info.ReturnType);
            Assert.Equal(SqlTypeCode.Integer, info.ReturnType.TypeCode);

            var result = await function.ExecuteAsync(context, new InvokeArgument("a", SqlObject.Integer(22)));

            Assert.NotNull(result);
            Assert.True(result.HasReturnedValue);
            Assert.NotNull(result.ReturnedValue);
            Assert.IsType <SqlConstantExpression>(result.ReturnedValue);
        }
 public SqlFunctionDelegate(SqlFunctionInfo functionInfo, Func <MethodContext, SqlExpression> body)
     : this(functionInfo, context => {
     var result = body(context);
     context.SetResult(result);
     return(Task.CompletedTask);
 }) {
 }
예제 #4
0
        public async Task ExecuteWithReference()
        {
            var info = new SqlFunctionInfo(new ObjectName("count"), PrimitiveTypes.BigInt());

            info.Parameters.Add(new SqlParameterInfo("a", PrimitiveTypes.VarChar()));

            var function = new SqlAggregateFunctionDelegate(info, accumulate => {
                SqlObject r;
                if (accumulate.IsFirst)
                {
                    r = accumulate.Current;
                }
                else
                {
                    var x = accumulate.Accumulation;
                    var y = accumulate.Current;

                    r = x.Add(y);
                }

                accumulate.SetResult(r);
            });

            var result = await function.ExecuteAsync(context, SqlExpression.Reference(ObjectName.Parse("a")));

            Assert.NotNull(result);
            Assert.NotNull(result.ReturnedValue);
            Assert.True(result.HasReturnedValue);

            Assert.IsType <SqlConstantExpression>(result.ReturnedValue);

            var value = ((SqlConstantExpression)result.ReturnedValue).Value;

            Assert.Equal(SqlObject.Integer(56), value);
        }
예제 #5
0
        public SqlAggregateFunctionDelegate(SqlFunctionInfo functionInfo, Func <IterateContext, Task> iterate)
            : base(functionInfo)
        {
            if (iterate == null)
            {
                throw new ArgumentNullException(nameof(iterate));
            }

            this.iterate = iterate;
        }
예제 #6
0
        public static void MakeFunctionInfo()
        {
            var name = ObjectName.Parse("a.func");
            var info = new SqlFunctionInfo(name, PrimitiveTypes.Integer());

            info.Parameters.Add(new SqlParameterInfo("a", PrimitiveTypes.Integer()));

            Assert.Equal(name, info.MethodName);
            Assert.NotNull(info.ReturnType);
            Assert.Equal(SqlTypeCode.Integer, info.ReturnType.TypeCode);
        }
예제 #7
0
        private void Register(string name, SqlParameterInfo[] parameters, SqlType returnType, Func <MethodContext, Task> body)
        {
            var functionInfo = new SqlFunctionInfo(ObjectName.Parse(name), returnType);

            foreach (var parameter in parameters)
            {
                functionInfo.Parameters.Add(parameter);
            }

            var function = new SqlFunctionDelegate(functionInfo, body);

            Register(function);
        }
예제 #8
0
        public static void MatchInvoke()
        {
            var name = ObjectName.Parse("a.func");
            var info = new SqlFunctionInfo(name, PrimitiveTypes.Integer());

            info.Parameters.Add(new SqlParameterInfo("a", PrimitiveTypes.Integer()));

            var function = new SqlFunctionDelegate(info, context => Task.CompletedTask);

            var invoke = new Invoke(name, new [] { new InvokeArgument(SqlObject.BigInt(11)) });

            Assert.True(function.Matches(null, invoke));
        }
예제 #9
0
        public static void GetString()
        {
            var name = ObjectName.Parse("a.func");
            var info = new SqlFunctionInfo(name, PrimitiveTypes.Integer());

            info.Parameters.Add(new SqlParameterInfo("a", PrimitiveTypes.Integer()));
            var function = new SqlFunctionDelegate(info, ctx => {
                var a = ctx.Value("a");
                return(Task.FromResult(a.Multiply(SqlObject.BigInt(2))));
            });

            var sql = $"FUNCTION a.func(a INTEGER) RETURNS INTEGER";

            Assert.Equal(sql, function.ToString());
        }
예제 #10
0
        public void ResolveDeterministricReturnType()
        {
            var name = ObjectName.Parse("a.func");
            var info = new SqlFunctionInfo(name, new SqlDeterministicType());

            info.Parameters.Add(new SqlParameterInfo("a", PrimitiveTypes.Integer()));
            info.Parameters.Add(new SqlParameterInfo("b",
                                                     PrimitiveTypes.String(),
                                                     SqlExpression.Constant(SqlObject.String(new SqlString("test")))));

            var function = new SqlFunctionDelegate(info, ctx => {
                var a = ctx.Value("a");
                var b = ctx.Value("b");
                Assert.NotNull(b);
                return(Task.FromResult(a.Multiply(SqlObject.BigInt(2))));
            });

            var returnType = function.ReturnType(context,
                                                 new Invoke(name, new[] { new InvokeArgument(SqlObject.Integer(33)), new InvokeArgument(SqlObject.Integer(2)) }));

            Assert.Equal(PrimitiveTypes.Integer(), returnType);
        }
예제 #11
0
        public void RegisterAndResolveFunction()
        {
            var name = ObjectName.Parse("a.func");
            var info = new SqlFunctionInfo(name, PrimitiveTypes.Integer());

            info.Parameters.Add(new SqlParameterInfo("a", PrimitiveTypes.Integer()));
            var function = new SqlFunctionDelegate(info, ctx => {
                var a = ctx.Value("a");
                return(Task.FromResult(a.Multiply(SqlObject.BigInt(2))));
            });

            registry.Register(function);

            var invoke = new Invoke(name);

            invoke.Arguments.Add(new InvokeArgument("a", SqlObject.Integer(11)));

            var method = (registry as IMethodResolver).ResolveMethod(context, invoke);

            Assert.NotNull(method);
            Assert.True(method.IsFunction);
            Assert.Equal(name, method.MethodInfo.MethodName);
        }
예제 #12
0
 public SqlAggregateFunctionDelegate(SqlFunctionInfo functionInfo, Action <IterateContext> iterate)
     : this(functionInfo, context => {
     iterate(context);
     return(Task.CompletedTask);
 }) {
 }
예제 #13
0
 public SqlAggregateFunction(SqlFunctionInfo functionInfo)
     : base(functionInfo)
 {
 }
예제 #14
0
 protected SqlFunctionBase(SqlFunctionInfo functionInfo)
     : base(functionInfo)
 {
 }
 public SqlFunctionDelegate(SqlFunctionInfo functionInfo, Func <MethodContext, Task <SqlObject> > body)
     : this(functionInfo, async context => {
     var result = await body(context);
     context.SetResult(result);
 }) {
 }
 public SqlFunctionDelegate(SqlFunctionInfo functionInfo, Func <MethodContext, Task> body)
     : base(functionInfo)
 {
     this.body = body;
 }