Пример #1
0
        public static void CreateTable(CodeNamespace codeNamespace, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            var classType = new CodeTypeDeclaration();

            foreach (var item in generationInfo.GetChilds(tableInfo))
            {
                CreateTable(codeNamespace, item, generationInfo);
            }

            CremaDeclCreator.CreateRowDecl(codeNamespace, tableInfo, generationInfo);

            classType.Attributes      = MemberAttributes.Public;
            classType.Name            = tableInfo.GetClassName();
            classType.IsClass         = true;
            classType.TypeAttributes |= System.Reflection.TypeAttributes.Sealed;
            classType.BaseTypes.Add(new CodeTypeReference("CremaTable", tableInfo.GetRowCodeType()));

            if (generationInfo.OmitComment == false)
            {
                classType.Comments.AddSummary(tableInfo.Comment);
            }
            if (generationInfo.OmitSignatureDate == false)
            {
                classType.Comments.Add(CremaCodeGenerator.Creator, tableInfo.CreationInfo.ID);
                classType.Comments.Add(CremaCodeGenerator.CreatedDateTime, tableInfo.CreationInfo.DateTime);
                classType.Comments.Add(CremaCodeGenerator.Modifier, tableInfo.ModificationInfo.ID);
                classType.Comments.Add(CremaCodeGenerator.ModifiedDateTime, tableInfo.ModificationInfo.DateTime);
                classType.Comments.Add(CremaCodeGenerator.ContentsModifier, tableInfo.ContentsInfo.ID);
                classType.Comments.Add(CremaCodeGenerator.ContentsModifiedDateTime, tableInfo.ContentsInfo.DateTime);
            }

            CreateTableFields(classType, tableInfo, generationInfo);
            //CreateProperties(classType, tableInfo);
            //CreateConstructor(classType, tableInfo);
            CreateTableFindMethodDecl(classType, tableInfo);
            //CreateCreateRowInstanceMethod(classType, tableInfo);


            codeNamespace.Types.Add(classType);
        }
Пример #2
0
        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(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(thisRef, "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();
                    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);

                        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 condition = new CodeBinaryOperatorExpression(item.GetFieldExpression(), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null));
                    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)
                    {
                        methodInvokeExp.Parameters.Add(item.GetFieldExpression());
                    }
                }
                cc.Statements.Add(methodInvokeExp);
            }

            classType.Members.Add(cc);
        }