/// <summary> /// Creates two methods: // Add // 1.Adds IRecord(base interface for database object) to list /// 2.Execute Takes all elements in list and save them permanently /// in file by calling corresponding method) /// /// /// </summary> public override void BuildMethods() { base.BuildMethods(); CodeMemberMethod AddRecordsMethod = BuildMethodSignature("Add", MemberAttributes.Assembly, new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("IRecord", "elem") }); CodeStatement csCollectionAddInvoke = GetMethodInvokationStatement("lstTableRecords", "Add", new List <string>() { "elem" }); AddRecordsMethod.Statements.Add(csCollectionAddInvoke); CodeMemberMethod ExecuteMethod = BuildMethodSignature("Execute", MemberAttributes.Assembly, new List <KeyValuePair <string, string> >()); CodeStatement AssignRecordStatement = GetAssignment("IRecord", "t", new CodeVariableReferenceExpression("lstTableRecords[i]")); CodeStatement SaveMethodInvokeStatement = GetMethodInvokationStatement("t", "Save", new List <string>()); CodeCompoundStatement SaveAllCommandsStatement = GetSimpleForInteger("i", new KeyValuePair <int, string>(0, "lstTableRecords.Count"), 1, new List <CodeStatement>() { AssignRecordStatement, SaveMethodInvokeStatement }); ExecuteMethod.Statements.AddRange(SaveAllCommandsStatement.Statements.ToArray()); CodeStatement ClearCollectionStatement = GetMethodInvokationStatement("lstTableRecords", "Clear", new List <string>()); ExecuteMethod.Statements.Add(ClearCollectionStatement); TheType.Members.Add(AddRecordsMethod); TheType.Members.Add(ExecuteMethod); }
/// <summary> /// Generates cycle statements of loading each db record to memory /// (Find in xml and convert it to appropriate object) /// </summary> /// <param name="elementsName">Name of the elements collection variable</param> /// <param name="indexName">Name of cycle index(commonly i)</param> /// <returns>List of statements for a cycle for</returns> private List <CodeStatement> CreateLoadXmlStatements(string elementsName, string indexName) { List <CodeStatement> lst = new List <CodeStatement>(); // Declaration of record(One line in db table for example) CodeStatement RecordDeclarationStatement = GetDeclaration("record", "T"); lst.Add(RecordDeclarationStatement); CodeStatement RecordObjectCreationStatement = GetObjectCreateStatement("T", "record", true); lst.Add(RecordObjectCreationStatement); CodeStatement CurrentElementAssignStatement = GetAssignment("XElement", "xeCurrent", new CodeVariableReferenceExpression(string.Format("{0}[{1}]", elementsName, indexName))); lst.Add(CurrentElementAssignStatement); CodeExpression SearchPropertyElementExpression = GetMethodInvokationExpression("xeCurrent", "Elements", new System.Collections.Generic.List <KeyValuePair <string, bool> >() { new KeyValuePair <string, bool>("Property", false) }); CodeStatement PropertyXElementAssignStatement = GetAssignment("IEnumerable<XElement>", "xeProperties", SearchPropertyElementExpression); lst.Add(PropertyXElementAssignStatement); CodeStatement PropertiesListAssignmentStatement = GetAssignment("List<XElement>", "lstProperties", GetMethodInvokationExpression("xeProperties", "ToList", new List <KeyValuePair <string, bool> >())); lst.Add(PropertiesListAssignmentStatement); List <CodeStatement> lstPropertyStatements = CreateLoadPropertyStatements("lstProperties", "j"); CodeCompoundStatement PropertyLoadCycleStatement = GetSimpleForInteger("j", new System.Collections.Generic.KeyValuePair <int, string>(0, "lstProperties.Count"), 1, lstPropertyStatements); lst.AddRange(PropertyLoadCycleStatement.Statements.ToArray()); CodeStatement CollectionAddInvokationStatement = GetMethodInvokationStatement("table", "Add", new List <string>() { "record" }); lst.Add(CollectionAddInvokationStatement); return(lst); }
/// <summary> /// Generate saving record statements for xml /// </summary> /// <param name="elementsName">Name of collection of records variable</param> /// <param name="indexName">Name of cycle index</param> /// <returns>list of cycle statements</returns> private List <CodeStatement> CreateSaveXmlStatements(string elementsName, string indexName) { List <CodeStatement> lst = new List <CodeStatement>(); CodeStatement RecordDeclarationStatement = GetDeclaration("record", "T"); lst.Add(RecordDeclarationStatement); CodeStatement RecordAssignStatement = GetAssignment("record", new KeyValuePair <string, bool>("table[i]", true)); lst.Add(RecordAssignStatement); CodeExpression GetTypeInvokeStatement = GetMethodInvokationExpression("record", "GetType", new System.Collections.Generic.List <KeyValuePair <string, bool> >()); CodeStatement TypeAssignStatement = GetAssignment("Type", "type", GetTypeInvokeStatement); lst.Add(TypeAssignStatement); CodeExpression GetPropertiesInvokeExpression = GetMethodInvokationExpression("type", "GetProperties", new System.Collections.Generic.List <KeyValuePair <string, bool> >()); CodeStatement PropertiesAssignStatement = GetAssignment("PropertyInfo []", "pi", GetPropertiesInvokeExpression); lst.Add(PropertiesAssignStatement); CodeStatement CurrentDeclarationStatement = GetDeclaration("xeCurrent", "XElement"); lst.Add(CurrentDeclarationStatement); CodeStatement CunbjcCreateStatement = GetObjectCreateStatement("XElement", "xeCurrent", true, new List <string>() { "Elements" }); lst.Add(CunbjcCreateStatement); List <CodeStatement> lstSavePropertyStatements = CreateSavePropertyStatements("pi", "j"); CodeCompoundStatement SavePropertyCycleStatement = GetSimpleForInteger("j", new System.Collections.Generic.KeyValuePair <int, string>(0, "pi.Length"), 1, lstSavePropertyStatements); lst.AddRange(SavePropertyCycleStatement.Statements.ToArray()); CodeStatement XElementAddInvokationStatement = GetMethodInvokationStatement("xu", "Add", new List <string>() { "xeCurrent" }); lst.Add(XElementAddInvokationStatement); return(lst); }
/// <summary> /// Creates event handler on table records list change /// The idea is bubbling this change upper to event in db manager /// </summary> public override void BuildEventHandlers() { base.BuildEventHandlers(); CodeMemberMethod cmmListChangeEvent = new CodeMemberMethod(); cmmListChangeEvent.Name = "List_Changed"; cmmListChangeEvent.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("System.Object"), "sender")); cmmListChangeEvent.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference("NotifyCollectionChangedEventArgs"), "e")); CodeExpression ceCheckNotNull = GetConditionCompareToNull("e.NewItems"); CodeStatement ITableAssignStatement = GetCastWithAssignment("ITable", "t", "e.NewItems[i]"); CodeStatement TableRecordsChangeEventStatement = GetDelegateCreateStatement("System.EventHandler", string.Format("t"), string.Format("List_Change"), "TableElementChanged", false); List <CodeStatement> lst = new List <CodeStatement>(); lst.Add(ITableAssignStatement); lst.Add(TableRecordsChangeEventStatement); CodeStatement TableChangedInvokationStatement = GetEventHandlerInvokation("TableChanged", "EventArgs"); CodeStatement IsInitedCheckStatement = GetUnoConditionIf("IsInited", CodeBinaryOperatorType.IdentityEquality, "true", new List <CodeStatement> { TableChangedInvokationStatement }); cmmListChangeEvent.Statements.Add(IsInitedCheckStatement); // Creates a for loop that sets testInt to 0 and continues incrementing testInt by 1 each loop until testInt is not less than 10. CodeCompoundStatement forLoopNewItemsStatement = GetSimpleForInteger("i", new System.Collections.Generic.KeyValuePair <int, string>(0, "e.NewItems.Count"), 1, lst); //CodeExpressionStatement expressionStatement; //expressionStatement = new CodeExpressionStatement(invoke1); CodeStatement[] trueStatements = forLoopNewItemsStatement.Statements.ToArray() ; CodeStatement[] falseStatements = { new CodeCommentStatement("Do this is false") }; CodeStatement IfHasNewItemsStatement = new CodeConditionStatement(ceCheckNotNull, trueStatements); cmmListChangeEvent.Statements.Add(IfHasNewItemsStatement); TheType.Members.Add(cmmListChangeEvent); }
/// <summary> /// Generates method that saves records of table to xml file /// </summary> private void SaveXml() { CodeMemberMethod SaveXmlmethod = BuildMethodSignature("SaveXml", MemberAttributes.Public, new System.Collections.Generic.List <System.Collections.Generic.KeyValuePair <string, string> >() { new System.Collections.Generic.KeyValuePair <string, string>("TableList<T>", "table") }); TheType.Members.Add(SaveXmlmethod); CodeStatement TableNameAssignStatement = GetAssignment("System.String", "tableName", new CodeVariableReferenceExpression("table.Name")); SaveXmlmethod.Statements.Add(TableNameAssignStatement); // CodeStatement cas12 = GetMethodInvokationAssignment("string", "fileName", "string", "Format", new System.Collections.Generic.List<KeyValuePair<string, bool>>() { new KeyValuePair<string, bool>("{0}.{1}", false), new KeyValuePair<string, bool>("tableName", true), new KeyValuePair<string, bool>("table.Format", true) }); CodeExpression FormatMethodInvokeStatement = GetMethodInvokationExpression("System.String", "Format", new System.Collections.Generic.List <KeyValuePair <string, bool> >() { new KeyValuePair <string, bool>("{0}\\\\Files\\\\{1}.{2}", false), new KeyValuePair <string, bool>(generateDirectory, false), new KeyValuePair <string, bool>("tableName", true), new KeyValuePair <string, bool>("Format", true) }); CodeStatement FileNameAssignStatement = GetAssignment("System.String", "fileName", FormatMethodInvokeStatement); SaveXmlmethod.Statements.Add(FileNameAssignStatement); CodeExpression LoadMethodInvokeExpression = GetMethodInvokationExpression("XDocument", "Load", new System.Collections.Generic.List <KeyValuePair <string, bool> > { new KeyValuePair <string, bool>("fileName", true) }); CodeStatement TableXElementAssignmentStatement = GetAssignment("XDocument", "xdTable", LoadMethodInvokeExpression); CodeStatement DataElementXElementAssignStatement = GetAssignment("System.String", "xmlDataElement", new CodePrimitiveExpression("Data")); CodeExpression SearchElementInXmlStatement = GetMethodInvokationExpression("xdTable.Root", "Element", new System.Collections.Generic.List <KeyValuePair <string, bool> >() { new KeyValuePair <string, bool>("xmlDataElement", true) }); CodeStatement XElementAssignStatement = GetAssignment("XElement", "xu", SearchElementInXmlStatement); CodeStatement CreateDataElementXmlObjStatement = GetObjectCreateStatement("XElement", "xu", true, new List <string>() { "Data" }); CodeStatement AddXElementToXmlStatement = GetMethodInvokationStatement("xdTable.Root", "Add", new List <string>() { "xu" }); CodeStatement RemoveAllInvokationStatement = GetMethodInvokationStatement("xu", "RemoveAll", new List <string>()); CodeStatement CheckRecordExistanceStatement = GetUnoConditionIf("xu", CodeBinaryOperatorType.ValueEquality, "null", new List <CodeStatement>() { CreateDataElementXmlObjStatement, AddXElementToXmlStatement }, new List <CodeStatement>() { RemoveAllInvokationStatement }); CodeExpression RemoveMethodInvokationExpression = GetMethodInvokationExpression("xu.Elements()", "Remove", new List <KeyValuePair <string, bool> >()); CodeStatement RemoveMethodInvokationStatement = new CodeExpressionStatement(RemoveMethodInvokationExpression); CodeStatement CollectionAddInvokationStatement = GetMethodInvokationStatement("table", "Add", new List <string>() { "xu.Elements(\"Element\")[i]" }); CodeExpression SearchElementsXElementExpression = GetMethodInvokationExpression("xu", "Elements", new List <KeyValuePair <string, bool> >() { new KeyValuePair <string, bool>("Elements", false) }); CodeStatement ElementAssignStatement = GetAssignment("IEnumerable<XElement>", "elements", SearchElementsXElementExpression); SaveXmlmethod.Statements.Add(TableXElementAssignmentStatement); SaveXmlmethod.Statements.Add(DataElementXElementAssignStatement); SaveXmlmethod.Statements.Add(XElementAssignStatement); SaveXmlmethod.Statements.Add(CheckRecordExistanceStatement); SaveXmlmethod.Statements.Add(ElementAssignStatement); SaveXmlmethod.Statements.Add(RemoveMethodInvokationStatement); List <CodeStatement> SaveRecordStatements = CreateSaveXmlStatements("table", "i"); CodeCompoundStatement SaveRecordCycleStatement = GetSimpleForInteger("i", new System.Collections.Generic.KeyValuePair <int, string>(0, "table.Count"), 1, SaveRecordStatements); SaveXmlmethod.Statements.AddRange(SaveRecordCycleStatement.Statements.ToArray()); CodeStatement XDocumentSaveInvokeStatement = GetMethodInvokationStatement("xdTable", "Save", new List <string>() { "fileName" }); SaveXmlmethod.Statements.Add(XDocumentSaveInvokeStatement); CodeMemberMethod SaveOtherTypesMethod = BuildMethodSignature("SaveOtherTypes", MemberAttributes.Public, new System.Collections.Generic.List <System.Collections.Generic.KeyValuePair <string, string> >() { new System.Collections.Generic.KeyValuePair <string, string>("TableList<T>", "table") }); TheType.Members.Add(SaveOtherTypesMethod); }
/// <summary> /// Generates methods for loading data from xml files and other types /// to memory /// </summary> public void LoadXml() { CodeMemberMethod LoadXmlMethod = BuildMethodSignature("LoadXml", MemberAttributes.Public, new System.Collections.Generic.List <System.Collections.Generic.KeyValuePair <string, string> >() { new System.Collections.Generic.KeyValuePair <string, string>("TableList<T>", "table") }); TheType.Members.Add(LoadXmlMethod); CodeStatement TableNameAssignment = GetAssignment("System.String", "tableName", new CodeVariableReferenceExpression("table.Name")); LoadXmlMethod.Statements.Add(TableNameAssignment); // CodeStatement cas12 = GetMethodInvokationAssignment("string", "fileName", "string", "Format", new System.Collections.Generic.List<KeyValuePair<string, bool>>() { new KeyValuePair<string, bool>("{0}.{1}", false), new KeyValuePair<string, bool>("tableName", true), new KeyValuePair<string, bool>("table.Format", true) }); CodeExpression FormatMethodInvokationStatement = GetMethodInvokationExpression("System.String", "Format", new System.Collections.Generic.List <KeyValuePair <string, bool> >() { new KeyValuePair <string, bool>("{0}\\\\Files\\\\{1}.{2}", false), new KeyValuePair <string, bool>(generateDirectory, false), new KeyValuePair <string, bool>("tableName", true), new KeyValuePair <string, bool>("Format", true) }); CodeStatement FileNameAssignmentStatement = GetAssignment("System.String", "fileName", FormatMethodInvokationStatement); LoadXmlMethod.Statements.Add(FileNameAssignmentStatement); CodeExpression LoadMethodInvokationStatement = GetMethodInvokationExpression("XDocument", "Load", new System.Collections.Generic.List <KeyValuePair <string, bool> > { new KeyValuePair <string, bool>("fileName", true) }); CodeStatement XmlAssignmentStatement = GetAssignment("XDocument", "xdTable", LoadMethodInvokationStatement); CodeStatement DataElementNameAssignmentStatement = GetAssignment("System.String", "xmlDataElement", new CodePrimitiveExpression("Data")); CodeExpression ElementMethodInvokationExpression = GetMethodInvokationExpression("xdTable.Root", "Element", new System.Collections.Generic.List <KeyValuePair <string, bool> >() { new KeyValuePair <string, bool>("xmlDataElement", true) }); CodeStatement DataElementAssignmentStatement = GetAssignment("XElement", "xu", ElementMethodInvokationExpression); CodeStatement CheckThatDataExistsStatement = GetUnoConditionIf("xu", CodeBinaryOperatorType.ValueEquality, "null", new List <CodeStatement>() { new CodeMethodReturnStatement() }); CodeExpression ElementsXmlSearchExpression = GetMethodInvokationExpression("xu", "Elements", new List <KeyValuePair <string, bool> >() { new KeyValuePair <string, bool>("Elements", false) }); CodeStatement ElementsAssignmentStatement = GetAssignment("IEnumerable<XElement>", "elements", ElementsXmlSearchExpression); CodeExpression ToListMethodInvokationExpression = GetMethodInvokationExpression("elements", "ToList", new List <KeyValuePair <string, bool> >()); CodeStatement ElementListAssignmentStatement = GetAssignment("List<XElement>", "lstElements", ToListMethodInvokationExpression); CodeStatement CollectionAddMethodnvokationStatement = GetMethodInvokationStatement("table", "Add", new List <string>() { "xu.Elements(\"Element\")[i]" }); LoadXmlMethod.Statements.Add(DataElementNameAssignmentStatement); LoadXmlMethod.Statements.Add(XmlAssignmentStatement); LoadXmlMethod.Statements.Add(DataElementAssignmentStatement); LoadXmlMethod.Statements.Add(ElementMethodInvokationExpression); LoadXmlMethod.Statements.Add(CheckThatDataExistsStatement); LoadXmlMethod.Statements.Add(ElementsAssignmentStatement); LoadXmlMethod.Statements.Add(ElementListAssignmentStatement); //LoadXmlMethod.Statements.Add(CollectionAddMethodnvokationStatement); List <CodeStatement> lstLoadDbRecordsStatements = CreateLoadXmlStatements("lstElements", "i"); CodeCompoundStatement LoadDbRecordCycleStatement = GetSimpleForInteger("i", new System.Collections.Generic.KeyValuePair <int, string>(0, "lstElements.Count"), 1, lstLoadDbRecordsStatements); LoadXmlMethod.Statements.AddRange(LoadDbRecordCycleStatement.Statements.ToArray()); CodeMemberMethod LoadJsonMethod = BuildMethodSignature("LoadJson", MemberAttributes.Public, new System.Collections.Generic.List <System.Collections.Generic.KeyValuePair <string, string> >() { new System.Collections.Generic.KeyValuePair <string, string>("TableList<T>", "table") }); TheType.Members.Add(LoadJsonMethod); CodeMemberMethod LoadOtherTypesMethod = BuildMethodSignature("LoadOtherTypes", MemberAttributes.Public, new System.Collections.Generic.List <System.Collections.Generic.KeyValuePair <string, string> >() { new System.Collections.Generic.KeyValuePair <string, string>("TableList<T>", "table") }); TheType.Members.Add(LoadOtherTypesMethod); }