private static void CreateSetChildsMethod(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo) { foreach (var item in generationInfo.GetChilds(tableInfo)) { var cmm = new CodeMemberMethod { Attributes = MemberAttributes.Public | MemberAttributes.Static, Name = tableInfo.TableName + "Set" + item.TableName }; cmm.Parameters.Add(tableInfo.GetRowCodeType(CodeType.Pointer), "target"); var arrayType = new CodeTypeReference(item.GetRowCodeType(CodeType.Pointer), 1); arrayType.SetCodeType(CodeType.Const | CodeType.Reference); var childNameType = new CodeTypeReference(typeof(string)); childNameType.SetCodeType(CodeType.Const | CodeType.Reference); cmm.Parameters.Add(childNameType, "childName"); cmm.Parameters.Add(arrayType, "childs"); { var methodRefExp = new CodeMethodReferenceExpression(tableInfo.GetRowCodeTypeExpression(CodeType.None), "SetParent"); //methodRefExp.TypeArguments.Add(tableInfo.GetRowCodeType()); //methodRefExp.TypeArguments.Add(item.GetRowCodeType()); var methodInvokeExp = new CodeMethodInvokeExpression(methodRefExp, new CodeVariableReferenceExpression("target"), new CodeVariableReferenceExpression("childs")); cmm.Statements.AddExpression(methodInvokeExp); } { var target = new CodeVariablePointerExpression("target"); var childName = new CodeVariablePointerExpression("childName"); var childs = new CodeVariableReferenceExpression("childs"); var targetField = item.GetFieldExpression(target); var targetInstance = new CodeObjectCreateExpression(item.GetCodeType(CodeType.None), childName, childs); cmm.Statements.AddAssign(targetField, targetInstance); } classType.Members.Add(cmm); } }
private static void CreateConstructor(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo) { var cc = new CodeConstructor { Attributes = MemberAttributes.Public }; cc.Parameters.Add(generationInfo.ReaderNamespace, "irow&", "row"); cc.Parameters.Add(tableInfo.GetCodeType(CodeType.Pointer), "table"); cc.AddConstructorStatement(string.Format("{0}(row)", CodeGenerationInfo.CremaRowName)); //cc.AddConstructorStatement("Table(*table)"); //foreach (var item in tableInfo.Columns) //{ // CodeBinaryOperatorExpression cbor = new CodeBinaryOperatorExpression(); // cbor.Left = new CodeVariableReferenceExpression("propertyName"); // cbor.Operator = CodeBinaryOperatorType.ValueEquality; // cbor.Right = new CodePrimitiveExpression(item.Name); // CodeConditionStatement ccs = new CodeConditionStatement(item.GetHasValueMethodExpression()); // CodeAssignStatement cas = new CodeAssignStatement(); // cas.Left = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), item.Name); // cas.Right = item.GetGetValueMethodExpression(); // ccs.TrueStatements.Add(cas); // cc.Statements.Add(ccs); //} //var query = from item in tableInfo.Columns // where item.IsKey // select item; ////method.AddStatement(string.Format("cremarow::initialize(row, iniutil::generate_hash({0}, {1}));", query.Count(), string.Join(", ", query))); //CodeMethodInvokeExpression generate_hashExp = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("iniutil"), "generate_hash"); //foreach (var item in query) //{ // CodeFieldReferenceExpression fieldExp = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), item.Name); // if (item.IsCustomType() == false) // generate_hashExp.Parameters.Add(fieldExp); // else // generate_hashExp.Parameters.Add(new CodeCastExpression(new CodeTypeReference(typeof(int)), fieldExp)); //} // assign table { var tableField = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "Table"); var tableVar = new CodeVariableReferenceExpression("table"); cc.Statements.AddAssign(tableField, tableVar); } // assign fields { int index = 0; foreach (var item in tableInfo.Columns) { var cas = new CodeAssignStatement { Left = item.GetFieldExpression(), Right = item.GetGetValueMethodExpression(index, false) }; if (item.IsKey == false) { var ccs = new CodeConditionStatement(item.GetHasValueMethodExpression(index, false)); ccs.TrueStatements.Add(cas); cc.Statements.Add(ccs); } else { cc.Statements.Add(cas); } index++; } } //CodeMethodInvokeExpression methodExp = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("cremarow"), "initialize"); //methodExp.Parameters.Add(new CodeVariableReferenceExpression("row")); //methodExp.Parameters.Add(generate_hashExp); //cc.Statements.Add(new CodeExpressionStatement(methodExp)); //var query = from item in columns // where item.IsKey // select item.Name; //method.AddStatement(string.Format("cremarow::initialize(row, iniutil::generate_hash({0}, {1}));", query.Count(), string.Join(", ", query))); foreach (var item in generationInfo.GetChilds(tableInfo)) { // check null and return defaultField { var state = new CodeConditionStatement(); var nullCompare = new CodeBinaryOperatorExpression(item.GetFieldExpression(), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null)); var condition = new CodeBinaryOperatorExpression(nullCompare, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(true)); state.Condition = condition; var codeTypeRef = new CodeTypeReferenceExpression(tableInfo.GetRowCodeType(CodeType.None)); var staticField = new CodeFieldReferenceExpression(codeTypeRef, item.GetFieldName() + "Empty"); state.TrueStatements.AddAssignReference(item.GetFieldExpression(), staticField); cc.Statements.Add(state); } // return field; //{ // var fieldExp = item.GetFieldExpression(); // cmp.GetStatements.AddMethodReturn(fieldExp, CodeType.None); //} //classType.Members.Add(cmp); } // invoke SetKey method { var methodRefExp = new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), "SetKey"); var methodInvokeExp = new CodeMethodInvokeExpression(methodRefExp); foreach (var item in tableInfo.Columns) { if (item.IsKey == true) { if (item.DataType == typeof(string).GetTypeName() == true) { methodInvokeExp.Parameters.Add(new CodeMethodInvokeExpression(item.GetFieldExpression(), "c_str")); } else { methodInvokeExp.Parameters.Add(item.GetFieldExpression()); } } } cc.Statements.Add(methodInvokeExp); } classType.Members.Add(cc); }