internal virtual void Execute(ManagementSession session) { var writeDocument = false; if (this.writer.WriteState == WriteState.Start) { this.writer.WriteStartDocument(); this.writer.WriteStartElement(Serialization.Allors); writeDocument = true; } this.writer.WriteStartElement(Serialization.Population); this.writer.WriteAttributeString(Serialization.Version, Serialization.VersionCurrent); this.writer.WriteStartElement(Serialization.Objects); this.writer.WriteStartElement(Serialization.Database); this.SaveObjects(session); this.writer.WriteEndElement(); this.writer.WriteEndElement(); this.writer.WriteStartElement(Serialization.Relations); this.writer.WriteStartElement(Serialization.Database); this.SaveRelations(session); this.writer.WriteEndElement(); this.writer.WriteEndElement(); this.writer.WriteEndElement(); if (writeDocument) { this.writer.WriteEndElement(); this.writer.WriteEndDocument(); } }
internal virtual void Execute(ManagementSession session) { var writeDocument = false; if (this.writer.WriteState == WriteState.Start) { this.writer.WriteStartDocument(); this.writer.WriteStartElement(Serialization.Allors); writeDocument = true; } this.writer.WriteStartElement(Serialization.Population); this.writer.WriteAttributeString(Serialization.Version, Serialization.VersionCurrent); this.writer.WriteStartElement(Serialization.Objects); this.writer.WriteStartElement(Serialization.Database); this.SaveObjects(session); this.writer.WriteEndElement(); this.writer.WriteEndElement(); this.writer.WriteStartElement(Serialization.Relations); this.writer.WriteStartElement(Serialization.Database); this.SaveRelations(session); this.writer.WriteEndElement(); this.writer.WriteEndElement(); this.writer.WriteEndElement(); if (writeDocument) { this.writer.WriteEndElement(); this.writer.WriteEndDocument(); } }
protected void SaveObjects(ManagementSession session) { var concreteCompositeType = new List <IClass>(this.database.MetaPopulation.Classes); concreteCompositeType.Sort(); foreach (var type in concreteCompositeType) { var atLeastOne = false; var sql = "SELECT " + Mapping.ColumnNameForObject + ", " + Mapping.ColumnNameForVersion + "\n"; sql += "FROM " + this.database.Mapping.TableNameForObjects + "\n"; sql += "WHERE " + Mapping.ColumnNameForClass + "=" + Mapping.ParamNameForClass + "\n"; sql += "ORDER BY " + Mapping.ColumnNameForObject; using (var command = session.Connection.CreateCommand()) { command.CommandText = sql; command.AddInParameter(Mapping.ParamNameForClass, type.Id); using (var reader = command.ExecuteReader()) { while (reader.Read()) { if (atLeastOne == false) { atLeastOne = true; this.writer.WriteStartElement(Serialization.ObjectType); this.writer.WriteAttributeString(Serialization.Id, type.Id.ToString()); } else { this.writer.WriteString(Serialization.ObjectsSplitter); } var objectId = long.Parse(reader[0].ToString()); var version = reader[1].ToString(); this.writer.WriteString(objectId + Serialization.ObjectSplitter + version); } reader.Close(); } } if (atLeastOne) { this.writer.WriteEndElement(); } } }
protected void SaveObjects(ManagementSession session) { var concreteCompositeType = new List<IClass>(this.database.MetaPopulation.Classes); concreteCompositeType.Sort(); foreach (var type in concreteCompositeType) { var atLeastOne = false; var sql = "SELECT " + Mapping.ColumnNameForObject + ", " + Mapping.ColumnNameForCache + "\n"; sql += "FROM " + this.database.Mapping.TableNameForObjects + "\n"; sql += "WHERE " + Mapping.ColumnNameForType + "=" + Mapping.ParamNameForType+ "\n"; sql += "ORDER BY " + Mapping.ColumnNameForObject; using (var command = session.CreateCommand(sql)) { command.AddInParameter(Mapping.ParamNameForType, type.Id); using (var reader = command.ExecuteReader()) { while (reader.Read()) { if (atLeastOne == false) { atLeastOne = true; this.writer.WriteStartElement(Serialization.ObjectType); this.writer.WriteAttributeString(Serialization.Id, type.Id.ToString()); } else { this.writer.WriteString(Serialization.ObjectsSplitter); } var objectId = this.database.ObjectIds.Parse(reader[0].ToString()); var cacheId = new ObjectVersionLong(reader[1].ToString()); this.writer.WriteString(objectId.Value + Serialization.ObjectSplitter + cacheId.Value); } reader.Close(); } } if (atLeastOne) { this.writer.WriteEndElement(); } } }
public void Save(XmlWriter writer) { lock (this) { var session = new ManagementSession(this, this.ManagementConnectionFactory); try { var save = new Save(this, writer); save.Execute(session); } finally { session.Rollback(); } } }
internal void Execute(ManagementSession session) { while (this.reader.Read()) { // only process elements, ignore others if (this.reader.NodeType.Equals(XmlNodeType.Element)) { if (this.reader.Name.Equals(Serialization.Population)) { Serialization.CheckVersion(this.reader); if (!this.reader.IsEmptyElement) { this.LoadPopulation(session); } return; } } } }
private void LoadObjectsSetCache(ManagementSession session) { var sql = this.database.Mapping.ProcedureNameForSetCache; var command = session.CreateSqlCommand(sql); command.CommandType = CommandType.StoredProcedure; var sqlParameter = command.CreateParameter(); sqlParameter.SqlDbType = SqlDbType.Structured; sqlParameter.TypeName = database.Mapping.TableTypeNameForVersionedObject; sqlParameter.ParameterName = Mapping.ParamNameForTableType; sqlParameter.Value = database.CreateVersionedObjectTable(this.objectVersionByObjectId); command.Parameters.Add(sqlParameter); command.ExecuteNonQuery(); }
private void LoadObjectsPostProcess(ManagementSession session) { var sql = new StringBuilder(); sql.Append("SET IDENTITY_INSERT " + this.database.Mapping.TableNameForObjects + " ON"); lock (this) { using (var command = session.CreateCommand(sql.ToString())) { command.ExecuteNonQuery(); } } foreach (var type in this.database.MetaPopulation.Composites) { if (type.IsClass) { sql = new StringBuilder(); sql.Append("INSERT INTO " + this.database.Mapping.TableNameForObjects + " (" + Mapping.ColumnNameForObject + "," + Mapping.ColumnNameForType + "," + Mapping.ColumnNameForCache + ")\n"); sql.Append("SELECT " + Mapping.ColumnNameForObject + "," + Mapping.ColumnNameForType + ", " + Reference.InitialCacheId + "\n"); sql.Append("FROM " + this.database.Mapping.TableNameForObjectByClass[type.ExclusiveClass]); lock (this) { using (var command = session.CreateCommand(sql.ToString())) { command.ExecuteNonQuery(); } } } } sql = new StringBuilder(); sql.Append("SET IDENTITY_INSERT " + this.database.Mapping.TableNameForObjects + " OFF"); lock (this) { using (var command = session.CreateCommand(sql.ToString())) { command.ExecuteNonQuery(); } } }
protected virtual void LoadRelations(ManagementSession session) { while (this.reader.Read()) { switch (this.reader.NodeType) { case XmlNodeType.Element: if (this.reader.Name.Equals(Serialization.Database)) { if (!this.reader.IsEmptyElement) { this.LoadDatabaseRelationTypes(session); } } else if (this.reader.Name.Equals(Serialization.Workspace)) { throw new Exception("Can not load workspace relations in a database."); } else { throw new Exception("Unknown child element <" + this.reader.Name + "> in parent element <" + Serialization.Relations + ">"); } break; case XmlNodeType.EndElement: if (!this.reader.Name.Equals(Serialization.Relations)) { throw new Exception("Expected closing element </" + Serialization.Relations + ">"); } return; } } }
protected virtual void LoadPopulation(ManagementSession session) { while (this.reader.Read()) { switch (this.reader.NodeType) { // only process elements, ignore others case XmlNodeType.Element: if (this.reader.Name.Equals(Serialization.Objects)) { if (!this.reader.IsEmptyElement) { this.LoadObjects(session); } this.LoadObjectsPostProcess(session); this.LoadObjectsSetCache(session); session.Commit(); } else if (this.reader.Name.Equals(Serialization.Relations)) { if (!this.reader.IsEmptyElement) { this.LoadRelations(session); } session.Commit(); } else { throw new Exception("Unknown child element <" + this.reader.Name + "> in parent element <" + Serialization.Population + ">"); } break; case XmlNodeType.EndElement: if (!this.reader.Name.Equals(Serialization.Population)) { throw new Exception("Expected closing element </" + Serialization.Population + ">"); } return; } } }
protected virtual void LoadDatabaseRelationTypes(ManagementSession session) { while (this.reader.Read()) { switch (this.reader.NodeType) { // only process elements, ignore others case XmlNodeType.Element: if (!this.reader.IsEmptyElement) { if (this.reader.Name.Equals(Serialization.RelationTypeUnit) || this.reader.Name.Equals(Serialization.RelationTypeComposite)) { var relationTypeIdString = this.reader.GetAttribute(Serialization.Id); if (string.IsNullOrEmpty(relationTypeIdString)) { throw new Exception("Relation type has no id"); } var relationTypeId = new Guid(relationTypeIdString); var relationType = (IRelationType)this.database.MetaPopulation.Find(relationTypeId); if (this.reader.Name.Equals(Serialization.RelationTypeUnit)) { if (relationType == null || relationType.RoleType.ObjectType.IsComposite) { this.CantLoadUnitRole(relationTypeId); } else { var relationsByExclusiveRootClass = new Dictionary<IObjectType, List<UnitRelation>>(); this.LoadUnitRelations(relationType, relationsByExclusiveRootClass); foreach (var dictionaryEntry in relationsByExclusiveRootClass) { var exclusiveRootClass = dictionaryEntry.Key; var relations = dictionaryEntry.Value; session.LoadUnitRelations(relations, exclusiveRootClass, relationType.RoleType); } } } else if (this.reader.Name.Equals(Serialization.RelationTypeComposite)) { if (relationType == null || relationType.RoleType.ObjectType.IsUnit) { this.CantLoadCompositeRole(relationTypeId); } else { var relations = new List<CompositeRelation>(); this.LoadCompositeRelations(relationType, relations); if (relations.Count > 0) { session.LoadCompositeRelations(relationType.RoleType, relations); } } } } else { throw new Exception( "Unknown child element <" + this.reader.Name + "> in parent element <" + Serialization.Database + ">"); } } break; case XmlNodeType.EndElement: if (!this.reader.Name.Equals(Serialization.Database)) { throw new Exception("Expected closing element </" + Serialization.Database + ">"); } return; } } }
protected virtual void LoadDatabaseObjectTypes(ManagementSession session) { while (this.reader.Read()) { switch (this.reader.NodeType) { // only process elements, ignore others case XmlNodeType.Element: if (this.reader.Name.Equals(Serialization.ObjectType)) { if (!this.reader.IsEmptyElement) { var objectTypeIdString = this.reader.GetAttribute(Serialization.Id); if (string.IsNullOrEmpty(objectTypeIdString)) { throw new Exception("Object type id is missing"); } var objectTypeId = new Guid(objectTypeIdString); var objectType = this.database.ObjectFactory.GetObjectTypeForType(objectTypeId); var canLoad = objectType != null && objectType.IsClass; var objectIdsString = this.reader.ReadString(); var objectIdStringArray = objectIdsString.Split(Serialization.ObjectsSplitterCharArray); var objectIds = new ObjectId[objectIdStringArray.Length]; var cacheIds = new ObjectVersion[objectIdStringArray.Length]; for (var i = 0; i < objectIds.Length; i++) { var objectIdString = objectIdStringArray[i]; if (canLoad) { var objectArray = objectIdString.Split(Serialization.ObjectSplitterCharArray); var objectId = this.database.ObjectIds.Parse(objectArray[0]); var cacheId = objectArray.Length > 1 ? new ObjectVersionLong(objectArray[1]) : new ObjectVersionLong(Reference.InitialCacheId); objectIds[i] = objectId; cacheIds[i] = cacheId; this.objectTypeByObjectId[objectId] = objectType; this.objectVersionByObjectId[objectId] = cacheId; } else { this.OnObjectNotLoaded(objectTypeId, objectIdString); } } if (canLoad) { session.LoadObjects(objectType, objectIds); } } } else { throw new Exception("Unknown child element <" + this.reader.Name + "> in parent element <" + Serialization.Database + ">"); } break; case XmlNodeType.EndElement: if (!this.reader.Name.Equals(Serialization.Database)) { throw new Exception("Expected closing element </" + Serialization.Database + ">"); } return; } } }
protected void SaveRelations(ManagementSession session) { var exclusiverRootClassesByObjectType = new Dictionary <IObjectType, HashSet <IObjectType> >(); var relations = new List <IRelationType>(this.database.MetaPopulation.RelationTypes); relations.Sort(); foreach (var relation in relations) { var associationType = relation.AssociationType; if (associationType.ObjectType.ExistClass) { var roleType = relation.RoleType; var sql = string.Empty; if (roleType.ObjectType.IsUnit) { HashSet <IObjectType> exclusiveRootClasses; if (!exclusiverRootClassesByObjectType.TryGetValue(associationType.ObjectType, out exclusiveRootClasses)) { exclusiveRootClasses = new HashSet <IObjectType>(); foreach (var concreteClass in associationType.ObjectType.Classes) { exclusiveRootClasses.Add(concreteClass.ExclusiveClass); } exclusiverRootClassesByObjectType[associationType.ObjectType] = exclusiveRootClasses; } var first = true; foreach (var exclusiveRootClass in exclusiveRootClasses) { if (first) { first = false; } else { sql += "UNION\n"; } sql += "SELECT " + Mapping.ColumnNameForObject + " As " + Mapping.ColumnNameForAssociation + ", " + this.database.Mapping.ColumnNameByRelationType[roleType.RelationType] + " As " + Mapping.ColumnNameForRole + "\n"; sql += "FROM " + this.database.Mapping.TableNameForObjectByClass[(IClass)exclusiveRootClass] + "\n"; sql += "WHERE " + this.database.Mapping.ColumnNameByRelationType[roleType.RelationType] + " IS NOT NULL\n"; } sql += "ORDER BY " + Mapping.ColumnNameForAssociation; } else { if ((roleType.IsMany && associationType.IsMany) || !relation.ExistExclusiveClasses) { sql += "SELECT " + Mapping.ColumnNameForAssociation + "," + Mapping.ColumnNameForRole + "\n"; sql += "FROM " + this.database.Mapping.TableNameForRelationByRelationType[relation] + "\n"; sql += "ORDER BY " + Mapping.ColumnNameForAssociation + "," + Mapping.ColumnNameForRole; } else { // use foreign keys if (roleType.IsOne) { sql += "SELECT " + Mapping.ColumnNameForObject + " As " + Mapping.ColumnNameForAssociation + ", " + this.database.Mapping.ColumnNameByRelationType[roleType.RelationType] + " As " + Mapping.ColumnNameForRole + "\n"; sql += "FROM " + this.database.Mapping.TableNameForObjectByClass[associationType.ObjectType.ExclusiveClass] + "\n"; sql += "WHERE " + this.database.Mapping.ColumnNameByRelationType[roleType.RelationType] + " IS NOT NULL\n"; sql += "ORDER BY " + Mapping.ColumnNameForAssociation; } else { // role.Many sql += "SELECT " + this.database.Mapping.ColumnNameByRelationType[associationType.RelationType] + " As " + Mapping.ColumnNameForAssociation + ", " + Mapping.ColumnNameForObject + " As " + Mapping.ColumnNameForRole + "\n"; sql += "FROM " + this.database.Mapping.TableNameForObjectByClass[((IComposite)roleType.ObjectType).ExclusiveClass] + "\n"; sql += "WHERE " + this.database.Mapping.ColumnNameByRelationType[associationType.RelationType] + " IS NOT NULL\n"; sql += "ORDER BY " + Mapping.ColumnNameForAssociation + "," + Mapping.ColumnNameForRole; } } } using (var command = session.Connection.CreateCommand()) { command.CommandText = sql; using (var reader = command.ExecuteReader()) { if (roleType.IsMany) { using (var relationTypeManyXmlWriter = new RelationTypeManyXmlWriter(relation, this.writer)) { while (reader.Read()) { var a = long.Parse(reader[0].ToString()); var r = long.Parse(reader[1].ToString()); relationTypeManyXmlWriter.Write(a, r); } relationTypeManyXmlWriter.Close(); } } else { using (var relationTypeOneXmlWriter = new RelationTypeOneXmlWriter(relation, this.writer)) { while (reader.Read()) { var a = long.Parse(reader[0].ToString()); if (roleType.ObjectType.IsUnit) { var unitTypeTag = ((IUnit)roleType.ObjectType).UnitTag; var r = command.GetValue(reader, unitTypeTag, 1); var content = Serialization.WriteString(unitTypeTag, r); relationTypeOneXmlWriter.Write(a, content); } else { var r = reader[1]; relationTypeOneXmlWriter.Write(a, XmlConvert.ToString(long.Parse(r.ToString()))); } } relationTypeOneXmlWriter.Close(); } } } } } } }
protected void SaveRelations(ManagementSession session) { var exclusiverRootClassesByObjectType = new Dictionary<IObjectType, HashSet<IObjectType>>(); var relations = new List<IRelationType>(this.database.MetaPopulation.RelationTypes); relations.Sort(); foreach (var relation in relations) { var associationType = relation.AssociationType; if (associationType.ObjectType.ExistClass) { var roleType = relation.RoleType; var sql = string.Empty; if (roleType.ObjectType.IsUnit) { HashSet<IObjectType> exclusiveRootClasses; if (!exclusiverRootClassesByObjectType.TryGetValue(associationType.ObjectType, out exclusiveRootClasses)) { exclusiveRootClasses = new HashSet<IObjectType>(); foreach (var concreteClass in associationType.ObjectType.Classes) { exclusiveRootClasses.Add(concreteClass.ExclusiveClass); } exclusiverRootClassesByObjectType[associationType.ObjectType] = exclusiveRootClasses; } var first = true; foreach (var exclusiveRootClass in exclusiveRootClasses) { if (first) { first = false; } else { sql += "UNION\n"; } sql += "SELECT " + Mapping.ColumnNameForObject + " As " + Mapping.ColumnNameForAssociation + ", " + this.database.Mapping.ColumnNameByRelationType[roleType.RelationType] + " As " + Mapping.ColumnNameForRole + "\n"; sql += "FROM " + this.database.Mapping.TableNameForObjectByClass[(IClass)exclusiveRootClass] + "\n"; sql += "WHERE " + this.database.Mapping.ColumnNameByRelationType[roleType.RelationType] + " IS NOT NULL\n"; } sql += "ORDER BY " + Mapping.ColumnNameForAssociation; } else { if ((roleType.IsMany && associationType.IsMany) || !relation.ExistExclusiveClasses) { sql += "SELECT " + Mapping.ColumnNameForAssociation + "," + Mapping.ColumnNameForRole + "\n"; sql += "FROM " + this.database.Mapping.TableNameForRelationByRelationType[relation] + "\n"; sql += "ORDER BY " + Mapping.ColumnNameForAssociation + "," + Mapping.ColumnNameForRole; } else { // use foreign keys if (roleType.IsOne) { sql += "SELECT " + Mapping.ColumnNameForObject + " As " + Mapping.ColumnNameForAssociation + ", " + this.database.Mapping.ColumnNameByRelationType[roleType.RelationType] + " As " + Mapping.ColumnNameForRole + "\n"; sql += "FROM " + this.database.Mapping.TableNameForObjectByClass[associationType.ObjectType.ExclusiveClass] + "\n"; sql += "WHERE " + this.database.Mapping.ColumnNameByRelationType[roleType.RelationType] + " IS NOT NULL\n"; sql += "ORDER BY " + Mapping.ColumnNameForAssociation; } else { // role.Many sql += "SELECT " + this.database.Mapping.ColumnNameByRelationType[associationType.RelationType] + " As " + Mapping.ColumnNameForAssociation + ", " + Mapping.ColumnNameForObject + " As " + Mapping.ColumnNameForRole + "\n"; sql += "FROM " + this.database.Mapping.TableNameForObjectByClass[((IComposite)roleType.ObjectType).ExclusiveClass] + "\n"; sql += "WHERE " + this.database.Mapping.ColumnNameByRelationType[associationType.RelationType] + " IS NOT NULL\n"; sql += "ORDER BY " + Mapping.ColumnNameForAssociation + "," + Mapping.ColumnNameForRole; } } } using (var command = session.CreateCommand(sql)) { using (var reader = command.ExecuteReader()) { if (roleType.IsMany) { using (var relationTypeManyXmlWriter = new RelationTypeManyXmlWriter(relation, this.writer)) { while (reader.Read()) { var a = long.Parse(reader[0].ToString()); var r = long.Parse(reader[1].ToString()); relationTypeManyXmlWriter.Write(a, r); } relationTypeManyXmlWriter.Close(); } } else { using (var relationTypeOneXmlWriter = new RelationTypeOneXmlWriter(relation, this.writer)) { while (reader.Read()) { var a = long.Parse(reader[0].ToString()); if (roleType.ObjectType.IsUnit) { var unitTypeTag = ((IUnit)roleType.ObjectType).UnitTag; var r = command.GetValue(reader, unitTypeTag, 1); var content = Serialization.WriteString(unitTypeTag, r); relationTypeOneXmlWriter.Write(a, content); } else { var r = reader[1]; relationTypeOneXmlWriter.Write(a, XmlConvert.ToString(long.Parse(r.ToString()))); } } relationTypeOneXmlWriter.Close(); } } } } } } }