Beispiel #1
0
        public void BoolNotEqual(IntPtr columnIndex, bool value)
        {
            NativeException nativeException;

            NativeMethods.bool_not_equal(this, columnIndex, MarshalHelpers.BoolToIntPtr(value), out nativeException);
            nativeException.ThrowIfNecessary();
        }
        public void SetBoolean(IntPtr propertyIndex, bool value)
        {
            NativeException nativeException;

            NativeMethods.set_bool(this, propertyIndex, MarshalHelpers.BoolToIntPtr(value), out nativeException);
            nativeException.ThrowIfNecessary();
        }
Beispiel #3
0
        public static void SetBoolean(TableHandle tableHandle, IntPtr columnIndex, IntPtr rowIndex, bool value)
        {
            NativeException nativeException;

            set_bool(tableHandle, columnIndex, rowIndex, MarshalHelpers.BoolToIntPtr(value), out nativeException);
            nativeException.ThrowIfNecessary();
        }
        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();
            }
        }
Beispiel #5
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);
        }
Beispiel #6
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();
        }
        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();
        }
        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();
            }
        }
Beispiel #9
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));
        }
Beispiel #10
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);
            }
        }
Beispiel #11
0
 public void BoolNotEqual(ColumnKey columnKey, bool value)
 {
     NativeMethods.bool_not_equal(this, columnKey, MarshalHelpers.BoolToIntPtr(value), out var nativeException);
     nativeException.ThrowIfNecessary();
 }