public static void Clear(object container, Hashtable table) { Triple t = (Triple)table[container]; if (t == null) { goto exit; } IPersistenceCapable parent = t.Parent as IPersistenceCapable; if (parent == null) { goto exit; } string field = t.Field; IStateManager stateManager; if ((stateManager = parent.NDOStateManager) == null) { goto exit; } stateManager.RemoveRangeRelatedObjects(parent, field, (IList)container); exit: ((IList)container).Clear(); }
public void TestIfMultikeysCanBeConverted() { var guid = new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); var guidString = guid.ToString().Replace("-", ""); IPersistenceCapable orderDetail = pm.FindObject(typeof(OrderDetail), new object[] { guid, guid }); string shortId = ((IPersistenceCapable)orderDetail).ShortId(); Assert.That(shortId.IsShortId()); Assert.That(shortId.GetEntityName() == "OrderDetail"); Assert.That(shortId.GetObjectType(pm) == typeof(OrderDetail)); string[] arr = shortId.Split('-'); Assert.AreEqual($"{guidString}+{guidString}", arr[2]); var decodedShortId = shortId.Decode(); arr = decodedShortId.Split('-'); Assert.AreEqual($"{guidString} {guidString}", arr[2]); #if !USEGUIDS orderDetail = pm.FindObject(typeof(OrderDetail), new object[] { 1, 2 }); shortId = orderDetail.ShortId(); #endif orderDetail = pm.FindObject(shortId); #if USEGUIDS Assert.AreEqual(guid, orderDetail.NDOObjectId.Id.Values[0]); Assert.AreEqual(guid, orderDetail.NDOObjectId.Id.Values[1]); #else Assert.AreEqual(1, orderDetail.NDOObjectId.Id.Values[0]); Assert.AreEqual(2, orderDetail.NDOObjectId.Id.Values[1]); #endif }
/// <summary> /// Constructs a RelationChangeRecord object. /// </summary> /// <param name="parent">Parent object.</param> /// <param name="child">Child object.</param> /// <param name="relationName">Field name of the relation.</param> /// <param name="isAdded">True, if the child object was added, false, if it was deleted.</param> public RelationChangeRecord(IPersistenceCapable parent, IPersistenceCapable child, string relationName, bool isAdded) { this.parent = parent; this.child = child; this.relationName = relationName; this.isAdded = isAdded; }
/// <summary> /// Unlock an object /// </summary> /// <param name="pc">The object to unlock</param> public void Unlock(IPersistenceCapable pc) { Entry e = GetEntry(pc); lockedObjects.Remove(pc.NDOObjectId); objects.Add(pc.NDOObjectId, new WeakReference <IPersistenceCapable>(pc)); }
public static void RemoveAt(object container, int index, Hashtable table) { Triple t = (Triple)table[container]; if (t == null) { goto exit; } IPersistenceCapable parent = t.Parent as IPersistenceCapable; if (parent == null) { goto exit; } string field = t.Field; IStateManager stateManager; if ((stateManager = parent.NDOStateManager) == null) { goto exit; } IPersistenceCapable element = (IPersistenceCapable)((IList)container)[index]; stateManager.RemoveRelatedObject(parent, field, (IPersistenceCapable)element); exit: ((IList)container).RemoveAt(index); }
/// <summary> /// Registers a child object /// </summary> /// <param name="parent"></param> /// <param name="container"></param> /// <param name="table"></param> /// <param name="field"></param> /// <returns></returns> public static object RegisterContainer(object parent, object container, Hashtable table, string field) { IPersistenceCapable pc = parent as IPersistenceCapable; if (pc == null) { goto exit; } if (pc.NDOStateManager == null || pc.NDOObjectState == NDOObjectState.Transient) { goto exit; } if (pc.NDOObjectState != NDOObjectState.Created) { IList result = pc.NDOStateManager.LoadRelation(pc, field); if (result != null) { container = result; } } if (container == null) { goto exit; } if (!table.Contains(container)) { table.Add(container, new Triple(parent, field)); } exit: return(container); }
public void RemoveRangeRelatedObjects(IPersistenceCapable pc, string fieldName, IList relatedObjects) { Debug.Assert(pc.NDOObjectState != NDOObjectState.Transient, "Transient object shouldn't have a state manager"); foreach (IPersistenceCapable relObj in relatedObjects) { pm.RemoveRelatedObject(pc, fieldName, relObj); } }
public bool IsRegistered(IPersistenceCapable pc) { if (pc.NDOObjectId == null) { throw new InternalException(60, "Cache.IsRegistered: ObjectId of an object of type " + pc.GetType().FullName + " is null."); } return(objects.ContainsKey(pc.NDOObjectId) || lockedObjects.ContainsKey(pc.NDOObjectId)); }
/// <summary> /// Remove an object from the locked objects cache /// </summary> /// <param name="pc">The object to remove</param> public void DeregisterLockedObject(IPersistenceCapable pc) { if (pc.NDOObjectId == null) { throw new InternalException(60, "Cache.DeregisterLockedObject: ObjectId of an object of type " + pc.GetType().FullName + " is null."); } lockedObjects.Remove(pc.NDOObjectId); }
/// <summary> /// Lock an object for use in a transaction; store the relatetd DataRow /// </summary> /// <param name="pc">The object to lock</param> /// <param name="row">The DataRow</param> /// <param name="relations">Relation info for the Object</param> public void Lock(IPersistenceCapable pc, DataRow row, List <KeyValuePair <Relation, object> > relations) { if (pc.NDOObjectId == null) { throw new InternalException(60, "Cache.Lock: ObjectId of an object of type " + pc.GetType().FullName + " is null."); } objects.Remove(pc.NDOObjectId); lockedObjects.Add(pc.NDOObjectId, new Entry(pc, row, relations)); }
public void Unlock(IPersistenceCapable pc) { PcWrapper pcw = new PcWrapper(pc); if (objects.Contains(pcw)) { objects.Remove(pcw); } }
/// <summary> /// Checks whether an object is an IPersistenceCapable and converts the object into an IPersistenceCapable. /// </summary> /// <param name="o"></param> /// <returns></returns> /// <remarks>Throws an NDOException, if the object can't be converted.</remarks> protected internal IPersistenceCapable CheckPc(object o) { IPersistenceCapable pc = o as IPersistenceCapable; if (pc == null && !(o == null)) { throw new NDOException(31, "Parameter should implement IPersistenceCapable. Check, if the type " + o.GetType().FullName + "," + o.GetType().Assembly.FullName + " is enhanced."); } return(pc); }
public void Unlock(IPersistenceCapable pc, Relation r, IPersistenceCapable child) { // Debug.WriteLine("Unlock: " + pc.GetType().Name + " " + r.FieldName + " " + child.GetType().Name); Triple t = new Triple(pc, r, child); if (relobjects.Contains(t)) { relobjects.Remove(t); } }
/// <summary> /// Gets a DataRow for a given object; if necessary the row will be constructed /// </summary> /// <param name="pc">Persistence capable object</param> /// <returns></returns> protected DataRow GetDataRow(IPersistenceCapable pc) { DataRow row; if ((row = this.cache.GetDataRow(pc)) != null) { return(row); } return(null); }
Entry GetEntry(IPersistenceCapable pc) { if (pc.NDOObjectId == null) { throw new InternalException(60, "Cache.GetEntry: ObjectId of an object of type " + pc.GetType().FullName + " is null."); } Entry e = null; lockedObjects.TryGetValue(pc.NDOObjectId, out e); return(e); }
/// <summary> /// Marks an object as dirty, if the enhanced code isn't able to do so by itself. /// </summary> /// <param name="pc">An object of a persistent class.</param> public static void MarkDirty(object pc) { IPersistenceCapable ipc = (IPersistenceCapable)pc; IStateManager sm = ipc.NDOStateManager; if (sm == null) { return; } sm.MarkDirty(ipc); }
public bool GetLock(IPersistenceCapable pc) { PcWrapper pcw = new PcWrapper(pc); if (objects.Contains(pcw)) { return(false); } objects.Add(pcw, null); return(true); }
/// <summary> /// Look for a FieldInfo in the given object; search all base types for the FieldInfo /// </summary> /// <param name="pc">Object to search the FieldInfo</param> /// <param name="name">Name of the field</param> /// <returns>The FieldInfo of the given field; An exception will be thrown, if the field can't be found.</returns> private FieldInfo GetFieldInfo(IPersistenceCapable pc, string name) { Type t = pc.GetType(); FieldInfo fi = new BaseClassReflector(t).GetField(name, BindingFlags.Instance | BindingFlags.NonPublic); if (fi == null) { throw new NDOException(9, "Invalid mapping: unknown field name " + name + " in type " + t); } return(fi); }
public static void SetRange(object container, int index, ICollection coll, Hashtable table) { if (container == null) { throw new NDOException(3, "SetRange: container is null"); } Triple t = (Triple)table[container]; if (t == null) { goto exit; } IPersistenceCapable parent = t.Parent as IPersistenceCapable; if (parent == null) { goto exit; } string field = t.Field; IStateManager stateManager; if ((stateManager = parent.NDOStateManager) == null) { goto exit; } if (coll == null) { goto exit; } int lastIndex = index + coll.Count; if (lastIndex > ((IList)container).Count) { ArgumentOutOfRangeException ex = new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException(string.Empty, "SetRange: " + ex.Message); } for (int i = index; i < lastIndex; i++) { stateManager.RemoveRelatedObject(parent, field, (IPersistenceCapable)((IList)container)[i]); } foreach (IPersistenceCapable pc in coll) { stateManager.AddRelatedObject(parent, field, pc); } exit: ((ArrayList)container).SetRange(index, coll); }
/// <summary> /// Adds a new object to the DataSource, the BindingSource is bound to. /// The object should be a persistent object. If it is in the transient state it will be made persistent. /// </summary> /// <param name="value">The object to be added.</param> /// <returns>The index of the created object in the DataSource.</returns> public override int Add(object value) { // Allows adding persistent objects, so make it persistent only, // if it is not yet persistent. IPersistenceCapable pc = (IPersistenceCapable)value; if (pc.NDOObjectState == NDOObjectState.Transient) { this.pm.MakePersistent(pc); this.pm.Save(); } return(base.Add(value)); }
/// <summary> /// Locks a relation and determines, whether the relation was locked before /// </summary> /// <param name="pc">Parent object</param> /// <param name="r">Relation</param> /// <param name="child">Child object</param> /// <returns>True, if the relation was locked before the function was called.</returns> public bool GetLock(IPersistenceCapable pc, Relation r, IPersistenceCapable child) { // Debug.WriteLine("Lock: " + pc.GetType().Name + " " + r.FieldName + " " + child.GetType().Name); Triple t = new Triple(pc, r, child); //int hash = this.GetHashCode(); seems to be debug code if (this.IsLocked(t)) { return(false); } relobjects.Add(t, null); return(true); }
public static void InsertRange(object container, int index, IEnumerable coll, Hashtable table) { if (container == null) { throw new NDOException(2, "InsertRange: container is null"); } Triple t = (Triple)table[container]; if (t == null) { goto exit; } IPersistenceCapable parent = t.Parent as IPersistenceCapable; if (parent == null) { goto exit; } string field = t.Field; IStateManager stateManager; if ((stateManager = parent.NDOStateManager) == null) { goto exit; } if (coll == null) { goto exit; } foreach (IPersistenceCapable pc in coll) { stateManager.AddRelatedObject(parent, field, pc); } exit: ArrayList temp = new ArrayList(); foreach (object o in coll) { temp.Add(o); } for (int i = temp.Count - 1; i >= 0; i--) { ((IList)container).Insert(index, temp[i]); } }
/// <summary> /// Iterate through an object tree to load child object instances. /// </summary> /// <param name="root">The root object of the object tree.</param> public void Iterate(IPersistenceCapable root) { this.pm = ObjectHelper.GetPersistenceManager(root); this.mapping = (Mappings)this.pm.NDOMapping; if (root.NDOObjectState == NDOObjectState.Hollow) { this.pm.LoadData(root); } Search(root); if (actionOnIncludedObject != null) { actionOnIncludedObject(root); } }
private void Search(IPersistenceCapable pc) { if (objects.Contains(pc.NDOObjectId)) { return; } objects.Add(pc.NDOObjectId, pc); pc.NDOStateManager = this.stateManager; if (pc.NDOObjectState == NDOObjectState.Persistent) { if (!this.cache.IsRegistered(pc)) { this.cache.Register(pc); } } Class cl = mapping.FindClass(pc.GetType()); if (pc.NDOLoadState.RelationLoadState == null) { pc.NDOLoadState.RelationLoadState = new BitArray(64); } foreach (Relation r in cl.Relations) { if (r.Multiplicity == RelationMultiplicity.Element) { IPersistenceCapable relObj = mapping.GetRelationField(pc, r.FieldName) as IPersistenceCapable; if (relObj != null) { Search(relObj); } } else { IList l = mapping.GetRelationContainer(pc, r); if (l != null) { foreach (IPersistenceCapable relObj2 in l) { Search(relObj2); } } } } }
public void TestCreateDeleteTransitionAbort() { pm.MakePersistent(m); ObjectId id = m.NDOObjectId; pm.Delete(m); pm.Abort(); Assert.Null(m.NDOObjectId, "Transient object shouldn't have ID"); Assert.Null(((IPersistenceCapable)m).NDOStateManager, "Transient object shouldn't have state manager"); Assert.AreEqual(NDOObjectState.Transient, m.NDOObjectState, "Status wrong"); IPersistenceCapable pc = pm.FindObject(id); Assert.NotNull(pc, "There should be a hollow object."); Assert.AreEqual(NDOObjectState.Hollow, pc.NDOObjectState, "Status wrong"); }
private void Search(IPersistenceCapable pc) { if (objects.Contains(pc)) { return; } objects.Add(pc, null); if (this.relationSelector != null) { Class cl = mapping.FindClass(pc.GetType()); foreach (Relation r in cl.Relations) { if (this.relationSelector(r)) { pm.LoadRelation(pc, r.FieldName, false); if (r.Multiplicity == RelationMultiplicity.Element) { IPersistenceCapable relObj = mapping.GetRelationField(pc, r.FieldName) as IPersistenceCapable; if (relObj == null) { continue; } if (relObj.NDOObjectState == NDOObjectState.Hollow) { pm.LoadData(relObj); } Search(relObj); this.actionOnIncludedObject?.Invoke(relObj); } else { IList l = mapping.GetRelationContainer(pc, r); foreach (IPersistenceCapable relObj2 in l) { if (relObj2.NDOObjectState == NDOObjectState.Hollow) { pm.LoadData(relObj2); } Search(relObj2); this.actionOnIncludedObject?.Invoke(relObj2); } } } } } }
object DeserializeRootArray(JToken rootArray) { var rootObjectsToken = (JArray)rootArray["rootObjects"]; var additionalObjectsToken = (JArray)rootArray["additionalObjects"]; if (rootObjectsToken.Count >= 4 && rootObjectsToken[0] is JArray) { return(DeserializeChangeSetContainer(rootObjectsToken)); } List <IPersistenceCapable> rootObjects = new List <IPersistenceCapable>(); List <IPersistenceCapable> additionalObjects = new List <IPersistenceCapable>(); foreach (var jObj in rootObjectsToken) { IPersistenceCapable e = DeserializeObject(jObj); if (e != null) { rootObjects.Add(e); } } foreach (var jObj in additionalObjectsToken) { IPersistenceCapable e = DeserializeObject(jObj); if (e != null) { additionalObjects.Add(e); } } foreach (var jObj in rootObjectsToken) { var shortId = (string)jObj["_oid"]; IPersistenceCapable e = rootObjects.First(o => ((IPersistenceCapable)o).ShortId() == shortId); FixRelations(jObj, e, rootObjects, additionalObjects); } foreach (var jObj in additionalObjectsToken) { var shortId = (string)jObj["_oid"]; IPersistenceCapable e = additionalObjects.First(o => ((IPersistenceCapable)o).ShortId() == shortId); FixRelations(jObj, e, rootObjects, additionalObjects); } return(new ArrayList(rootObjects)); }
void FixRelations(JToken jObj, IPersistenceCapable e, List <IPersistenceCapable> rootObjects, List <IPersistenceCapable> additionalObjects) { var t = e.GetType(); FieldMap fm = new FieldMap(t); var mc = Metaclasses.GetClass(t); foreach (var fi in fm.Relations) { var token = jObj[fi.Name]; if (token == null) { continue; } bool isArray = typeof(IList).IsAssignableFrom(fi.FieldType); if (token is JArray jarray) { if (isArray) { IList container = (IList)fi.GetValue(e); if (container == null) { throw new NDOException(20002, $"Container object of relation {t.Name}.{fi.Name} is not initialized. Please initialize the field in your class constructor."); } container.Clear(); foreach (var relJObj in jarray) { container.Add(DeserializeObject(relJObj)); } } } else { if (!isArray) { if (token.Type == JTokenType.Null) { fi.SetValue(e, null); } else { fi.SetValue(e, DeserializeObject(token)); } } } } }
public static void RemoveRange(object container, int index, int count, Hashtable table) { if (container == null) { throw new NDOException(4, "RemoveRange: container is null"); } Triple t = (Triple)table[container]; if (t == null) { goto exit; } IPersistenceCapable parent = t.Parent as IPersistenceCapable; if (parent == null) { goto exit; } string field = t.Field; IStateManager stateManager; if ((stateManager = parent.NDOStateManager) == null) { goto exit; } int lastIndex = index + count; if (lastIndex > ((IList)container).Count) { ArgumentOutOfRangeException ex = new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException(string.Empty, "RemoveRange: " + ex.Message); } for (int i = index; i < lastIndex; i++) { stateManager.RemoveRelatedObject(parent, field, (IPersistenceCapable)((IList)container)[i]); } exit: for (int i = 0; i < count; i++) { ((IList)container).RemoveAt(index); } }
ChangeSetContainer CreateChangeSet(bool acceptChanges) { ChangeSetContainer csc = new ChangeSetContainer(); var addedObjects = new List <IPersistenceCapable>(); var deletedObjects = new List <ObjectId>(); var changedObjects = new List <IPersistenceCapable>(); foreach (var cacheEntry in cache.LockedObjects) { IPersistenceCapable pc = cacheEntry.pc; if (pc.NDOObjectState == NDOObjectState.Created) { addedObjects.Add(pc); if (acceptChanges) { pc.NDOObjectState = NDOObjectState.Persistent; } } else if (pc.NDOObjectState == NDOObjectState.Deleted) { deletedObjects.Add(pc.NDOObjectId); if (acceptChanges) { pc.NDOObjectState = NDOObjectState.Transient; } } else if (pc.NDOObjectState == NDOObjectState.PersistentDirty) { changedObjects.Add(pc); if (acceptChanges) { pc.NDOObjectState = NDOObjectState.Persistent; } } } csc.RelationChanges = RelationChanges.ToList(); csc.ChangedObjects = changedObjects; csc.AddedObjects = addedObjects; csc.DeletedObjects = deletedObjects; if (acceptChanges) { cache.UnlockAll(); RelationChanges.Clear(); } return(csc); }