Example #1
0
        private static void CreateSetChildsMethod(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            foreach (var item in generationInfo.GetChilds(tableInfo))
            {
                var cmm = new CodeMemberMethod();
                cmm.Attributes = MemberAttributes.FamilyAndAssembly | MemberAttributes.Static;
                cmm.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(null, "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 #2
0
        private static void CreateConstructor(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            var cc = new CodeConstructor();

            cc.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();
                    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);
        }
Example #3
0
        private static void CreateConstructorFromTable(CodeTypeDeclaration classType, TableInfo tableInfo, CodeGenerationInfo generationInfo)
        {
            var cc = new CodeConstructor();

            cc.Attributes = MemberAttributes.Public;
            cc.Name       = tableInfo.TableName;
            cc.Parameters.Add(generationInfo.ReaderNamespace, "itable&", "table");
            //cc.BaseConstructorArgs.Add("table");

            //cc.AddConstructorStatement(string.Format("{0}(table)", CodeGenerationInfo.CremaTableName));

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

            {
                var table   = new CodeVariableReferenceExpression("table");
                var dataSet = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(table, "dataset"));
                var tables  = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(dataSet, "tables"));

                foreach (var item in generationInfo.GetChilds(tableInfo))
                {
                    var tableName  = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(table, "name"));
                    var childName  = new CodeBinaryOperatorExpression(tableName, CodeBinaryOperatorType.Add, new CodePrimitiveExpression("." + item.TableName));
                    var childTable = new CodeIndexerExpression(tables, childName);
                    var childField = new CodeFieldReferenceExpression(thisRef, item.TableName);
                    var instance   = new CodeObjectCreateExpression(item.GetCodeType(CodeType.None), childTable);

                    cc.Statements.AddAssign(childField, instance);
                    //cc.AddConstructorStatement(string.Format("{0}(table.dataset().tables()[\"{1}\"])", item.GetFieldName(), item.GetUniqueName()));
                }
            }


            {
                var table        = new CodeVariableReferenceExpression("table");
                var readFromFile = new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), "ReadFromTable");
                var methodInvoke = new CodeMethodInvokeExpression(readFromFile, table);

                cc.Statements.Add(methodInvoke);
            }

            // SetRelations
            {
                var table = new CodeVariableReferenceExpression("table");
                foreach (var item in generationInfo.GetChilds(tableInfo))
                {
                    var setRelations   = new CodeMethodReferenceExpression(thisRef, "SetRelations");
                    var field          = new CodeFieldPointerExpression(thisRef, item.TableName);
                    var rows           = new CodePropertyReferenceExpression(field, "Rows");
                    var codeTypeRef    = new CodeTypeReferenceExpression(tableInfo.GetRowCodeType(CodeType.None));
                    var setChildAction = new CodePropertyReferenceExpression(null, tableInfo.TableName + "Set" + item.TableName);
                    var tableName      = new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(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);
        }