Example #1
0
        public IDictionary <string, string> Generate(CodeGenerationInfo generationInfo)
        {
            var codes           = new Dictionary <string, string>();
            var codeDomProvider = new CodeDom.NativeCCodeProvider();
            var options         = new CodeGeneratorOptions
            {
                BlankLinesBetweenMembers = generationInfo.BlankLinesBetweenMembers,
                BracingStyle             = "C",
                ElseOnClosing            = false,
                IndentString             = IndentedTextWriter.DefaultTabString
            };

            options["CustomGeneratorOptionStringExampleID"] = "BuildFlags: /A /B /C /D /E";

            ColumnInfoExtensions.TypeNamespace = generationInfo.Namespace;

            var cremaTypes  = GenerateTypes(codeDomProvider, options, generationInfo);
            var cremaTables = GenerateTables(codeDomProvider, options, generationInfo);

            SplitCode(cremaTypes, $"{generationInfo.Prefix}types{generationInfo.Postfix}.h", out var cremaTypesHeader, out var cremaTypesCpp);
            SplitCode(cremaTables, $"{generationInfo.Prefix}tables{generationInfo.Postfix}.h", out var cremaTablesHeader, out var cremaTablesCpp);

            codes.Add($"{generationInfo.Prefix}types{generationInfo.Postfix}.h", cremaTypesHeader);
            codes.Add($"{generationInfo.Prefix}types{generationInfo.Postfix}.cpp", cremaTypesCpp);
            codes.Add($"{generationInfo.Prefix}tables{generationInfo.Postfix}.h", cremaTablesHeader);
            codes.Add($"{generationInfo.Prefix}tables{generationInfo.Postfix}.cpp", cremaTablesCpp);

            return(codes);
        }
        private static void CreateLoadFromFile(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var cc = new CodeMemberMethod
            {
                Attributes = MemberAttributes.Public | MemberAttributes.Final,
                Name       = "Load"
            };

            var codeTypeRef = new CodeTypeReference(typeof(string));

            codeTypeRef.SetCodeType(CodeType.Reference | CodeType.Const);
            cc.Parameters.Add(codeTypeRef, "filename");
            cc.Parameters.Add(new CodeTypeReference(typeof(bool)), "verifyRevision");

            var paramExp        = new CodeVariableReferenceExpression("filename");
            var readerTypeRef   = new CodeTypeReferenceExpression(string.Join("::", generationInfo.ReaderNamespace, "CremaReader"));
            var readerInvokeExp = new CodeMethodInvokeExpression(readerTypeRef, "ReadFromFile", paramExp);

            var methodInvokeExp = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "Load", readerInvokeExp, new CodeVariableReferenceExpression("verifyRevision"));

            cc.Statements.Add(methodInvokeExp);
            //cc.ChainedConstructorArgs.Add(methodInvokeExp);
            //cc.ChainedConstructorArgs.Add(new CodeVariableReferenceExpression("verifyRevision"));

            classType.Members.Add(cc);
        }
Example #3
0
        private string GenerateReaderCpp(CodeGenerationInfo generationInfo)
        {
            var assembly     = System.Reflection.Assembly.GetExecutingAssembly();
            var resourceName = string.Join(".", assembly.GetName().Name, "code", "crema_reader.cpp");
            var code         = this.GetResourceString(resourceName);

            return(this.ChangeReadNamespace(code, generationInfo));
        }
        private static void CreateTagsField(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var cmf = new CodeMemberField
            {
                Attributes = MemberAttributes.Private,
                Name       = "_tags",
                Type       = new CodeTypeReference(typeof(string))
            };

            classType.Members.Add(cmf);
        }
Example #5
0
        private IDictionary <string, string> GenerateBases(CodeGenerationInfo generationInfo)
        {
            var codes    = new Dictionary <string, string>();
            var assembly = System.Reflection.Assembly.GetExecutingAssembly();

            codes.Add("crema_base.h", this.GenerateBaseHeader(generationInfo));
            codes.Add("crema_base.cpp", this.GenerateBaseCpp(generationInfo));
            codes.Add("crema_reader.h", this.GenerateReaderHeader(generationInfo));
            codes.Add("crema_reader.cpp", this.GenerateReaderCpp(generationInfo));

            return(codes);
        }
 private static void CreateFieldsTable(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
 {
     foreach (var item in generationInfo.GetTables(true))
     {
         var cmf = new CodeMemberField
         {
             Attributes = MemberAttributes.Public,
             Name       = item.Name,
             Type       = item.GetCodeType(CodeType.Pointer | CodeType.Const)
         };
         classType.Members.Add(cmf);
     }
 }
Example #7
0
        private string ChangeBaseNamespace(string text, CodeGenerationInfo generationInfo)
        {
            var segments       = generationInfo.BaseNamespace.Split(new string[] { "::", }, StringSplitOptions.RemoveEmptyEntries);
            var firstNamespace = string.Join(" { ", segments.Select(item => "namespace " + item));
            var lastNamespace  = string.Join(" ", segments.Select(item => "} /*namespace " + item + "*/"));

            var first = "namespace CremaCode";
            var last  = "} /*namespace CremaCode*/";

            text = text.Replace(last, lastNamespace);
            text = text.Replace(first, firstNamespace);
            return(text);
        }
        private static void CreateTagsMethod(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var cmm = new CodeMemberMethod
            {
                Attributes = MemberAttributes.Public | MemberAttributes.Final,
                Name       = "tags",
                ReturnType = new CodeTypeReference(typeof(string))
            };

            cmm.Statements.AddMethodReturn(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "_tags"));

            classType.Members.Add(cmm);
        }
        private static void CreateDestructor(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var cc = new CodeDestructor
            {
                Attributes = MemberAttributes.Public | MemberAttributes.Override
            };

            foreach (var item in generationInfo.GetTables())
            {
                var field  = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), item.Name);
                var delete = new CodeObjectDeleteExpression(field);

                cc.Statements.Add(delete);
            }

            classType.Members.Add(cc);
        }
Example #10
0
        public static void Create(CodeNamespace codeNamespace, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            var classType = new CodeTypeDeclaration();

            codeNamespace.Types.Add(classType);

            classType.IsClass = true;
            classType.Name    = tableInfo.GetRowClassName();
            classType.BaseTypes.Add(generationInfo.BaseNamespace, "CremaRow");

            CreateColumnFields(classType, tableInfo, generationInfo);
            CreateTableField(classType, tableInfo);
            CreateChildFields(classType, tableInfo, generationInfo);
            CreateStaticChildEmptyFields(classType, tableInfo, generationInfo);
            CreateParentField(classType, tableInfo);
            CreateConstructor(classType, tableInfo, generationInfo);
            CreateSetChildsMethod(classType, tableInfo, generationInfo);
        }
        public static void Create(CodeNamespace codeNamespace, CodeGenerationInfo generationInfo)
        {
            codeNamespace.Comments.Add(new CodeCommentStatement($"------------------------------------------------------------------------------"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"dataBase: {generationInfo.DataBaseName}"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"revision: {generationInfo.Revision}"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"requested revision: {generationInfo.RequestedRevision}"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"hash value: {generationInfo.TypesHashValue}"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"tags: {generationInfo.Tags}"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"------------------------------------------------------------------------------"));

            foreach (var item in generationInfo.Types)
            {
                CreateDataType(codeNamespace, item, generationInfo);
            }

            //if (NoCpp == false)
            //    codeNamespace.AddHeaderStatement("void register_enums();");
        }
Example #12
0
        public void Generate(string outputPath, GenerationSet metaData, CodeGenerationSettings settings)
        {
            var path           = PathUtility.GetFullPath(outputPath);
            var generationInfo = new CodeGenerationInfo(metaData, settings)
            {
                RelativePath = string.Empty,
            };

            if (settings.BasePath != string.Empty)
            {
                var relativePath = UriUtility.MakeRelativeOfDirectory(path, DirectoryUtility.GetAbsolutePath(path, settings.BasePath));
                generationInfo.RelativePath = relativePath + "/";
            }

            {
                var codes    = this.Generate(generationInfo);
                var dirInfo  = new DirectoryInfo(path);
                var rootPath = generationInfo.Namespace.Replace('.', Path.DirectorySeparatorChar);
                foreach (var item in codes)
                {
                    var ext      = Path.GetExtension(item.Key);
                    var filename = FileUtility.RemoveExtension(item.Key).Replace('.', Path.DirectorySeparatorChar) + ext;
                    filename = Path.Combine(dirInfo.FullName, filename);
                    FileUtility.Prepare(filename);

                    using var sw = new StreamWriter(filename, false, Encoding.UTF8);
                    sw.WriteLine(item.Value);
                    this.PrintResult(filename);
                }
            }

            if (settings.Options.HasFlag(CodeGenerationOptions.OmitBaseCode) == false)
            {
                var codes     = this.GenerateBases(generationInfo);
                var codesPath = DirectoryUtility.GetAbsolutePath(path, settings.BasePath);
                foreach (var item in codes)
                {
                    var codePath = FileUtility.Prepare(codesPath, item.Key);
                    using var writer = new StreamWriter(codePath, false, Encoding.UTF8);
                    writer.WriteLine(item.Value);
                    this.PrintResult(codePath);
                }
            }
        }
        private static void CreateConstructor(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var cc = new CodeConstructor
            {
                Attributes = MemberAttributes.Public
            };

            cc.Parameters.Add(generationInfo.ReaderNamespace, "idataset&", "dataSet");
            cc.Parameters.Add(new CodeTypeReference(typeof(bool)), "verifyRevision");

            foreach (var item in generationInfo.GetTables(true))
            {
                var field = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), item.Name);
                cc.Statements.AddAssign(field, new CodePrimitiveExpression(null));
            }

            var methodInvokeExp = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "Load", new CodeVariableReferenceExpression("dataSet"), new CodeVariableReferenceExpression("verifyRevision"));

            cc.Statements.Add(methodInvokeExp);

            classType.Members.Add(cc);
        }
        public static void Create(CodeNamespace codeNamespace, CodeGenerationInfo generationInfo)
        {
            codeNamespace.Comments.Add(new CodeCommentStatement($"------------------------------------------------------------------------------"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"dataBase: {generationInfo.DataBaseName}"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"revision: {generationInfo.Revision}"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"requested revision: {generationInfo.RequestedRevision}"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"hash value: {generationInfo.TablesHashValue}"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"tags: {generationInfo.Tags}"));
            codeNamespace.Comments.Add(new CodeCommentStatement($"------------------------------------------------------------------------------"));

            foreach (var item in generationInfo.GetTables())
            {
                CremaTableClassCreator.Create(codeNamespace, item, generationInfo);
            }

            var classType = new CodeTypeDeclaration();

            codeNamespace.Types.Add(classType);
            classType.Name    = generationInfo.ClassName;
            classType.IsClass = true;
            classType.BaseTypes.Add(generationInfo.BaseNamespace, "CremaData");

            CreateNameField(classType, generationInfo);
            CreateRevisionField(classType, generationInfo);
            CreateTypesHashValueField(classType, generationInfo);
            CreateTablesHashValueField(classType, generationInfo);
            CreateTagsField(classType, generationInfo);
            CreateNameMethod(classType, generationInfo);
            CreateRevisionMethod(classType, generationInfo);
            CreateTypesHashValueMethod(classType, generationInfo);
            CreateTablesHashValueMethod(classType, generationInfo);
            CreateTagsMethod(classType, generationInfo);
            CreateFieldsTable(classType, generationInfo);
            CreateConstructor(classType, generationInfo);
            CreateConstructorFromFile(classType, generationInfo);
            CreateLoadFromFile(classType, generationInfo);
            CreateLoad(classType, generationInfo);
            CreateDestructor(classType, generationInfo);
        }
Example #15
0
        private static void CreateStaticChildEmptyFields(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            foreach (var item in generationInfo.GetChilds(tableInfo))
            {
                var cmf = new CodeMemberField
                {
                    Attributes = MemberAttributes.Private | MemberAttributes.Static,
                    Name       = item.GetFieldName() + "Empty",
                    Type       = item.GetCodeType(CodeType.None)
                };
                //cmf.InitExpression = new CodeObjectCreateExpression(item.GetCodeType(CodeType.None));

                classType.Members.Add(cmf);
            }
        }
Example #16
0
        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);
            }
        }
Example #17
0
        private static void CreateColumnFields(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            foreach (var item in tableInfo.Columns)
            {
                var cmf = new CodeMemberField
                {
                    Attributes = MemberAttributes.Public,
                    Name       = item.Name,
                    Type       = item.GetCodeType(CodeType.None),

                    InitExpression = item.GetInitExpression()
                };

                if (generationInfo.NoComment == false)
                {
                    cmf.Comments.AddSummary(item.Comment);
                }
                if (generationInfo.NoChanges == false)
                {
                    cmf.Comments.Add(CremaSchema.Creator, item.CreationInfo.ID);
                    cmf.Comments.Add(CremaSchema.CreatedDateTime, item.CreationInfo.DateTime);
                    cmf.Comments.Add(CremaSchema.Modifier, item.ModificationInfo.ID);
                    cmf.Comments.Add(CremaSchema.ModifiedDateTime, item.ModificationInfo.DateTime);
                }
                classType.Members.Add(cmf);
            }
        }
        public static CodeStatement CreateTryCatchStatement(CodeTypeDeclaration classType, CodeStatement statement, CodeGenerationInfo generationInfo)
        {
            var tryStatement = new CodeTryCatchFinallyStatement();

            tryStatement.TryStatements.Add(statement);

            var catchClause     = new CodeCatchClause("e", new CodeTypeReference("std::exception&"));
            var methodRefExp    = new CodeMethodReferenceExpression(new CodeTypeReferenceExpression($"{generationInfo.BaseNamespace}.CremaData"), "InvokeErrorOccured");
            var methodInvokeExp = new CodeMethodInvokeExpression(methodRefExp, new CodeVariableReferenceExpression("e"));

            var conditionStatement = new CodeConditionStatement
            {
                Condition = new CodeBinaryOperatorExpression(methodInvokeExp, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(false))
            };

            conditionStatement.TrueStatements.Add(new CodeThrowExceptionStatement(new CodeVariableReferenceExpression("e")));

            catchClause.Statements.Add(conditionStatement);
            tryStatement.CatchClauses.Add(catchClause);

            return(tryStatement);
        }
        private static CodeStatement CreateCompareRevisionStatement(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var ccs = new CodeConditionStatement();

            var left1  = new CodeVariableReferenceExpression("verifyRevision");
            var right1 = new CodePrimitiveExpression(true);
            var left   = new CodeBinaryOperatorExpression(left1, CodeBinaryOperatorType.IdentityEquality, right1);

            var left2  = new CodeCastExpression(typeof(string), new CodePrimitiveExpression(generationInfo.Revision));
            var right2 = new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("dataSet"), "revision()");
            var right  = new CodeBinaryOperatorExpression(left2, CodeBinaryOperatorType.IdentityInequality, right2);

            ccs.Condition = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.BooleanAnd, right);

            var message   = string.Format("데이터의 리비전 코드 리비전({0})과 다릅니다.", generationInfo.Revision);
            var exception = new CodeMethodInvokeExpression(null, "std::logic_error", new CodePrimitiveExpression(message));

            ccs.TrueStatements.Add(new CodeThrowExceptionStatement(exception));

            return(ccs);
        }
        private static CodeStatement CreateCompareTablesHashValueStatement(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var ccs = new CodeConditionStatement();

            var left  = new CodePrimitiveExpression(generationInfo.TablesHashValue);
            var right = new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("dataSet"), "tables_hash_value()");

            ccs.Condition = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.IdentityInequality, right);

            var message   = "테이블 해시값이 잘못되었습니다.";
            var exception = new CodeMethodInvokeExpression(null, "std::logic_error", new CodePrimitiveExpression(message));

            ccs.TrueStatements.Add(new CodeThrowExceptionStatement(exception));

            return(ccs);
        }
        private static void CreateDataType(CodeNamespace codeNamespace, TypeInfo dataTypeInfo, CodeGenerationInfo generationInfo)
        {
            var classType = new CodeTypeDeclaration(dataTypeInfo.Name)
            {
                Attributes = MemberAttributes.Public,
                IsEnum     = true
            };

            if (dataTypeInfo.IsFlag == true)
            {
                var cad = new CodeAttributeDeclaration(new CodeTypeReference(typeof(FlagsAttribute)));

                classType.CustomAttributes.Add(cad);
            }

            if (generationInfo.NoComment == false)
            {
                classType.Comments.AddSummary(dataTypeInfo.Comment);
            }

            if (generationInfo.NoChanges == false)
            {
                classType.Comments.Add(CremaSchema.Creator, dataTypeInfo.CreationInfo.ID);
                classType.Comments.Add(CremaSchema.CreatedDateTime, dataTypeInfo.CreationInfo.DateTime);
                classType.Comments.Add(CremaSchema.Modifier, dataTypeInfo.ModificationInfo.ID);
                classType.Comments.Add(CremaSchema.ModifiedDateTime, dataTypeInfo.ModificationInfo.DateTime);
            }

            foreach (var item in dataTypeInfo.Members)
            {
                CreateDataMember(classType, item, generationInfo);
            }
            //CreateEnumRegisterMethod(enum1, isFlag);
            codeNamespace.Types.Add(classType);
        }
        private static void CreateDataMember(CodeTypeDeclaration classType, TypeMemberInfo typeMemberInfo, CodeGenerationInfo generationInfo)
        {
            var cmm = new CodeMemberField
            {
                Name           = typeMemberInfo.Name,
                InitExpression = new CodeSnippetExpression(generationInfo.EnumFomrat(typeMemberInfo.Value))
            };

            if (generationInfo.NoComment == false)
            {
                cmm.Comments.AddSummary(typeMemberInfo.Comment);
            }
            if (generationInfo.NoChanges == false)
            {
                cmm.Comments.Add(CremaSchema.Creator, typeMemberInfo.CreationInfo.ID);
                cmm.Comments.Add(CremaSchema.CreatedDateTime, typeMemberInfo.CreationInfo.DateTime);
                cmm.Comments.Add(CremaSchema.Modifier, typeMemberInfo.ModificationInfo.ID);
                cmm.Comments.Add(CremaSchema.ModifiedDateTime, typeMemberInfo.ModificationInfo.DateTime);
            }

            classType.Members.Add(cmm);
        }
Example #23
0
        private string GenerateTables(CodeDomProvider codeDomProvider, CodeGeneratorOptions options, CodeGenerationInfo generationInfo)
        {
            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                var codeGenerator = codeDomProvider.CreateGenerator(sw);
                var compileUnit   = new CodeCompileUnit();

                compileUnit.AddCustomInclude($"{generationInfo.RelativePath}crema_reader");
                compileUnit.AddCustomInclude($"{generationInfo.Prefix}types{generationInfo.Postfix}");
                compileUnit.AddCustomInclude($"{generationInfo.RelativePath}crema_base");

                var codeNamespace = new CodeNamespace(generationInfo.Namespace);
                codeNamespace.Imports.Add(new CodeNamespaceImport(generationInfo.ReaderNamespace));

                CremaDataClassCreator.Create(codeNamespace, generationInfo);

                compileUnit.Namespaces.Add(codeNamespace);

                codeGenerator.GenerateCodeFromCompileUnit(compileUnit, sw, options);
            }

            return(sb.ToString());
        }
        private static void CreateLoad(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var cc = new CodeMemberMethod
            {
                Attributes = MemberAttributes.Public | MemberAttributes.Final,
                Name       = "Load"
            };

            cc.Parameters.Add(generationInfo.ReaderNamespace, "idataset&", "dataSet");
            cc.Parameters.Add(new CodeTypeReference(typeof(bool)), "verifyRevision");

            var dataSet = new CodeVariableReferenceExpression("dataSet");

            {
                var ccs = CreateCompareDataBaseStatement(classType, generationInfo);
                var tst = CreateTryCatchStatement(classType, ccs, generationInfo);
                cc.Statements.Add(tst);
            }

            {
                var ccs = CreateCompareRevisionStatement(classType, generationInfo);
                var tst = CreateTryCatchStatement(classType, ccs, generationInfo);
                cc.Statements.Add(tst);
            }

            {
                var ccs = CreateCompareTypesHashValueStatement(classType, generationInfo);
                var tst = CreateTryCatchStatement(classType, ccs, generationInfo);
                cc.Statements.Add(tst);
            }

            {
                var ccs = CreateCompareTablesHashValueStatement(classType, generationInfo);
                var tst = CreateTryCatchStatement(classType, ccs, generationInfo);
                cc.Statements.Add(tst);
            }

            {
                var field    = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_name");
                var property = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(dataSet, "name"));
                cc.Statements.AddAssign(field, property);
            }

            {
                var field    = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_revision");
                var property = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(dataSet, "revision"));
                cc.Statements.AddAssign(field, property);
            }

            {
                var field    = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_typesHashValue");
                var property = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(dataSet, "types_hash_value"));
                cc.Statements.AddAssign(field, property);
            }

            {
                var field    = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_tablesHashValue");
                var property = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(dataSet, "tables_hash_value"));
                cc.Statements.AddAssign(field, property);
            }

            {
                var field    = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_tags");
                var property = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(dataSet, "tags"));
                cc.Statements.AddAssign(field, property);
            }

            foreach (var item in generationInfo.GetTables(true))
            {
                var field       = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), item.Name);
                var operatorExp = new CodeBinaryOperatorExpression(field, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null));
                var ccs         = new CodeConditionStatement(operatorExp);
                var delete      = new CodeObjectDeleteExpression(field);
                ccs.TrueStatements.Add(delete);
                cc.Statements.Add(ccs);

                var tableName  = new CodePrimitiveExpression(item.Name);
                var tablesProp = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(dataSet, "tables"));
                var table      = new CodeIndexerExpression(tablesProp, tableName);

                //var field = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), item.Name);
                var instance = new CodeObjectCreateExpression(item.GetCodeType(generationInfo.Namespace, CodeType.None), table);

                cc.Statements.AddAssign(field, instance);
            }

            classType.Members.Add(cc);
        }
Example #25
0
        private string GenerateTypes(CodeDomProvider codeDomProvider, CodeGeneratorOptions options, CodeGenerationInfo generationInfo)
        {
            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                var codeGenerator = codeDomProvider.CreateGenerator(sw);
                var compileUnit   = new CodeCompileUnit();

                var codeNamespace = new CodeNamespace(generationInfo.Namespace);
                codeNamespace.Imports.Add(new CodeNamespaceImport(generationInfo.ReaderNamespace));

                CremaTypeEnumCreator.Create(codeNamespace, generationInfo);

                compileUnit.Namespaces.Add(codeNamespace);

                codeGenerator.GenerateCodeFromCompileUnit(compileUnit, sw, options);
            }

            return(sb.ToString());
        }
Example #26
0
        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);
        }
        private static CodeStatement CreateCompareDataBaseStatement(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var ccs = new CodeConditionStatement();

            var left  = new CodePrimitiveExpression(generationInfo.DataBaseName);
            var right = new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("dataSet"), "name()");

            ccs.Condition = new CodeBinaryOperatorExpression(left, CodeBinaryOperatorType.IdentityInequality, right);

            var message   = string.Format("데이터의 이름이 코드 이름({0})과 다릅니다.", generationInfo.DataBaseName);
            var exception = new CodeMethodInvokeExpression(null, "std::logic_error", new CodePrimitiveExpression(message));

            ccs.TrueStatements.Add(new CodeThrowExceptionStatement(exception));

            return(ccs);
        }