private string GenerateTables(CodeDomProvider codeDomProvider, CodeGeneratorOptions options, CodeGenerationInfo generationInfo) { var sb = new StringBuilder(); ColumnInfoExtensions.TypeNamespace = "types"; using (var writer = new StringWriter(sb)) { var codeGenerator = codeDomProvider.CreateGenerator(writer); var compileUnit = new CodeCompileUnit(); var codeNamespace = new CodeNamespace(); this.AppendLint(sb, generationInfo); codeNamespace.Imports.Add(new CodeNamespaceImport($"reader = require(\"./{generationInfo.RelativePath}{generationInfo.Namespace}-reader\")")); codeNamespace.Imports.Add(new CodeNamespaceImport($"base = require(\"./{generationInfo.RelativePath}{generationInfo.Namespace}-base\")")); if (generationInfo.Types.Any() == true) { codeNamespace.Imports.Add(new CodeNamespaceImport($"types = require(\"./{generationInfo.Namespace}-types\")")); } CremaDataClassCreator.Create(codeNamespace, generationInfo); compileUnit.Namespaces.Add(codeNamespace); codeGenerator.GenerateCodeFromCompileUnit(compileUnit, writer, options); } return(System.Text.RegularExpressions.Regex.Replace(sb.ToString(), "^\\s+$", string.Empty, System.Text.RegularExpressions.RegexOptions.Multiline)); }
private static void CreateConstructor(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo) { var cc = new CodeConstructor(); cc.Attributes = MemberAttributes.Public; cc.Parameters.Add("reader", "IRow", "row"); cc.Parameters.Add(tableInfo.GetCodeType(), "table"); cc.BaseConstructorArgs.AddVariableReference("row"); // assign table field { var tableField = tableInfo.GetFieldExpression(); var table = new CodeVariableReferenceExpression("table"); cc.Statements.AddAssign(tableField, table); } // assign column fields { var index = 0; foreach (var item in tableInfo.Columns) { var cas = new CodeAssignStatement(); cas.Left = item.GetFieldExpression(); cas.Right = item.GetGetValueMethodExpression(index, false); if (item.IsKey == false) { var ccs = new CodeConditionStatement(item.GetHasValueMethodExpression(index, false)); ccs.TrueStatements.Add(cas); var tst = CremaDataClassCreator.CreateTryCatchStatement(classType, ccs, false); cc.Statements.Add(tst); } else { cc.Statements.Add(cas); } index++; } } // invoke setKey { var setKey = new CodeMethodReferenceExpression(thisRef, "setKey"); var invokeSetKey = new CodeMethodInvokeExpression(setKey); foreach (var item in tableInfo.Columns) { if (item.IsKey == true) { invokeSetKey.Parameters.Add(item.GetFieldExpression()); } } cc.Statements.Add(invokeSetKey); } classType.Members.Add(cc); }
private static void CreateCreateFromTableMethod(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo) { var cmm = new CodeMemberMethod(); cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static; cmm.Name = "createFromTable"; cmm.Parameters.Add("reader", "ITable", "table"); cmm.ReturnType = tableInfo.GetCodeType(); // verify hashValue { var css = CreateCompareTypeStatement(classType, tableInfo, generationInfo); var tst = CremaDataClassCreator.CreateTryCatchStatement(classType, css, generationInfo.IsDevmode); cmm.Statements.Add(css); } // create instance; { var create = new CodeObjectCreateExpression(tableInfo.GetCodeType()); cmm.Statements.AddVariableDeclaration(tableInfo.GetCodeType().BaseType, "instance", create); } // invoke readFromFile { var instance = new CodeVariableReferenceExpression("instance"); var table = new CodeVariableReferenceExpression("table"); var readFromTable = new CodeMethodReferenceExpression(instance, "readFromTable"); cmm.Statements.Add(new CodeMethodInvokeExpression(readFromTable, table)); } // return instance; { var instance = new CodeVariableReferenceExpression("instance"); cmm.Statements.AddMethodReturn(instance); } classType.Members.Add(cmm); }