예제 #1
0
        public void Compile(string outputPath, GenerationSet metaData, CodeGenerationSettings settings, string target)
        {
            var generationInfo = new CodeGenerationInfo(outputPath, metaData, settings);

            if (settings.Options.HasFlag(CodeGenerationOptions.OmitCode) == false && settings.Options.HasFlag(CodeGenerationOptions.OmitBaseCode) == false)
            {
                var filename = this.CompileAll(outputPath, generationInfo, target);
                this.PrintResult(filename);
            }
            else if (settings.Options.HasFlag(CodeGenerationOptions.OmitCode) == true)
            {
                var filename = this.CompileBase(outputPath, generationInfo, target);
                this.PrintResult(filename);
            }
            else if (settings.Options.HasFlag(CodeGenerationOptions.OmitBaseCode) == true)
            {
                var tempPath = PathUtility.GetTempPath(true);
                try
                {
                    var readerPath = this.CompileBase(tempPath, generationInfo, target);
                    var filename   = this.Compile(outputPath, readerPath, generationInfo, target);
                    this.PrintResult(filename);
                }
                finally
                {
                    DirectoryUtility.Delete(tempPath);
                }
            }
        }
예제 #2
0
        public static void Create(CodeNamespace codeNamespace, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            var classType = new CodeTypeDeclaration()
            {
                Attributes = MemberAttributes.Public,
                Name       = tableInfo.GetRowClassName(),
                IsClass    = true
            };

            classType.TypeAttributes |= TypeAttributes.Sealed;
            classType.BaseTypes.Add(generationInfo.BaseNamespace, "CremaRow");

            CreateChildStaticEmptyFields(classType, tableInfo, generationInfo);
            CreateConstructor(classType, tableInfo, generationInfo);
            CreateTableField(classType, tableInfo);
            CreateColumnFields(classType, tableInfo, generationInfo);
            CreateChildFields(classType, tableInfo, generationInfo);

            CreateSetChildsMethod(classType, tableInfo, generationInfo);
            CreateColumnProperties(classType, tableInfo, generationInfo);
            CreateChildProperties(classType, tableInfo, generationInfo);
            CreateTableProperty(classType, tableInfo);
            CreateParentProperty(classType, tableInfo);

            codeNamespace.Types.Add(classType);
        }
예제 #3
0
        private static CodeTypeDeclaration CreateCore(TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            var classType = new CodeTypeDeclaration()
            {
                Attributes = MemberAttributes.Public,
                Name       = tableInfo.GetClassName(),
                IsClass    = true
            };

            classType.TypeAttributes |= TypeAttributes.Sealed;
            classType.BaseTypes.Add(generationInfo.BaseNamespace, "CremaTable", tableInfo.GetRowCodeType());
            if (generationInfo.OmitComment == false)
            {
                classType.Comments.AddSummary(tableInfo.Comment);
            }
            if (generationInfo.OmitSignatureDate == false)
            {
                classType.Comments.Add(CremaSchema.Creator, tableInfo.CreationInfo.ID);
                classType.Comments.Add(CremaSchema.CreatedDateTime, tableInfo.CreationInfo.DateTime);
                classType.Comments.Add(CremaSchema.Modifier, tableInfo.ModificationInfo.ID);
                classType.Comments.Add(CremaSchema.ModifiedDateTime, tableInfo.ModificationInfo.DateTime);
                classType.Comments.Add(CremaSchema.ContentsModifier, tableInfo.ContentsInfo.ID);
                classType.Comments.Add(CremaSchema.ContentsModifiedDateTime, tableInfo.ContentsInfo.DateTime);
            }

            CreateChildFields(classType, tableInfo, generationInfo);
            CreateChildProperties(classType, tableInfo, generationInfo);
            CreateConstructor(classType, tableInfo, generationInfo);
            CreateConstructorFromTable(classType, tableInfo, generationInfo);
            CreateConstructorFromRows(classType, tableInfo);
            CreateFindMethod(classType, tableInfo, generationInfo);
            CreateCreateRowInstanceMethod(classType, tableInfo, generationInfo);

            return(classType);
        }
예제 #4
0
        private static void CreateTagsField(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var cmf = new CodeMemberField();

            cmf.Attributes = MemberAttributes.Private;
            cmf.Name       = "_tags";
            cmf.Type       = new CodeTypeReference(typeof(string));

            classType.Members.Add(cmf);
        }
예제 #5
0
        private string GenerateBase(CodeGenerationInfo generationInfo)
        {
            var assembly     = System.Reflection.Assembly.GetExecutingAssembly();
            var resourceName = string.Join(".", this.GetType().Namespace, "Code", "CremaBase.cs");
            var code         = this.GetResourceString(resourceName);

            code = code.Replace("namespace Ntreev.Crema.Code", "namespace " + generationInfo.BaseNamespace);
            code = code.Replace("Ntreev.Crema.Reader", generationInfo.ReaderNamespace);
            return(code);
        }
예제 #6
0
        private static void CreateTagsProperty(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var cmm = new CodeMemberProperty();

            cmm.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            cmm.Name       = "Tags";
            cmm.HasGet     = true;
            cmm.Type       = new CodeTypeReference(typeof(string));
            cmm.GetStatements.AddMethodReturn(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "_tags"));

            classType.Members.Add(cmm);
        }
예제 #7
0
        public static void Create(CodeNamespace codeNamespace, CodeGenerationInfo generationInfo)
        {
            foreach (var item in generationInfo.Tables)
            {
                if (item.TemplatedParent != string.Empty)
                {
                    continue;
                }

                Create(codeNamespace, item, generationInfo);
            }
        }
예제 #8
0
 private static void CreateTableFields(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
 {
     foreach (var item in generationInfo.GetTables(true))
     {
         var cmf = new CodeMemberField()
         {
             Attributes = MemberAttributes.Private,
             Name       = item.GetFieldName(),
             Type       = item.GetCodeType()
         };
         classType.Members.Add(cmf);
     }
 }
예제 #9
0
        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($"devmode: {generationInfo.IsDevmode}"));
            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)
            {
                CreateType(codeNamespace, item, generationInfo);
            }
        }
예제 #10
0
        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($"devmode: {generationInfo.IsDevmode}"));
            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()
            {
                Attributes = MemberAttributes.Public,
                Name       = generationInfo.ClassName,
                IsClass    = true
            };

            classType.TypeAttributes |= TypeAttributes.Sealed;
            classType.BaseTypes.Add(generationInfo.BaseNamespace, "CremaData");

            CreateNameField(classType, generationInfo);
            CreateRevisionField(classType, generationInfo);
            CreateTypesHashValueField(classType, generationInfo);
            CreateTablesHashValueField(classType, generationInfo);
            CreateTagsField(classType, generationInfo);
            CreateNameProperty(classType, generationInfo);
            CreateRevisionProperty(classType, generationInfo);
            CreateTypesHashValueProperty(classType, generationInfo);
            CreateTablesHashValueProperty(classType, generationInfo);
            CreateTagsProperty(classType, generationInfo);
            CreateTableFields(classType, generationInfo);
            CreateConstructor(classType, generationInfo);
            CreateConstructorFromFile(classType, generationInfo);
            CreateConstructorFromStream(classType, generationInfo);
            CreateLoadFromFile(classType, generationInfo);
            CreateLoadStream(classType, generationInfo);
            CreateLoad(classType, generationInfo);
            CreateTableProperties(classType, generationInfo);

            codeNamespace.Types.Add(classType);
        }
예제 #11
0
        private IDictionary <string, string> GenerateCodes(CodeGenerationInfo generationInfo)
        {
            var codes = new Dictionary <string, string>();

            var cremaTypes  = this.GenerateTypeCode(generationInfo);
            var cremaTables = this.GenerateTableCode(generationInfo);

            codes.Add($"{generationInfo.Prefix}Types{generationInfo.Postfix}.cs", cremaTypes);
            codes.Add($"{generationInfo.Prefix}Tables{generationInfo.Postfix}.cs", cremaTables);

            foreach (var item in codes.ToArray())
            {
                codes[item.Key] = item.Value.Replace("Ntreev.Crema.Reader", generationInfo.ReaderNamespace);
            }

            return(codes);
        }
예제 #12
0
        public static void Create(CodeNamespace codeNamespace, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            foreach (var item in generationInfo.GetChilds(tableInfo))
            {
                if (item.TemplatedParent != string.Empty)
                {
                    continue;
                }

                Create(codeNamespace, item, generationInfo);
            }

            CremaRowClassCreator.Create(codeNamespace, tableInfo, generationInfo);
            var classType = CreateCore(tableInfo, generationInfo);

            codeNamespace.Types.Add(classType);
        }
예제 #13
0
        private static void CreateTableProperties(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            foreach (var item in generationInfo.GetTables(true))
            {
                var cmp = new CodeMemberProperty()
                {
                    Attributes = MemberAttributes.Public | MemberAttributes.Final,
                    Name       = item.GetPropertyName(),
                    Type       = item.GetCodeType(),
                    HasGet     = true,
                    HasSet     = false
                };
                cmp.Comments.AddSummary(item.Comment);
                cmp.GetStatements.AddMethodReturn(item.GetFieldExpression());

                classType.Members.Add(cmp);
            }
        }
예제 #14
0
        private string GenerateTypeCode(CodeGenerationInfo generationInfo)
        {
            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                var codeDomProvider = new CSharpCodeProvider();
                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, this.options);
            }

            return(sb.ToString());
        }
예제 #15
0
        private string CompileAll(string outputPath, CodeGenerationInfo generationInfo, string target)
        {
            var sourceList = new List <string>();

            sourceList.AddRange(this.GenerateCodes(generationInfo).Values);
            sourceList.AddRange(this.GenerateBaseCodes(generationInfo).Values);

            sourceList.Add(this.GenerateAssemblyInfo(generationInfo.Namespace));

            var providerOptions = new Dictionary <string, string>
            {
                { "CompilerVersion", target == string.Empty ? "v3.5" : target }
            };
            var codeDomProvider = new CSharpCodeProvider(providerOptions);

            var cp = new CompilerParameters()
            {
                OutputAssembly     = Path.Combine(outputPath, generationInfo.Namespace + ".dll"),
                CompilerOptions    = "/optimize",
                GenerateExecutable = false,
                GenerateInMemory   = false
            };

            cp.ReferencedAssemblies.Add("System.dll");
            cp.ReferencedAssemblies.Add("System.Core.dll");
            cp.ReferencedAssemblies.Add("System.Data.dll");
            cp.ReferencedAssemblies.Add("System.Xml.dll");

            FileUtility.Prepare(cp.OutputAssembly);

            var result = codeDomProvider.CompileAssemblyFromSource(cp, sourceList.ToArray());

            if (result.Errors.HasErrors)
            {
                foreach (var item in result.Errors)
                {
                    Trace.WriteLine(item);
                }

                throw new Exception("에러가 발생했습니다.");
            }

            return(cp.OutputAssembly);
        }
예제 #16
0
        private static void CreateConstructor(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var cc = new CodeConstructor()
            {
                Attributes = MemberAttributes.Public
            };

            cc.Parameters.Add("IDataSet", "dataSet");
            cc.Parameters.Add(new CodeTypeReference(typeof(bool)), "verifyRevision");

            //cc.BaseConstructorArgs.Add(new CodePrimitiveExpression(generationInfo.DataBaseName));
            //cc.BaseConstructorArgs.Add(new CodePrimitiveExpression(generationInfo.Revision));

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

            cc.Statements.Add(methodInvokeExp);

            classType.Members.Add(cc);
        }
예제 #17
0
        private static void CreateConstructorFromFile(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var cc = new CodeConstructor()
            {
                Attributes = MemberAttributes.Public
            };

            cc.Parameters.Add(typeof(string), "filename");
            cc.Parameters.Add(new CodeTypeReference(typeof(bool)), "verifyRevision");

            var readerRef       = new CodeTypeReferenceExpression(string.Join(".", generationInfo.ReaderNamespace, "CremaReader"));
            var paramExp        = new CodeVariableReferenceExpression("filename");
            var methodInvokeExp = new CodeMethodInvokeExpression(readerRef, "Read", paramExp);

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

            classType.Members.Add(cc);
        }
예제 #18
0
        private static void CreateLoadStream(CodeTypeDeclaration classType, CodeGenerationInfo generationInfo)
        {
            var cc = new CodeMemberMethod()
            {
                Attributes = MemberAttributes.Public | MemberAttributes.Final,
                Name       = "Load",
            };

            cc.Parameters.Add(typeof(Stream), "stream");
            cc.Parameters.Add(typeof(bool), "verifyRevision");

            var readerRef = new CodeTypeReferenceExpression(string.Join(".", generationInfo.ReaderNamespace, "CremaReader"));
            var paramExp  = new CodeVariableReferenceExpression("stream");
            var readExp   = new CodeMethodInvokeExpression(readerRef, "Read", paramExp);

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

            cc.Statements.Add(methodInvokeExp);

            classType.Members.Add(cc);
        }
예제 #19
0
        private IDictionary <string, string> GenerateBaseCodes(CodeGenerationInfo generationInfo)
        {
            var codes = new Dictionary <string, string>();

            var cremaBase = this.GenerateBase(generationInfo);

            codes.Add($"{generationInfo.RelativePath}CremaBase.cs", cremaBase);

            var assembly = System.Reflection.Assembly.GetExecutingAssembly();
            var ns       = string.Join(".", this.GetType().Namespace, "Reader");
            var files    = assembly.GetManifestResourceNames().Where(item => item.StartsWith(ns));

            foreach (var item in files)
            {
                var value = this.GetResourceString(item);
                var key   = Regex.Replace(item, "(^" + this.GetType().Namespace + @"[.])(\S+)[.]cs", "$2").Replace('.', Path.DirectorySeparatorChar);
                value = value.Replace("Ntreev.Crema.Reader", generationInfo.ReaderNamespace);
                codes.Add($"{generationInfo.RelativePath}{key}.cs", value);
            }

            return(codes);
        }
예제 #20
0
        public void Generate(string outputPath, GenerationSet metaData, CodeGenerationSettings settings)
        {
            var generationInfo = new CodeGenerationInfo(outputPath, metaData, settings);

            if (settings.Options.HasFlag(CodeGenerationOptions.OmitCode) == false)
            {
                var codes = this.GenerateCodes(generationInfo);
                foreach (var item in codes)
                {
                    var codePath = FileUtility.WriteAllText(item.Value, Encoding.UTF8, outputPath, item.Key);
                    this.PrintResult(codePath);
                }
            }

            if (settings.Options.HasFlag(CodeGenerationOptions.OmitBaseCode) == false)
            {
                var codes = this.GenerateBaseCodes(generationInfo);
                foreach (var item in codes)
                {
                    var codePath = FileUtility.WriteAllText(item.Value, Encoding.UTF8, outputPath, item.Key);
                    this.PrintResult(codePath);
                }
            }
        }
예제 #21
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(), "table");
            cc.BaseConstructorArgs.Add("table.Columns");
            cc.BaseConstructorArgs.Add("row");

            // assign table field
            {
                var leftExp  = tableInfo.GetFieldExpression();
                var rightExp = new CodeVariableReferenceExpression("table");
                cc.Statements.AddAssign(leftExp, rightExp);
            }

            // assign fields
            {
                var 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++;
                }
            }

            classType.Members.Add(cc);
        }
예제 #22
0
        private static void CreateChildProperties(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            foreach (var item in generationInfo.GetChilds(tableInfo))
            {
                var cmp = new CodeMemberProperty()
                {
                    Attributes = MemberAttributes.Public | MemberAttributes.Final,
                    Name       = item.GetPropertyName(),
                    Type       = item.GetCodeType()
                };
                if (generationInfo.OmitComment == false)
                {
                    cmp.Comments.AddSummary(item.Comment);
                }
                if (generationInfo.OmitSignatureDate == false)
                {
                    cmp.Comments.Add(CremaSchema.Creator, item.CreationInfo.ID);
                    cmp.Comments.Add(CremaSchema.CreatedDateTime, item.CreationInfo.DateTime);
                    cmp.Comments.Add(CremaSchema.Modifier, item.ModificationInfo.ID);
                    cmp.Comments.Add(CremaSchema.ModifiedDateTime, item.ModificationInfo.DateTime);
                }
                cmp.HasGet = true;
                cmp.HasSet = false;

                {
                    var state   = new CodeConditionStatement();
                    var testExp = new CodeBinaryOperatorExpression(item.GetFieldExpression(), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null));
                    state.Condition = testExp;

                    var staticRefExp = new CodeTypeReferenceExpression(tableInfo.GetRowCodeType());
                    var fieldExp     = new CodeFieldReferenceExpression(staticRefExp, item.GetFieldName() + "Empty");
                    state.TrueStatements.AddMethodReturn(fieldExp);

                    cmp.GetStatements.Add(state);
                }

                {
                    var fieldExp = item.GetFieldExpression();
                    cmp.GetStatements.AddMethodReturn(fieldExp);
                }

                classType.Members.Add(cmp);
            }
        }
예제 #23
0
        private static void CreateColumnProperties(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            foreach (var item in tableInfo.Columns)
            {
                var cmp = new CodeMemberProperty()
                {
                    Attributes = MemberAttributes.Public | MemberAttributes.Final,
                    Name       = item.Name,
                    Type       = item.GetCodeType()
                };
                if (generationInfo.OmitComment == false)
                {
                    cmp.Comments.AddSummary(item.Comment);
                }
                if (generationInfo.OmitSignatureDate == false)
                {
                    cmp.Comments.Add(CremaSchema.Creator, item.CreationInfo.ID);
                    cmp.Comments.Add(CremaSchema.CreatedDateTime, item.CreationInfo.DateTime);
                    cmp.Comments.Add(CremaSchema.Modifier, item.ModificationInfo.ID);
                    cmp.Comments.Add(CremaSchema.ModifiedDateTime, item.ModificationInfo.DateTime);
                }
                cmp.HasGet = true;
                cmp.HasSet = false;
                cmp.GetStatements.AddMethodReturn(item.GetFieldExpression());

                classType.Members.Add(cmp);
            }
        }
예제 #24
0
 private static void CreateChildStaticEmptyFields(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(),
             InitExpression = new CodeObjectCreateExpression(item.GetCodeType())
         };
         classType.Members.Add(cmf);
     }
 }
예제 #25
0
 private static void CreateColumnFields(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
 {
     foreach (var item in tableInfo.Columns)
     {
         var cmf = new CodeMemberField()
         {
             Attributes = MemberAttributes.Private,
             Name       = item.GetFieldName(),
             Type       = item.GetCodeType()
         };
         classType.Members.Add(cmf);
     }
 }
예제 #26
0
        private static void CreateSetChildsMethod(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            foreach (var item in generationInfo.GetChilds(tableInfo))
            {
                var cmm = new CodeMemberMethod()
                {
                    Attributes = MemberAttributes.FamilyAndAssembly | MemberAttributes.Static,
                    Name       = "Set" + item.TableName
                };
                cmm.Parameters.Add(tableInfo.GetRowCodeType(), "target");
                cmm.Parameters.Add(typeof(string), "childName");
                cmm.Parameters.Add(item.GetRowCodeType(), 1, "childs");

                {
                    var methodRefExp = new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(tableInfo.GetRowCodeType()), "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 CodeVariableReferenceExpression("target");
                    var childName      = new CodeVariableReferenceExpression("childName");
                    var childs         = new CodeVariableReferenceExpression("childs");
                    var targetField    = item.GetFieldExpression(target);
                    var targetInstance = new CodeObjectCreateExpression(item.GetCodeType(), childName, childs);
                    cmm.Statements.AddAssign(targetField, targetInstance);
                }

                classType.Members.Add(cmm);
            }
        }
예제 #27
0
        private static void CreateConstructorFromTable(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            var cc = new CodeConstructor()
            {
                Attributes = MemberAttributes.Public
            };

            cc.Parameters.Add(generationInfo.ReaderNamespace, "ITable", "table");
            cc.BaseConstructorArgs.Add("table");

            // verify revision
            {
                var css = CreateCompareTypeStatement(classType, tableInfo, generationInfo);
                var tst = CremaDataClassCreator.CreateTryCatchStatement(classType, css, generationInfo);
                cc.Statements.Add(tst);
            }

            // initialize childs
            {
                var table   = new CodeVariableReferenceExpression("table");
                var dataSet = new CodePropertyReferenceExpression(table, "DataSet");
                var tables  = new CodePropertyReferenceExpression(dataSet, "Tables");

                foreach (var item in generationInfo.GetChilds(tableInfo))
                {
                    var tableName  = new CodePropertyReferenceExpression(table, "Name");
                    var childName  = new CodeBinaryOperatorExpression(tableName, CodeBinaryOperatorType.Add, new CodePrimitiveExpression("." + item.TableName));
                    var childTable = new CodeIndexerExpression(tables, childName);
                    var childField = item.GetFieldExpression();
                    var instance   = new CodeObjectCreateExpression(item.GetCodeType(), childTable);

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

            // SetRelations
            {
                var table = new CodeVariableReferenceExpression("table");
                foreach (var item in generationInfo.GetChilds(tableInfo))
                {
                    var setRelations   = new CodeMethodReferenceExpression(thisRef, "SetRelations");
                    var rows           = new CodePropertyReferenceExpression(item.GetFieldExpression(), "Rows");
                    var setChildAction = new CodePropertyReferenceExpression(tableInfo.GetRowCodeTypeExpression(), "Set" + item.TableName);
                    var tableName      = new CodePropertyReferenceExpression(table, "Name");
                    var childName      = new CodeBinaryOperatorExpression(tableName, CodeBinaryOperatorType.Add, new CodePrimitiveExpression("." + item.TableName));

                    var setRelationsInvoke = new CodeMethodInvokeExpression(setRelations, childName, rows, setChildAction);
                    cc.Statements.Add(setRelationsInvoke);
                }
            }

            classType.Members.Add(cc);
        }
예제 #28
0
        private static CodeStatement CreateCompareTypeStatement(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            var table     = new CodeVariableReferenceExpression("table");
            var hashValue = new CodePropertyReferenceExpression(table, "HashValue");

            var state = new CodeConditionStatement()
            {
                Condition = new CodeBinaryOperatorExpression(hashValue, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(tableInfo.HashValue))
            };
            var message   = string.Format("{0} 테이블과 데이터의 형식이 맞지 않습니다.", tableInfo.Name);
            var exception = new CodeObjectCreateExpression(typeof(Exception), new CodePrimitiveExpression(message));

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

            return(state);
        }
예제 #29
0
        private static void CreateCreateRowInstanceMethod(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            var cmm = new CodeMemberMethod()
            {
                Attributes = MemberAttributes.Family | MemberAttributes.Override,
                Name       = "CreateRowInstance",
                ReturnType = tableInfo.GetRowCodeType()
            };

            cmm.Parameters.Add(generationInfo.ReaderNamespace, "IRow", "row");
            cmm.Parameters.Add(typeof(object), "table");

            // statement
            {
                var row      = new CodeVariableReferenceExpression("row");
                var tablePtr = new CodeVariableReferenceExpression("table");
                var table    = new CodeCastExpression(tableInfo.GetCodeType(), tablePtr);
                var instance = new CodeObjectCreateExpression(tableInfo.GetRowCodeType(), row, table);

                cmm.Statements.AddMethodReturn(instance);
            }

            classType.Members.Add(cmm);
        }
예제 #30
0
        private static void CreateFindMethod(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            var cmm = new CodeMemberMethod()
            {
                Attributes = MemberAttributes.Public | MemberAttributes.Final,
                Name       = "Find",
                ReturnType = tableInfo.GetRowCodeType()
            };

            cmm.Parameters.Add(tableInfo.Columns.Where(item => item.IsKey));

            // invoke base.FindRow
            {
                var query = from item in tableInfo.Columns
                            where item.IsKey
                            select item.IsCustomType()
                        ? new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(item.Name), "ToString"), new CodePrimitiveExpression("D"))
                        : new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeVariableReferenceExpression(item.Name), "ToString"));

                var invokeGenerateHashCode = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("CremaUtility"), "GenerateHashCode");
                invokeGenerateHashCode.Parameters.AddRange(query.ToArray());
                var invokeFindRow = new CodeMethodInvokeExpression(baseRef, "FindRow", invokeGenerateHashCode);

                cmm.Statements.AddMethodReturn(invokeFindRow);
            }

            classType.Members.Add(cmm);
        }