示例#1
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(), "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);
        }
示例#2
0
        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);
        }