protected virtual void DropTables(ManagementSession session) { foreach (SchemaTable table in Schema) { this.DropTable(session, table); } }
protected virtual void CreateProcedures(ManagementSession session) { foreach (var procedure in Schema.Procedures) { this.CreateProcedure(session, procedure); } }
public 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.ToString()); 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 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 (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 DropIndexes(ManagementSession session) { foreach (SchemaTable table in Schema) { foreach (SchemaColumn column in table) { if (column.IndexType != SchemaIndexType.None) { this.DropIndex(session, table, column); } } } }
protected void SaveObjects(ManagementSession session) { var concreteCompositeType = new List <IClass>(this.database.MetaPopulation.Classes); concreteCompositeType.Sort(MetaObjectComparer.ById); foreach (var type in concreteCompositeType) { var atLeastOne = false; var sql = "SELECT " + this.database.Schema.ObjectId + "\n"; sql += "FROM " + this.database.Schema.Table(type.ExclusiveClass) + "\n"; sql += "WHERE " + this.database.Schema.TypeId + "=" + this.database.Schema.TypeId.Param.InvocationName + "\n"; sql += "ORDER BY " + this.database.Schema.ObjectId; using (var command = session.CreateCommand(sql)) { command.AddInParameter(this.database.Schema.TypeId.Param.Name, 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()); this.writer.WriteString(objectId.ToString()); } reader.Close(); } } if (atLeastOne) { this.writer.WriteEndElement(); } } }
protected virtual void TruncateTables(ManagementSession session) { this.ResetSequence(session); foreach (SchemaTable table in Schema) { switch (table.Kind) { case SchemaTableKind.System: case SchemaTableKind.Object: case SchemaTableKind.Relation: this.TruncateTables(session, table); break; } } }
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); } else if (reader.Name.Equals(Serialization.Relations)) { if (!reader.IsEmptyElement) { this.LoadRelations(session); } } 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 LoadObjectsPostProcess(ManagementSession session) { foreach (var type in this.database.MetaPopulation.Composites) { if (type is IClass) { var sql = new StringBuilder(); sql.Append("INSERT INTO " + this.database.Schema.Objects + " (" + this.database.Schema.ObjectId + "," + this.database.Schema.TypeId + "," + this.database.Schema.CacheId + ")\n"); sql.Append("SELECT " + this.database.Schema.ObjectId + "," + this.database.Schema.TypeId + ", " + Reference.InitialVersion + "\n"); sql.Append("FROM " + this.database.Schema.Table(type.ExclusiveClass)); lock (this) { using (var command = session.CreateCommand(sql.ToString())) { command.ExecuteNonQuery(); } } } } }
public 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)) { // TODO: check version // Serialization.CheckVersion(this.xml.Population.Version); if (!this.reader.IsEmptyElement) { this.LoadPopulation(session); } 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 is IComposite) { this.CantLoadUnitRole(relationTypeId); } else { var relationsByExclusiveLeafClass = new Dictionary <IObjectType, List <UnitRelation> >(); this.LoadUnitRelations(relationType, relationsByExclusiveLeafClass); foreach (var dictionaryEntry in relationsByExclusiveLeafClass) { var exclusiveLeafClass = dictionaryEntry.Key; var relations = dictionaryEntry.Value; var loadUnitRelations = session.LoadUnitRelationsFactory.Create(); loadUnitRelations.Execute(relations, exclusiveLeafClass, relationType.RoleType); } } } else if (this.reader.Name.Equals(Serialization.RelationTypeComposite)) { if (relationType == null || relationType.RoleType.ObjectType is IUnit) { this.CantLoadCompositeRole(relationTypeId); } else { var relations = new List <CompositeRelation>(); this.LoadCompositeRelations(relationType, relations); if (relations.Count > 0) { var loadCompositeRelations = session.LoadCompositeRelationsFactory.Create(relationType.RoleType); loadCompositeRelations.Execute(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 DropFunctions(ManagementSession session) { }
protected abstract void DropTable(ManagementSession session, SchemaTable table);
protected abstract void TruncateTables(ManagementSession session, SchemaTable table);
protected virtual void ResetSequence(ManagementSession session) { }
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 is IClass; var objectIdsString = this.reader.ReadString(); var objectIdStringArray = objectIdsString.Split(Serialization.ObjectsSplitterCharArray); var objectIds = new long[objectIdStringArray.Length]; for (var i = 0; i < objectIds.Length; i++) { var objectIdString = objectIdStringArray[i]; var objectId = long.Parse(objectIdString); if (canLoad) { objectIds[i] = objectId; this.objectTypeByObjectId[objectId] = (IClass)objectType; } else { this.OnObjectNotLoaded(objectTypeId, objectId); } } if (canLoad) { var loadObjects = session.LoadObjectsFactory.Create(objectType); loadObjects.Execute((IClass)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 virtual void DropTriggers(ManagementSession session) { }
protected abstract void CreateProcedure(ManagementSession session, SchemaProcedure procedure);
protected abstract void CreateIndex(ManagementSession session, SchemaTable table, SchemaColumn column);
protected virtual void CreateFunctions(ManagementSession session) { }
protected virtual void CreateTriggers(ManagementSession session) { }
protected void SaveRelations(ManagementSession session) { var exclusiveLeafClassesByObjectType = new Dictionary <IObjectType, HashSet <IClass> >(); var relations = new List <IRelationType>(this.database.MetaPopulation.RelationTypes); relations.Sort(MetaObjectComparer.ById); foreach (var relation in relations) { var associationType = relation.AssociationType; if (associationType.ObjectType.ExistClass) { var roleType = relation.RoleType; var sql = string.Empty; if (roleType.ObjectType is IUnit) { HashSet <IClass> exclusiveLeafClasses; if (!exclusiveLeafClassesByObjectType.TryGetValue(associationType.ObjectType, out exclusiveLeafClasses)) { exclusiveLeafClasses = new HashSet <IClass>(); foreach (var concreteClass in associationType.ObjectType.Classes) { exclusiveLeafClasses.Add(concreteClass.ExclusiveClass); } exclusiveLeafClassesByObjectType[associationType.ObjectType] = exclusiveLeafClasses; } var first = true; foreach (var exclusiveLeafClass in exclusiveLeafClasses) { if (first) { first = false; } else { sql += "UNION\n"; } sql += "SELECT " + this.database.Schema.ObjectId + " As " + this.database.Schema.AssociationId + ", " + this.database.Schema.Column(roleType) + " As " + this.database.Schema.RoleId + "\n"; sql += "FROM " + this.database.Schema.Table(exclusiveLeafClass) + "\n"; sql += "WHERE " + this.database.Schema.Column(roleType) + " IS NOT NULL\n"; } sql += "ORDER BY " + this.database.Schema.AssociationId; } else { if ((roleType.IsMany && associationType.IsMany) || !relation.ExistExclusiveClasses) { sql += "SELECT " + this.database.Schema.AssociationId + "," + this.database.Schema.RoleId + "\n"; sql += "FROM " + this.database.Schema.Table(relation) + "\n"; sql += "ORDER BY " + this.database.Schema.AssociationId + "," + this.database.Schema.RoleId; } else { // use foreign keys if (roleType.IsOne) { sql += "SELECT " + this.database.Schema.ObjectId + " As " + this.database.Schema.AssociationId + ", " + this.database.Schema.Column(roleType) + " As " + this.database.Schema.RoleId + "\n"; sql += "FROM " + this.database.Schema.Table(associationType.ObjectType.ExclusiveClass) + "\n"; sql += "WHERE " + this.database.Schema.Column(roleType) + " IS NOT NULL\n"; sql += "ORDER BY " + this.database.Schema.AssociationId; } else { // role.Many var compositeType = (IComposite)roleType.ObjectType; sql += "SELECT " + this.database.Schema.Column(associationType) + " As " + this.database.Schema.AssociationId + ", " + this.database.Schema.ObjectId + " As " + this.database.Schema.RoleId + "\n"; sql += "FROM " + this.database.Schema.Table(compositeType.ExclusiveClass) + "\n"; sql += "WHERE " + this.database.Schema.Column(associationType) + " IS NOT NULL\n"; sql += "ORDER BY " + this.database.Schema.AssociationId + "," + this.database.Schema.RoleId; } } } 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 is IUnit) { var unitType = (IUnit)roleType.ObjectType; var unitTypeTag = unitType.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 virtual void DropUserDefinedTypes(ManagementSession session) { }
protected virtual void DropProcedures(ManagementSession session) { }
public abstract void DropIndex(ManagementSession session, SchemaTable table, SchemaColumn column);