예제 #1
0
        private void Create(
            IDbFunction function,
            string functionsVariable,
            CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
        {
            if (function.Translation != null)
            {
                throw new InvalidOperationException(RelationalStrings.CompiledModelFunctionTranslation(function.Name));
            }

            if (function is IConventionDbFunction conventionFunction &&
                conventionFunction.GetTypeMappingConfigurationSource() != null)
            {
                throw new InvalidOperationException(RelationalStrings.CompiledModelFunctionTypeMapping(
                                                        function.Name, "Customize()", parameters.ClassName));
            }

            AddNamespace(function.ReturnType, parameters.Namespaces);

            var code             = Dependencies.CSharpHelper;
            var functionVariable = code.Identifier(
                function.MethodInfo?.Name ?? function.Name, parameters.ScopeVariables, capitalize: false);
            var mainBuilder = parameters.MainBuilder;

            mainBuilder
            .Append("var ").Append(functionVariable).AppendLine(" = new RuntimeDbFunction(").IncrementIndent()
            .Append(code.Literal(function.ModelName)).AppendLine(",")
            .Append(parameters.TargetName).AppendLine(",")
            .Append(code.Literal(function.ReturnType)).AppendLine(",")
            .Append(code.Literal(function.Name));

            if (function.Schema != null)
            {
                mainBuilder.AppendLine(",")
                .Append("schema: ").Append(code.Literal(function.Schema));
            }

            if (function.StoreType != null)
            {
                mainBuilder.AppendLine(",")
                .Append("storeType: ").Append(code.Literal(function.StoreType));
            }

            if (function.MethodInfo != null)
            {
                var method = function.MethodInfo;
                mainBuilder.AppendLine(",")
                .Append("methodInfo: ").Append(code.Literal(method.DeclaringType !)).AppendLine(".GetMethod(").IncrementIndent()
                .Append(code.Literal(method.Name !)).AppendLine(",")
                .Append(method.IsPublic ? "BindingFlags.Public" : "BindingFlags.NonPublic")
                .Append(method.IsStatic ? " | BindingFlags.Static" : " | BindingFlags.Instance")
                .AppendLine(" | BindingFlags.DeclaredOnly,")
                .AppendLine("null,")
                .Append("new Type[] { ").Append(string.Join(", ", method.GetParameters().Select(p => code.Literal((Type)p.ParameterType)))).AppendLine(" },")
                .Append("null)").DecrementIndent();
            }

            if (function.IsScalar)
            {
                mainBuilder.AppendLine(",")
                .Append("scalar: ").Append(code.Literal(function.IsScalar));
            }

            if (function.IsAggregate)
            {
                mainBuilder.AppendLine(",")
                .Append("aggregate: ").Append(code.Literal(function.IsAggregate));
            }

            if (function.IsNullable)
            {
                mainBuilder.AppendLine(",")
                .Append("nullable: ").Append(code.Literal(function.IsNullable));
            }

            if (function.IsBuiltIn)
            {
                mainBuilder.AppendLine(",")
                .Append("builtIn: ").Append(code.Literal(function.IsBuiltIn));
            }

            mainBuilder.AppendLine(");").DecrementIndent()
            .AppendLine();

            var parameterParameters = parameters with {
                TargetName = functionVariable
            };

            foreach (var parameter in function.Parameters)
            {
                Create(parameter, parameterParameters);
            }

            CreateAnnotations(
                function,
                Generate,
                parameters with {
                TargetName = functionVariable
            });