private static void AddQueryNotEqual(QueryHandle queryHandle, string columnName, object value)
        {
            var columnIndex = NativeQuery.get_column_index((QueryHandle)queryHandle, columnName, (IntPtr)columnName.Length);

            var valueType = value.GetType();

            if (value.GetType() == typeof(string))
            {
                string valueStr = (string)value;
                NativeQuery.string_not_equal((QueryHandle)queryHandle, columnIndex, valueStr, (IntPtr)valueStr.Length);
            }
            else if (valueType == typeof(bool))
            {
                NativeQuery.bool_not_equal((QueryHandle)queryHandle, columnIndex, MarshalHelpers.BoolToIntPtr((bool)value));
            }
            else if (valueType == typeof(int))
            {
                NativeQuery.int_not_equal((QueryHandle)queryHandle, columnIndex, (IntPtr)((int)value));
            }
            else if (valueType == typeof(long))
            {
                NativeQuery.long_not_equal((QueryHandle)queryHandle, columnIndex, (long)value);
            }
            else if (valueType == typeof(float))
            {
                NativeQuery.float_not_equal((QueryHandle)queryHandle, columnIndex, (float)value);
            }
            else if (valueType == typeof(double))
            {
                NativeQuery.double_not_equal((QueryHandle)queryHandle, columnIndex, (double)value);
            }
            else if (valueType == typeof(DateTimeOffset))
            {
                NativeQuery.timestamp_milliseconds_not_equal(queryHandle, columnIndex, ((DateTimeOffset)value).ToRealmUnixTimeMilliseconds());
            }
            else if (valueType == typeof(byte[]))
            {
                var buffer = (byte[])value;
                if (buffer.Length == 0)
                {
                    // see RealmObject.SetByteArrayValue
                    NativeQuery.binary_not_equal(queryHandle, columnIndex, (IntPtr)0x1, IntPtr.Zero);
                    return;
                }

                unsafe
                {
                    fixed(byte *bufferPtr = (byte[])value)
                    {
                        NativeQuery.binary_not_equal(queryHandle, columnIndex, (IntPtr)bufferPtr, (IntPtr)buffer.LongLength);
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 2
0
        public long?GetNullableInt64(IntPtr propertyIndex)
        {
            NativeException nativeException;
            long            value;
            var             hasValue = MarshalHelpers.IntPtrToBool(NativeMethods.get_nullable_int64(this, propertyIndex, out value, out nativeException));

            nativeException.ThrowIfNecessary();
            return(hasValue ? value : (long?)null);
        }
Ejemplo n.º 3
0
        public double?GetNullableDouble(IntPtr propertyIndex)
        {
            NativeException nativeException;
            double          value;
            var             hasValue = MarshalHelpers.IntPtrToBool(NativeMethods.get_nullable_double(this, propertyIndex, out value, out nativeException));

            nativeException.ThrowIfNecessary();
            return(hasValue ? value : (double?)null);
        }
Ejemplo n.º 4
0
        public static double?GetNullableDouble(TableHandle tableHandle, IntPtr columnIndex, IntPtr rowIndex)
        {
            NativeException nativeException;
            double          value;
            var             hasValue = MarshalHelpers.IntPtrToBool(NativeTable.get_nullable_double(tableHandle, columnIndex, rowIndex, out value, out nativeException));

            nativeException.ThrowIfNecessary();
            return(hasValue ? value : (double?)null);
        }
Ejemplo n.º 5
0
        public static bool?GetNullableBoolean(TableHandle tableHandle, IntPtr columnIndex, IntPtr rowIndex)
        {
            NativeException nativeException;
            IntPtr          value;
            var             hasValue = MarshalHelpers.IntPtrToBool(NativeTable.get_nullable_bool(tableHandle, columnIndex, rowIndex, out value, out nativeException));

            nativeException.ThrowIfNecessary();
            return(hasValue ? MarshalHelpers.IntPtrToBool(value) : (bool?)null);
        }
Ejemplo n.º 6
0
        public DateTimeOffset?GetNullableDateTimeOffset(IntPtr propertyIndex)
        {
            NativeException nativeException;
            long            ticks;
            var             hasValue = MarshalHelpers.IntPtrToBool(NativeMethods.get_nullable_timestamp_ticks(this, propertyIndex, out ticks, out nativeException));

            nativeException.ThrowIfNecessary();
            return(hasValue ? new DateTimeOffset(ticks, TimeSpan.Zero) : (DateTimeOffset?)null);
        }
Ejemplo n.º 7
0
        public bool?GetNullableBoolean(IntPtr propertyIndex)
        {
            NativeException nativeException;
            IntPtr          value;
            var             hasValue = MarshalHelpers.IntPtrToBool(NativeMethods.get_nullable_bool(this, propertyIndex, out value, out nativeException));

            nativeException.ThrowIfNecessary();
            return(hasValue ? MarshalHelpers.IntPtrToBool(value) : (bool?)null);
        }
Ejemplo n.º 8
0
        public static DateTimeOffset?GetNullableDateTimeOffset(TableHandle tableHandle, IntPtr columnIndex,
                                                               IntPtr rowIndex)
        {
            NativeException nativeException;
            long            ticks;
            var             hasValue = MarshalHelpers.IntPtrToBool(NativeTable.get_nullable_timestamp_ticks(tableHandle, columnIndex, rowIndex, out ticks, out nativeException));

            nativeException.ThrowIfNecessary();
            return(hasValue ? new DateTimeOffset(ticks, TimeSpan.Zero) : (DateTimeOffset?)null);
        }
Ejemplo n.º 9
0
        protected DateTimeOffset?GetNullableDateTimeOffsetValue(string propertyName)
        {
            Debug.Assert(_realm != null, "Object is not managed, but managed access was attempted");

            var rowIndex = _rowHandle.RowIndex;

            long unixTimeSeconds = 0;
            var  hasValue        = MarshalHelpers.IntPtrToBool(NativeTable.get_nullable_datetime_seconds(_metadata.Table, _metadata.ColumnIndices[propertyName], (IntPtr)rowIndex, ref unixTimeSeconds));

            return(hasValue ? DateTimeOffsetExtensions.FromUnixTimeSeconds(unixTimeSeconds) : (DateTimeOffset?)null);
        }
Ejemplo n.º 10
0
        protected bool?GetNullableBooleanValue(string propertyName)
        {
            Debug.Assert(_realm != null, "Object is not managed, but managed access was attempted");

            var rowIndex = _rowHandle.RowIndex;

            var retVal   = IntPtr.Zero;
            var hasValue = MarshalHelpers.IntPtrToBool(NativeTable.get_nullable_bool(_metadata.Table, _metadata.ColumnIndices[propertyName], (IntPtr)rowIndex, ref retVal));

            return(hasValue ? MarshalHelpers.IntPtrToBool(retVal) : (bool?)null);
        }
Ejemplo n.º 11
0
        protected double?GetNullableDoubleValue(string propertyName)
        {
            Debug.Assert(_realm != null, "Object is not managed, but managed access was attempted");

            var rowIndex = _rowHandle.RowIndex;

            var retVal   = 0.0d;
            var hasValue = MarshalHelpers.IntPtrToBool(NativeTable.get_nullable_double(_metadata.Table, _metadata.ColumnIndices[propertyName], (IntPtr)rowIndex, ref retVal));

            return(hasValue ? retVal : (double?)null);
        }
Ejemplo n.º 12
0
        public unsafe int Find(byte[] value)
        {
            var result = IntPtr.Zero;

            MarshalHelpers.SetByteArray(value, (IntPtr buffer, IntPtr bufferSize, bool hasValue, out NativeException ex) =>
            {
                result = NativeMethods.find_binary(this, buffer, bufferSize, hasValue, out ex);
            });

            return((int)result);
        }
Ejemplo n.º 13
0
        private static IntPtr GenerateObjectSchema(Type objectClass)
        {
            IntPtr objectSchemaPtr = IntPtr.Zero;

            if (ObjectSchemaCache.TryGetValue(objectClass, out objectSchemaPtr))
            {
                return(objectSchemaPtr); // use cached schema
            }

            objectSchemaPtr = NativeObjectSchema.create(objectClass.Name);
            ObjectSchemaCache[objectClass] = objectSchemaPtr;  // save for later lookup
            var propertiesToMap = objectClass.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Public)
                                  .Where(p =>
            {
                return(p.GetCustomAttributes(false).OfType <WovenPropertyAttribute>().Any());
            });

            foreach (var p in propertiesToMap)
            {
                var mapToAttribute = p.GetCustomAttributes(false).FirstOrDefault(a => a is MapToAttribute) as MapToAttribute;
                var propertyName   = mapToAttribute != null ? mapToAttribute.Mapping : p.Name;

                var objectIdAttribute = p.GetCustomAttributes(false).FirstOrDefault(a => a is ObjectIdAttribute);
                var isObjectId        = objectIdAttribute != null;

                var indexedAttribute = p.GetCustomAttributes(false).FirstOrDefault(a => a is IndexedAttribute);
                var isIndexed        = indexedAttribute != null;

                var isNullable = !(p.PropertyType.IsValueType ||
                                   p.PropertyType.Name == "RealmList`1") ||
                                 // IGNORING IList FOR NOW  p.PropertyType.Name == "IList`1") ||
                                 Nullable.GetUnderlyingType(p.PropertyType) != null;

                var objectType = "";
                if (!p.PropertyType.IsValueType && p.PropertyType.Name != "String")
                {
                    if (p.PropertyType.Name == "RealmList`1")  // IGNORING IList FOR NOW   || p.PropertyType.Name == "IList`1")
                    {
                        objectType = p.PropertyType.GetGenericArguments()[0].Name;
                    }
                    else
                    {
                        if (p.PropertyType.BaseType.Name == "RealmObject")
                        {
                            objectType = p.PropertyType.Name;
                        }
                    }
                }
                var columnType = p.PropertyType;
                NativeObjectSchema.add_property(objectSchemaPtr, propertyName, MarshalHelpers.RealmColType(columnType), objectType,
                                                MarshalHelpers.BoolToIntPtr(isObjectId), MarshalHelpers.BoolToIntPtr(isIndexed), MarshalHelpers.BoolToIntPtr(isNullable));
            }
            return(objectSchemaPtr);
        }
Ejemplo n.º 14
0
 public unsafe void SetByteArray(IntPtr propertyIndex, byte[] value)
 {
     MarshalHelpers.SetByteArray(value, (IntPtr buffer, IntPtr bufferSize, bool hasValue, out NativeException ex) =>
     {
         if (hasValue)
         {
             NativeMethods.set_binary(this, propertyIndex, buffer, bufferSize, out ex);
         }
         else
         {
             NativeMethods.set_null(this, propertyIndex, out ex);
         }
     });
 }
Ejemplo n.º 15
0
        public static void SetNullableBoolean(TableHandle tableHandle, IntPtr columnIndex, IntPtr rowIndex, bool?value)
        {
            NativeException nativeException;

            if (value.HasValue)
            {
                set_bool(tableHandle, columnIndex, rowIndex, MarshalHelpers.BoolToIntPtr(value.Value), out nativeException);
            }
            else
            {
                set_null(tableHandle, columnIndex, rowIndex, out nativeException);
            }
            nativeException.ThrowIfNecessary();
        }
Ejemplo n.º 16
0
        public void SetNullableBoolean(IntPtr propertyIndex, bool?value)
        {
            NativeException nativeException;

            if (value.HasValue)
            {
                NativeMethods.set_bool(this, propertyIndex, MarshalHelpers.BoolToIntPtr(value.Value), out nativeException);
            }
            else
            {
                NativeMethods.set_null(this, propertyIndex, out nativeException);
            }

            nativeException.ThrowIfNecessary();
        }
Ejemplo n.º 17
0
        private static void AddQueryNotEqual(QueryHandle queryHandle, string columnName, object value)
        {
            var columnIndex = NativeQuery.get_column_index((QueryHandle)queryHandle, columnName, (IntPtr)columnName.Length);

            var valueType = value.GetType();

            if (value.GetType() == typeof(string))
            {
                string valueStr = (string)value;
                NativeQuery.string_not_equal((QueryHandle)queryHandle, columnIndex, valueStr, (IntPtr)valueStr.Length);
            }
            else if (valueType == typeof(bool))
            {
                NativeQuery.bool_not_equal((QueryHandle)queryHandle, columnIndex, MarshalHelpers.BoolToIntPtr((bool)value));
            }
            else if (valueType == typeof(int))
            {
                NativeQuery.int_not_equal((QueryHandle)queryHandle, columnIndex, (IntPtr)((int)value));
            }
            else if (valueType == typeof(float))
            {
                NativeQuery.float_not_equal((QueryHandle)queryHandle, columnIndex, (float)value);
            }
            else if (valueType == typeof(double))
            {
                NativeQuery.double_not_equal((QueryHandle)queryHandle, columnIndex, (double)value);
            }
            else if (valueType == typeof(DateTimeOffset))
            {
                NativeQuery.datetime_seconds_not_equal(queryHandle, columnIndex, ((DateTimeOffset)value).ToUnixTimeSeconds());
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 18
0
 public unsafe void Add(byte[] value)
 {
     MarshalHelpers.SetByteArray(value, (IntPtr buffer, IntPtr bufferSize, bool hasValue, out NativeException ex) =>
                                 NativeMethods.add_binary(this, buffer, bufferSize, hasValue, out ex));
 }
Ejemplo n.º 19
0
 public override byte[] GetByteArrayAtIndex(int index)
 {
     return(MarshalHelpers.GetByteArray((IntPtr buffer, IntPtr bufferLength, out bool isNull, out NativeException ex) =>
                                        NativeMethods.get_binary(this, (IntPtr)index, buffer, bufferLength, out isNull, out ex)));
 }
Ejemplo n.º 20
0
 public override string GetStringAtIndex(int index)
 {
     return(MarshalHelpers.GetString((IntPtr buffer, IntPtr length, out bool isNull, out NativeException ex) =>
                                     NativeMethods.get_string(this, (IntPtr)index, buffer, length, out isNull, out ex)));
 }
Ejemplo n.º 21
0
 public void BoolNotEqual(IntPtr columnIndex, bool value)
 {
     NativeMethods.bool_not_equal(this, columnIndex, MarshalHelpers.BoolToIntPtr(value), out var nativeException);
     nativeException.ThrowIfNecessary();
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Update a Realm and outstanding objects to point to the most recent data for this Realm.
 /// This is only necessary when you have a Realm on a non-runloop thread that needs manual refreshing.
 /// </summary>
 /// <returns>
 /// Whether the realm had any updates. Note that this may return true even if no data has actually changed.
 /// </returns>
 public bool Refresh()
 {
     return(MarshalHelpers.IntPtrToBool(NativeSharedRealm.refresh(SharedRealmHandle)));
 }
Ejemplo n.º 23
0
 public string GetString(IntPtr propertyIndex)
 {
     return(MarshalHelpers.GetString((IntPtr buffer, IntPtr length, out bool isNull, out NativeException ex) => NativeMethods.get_string(this, propertyIndex, buffer, length, out isNull, out ex)));
 }
Ejemplo n.º 24
0
        public static Realm GetInstance(RealmConfiguration config = null)
        {
            config = config ?? RealmConfiguration.DefaultConfiguration;

            // TODO cache these initializers but note complications with ObjectClasses
            var schemaInitializer = new SchemaInitializerHandle();

            if (config.ObjectClasses == null)
            {
                foreach (var realmObjectClass in RealmObjectClasses)
                {
                    var objectSchemaHandle = GenerateObjectSchema(realmObjectClass);
                    NativeSchema.initializer_add_object_schema(schemaInitializer, objectSchemaHandle);
                }
            }
            else
            {
                foreach (var selectedRealmObjectClass in config.ObjectClasses)
                {
                    if (selectedRealmObjectClass.BaseType != typeof(RealmObject))
                    {
                        throw new ArgumentException($"The class {selectedRealmObjectClass.FullName} must descend directly from RealmObject");
                    }

                    Debug.Assert(RealmObjectClasses.Contains(selectedRealmObjectClass));  // user-specified class must have been picked up by our static ctor
                    var objectSchemaHandle = GenerateObjectSchema(selectedRealmObjectClass);
                    NativeSchema.initializer_add_object_schema(schemaInitializer, objectSchemaHandle);
                }
            }

            var schemaHandle = new SchemaHandle(schemaInitializer);

            var srHandle = new SharedRealmHandle();

            var    readOnly     = MarshalHelpers.BoolToIntPtr(config.ReadOnly);
            var    durability   = MarshalHelpers.BoolToIntPtr(false);
            var    databasePath = config.DatabasePath;
            IntPtr srPtr        = IntPtr.Zero;

            try {
                srPtr = NativeSharedRealm.open(schemaHandle,
                                               databasePath, (IntPtr)databasePath.Length,
                                               readOnly, durability,
                                               config.EncryptionKey,
                                               config.SchemaVersion);
            } catch (RealmMigrationNeededException) {
                if (config.ShouldDeleteIfMigrationNeeded)
                {
                    DeleteRealm(config);
                }
                else
                {
                    throw; // rethrow te exception
                    //TODO when have Migration but also consider programmer control over auto migration
                    //MigrateRealm(configuration);
                }
                // create after deleting old reopen after migrating
                srPtr = NativeSharedRealm.open(schemaHandle,
                                               databasePath, (IntPtr)databasePath.Length,
                                               readOnly, durability,
                                               config.EncryptionKey,
                                               config.SchemaVersion);
            }

            RuntimeHelpers.PrepareConstrainedRegions();
            try { /* Retain handle in a constrained execution region */ }
            finally
            {
                srHandle.SetHandle(srPtr);
            }

            return(new Realm(srHandle, config));
        }
Ejemplo n.º 25
0
        protected void SetNullableBooleanValue(string propertyName, bool?value)
        {
            Debug.Assert(_realm != null, "Object is not managed, but managed access was attempted");

            if (!_realm.IsInTransaction)
            {
                throw new RealmOutsideTransactionException("Cannot set values outside transaction");
            }

            var rowIndex = _rowHandle.RowIndex;

            if (value.HasValue)
            {
                NativeTable.set_bool(_metadata.Table, _metadata.ColumnIndices[propertyName], (IntPtr)rowIndex, MarshalHelpers.BoolToIntPtr(value.Value));
            }
            else
            {
                NativeTable.set_null(_metadata.Table, _metadata.ColumnIndices[propertyName], (IntPtr)rowIndex);
            }
        }
Ejemplo n.º 26
0
 public byte[] GetByteArray(IntPtr propertyIndex)
 {
     return(MarshalHelpers.GetByteArray((IntPtr buffer, IntPtr bufferLength, out bool isNull, out NativeException ex) =>
                                        NativeMethods.get_binary(this, propertyIndex, buffer, bufferLength, out isNull, out ex)));
 }
Ejemplo n.º 27
0
 public unsafe void Insert(int targetIndex, byte[] value)
 {
     MarshalHelpers.SetByteArray(value, (IntPtr buffer, IntPtr bufferSize, bool hasValue, out NativeException ex) =>
                                 NativeMethods.insert_binary(this, (IntPtr)targetIndex, buffer, bufferSize, hasValue, out ex));
 }
Ejemplo n.º 28
0
 /// <summary>
 /// Determines whether this instance is the same core instance as the specified rhs.
 /// </summary>
 /// <remarks>
 /// You can, and should, have multiple instances open on different threads which have the same path and open the same Realm.
 /// </remarks>
 /// <returns><c>true</c> if this instance is the same core instance the specified rhs; otherwise, <c>false</c>.</returns>
 /// <param name="rhs">The Realm to compare with the current Realm.</param>
 public bool IsSameInstance(Realm rhs)
 {
     return(MarshalHelpers.IntPtrToBool(NativeSharedRealm.is_same_instance(SharedRealmHandle, rhs.SharedRealmHandle)));
 }
Ejemplo n.º 29
0
 public void BoolEqual(ColumnKey columnKey, bool value)
 {
     NativeMethods.bool_equal(this, columnKey, MarshalHelpers.BoolToIntPtr(value), out var nativeException);
     nativeException.ThrowIfNecessary();
 }
Ejemplo n.º 30
0
 public void SetBoolean(IntPtr propertyIndex, bool value)
 {
     NativeMethods.set_bool(this, propertyIndex, MarshalHelpers.BoolToIntPtr(value), out var nativeException);
     nativeException.ThrowIfNecessary();
 }