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);
        }
Beispiel #2
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);
        }
        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));
        }
        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());
        }
        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);
        }
        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);
        }