Пример #1
0
        internal RealmObject MakeObjectForRow(Type objectType, RowHandle rowHandle)
        {
            RealmObject ret = Metadata[objectType].Helper.CreateInstance();

            ret._Manage(this, rowHandle);
            return(ret);
        }
Пример #2
0
 private void AddObjectToRealmIfNeeded(RealmObject obj)
 {
     if (!obj.IsManaged)
     {
         _realm.Add(obj);
     }
 }
Пример #3
0
        internal RealmObject MakeObjectForRow(RealmObject.Metadata metadata, RowHandle row)
        {
            RealmObject ret = metadata.Helper.CreateInstance();

            ret._Manage(this, row, metadata);
            return(ret);
        }
Пример #4
0
        private void AddInternal(RealmObject obj, Type objectType, bool update)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (objectType == null)
            {
                throw new ArgumentNullException(nameof(objectType));
            }

            if (obj.IsManaged)
            {
                if (obj.Realm.SharedRealmHandle == this.SharedRealmHandle)
                {
                    // Already managed by this realm, so nothing to do.
                    return;
                }

                throw new RealmObjectManagedByAnotherRealmException("Cannot start to manage an object with a realm when it's already managed by another realm");
            }

            if (!Metadata.TryGetValue(objectType.Name, out var metadata))
            {
                throw new ArgumentException($"The class {objectType.Name} is not in the limited set of classes for this realm");
            }

            var objectPtr = IntPtr.Zero;

            if (update && metadata.Helper.TryGetPrimaryKeyValue(obj, out var pkValue))
            {
                switch (pkValue)
                {
                    case string stringValue:
                        objectPtr = metadata.Table.Find(SharedRealmHandle, stringValue);
                        break;
                    case null:
                        objectPtr = metadata.Table.Find(SharedRealmHandle, (long?)null);
                        break;
                    default:
                        // We know it must be convertible to long, so optimistically do it.
                        objectPtr = metadata.Table.Find(SharedRealmHandle, Convert.ToInt64(pkValue));
                        break;
                }
            }

            var setPrimaryKey = false;
            if (objectPtr == IntPtr.Zero)
            {
                objectPtr = metadata.Table.AddEmptyObject(SharedRealmHandle);
                setPrimaryKey = true;
            }

            var objectHandle = CreateObjectHandle(objectPtr, SharedRealmHandle);

            obj._SetOwner(this, objectHandle, metadata);
            metadata.Helper.CopyToRealm(obj, update, setPrimaryKey);
            obj.OnManaged();
        }
Пример #5
0
        /// <summary>
        /// This <see cref="Realm"/> will start managing a <see cref="RealmObject"/> which has been created as a standalone object.
        /// </summary>
        /// <param name="obj">Must be a standalone object, <c>null</c> not allowed.</param>
        /// <param name="update">If <c>true</c>, and an object with the same primary key already exists, performs an update.</param>
        /// <exception cref="RealmInvalidTransactionException">
        /// If you invoke this when there is no write <see cref="Transaction"/> active on the <see cref="Realm"/>.
        /// </exception>
        /// <exception cref="RealmObjectManagedByAnotherRealmException">
        /// You can't manage an object with more than one <see cref="Realm"/>.
        /// </exception>
        /// <remarks>
        /// If the object is already managed by this <see cref="Realm"/>, this method does nothing.
        /// This method modifies the object in-place, meaning that after it has run, <c>obj</c> will be managed.
        /// Cyclic graphs (<c>Parent</c> has <c>Child</c> that has a <c>Parent</c>) will result in undefined behavior.
        /// You have to break the cycle manually and assign relationships after all object have been managed.
        /// </remarks>
        /// <returns>The passed object.</returns>
        public RealmObject Add(RealmObject obj, bool update = false)
        {
            ThrowIfDisposed();

            AddInternal(obj, obj?.GetType(), update);
            return(obj);
        }
Пример #6
0
        /// <summary>
        /// Removes a persistent object from this Realm, effectively deleting it.
        /// </summary>
        /// <param name="obj">Must be an object persisted in this Realm.</param>
        /// <exception cref="RealmInvalidTransactionException">
        /// If you invoke this when there is no write <see cref="Transaction"/> active on the <see cref="Realm"/>.
        /// </exception>
        /// <exception cref="ArgumentNullException">If <c>obj</c> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If you pass a standalone object.</exception>
        public void Remove(RealmObject obj)
        {
            ThrowIfDisposed();

            Argument.NotNull(obj, nameof(obj));
            Argument.Ensure(obj.IsManaged, "Object is not managed by Realm, so it cannot be removed.", nameof(obj));

            obj.ObjectHandle.RemoveFromRealm(SharedRealmHandle);
        }
Пример #7
0
        private void AddInternal(RealmObject obj, Type objectType, bool update)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (objectType == null)
            {
                throw new ArgumentNullException(nameof(objectType));
            }

            if (obj.IsManaged)
            {
                if (obj.Realm.SharedRealmHandle == this.SharedRealmHandle)
                {
                    // Already managed by this realm, so nothing to do.
                    return;
                }

                throw new RealmObjectManagedByAnotherRealmException("Cannot start to manage an object with a realm when it's already managed by another realm");
            }

            var metadata = Metadata[objectType.Name];

            var objectPtr = IntPtr.Zero;

            object pkValue;

            if (update && metadata.Helper.TryGetPrimaryKeyValue(obj, out pkValue))
            {
                if (pkValue is string)
                {
                    objectPtr = metadata.Table.Find(SharedRealmHandle, (string)pkValue);
                }
                else if (pkValue == null)
                {
                    objectPtr = metadata.Table.Find(SharedRealmHandle, (long?)null);
                }
                else
                {
                    // We know it must be convertible to long, so optimistically do it.
                    objectPtr = metadata.Table.Find(SharedRealmHandle, Convert.ToInt64(pkValue));
                }
            }

            if (objectPtr == IntPtr.Zero)
            {
                objectPtr = metadata.Table.AddEmptyObject(SharedRealmHandle);
            }

            var objectHandle = CreateObjectHandle(objectPtr, SharedRealmHandle);

            obj._SetOwner(this, objectHandle, metadata);
            metadata.Helper.CopyToRealm(obj, update);
        }
Пример #8
0
        /// <summary>
        /// Removes a persistent object from this realm, effectively deleting it.
        /// </summary>
        /// <param name="obj">Must be an object persisted in this realm.</param>
        /// <exception cref="RealmOutsideTransactionException">If you invoke this when there is no write Transaction active on the realm.</exception>
        /// <exception cref="System.ArgumentNullException">If you invoke this with a standalone object.</exception>
        public void Remove(RealmObject obj)
        {
            if (!IsInTransaction)
            {
                throw new RealmOutsideTransactionException("Cannot remove Realm object outside write transactions");
            }

            var tableHandle = Metadata[obj.GetType()].Table;

            NativeTable.remove_row(tableHandle, (RowHandle)obj.RowHandle);
        }
Пример #9
0
        /// <summary>
        /// Removes a persistent object from this realm, effectively deleting it.
        /// </summary>
        /// <param name="obj">Must be an object persisted in this realm.</param>
        /// <exception cref="RealmInvalidTransactionException">If you invoke this when there is no write Transaction active on the realm.</exception>
        /// <exception cref="ArgumentNullException">If you invoke this with a standalone object.</exception>
        public void Remove(RealmObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (!obj.IsManaged)
            {
                throw new ArgumentException("Object is not managed by Realm, so it cannot be removed.", nameof(obj));
            }

            obj.ObjectHandle.RemoveFromRealm(SharedRealmHandle);
        }
Пример #10
0
        private void AddInternal(RealmObject obj, Type objectType, bool update)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (objectType == null)
            {
                throw new ArgumentNullException(nameof(objectType));
            }

            if (obj.IsManaged)
            {
                if (obj.Realm.SharedRealmHandle == this.SharedRealmHandle)
                {
                    // Already managed by this realm, so nothing to do.
                    return;
                }

                throw new RealmObjectManagedByAnotherRealmException("Cannot start to manage an object with a realm when it's already managed by another realm");
            }

            var objectName = objectType.GetTypeInfo().GetMappedOrOriginalName();

            if (!Metadata.TryGetValue(objectName, out var metadata))
            {
                throw new ArgumentException($"The class {objectType.Name} is not in the limited set of classes for this realm");
            }

            ObjectHandle objectHandle;
            bool         isNew;

            if (metadata.Helper.TryGetPrimaryKeyValue(obj, out var primaryKey))
            {
                var pkProperty = metadata.Schema.PrimaryKeyProperty.Value;
                objectHandle = SharedRealmHandle.CreateObjectWithPrimaryKey(pkProperty, primaryKey, metadata.Table, objectName, update, out isNew);
            }
            else
            {
                isNew        = true; // Objects without PK are always new
                objectHandle = SharedRealmHandle.CreateObject(metadata.Table);
            }

            obj._SetOwner(this, objectHandle, metadata);

            // If an object is newly created, we don't need to invoke setters of properties with default values.
            metadata.Helper.CopyToRealm(obj, update, isNew);
            obj.OnManaged();
        }
Пример #11
0
        public void SetObject(Realm realm, IntPtr propertyIndex, RealmObject @object)
        {
            if (@object == null)
            {
                this.ClearLink(propertyIndex);
            }
            else
            {
                if ([email protected])
                {
                    realm.Add(@object);
                }

                this.SetLink(propertyIndex, @object.ObjectHandle);
            }
        }
Пример #12
0
 public static void SetObject(Realm realm, TableHandle table, IntPtr columnIndex, IntPtr rowIndex, RealmObject @object)
 {
     if (@object == null)
     {
         NativeTable.ClearLink(table, columnIndex, rowIndex);
     }
     else
     {
         if ([email protected])
         {
             realm.Manage(@object);
         }
         NativeTable.SetLink(table, columnIndex, rowIndex, @object.RowHandle.RowIndex);
     }
 }
Пример #13
0
 /// <summary>
 /// Removes a persistent object from this Realm, effectively deleting it.
 /// </summary>
 /// <param name="obj">Must be an object persisted in this Realm.</param>
 /// <exception cref="RealmInvalidTransactionException">
 /// If you invoke this when there is no write <see cref="Transaction"/> active on the <see cref="Realm"/>.
 /// </exception>
 /// <exception cref="ArgumentNullException">If <c>obj</c> is <c>null</c>.</exception>
 /// <exception cref="ArgumentException">If you pass a standalone object.</exception>
 public void Remove(RealmObject obj)
 {
     RealmPCLHelpers.ThrowProxyShouldNeverBeUsed();
 }
Пример #14
0
 /// <summary>
 /// This <see cref="Realm"/> will start managing a <see cref="RealmObject"/> which has been created as a standalone object.
 /// </summary>
 /// <param name="obj">Must be a standalone object, <c>null</c> not allowed.</param>
 /// <param name="update">If <c>true</c>, and an object with the same primary key already exists, performs an update.</param>
 /// <exception cref="RealmInvalidTransactionException">
 /// If you invoke this when there is no write <see cref="Transaction"/> active on the <see cref="Realm"/>.
 /// </exception>
 /// <exception cref="RealmObjectManagedByAnotherRealmException">
 /// You can't manage an object with more than one <see cref="Realm"/>.
 /// </exception>
 /// <remarks>
 /// If the object is already managed by this <see cref="Realm"/>, this method does nothing.
 /// This method modifies the object in-place, meaning that after it has run, <c>obj</c> will be managed.
 /// Cyclic graphs (<c>Parent</c> has <c>Child</c> that has a <c>Parent</c>) will result in undefined behavior.
 /// You have to break the cycle manually and assign relationships after all object have been managed.
 /// </remarks>
 /// <returns>The passed object.</returns>
 public RealmObject Add(RealmObject obj, bool update = false)
 {
     RealmPCLHelpers.ThrowProxyShouldNeverBeUsed();
     return(null);
 }
Пример #15
0
 /// <summary>
 /// This realm will start managing a RealmObject which has been created as a standalone object.
 /// </summary>
 /// <param name="obj">Must be a standalone object, null not allowed.</param>
 /// <param name="update">If true, and an object with the same primary key already exists, performs an update.</param>
 /// <exception cref="RealmInvalidTransactionException">If you invoke this when there is no write Transaction active on the realm.</exception>
 /// <exception cref="RealmObjectManagedByAnotherRealmException">You can't manage an object with more than one realm</exception>
 /// <remarks>
 /// If the object is already managed by this realm, this method does nothing.
 /// Cyclic graphs (<c>Parent</c> has <c>Child</c> that has a <c>Parent</c>) will result in undefined behavior. You have to break the cycle manually and assign relationships after all object have been managed.
 /// </remarks>
 public void Add(RealmObject obj, bool update = false)
 {
     AddInternal(obj, obj?.GetType(), update);
 }
Пример #16
0
 public RealmObject Add(RealmObject obj, bool update)
 {
     return(null);
 }
Пример #17
0
 /// <summary>
 /// This realm will start managing a RealmObject which has been created as a standalone object.
 /// </summary>
 /// <param name="obj">Must be a standalone object, null not allowed.</param>
 /// <param name="update">If true, and an object with the same primary key already exists, performs an update.</param>
 /// <exception cref="RealmInvalidTransactionException">If you invoke this when there is no write Transaction active on the realm.</exception>
 /// <exception cref="RealmObjectManagedByAnotherRealmException">You can't manage an object with more than one realm</exception>
 /// <remarks>
 /// If the object is already managed by this realm, this method does nothing.
 /// Cyclic graphs (<c>Parent</c> has <c>Child</c> that has a <c>Parent</c>) will result in undefined behavior. You have to break the cycle manually and assign relationships after all object have been managed.
 /// </remarks>
 public void Add(RealmObject obj, bool update = false)
 {
     RealmPCLHelpers.ThrowProxyShouldNeverBeUsed();
 }
Пример #18
0
 internal RealmList(RealmObject parent, LinkListHandle adoptedList)
 {
     _parent     = parent;
     _listHandle = adoptedList;
 }