public override DeleteEntityCommand Visit(DeleteEntityCommand c) { _RWL.AcquireWriterLock(Timeout.Infinite); try { Entity processedEntity = _Entities[CacheEngine.GetCacheKey(c.Type, c.ParentId)]; if (processedEntity == null) return c; // Removes the entity from the cache _Entities.Remove(processedEntity); //// Removes also all the references to this entity //foreach (Entity e in _Entities) //{ // if (e != null) // e.RemoveReference(processedEntity); //} } finally { _RWL.ReleaseWriterLock(); } return c; }
public override DeleteEntityCommand Visit(DeleteEntityCommand c) { Entity e = new Entity(SyncUtils.DELETE_ENTITY); PopulateDefaults(e, c); e.SetValue(SyncUtils.PARENTID, c.ParentId); e.SetValue(SyncUtils.TYPE, c.Type); transaction.Serialize(e); return c; }
public void Process(DeleteEntityCommand c) { XmlNode entity = _Document.GetElementById(GetKey(c.Type, c.ParentId)); if (entity == null) throw new UniversalStorageException("The entity to delete was not found"); _Document.DocumentElement.RemoveChild(entity); XmlNodeList referencers = _Document.SelectNodes(String.Format("//Reference[@RefId='{0}']", GetKey(c.Type, c.ParentId))); foreach(XmlNode n in referencers) n.ParentNode.RemoveChild(n); }
public override DeleteEntityCommand Visit(DeleteEntityCommand c) { _RWL.AcquireWriterLock(); try { Entity processedEntity = GetEntity(c.Type, c.ParentId); if (processedEntity == null) return c; _Entities.Remove(MemoryEngine.GetCacheKey(c.Type, c.ParentId)); foreach (Entity entity in _Entities.Values) entity.Remove(processedEntity); } finally { _RWL.ReleaseWriterLock(); } return c; }
internal Command CreateCommand(Entity e) { switch (e.Type) { case SyncUtils.CREATE_ENTITY: CreateEntityCommand ce = new CreateEntityCommand( e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.TYPE) ); return ce; case SyncUtils.DELETE_ENTITY: DeleteEntityCommand de = new DeleteEntityCommand( e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.TYPE) ); return de; case SyncUtils.CREATE_ATTRIBUTE: CreateAttributeCommand ca = new CreateAttributeCommand( e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.PARENTTYPE), e.GetString(SyncUtils.NAME), MetaData.TypeResolver.GetType(e.GetString(SyncUtils.TYPE)), Factory.Serializer.Unserialize(e.GetString(SyncUtils.VALUE)) ); return ca; case SyncUtils.DELETE_ATTRIBUTE: DeleteAttributeCommand da = new DeleteAttributeCommand( e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.PARENTTYPE), e.GetString(SyncUtils.NAME), MetaData.TypeResolver.GetType(e.GetString(SyncUtils.TYPE)), null ); return da; case SyncUtils.UPDATE_ATTRIBUTE: UpdateAttributeCommand ua = new UpdateAttributeCommand( e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.PARENTTYPE), e.GetString(SyncUtils.NAME), MetaData.TypeResolver.GetType(e.GetString(SyncUtils.TYPE)), Factory.Serializer.Unserialize(e.GetString(SyncUtils.VALUE)) ); return ua; case SyncUtils.CREATE_REFERENCE: CreateReferenceCommand cr = new CreateReferenceCommand( e.GetString(SyncUtils.ROLE), e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.PARENTTYPE), e.GetString(SyncUtils.CHILDID), e.GetString(SyncUtils.CHILDTYPE) ); return cr; case SyncUtils.DELETE_REFERENCE: DeleteReferenceCommand dr = new DeleteReferenceCommand( e.GetString(SyncUtils.ROLE), e.GetString(SyncUtils.PARENTID), e.GetString(SyncUtils.PARENTTYPE), e.GetString(SyncUtils.CHILDID), e.GetString(SyncUtils.CHILDTYPE) ); return dr; default: throw new UniversalStorageException("Unexpected command type"); } }
public void Process(DeleteEntityCommand c) { EntityMapping deleteEntityMapping = _Mapping.Entities[c.Type, true]; Hashtable cmds = new Hashtable(); foreach (ReferenceMapping refm in deleteEntityMapping.References) { Evaluant.Uss.Models.Reference reference = _Engine.Model.GetReference(c.Type, refm.Name); EntityMapping entityChildMapping = _Mapping.Entities[refm.EntityChild]; if (!reference.ToMany) continue; foreach (RuleMapping rulem in refm.Rules) { if (rulem.ParentTable == deleteEntityMapping.Table) { // update child table SQLCommand cmd = (SQLCommand)cmds[rulem.ChildTable]; EntityMapping childEntity = _Mapping.Entities[refm.EntityChild]; if (!reference.IsComposition && rulem.ChildTable == childEntity.Table) { if (cmd == null) { cmd = new UpdateCommand(rulem, rulem.ChildTable); cmds.Add(rulem.ChildTable, cmd); } } else { if (cmd == null) { cmd = new DeleteCommand(rulem, rulem.ChildTable); cmds.Add(rulem.ChildTable, cmd); } } //if (cmd is DeleteCommand) // continue; for (int i = 0; i < rulem.ParentFields.Length; i++) { DbType dbType; if (deleteEntityMapping.IdFields.Contains(refm.Rules[0].ParentFields[i])) { dbType = _Dialect.GetDbTypeToPrimaryKey(deleteEntityMapping.Ids[refm.Rules[0].ParentFields[i]].Generator); } else { dbType = _Dialect.GetDbTypeToPrimaryKey(childEntity.Ids[refm.Rules[0].ChildFields[i]].Generator); } Constant newValue; if (rulem.ParentDefaultValues.Length > i && !string.IsNullOrEmpty(rulem.ParentDefaultValues[i])) newValue = new Constant(rulem.ParentDefaultValues[i], dbType); else newValue = new Constant(DBNull.Value, dbType); // SSX 28/01/08: add contains test if (entityChildMapping.Ids[rulem.ChildFields[i]] == null && cmd is UpdateCommand && !((UpdateCommand)cmd).ColumnValueCollection.Contains(rulem.ChildFields[i])) ((UpdateCommand)cmd).ColumnValueCollection.Add(rulem.ChildFields[i], newValue); Column childField = new Column(rulem, rulem.ChildFields[i]); Constant childParameter = new Constant(ConvertId(deleteEntityMapping, rulem.ParentFields[i], c.ParentId), dbType); BinaryLogicExpression parentClause = new BinaryLogicExpression(childField, BinaryLogicOperator.Equals, childParameter); if (cmd is UpdateCommand) ((UpdateCommand)cmd).WhereClause.SearchCondition.Add(parentClause); else ((DeleteCommand)cmd).Condition.SearchCondition.Add(parentClause); } if (!string.IsNullOrEmpty(rulem.Constraint)) DisableForeignKeys(rulem.ChildTable); } } } // Deletes all references to and from it first foreach (EntityMapping em in _Mapping.Entities) { foreach (ReferenceMapping refm in em.References) { //Evaluant.Uss.Models.Model childModel = _Engine.Model.GetEntity( if (refm.EntityChild == deleteEntityMapping.Type) // To { for (int indexRule = 0; indexRule < refm.Rules.Count; indexRule++) { RuleMapping rulem = refm.Rules[indexRule]; if ((rulem.ChildField == deleteEntityMapping.IdFields && deleteEntityMapping.DiscriminatorField == null) || (rulem.ChildField + ";" + deleteEntityMapping.DiscriminatorField == deleteEntityMapping.IdFields && deleteEntityMapping.DiscriminatorField != null)) { // delete childtable if (cmds.ContainsKey(rulem.ChildTable)) continue; DeleteCommand cmd = new DeleteCommand(rulem, rulem.ChildTable); cmds.Add(rulem.ChildTable, cmd); for (int index = 0; index < rulem.ChildFields.Length; index++) { DbType dbType = _Dialect.GetDbTypeToPrimaryKey(deleteEntityMapping.Ids[index].Generator); cmd.Condition.SearchCondition.Add(new BinaryLogicExpression( new Column(rulem, rulem.ChildFields[index]), BinaryLogicOperator.Equals, new Constant(ConvertId(deleteEntityMapping, rulem.ChildFields[index], c.ParentId), dbType) )); } if (!string.IsNullOrEmpty(refm.DiscriminatorField) && indexRule == refm.Rules.Count - 1) { cmd.Condition.SearchCondition.Add(new BinaryLogicExpression( new Column(rulem, refm.DiscriminatorField), BinaryLogicOperator.Equals, new Parameter(rulem, refm.DiscriminatorValue) )); } if (!string.IsNullOrEmpty(rulem.Constraint)) DisableForeignKeys(rulem.ChildTable); } if (rulem.ParentField != em.IdFields && em.DiscriminatorField != null || rulem.ParentTable != em.Table) { // update parent table SQLCommand cmd = (SQLCommand)cmds[rulem.ParentTable]; if (cmd == null) { cmd = new UpdateCommand(rulem, rulem.ParentTable); cmds.Add(rulem.ParentTable, cmd); } if (cmd is DeleteCommand) continue; for (int i = 0; i < rulem.ParentFields.Length; i++) { DbType dbType = _Dialect.GetDbTypeToPrimaryKey(deleteEntityMapping.Ids[i].Generator); Constant newValue = new Constant(DBNull.Value, dbType); if (rulem.ParentDefaultValues.Length > i && !string.IsNullOrEmpty(rulem.ParentDefaultValues[i])) newValue = new Constant(rulem.ParentDefaultValues[i], dbType); // SSX 28/01/08: add contains test if (em.Ids[rulem.ParentFields[i]] == null && !((UpdateCommand)cmd).ColumnValueCollection.Contains(rulem.ParentFields[i])) ((UpdateCommand)cmd).ColumnValueCollection.Add(rulem.ParentFields[i], newValue); Column parentField = new Column(rulem, rulem.ParentFields[i]); Constant parentParameter = new Constant(ConvertId(deleteEntityMapping, rulem.ParentFields[i], c.ParentId), dbType); BinaryLogicExpression parentClause = new BinaryLogicExpression(parentField, BinaryLogicOperator.Equals, parentParameter); ((UpdateCommand)cmd).WhereClause.SearchCondition.Add(parentClause); } // UPDATE parentTable SET parentField = NULL WHERE (parentField = @parentValue AND discriminator = 'name's relation') if (!string.IsNullOrEmpty(refm.DiscriminatorField) && indexRule == refm.Rules.Count - 1) { Column discriminatorField = new Column(rulem, rulem.ParentField); Constant discriminatorValue = new Constant(refm.DiscriminatorValue, DbType.AnsiString); BinaryLogicExpression discriminatorClause = new BinaryLogicExpression(discriminatorField, BinaryLogicOperator.Equals, discriminatorValue); ((UpdateCommand)cmd).WhereClause.SearchCondition.Add(discriminatorClause); } if (!string.IsNullOrEmpty(rulem.Constraint)) DisableForeignKeys(rulem.ParentTable); } } } } } // Deletes all attributes if they are in a separate table if (deleteEntityMapping.Attributes != null) { foreach (AttributeMapping am in deleteEntityMapping.Attributes) { if (am.Table != deleteEntityMapping.Table) { DeleteCommand cmd = new DeleteCommand(am, am.Table); for (int index = 0; index < deleteEntityMapping.Ids.Count; index++) { PrimaryKeyMapping pkm = deleteEntityMapping.Ids[index]; cmd.Condition.SearchCondition.Add(new BinaryLogicExpression( new Column(am, am.ParentFields[index]), BinaryLogicOperator.Equals, new Constant(ConvertId(deleteEntityMapping, pkm.Field, c.ParentId), _Dialect.GetDbTypeToPrimaryKey(pkm.Generator)))); } cmds.Add(am.Table, cmd); } } } // SSX 28/01/08: remove table if already existing if (cmds.Contains(deleteEntityMapping.Table)) { cmds.Remove(deleteEntityMapping.Table); } if (!cmds.ContainsKey(deleteEntityMapping.Table)) { DeleteCommand cmd = new DeleteCommand(deleteEntityMapping, deleteEntityMapping.Table); for (int index = 0; index < deleteEntityMapping.Ids.Count; index++) { PrimaryKeyMapping pkm = deleteEntityMapping.Ids[index]; cmd.Condition.SearchCondition.Add(new BinaryLogicExpression( new Column(deleteEntityMapping, pkm.Field), BinaryLogicOperator.Equals, new Constant(ConvertId(deleteEntityMapping, pkm.Field, c.ParentId), _Dialect.GetDbTypeToPrimaryKey(pkm.Generator)))); } cmds.Add(deleteEntityMapping.Table, cmd); } foreach (string tableName in cmds.Keys) { foreach (string query in _Dialect.RenderQueries((ISQLExpression)cmds[tableName])) { if (!string.IsNullOrEmpty(query)) { IDbCommand cmd = _Driver.CreateCommand(query, _Connection, _Transaction); if (_Engine.TraceSqlSwitch.Enabled) TraceHelpler.Trace(cmd, _Dialect); cmd.ExecuteNonQuery(); } } } }
public abstract DeleteEntityCommand Visit(DeleteEntityCommand item);
public override DeleteEntityCommand Visit(DeleteEntityCommand item) { throw new NotImplementedException(); }