Ejemplo n.º 1
0
        public void GenNodeClass(CSharpWriter csw)
        {
            if (Rule.ClassName != null)
            {
                csw.Begin("public partial class {0}", Rule.ClassName);
            }
            else
            {
                csw.Begin("public class {0} : SqlNode", Rule.GetResultType());
            }
            GenVarMembers(csw);
            Rule.Chain.WriteSqlNodeMembers(csw, this);
            csw.Begin("public override void GenerateSql(ISqlDumper dmp)");
            Rule.Chain.WriteGenSqlBody(csw, this);
            csw.End(); // GenerateSql
            csw.Begin("public override void EnumSymbols(ISymbolEnumerator en)");
            foreach (var vd in Vars)
            {
                switch (vd.TypeGroup)
                {
                case VariableTypeGroup.SubNode:
                    csw.WriteLine("{0}.EnumSymbols(en);", vd.Name);
                    break;

                case VariableTypeGroup.SymbolPos:
                    csw.WriteLine("en.EnumSymbol({0}, this);", vd.Name);
                    break;
                }
            }
            csw.End(); // EnumSymbols
            csw.End(); // class
        }
Ejemplo n.º 2
0
        public override void WriteParserMembers(CSharpWriter csw, RuleCompiler cmp)
        {
            foreach (var chain in Chains)
            {
                chain.WriteParserMembers(csw, cmp);
            }
            WriteParseMethodHeader(csw, cmp);
            int chindex = 0;

            foreach (var chain in Chains)
            {
                csw.Begin("if ({0}(args))", chain.ParseFuncName);
                csw.WriteLine("args.{0} = {1}.{2}.{3};", EnumVarName, cmp.Rule.GetResultType(), EnumTypeName, EnumElems[chindex]);
                if (!Mandatory)
                {
                    csw.WriteLine("args.{0} = true;", IsVariable);
                }
                csw.WriteLine("return true;");
                csw.End();
                chindex++;
            }
            if (Mandatory)
            {
                csw.WriteLine("return false;");
            }
            else
            {
                csw.WriteLine("args.{0} = false;", IsVariable);
                csw.WriteLine("return true;");
            }
            csw.End(); // method
        }
Ejemplo n.º 3
0
 public override void WriteParserMembers(CSharpWriter csw, RuleCompiler cmp)
 {
     WriteParseMethodHeader(csw, cmp);
     csw.Begin("if (IsTerminal(\"{0}\"))", Symbol);
     csw.WriteLine("args.{0} = CurrentOriginal;", TerminalPosName);
     csw.WriteLine("NextToken();");
     csw.WriteLine("return true;");
     csw.End();
     csw.WriteLine("return false;");
     csw.End(); // method
 }
Ejemplo n.º 4
0
 public void GenCode(CSharpWriter csw, string ns, string cls)
 {
     csw.Begin("namespace {0}", ns);
     foreach (var rule in Rules)
     {
         rule.GenClassDefs(csw);
     }
     csw.Begin("public partial class {0}", cls);
     foreach (var rule in Rules)
     {
         rule.GenParserMethods(csw);
     }
     csw.End();
     csw.End();
 }
Ejemplo n.º 5
0
        public override void WriteParserMembers(CSharpWriter csw, RuleCompiler cmp)
        {
            WriteParseMethodHeader(csw, cmp);
            Rule target = cmp.Rules.FindRule(RefName);

            target.GenRuleTestBody(csw, cmp, MemberNodeName);
            csw.End();
        }
Ejemplo n.º 6
0
 internal void WriteParserMembers(CSharpWriter csw, RuleCompiler cmp)
 {
     foreach (RuleItem item in Items)
     {
         item.WriteParserMembers(csw, cmp);
     }
     csw.Begin("public bool {0}({1} args)", ParseFuncName, cmp.Rule.ArgsClassName);
     csw.WriteLine("var beginMark = MarkPosition();");
     foreach (RuleItem item in Items)
     {
         csw.Begin("if (!{0}(args))", item.ParseFuncName);
         csw.WriteLine("GoToMark(beginMark);");
         csw.WriteLine("return false;");
         csw.End();
     }
     csw.WriteLine("return true;");
     csw.End();
 }
Ejemplo n.º 7
0
 public void GenParserMethods(CSharpWriter csw)
 {
     Rule.Chain.WriteParserMembers(csw, this);
     csw.Method(Rule.GetResultType(), Rule.ParseFuncName);
     csw.WriteLine("var args = new {0}();", Rule.ArgsClassName);
     csw.WriteLine("bool ok = {0}(args);", Rule.Chain.ParseFuncName);
     csw.Begin("if (ok)");
     csw.WriteLine("var res = new {0}();", Rule.GetResultType());
     foreach (var vd in Vars)
     {
         csw.WriteLine("res.{0} = args.{0};", vd.Name);
         if (vd.TypeGroup == VariableTypeGroup.SubNode)
         {
             csw.WriteLine("if (res.{0} != null) res.{0}.Parent = res;", vd.Name);
         }
     }
     csw.WriteLine("return res;");
     csw.End(); // if (ok)
     csw.WriteLine("return null;");
     csw.End(); // method
 }
Ejemplo n.º 8
0
 public override void WriteGenSqlBody(CSharpWriter csw, RuleCompiler cmp)
 {
     if (!Mandatory)
     {
         csw.Begin("if ({0})", IsVariable);
     }
     csw.Begin("switch ({0})", EnumVarName);
     for (int i = 0; i < Chains.Count; i++)
     {
         csw.WriteLine("case {0}.{1}:", EnumTypeName, EnumElems[i]);
         csw.Inc();
         Chains[i].WriteGenSqlBody(csw, cmp);
         csw.WriteLine("break;");
         csw.Dec();
     }
     csw.End(); // switch
     if (!Mandatory)
     {
         csw.End();             // if ($IsVariable)
     }
 }
Ejemplo n.º 9
0
 public void GenArgsClass(CSharpWriter csw)
 {
     csw.Class(Rule.ArgsClassName);
     GenVarMembers(csw);
     csw.End();
 }
Ejemplo n.º 10
0
 public override void WriteParserMembers(CSharpWriter csw, RuleCompiler cmp)
 {
     WriteParseMethodHeader(csw, cmp);
     csw.WriteLine("return true;");
     csw.End(); // method
 }
Ejemplo n.º 11
0
        protected override void DoRun(IJobRunEnv env)
        {
            using (StreamWriter sw = new StreamWriter(File))
            {
                CSharpWriter csw = new CSharpWriter(sw);
                csw.WriteLine("// ***********************************************************************");
                csw.WriteLine("// This code was generated by DatAdmin DB MODEL Plugin, please do not edit");
                csw.WriteLine("// ***********************************************************************");
                csw.WriteLine("using System.Data.Common;");
                csw.Begin("namespace {0}", NamespaceName);
                csw.Begin("public static class {0}", ClassName);

                csw.WriteLine("public static string DBVERSION = {0};", CSharpWriter.StringLiteral(m_versionDb.Versions.Last().Name));

                csw.Begin("private static void ExecuteNonQuery(DbConnection conn, DbTransaction tran, string sql)");
                csw.Begin("using (DbCommand cmd = conn.CreateCommand())");
                csw.WriteLine("cmd.CommandText = sql;");
                csw.WriteLine("cmd.Transaction = tran;");
                csw.WriteLine("cmd.ExecuteNonQuery();");
                csw.End(); // using
                csw.End(); // function

                csw.Begin("public static string GetVersion(DbConnection conn, DbTransaction tran)");
                csw.Begin("try");
                csw.Begin("using (DbCommand cmd = conn.CreateCommand())");
                csw.WriteLine("cmd.Transaction = tran;");
                csw.WriteLine("cmd.CommandText = {0};", CSharpWriter.StringLiteral(m_versionDb.m_props.GetVersionSql));
                csw.WriteLine("return cmd.ExecuteScalar().ToString();");
                csw.End(); // using
                csw.End(); // try
                csw.Begin("catch");
                csw.WriteLine("return null;");
                csw.End(); // catch
                csw.End(); // function

                int vindex = 0;
                foreach (var vd in m_versionDb.Versions)
                {
                    csw.WriteLine("// update to version {0}", vd.Name);
                    csw.Begin("private static void UpdateToVersion_{0}(DbConnection conn, DbTransaction tran)", vindex);
                    foreach (string sql in vd.GetAlterSqls())
                    {
                        csw.WriteLine("ExecuteNonQuery(conn, tran, {0});", CSharpWriter.StringLiteral(sql));
                    }
                    csw.End();
                    vindex++;
                }

                csw.Begin("public static void UpdateDb(DbConnection conn)");
                if (UseTransaction)
                {
                    csw.Begin("using (DbTransaction tran = conn.BeginTransaction())");
                    csw.Begin("try");
                }
                else
                {
                    csw.WriteLine("DbTransaction tran = null;");
                }
                for (int i = 0; i < m_versionDb.Versions.Count; i++)
                {
                    if (i == 0)
                    {
                        csw.WriteLine("if (GetVersion(conn, tran) == null) UpdateToVersion_0(conn, tran);");
                    }
                    else
                    {
                        csw.WriteLine("if (GetVersion(conn, tran) == {0}) UpdateToVersion_{1}(conn, tran);", CSharpWriter.StringLiteral(m_versionDb.Versions[i - 1].Name), i);
                    }
                }
                if (UseTransaction)
                {
                    csw.End(); // try
                    csw.Begin("catch");
                    csw.WriteLine("tran.Rollback();");
                    csw.WriteLine("throw;");
                    csw.End(); // catch
                    csw.WriteLine("tran.Commit();");
                    csw.End(); // using
                }
                csw.End();     // function Run

                csw.Begin("public static void CheckVersion(DbConnection conn)");
                csw.WriteLine("string realversion = GetVersion(conn, null);");
                csw.Begin("if (realversion != DBVERSION)");
                csw.WriteLine("throw new Exception(String.Format(\"Bad version of database, code_version={0}, db_version={1}\", DBVERSION, realversion));");
                csw.End(); // if
                csw.End(); //  CheckVersion

                csw.End(); // class
                csw.End(); // namespace
            }
        }