示例#1
0
        public static Result <EmitFunction> CompileNamed(
            SdmapCompilerContext context,
            NamedSqlContext parseTree)
        {
            var id       = parseTree.GetToken(SYNTAX, 0).GetText();
            var fullName = context.GetFullNameInCurrentNs(id);

            var core = new CoreSqlVisitor(context);

            return(core.Process(parseTree.coreSql(), fullName)
                   .OnSuccess(() => core.Function));
        }
示例#2
0
        public override Result VisitNamedSql([NotNull] NamedSqlContext context)
        {
            _writer.WriteIndentLine(
                $"{_config.AccessModifier} class {context.SYNTAX().GetText()}");
            _writer.UsingIndent(() =>
            {                                              // _ internal class {id} <CRLF>
                _writer.WriteIndentLine($": {nameof(ISdmapEmiter)}");
            });                                            // _ _ : IBase
            return(_writer.UsingIndent("{", "}", () =>
            {
                return ClassGeneration();
            }));


            Result ClassGeneration()
            {
                _writer.WriteIndentLine(      // _ internal res B()
                    $"{_config.AccessModifier} Result<string> BuildText(object self)");
                return(_writer.UsingIndent("{", "}", () =>
                {
                    return Result.Combine(
                        MethodGeneration(),
                        UnnamedSqlGeneration());
                }));
            }

            Result MethodGeneration()
            {
                _writer.WriteIndentLine("var sb = new StringBuilder();");
                var r = base.VisitNamedSql(context);

                _writer.WriteIndentLine("return Result.Ok(sb.ToString());");
                return(r);
            }

            Result UnnamedSqlGeneration()
            {
                foreach (var unnamed in _unnamedSqls)
                {
                    _writer.WriteLine();
                    _writer.WriteIndentLine($"private Result<string> {unnamed.Key}(object self)");
                    var ok = _writer.UsingIndent("{", "}", () =>
                    {
                        return(unnamed.Value(_writer));
                    });
                    if (ok.IsFailure)
                    {
                        return(ok);
                    }
                }
                return(Result.Ok());
            }
        }
示例#3
0
 public static SqlEmiter CreateNamed(NamedSqlContext parseTree, string ns)
 {
     return(new SqlEmiter(parseTree, ns,
                          ctx => CompileNamed(ctx, parseTree)));
 }