private static int PrimaryKeyCompare(SoodaObject dbo1, SoodaObject dbo2)
 {
     return ((IComparable)dbo1.GetPrimaryKeyValue()).CompareTo(dbo2.GetPrimaryKeyValue());
 }
Example #2
0
        internal void SetRefFieldValue(int tableNumber, string fieldName, int fieldOrdinal, SoodaObject newValue, SoodaObject[] refcache, int refCacheOrdinal, ISoodaObjectFactory factory)
        {
            if (newValue != null)
            {
                // transaction check
                if (newValue.GetTransaction() != this.GetTransaction())
                    throw new SoodaException("Attempted to assign object " + newValue.GetObjectKeyString() + " from another transaction to " + this.GetObjectKeyString() + "." + fieldName);
            }

            EnsureFieldsInited();
            EnsureDataLoaded(tableNumber);

            SoodaObject oldValue = null;

            SoodaObjectImpl.GetRefFieldValue(ref oldValue, this, tableNumber, fieldOrdinal, GetTransaction(), factory);
            if (Object.Equals(oldValue, newValue))
                return;
            object[] triggerArgs = new object[] { oldValue, newValue };

            if (AreFieldUpdateTriggersEnabled())
            {
                MethodInfo mi = this.GetType().GetMethod("BeforeFieldUpdate_" + fieldName, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public);
                if (mi != null)
                    mi.Invoke(this, triggerArgs);
            }
            Sooda.Schema.FieldInfo fieldInfo = GetClassInfo().UnifiedFields[fieldOrdinal];
            StringCollection backRefCollections = GetTransaction().Schema.GetBackRefCollections(fieldInfo);
            if (oldValue != null && backRefCollections != null)
            {
                foreach (string collectionName in backRefCollections)
                {
                    PropertyInfo coll = oldValue.GetType().GetProperty(collectionName, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public);
                    if (coll == null)
                        throw new Exception(collectionName + " not found in " + oldValue.GetType().Name + " while setting " + this.GetType().Name + "." + fieldName);
                    ISoodaObjectListInternal listInternal = (ISoodaObjectListInternal)coll.GetValue(oldValue, null);
                    listInternal.InternalRemove(this);
                }
            }
            SetFieldValue(fieldOrdinal, newValue != null ? newValue.GetPrimaryKeyValue() : null);
            refcache[refCacheOrdinal] = null;
            if (newValue != null && backRefCollections != null)
            {
                foreach (string collectionName in backRefCollections)
                {
                    PropertyInfo coll = newValue.GetType().GetProperty(collectionName, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public);
                    if (coll == null)
                        throw new Exception(collectionName + " not found in " + newValue.GetType().Name + " while setting " + this.GetType().Name + "." + fieldName);
                    ISoodaObjectListInternal listInternal = (ISoodaObjectListInternal)coll.GetValue(newValue, null);
                    listInternal.InternalAdd(this);
                }
            }
            if (AreFieldUpdateTriggersEnabled())
            {
                MethodInfo mi = this.GetType().GetMethod("AfterFieldUpdate_" + fieldName, BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public);
                if (mi != null)
                    mi.Invoke(this, triggerArgs);
            }
        }
        static int Compare(SoodaObject o1, SoodaObject o2)
        {
            int retval = string.CompareOrdinal(o1.GetClassInfo().Name, o2.GetClassInfo().Name);
            if (retval != 0)
                return retval;

            return ((IComparable) o1.GetPrimaryKeyValue()).CompareTo(o2.GetPrimaryKeyValue());
        }
        protected internal void UnregisterObject(SoodaObject o)
        {
            object pkValue = o.GetPrimaryKeyValue();

            if (ExistsObjectWithKey(o.GetClassInfo().Name, pkValue))
            {
                UnregisterObjectWithKey(o.GetClassInfo().Name, pkValue);
                for (ClassInfo ci = o.GetClassInfo().InheritsFromClass; ci != null; ci = ci.InheritsFromClass)
                {
                    UnregisterObjectWithKey(ci.Name, pkValue);
                }
                RemoveWeakSoodaObjectFromCollection(_objectList, o);

                List<WeakSoodaObject> al;
                if (_objectsByClass.TryGetValue(o.GetClassInfo().Name, out al))
                {
                    RemoveWeakSoodaObjectFromCollection(al, o);
                }
            }
        }
        protected internal bool IsRegistered(SoodaObject o)
        {
            object pkValue = o.GetPrimaryKeyValue();

            return ExistsObjectWithKey(o.GetClassInfo().Name, pkValue);
        }
        protected internal void RegisterObject(SoodaObject o)
        {
            // Console.WriteLine("Registering object {0}...", o.GetObjectKey());

            object pkValue = o.GetPrimaryKeyValue();
            // Console.WriteLine("Adding key: " + o.GetObjectKey() + " of type " + o.GetType());
            for (ClassInfo ci = o.GetClassInfo(); ci != null; ci = ci.InheritsFromClass)
            {
                AddObjectWithKey(ci.Name, pkValue, o);

                List<WeakSoodaObject> al;
                if (!_objectsByClass.TryGetValue(ci.Name, out al))
                {
                    al = new List<WeakSoodaObject>();
                    _objectsByClass[ci.Name] = al;
                }
                al.Add(new WeakSoodaObject(o));
            }

            if (!UseWeakReferences)
                _strongReferences.Add(o);

            _objectList.Add(new WeakSoodaObject(o));

            if (_precommitQueue != null)
                _precommitQueue.Enqueue(o);

        }