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(); } } }
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 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(); } } } } } } }