internal ResultsHandle MakeResultsForTable(Type tableType) { var tableHandle = Metadata[tableType].Table; var objSchema = Realm.ObjectSchemaCache[tableType]; IntPtr resultsPtr = NativeResults.create_for_table(SharedRealmHandle, tableHandle, objSchema); return(CreateResultsHandle(resultsPtr)); }
/// <summary> /// Remove objects matcing a query from the realm. /// </summary> /// <typeparam name="T">Type of the objects to remove.</typeparam> /// <param name="range">The query to match for.</param> public void RemoveRange <T>(RealmResults <T> range) where T : RealmObject { if (!IsInTransaction) { throw new RealmOutsideTransactionException("Cannot remove Realm objects outside write transactions"); } NativeResults.clear(range.ResultsHandle); }
internal T this[int index] { get { var row = NativeResults.get_row(ResultsHandle, (IntPtr)index); var rowHandle = Realm.CreateRowHandle(row, _realm.SharedRealmHandle); return((T)(object)_realm.MakeObjectForRow(typeof(T), rowHandle)); } }
/// <summary> /// Remove all objects of all types managed by this realm. /// </summary> public void RemoveAll() { if (!IsInTransaction) { throw new RealmOutsideTransactionException("Cannot remove all Realm objects outside write transactions"); } var objectClasses = Config.ObjectClasses ?? RealmObjectClasses; foreach (var objectClass in objectClasses) { var resultsHandle = MakeResultsForTable(objectClass); NativeResults.clear(resultsHandle); } }
internal ResultsHandle MakeResultsForQuery(Type tableType, QueryHandle builtQuery, SortOrderHandle optionalSortOrder) { var objSchema = Realm.ObjectSchemaCache[tableType]; IntPtr resultsPtr = IntPtr.Zero; if (optionalSortOrder == null) { resultsPtr = NativeResults.create_for_query(SharedRealmHandle, builtQuery, objSchema); } else { resultsPtr = NativeResults.create_for_query_sorted(SharedRealmHandle, builtQuery, objSchema, optionalSortOrder); } return(CreateResultsHandle(resultsPtr)); }
/// <summary> /// Fast count all objects of a given class. /// </summary> /// <remarks> /// Resolves to this method instead of the LINQ static extension <c>Count<T>(this IEnumerable<T>)</c>, when used directly on Realm.All. /// </remarks> public int Count() { if (_allRecords) { // use the type captured at build based on generic T var tableHandle = _realm.Metadata[ElementType].Table; return((int)NativeTable.count_all(tableHandle)); } // normally we would be in RealmQRealmResultsr.VisitMethodCall, not here // however, if someone CASTS a RealmResults<blah> variable from a Where call to // a RealmResults<blah> they change its compile-time type from IQueryable<blah> (which invokes LINQ) // to RealmResults<blah> and thus ends up here. // as in the unit test CountFoundWithCasting return((int)NativeResults.count(ResultsHandle)); }
public override bool Equals(object p) { // If parameter is null, return false. if (ReferenceEquals(p, null)) { return(false); } // Optimization for a common success case. if (ReferenceEquals(this, p)) { return(true); } return(NativeResults.is_same_internal_results(this, (ResultsHandle)p) != IntPtr.Zero); }
private void SubscribeForNotifications() { Debug.Assert(_notificationToken == null); var managedResultsHandle = GCHandle.Alloc(this); var token = new NotificationTokenHandle(ResultsHandle); var tokenHandle = NativeResults.add_notification_callback(ResultsHandle, GCHandle.ToIntPtr(managedResultsHandle), RealmResultsNativeHelper.NotificationCallback);; RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { token.SetHandle(tokenHandle); } _notificationToken = token; }
/// <summary> /// Move the iterator to the next related object, starting "before" the first object. /// </summary> /// <returns>True only if can advance.</returns> public bool MoveNext() { if (_enumeratingResults == null) { return(false); } ++_index; var rowPtr = NativeResults.get_row(_enumeratingResults, (IntPtr)_index); var rowHandle = Realm.CreateRowHandle(rowPtr, _realm.SharedRealmHandle); object nextObj = null; if (!rowHandle.IsInvalid) { nextObj = _realm.MakeObjectForRow(_retType, rowHandle); } Current = (T)nextObj; return(nextObj != null); }
internal override Expression VisitMethodCall(MethodCallExpression m) { if (m.Method.DeclaringType == typeof(Queryable)) { if (m.Method.Name == "Where") { this.Visit(m.Arguments[0]); LambdaExpression lambda = (LambdaExpression)StripQuotes(m.Arguments[1]); this.Visit(lambda.Body); return(m); } if (m.Method.Name == "OrderBy") { this.Visit(m.Arguments[0]); AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), true, true); return(m); } if (m.Method.Name == "OrderByDescending") { this.Visit(m.Arguments[0]); AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), true, false); return(m); } if (m.Method.Name == "ThenBy") { this.Visit(m.Arguments[0]); AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), false, true); return(m); } if (m.Method.Name == "ThenByDescending") { this.Visit(m.Arguments[0]); AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), false, false); return(m); } if (m.Method.Name == "Count") { RecurseToWhereOrRunLambda(m); int foundCount = (int)NativeQuery.count(_coreQueryHandle); return(Expression.Constant(foundCount)); } if (m.Method.Name == "Any") { RecurseToWhereOrRunLambda(m); var rowPtr = NativeQuery.findDirect(_coreQueryHandle, IntPtr.Zero); var firstRow = Realm.CreateRowHandle(rowPtr, _realm.SharedRealmHandle); bool foundAny = !firstRow.IsInvalid; return(Expression.Constant(foundAny)); } if (m.Method.Name == "First") { RecurseToWhereOrRunLambda(m); RowHandle firstRow = null; if (_optionalSortOrderHandle == null) { var rowPtr = NativeQuery.findDirect(_coreQueryHandle, IntPtr.Zero); firstRow = Realm.CreateRowHandle(rowPtr, _realm.SharedRealmHandle); } else { using (ResultsHandle rh = _realm.MakeResultsForQuery(_retType, _coreQueryHandle, _optionalSortOrderHandle)) { var rowPtr = NativeResults.get_row(rh, IntPtr.Zero); firstRow = Realm.CreateRowHandle(rowPtr, _realm.SharedRealmHandle); } } if (firstRow == null || firstRow.IsInvalid) { throw new InvalidOperationException("Sequence contains no matching element"); } return(Expression.Constant(_realm.MakeObjectForRow(_retType, firstRow))); } if (m.Method.Name == "Single") // same as unsorted First with extra checks { RecurseToWhereOrRunLambda(m); var rowPtr = NativeQuery.findDirect(_coreQueryHandle, IntPtr.Zero); var firstRow = Realm.CreateRowHandle(rowPtr, _realm.SharedRealmHandle); if (firstRow.IsInvalid) { throw new InvalidOperationException("Sequence contains no matching element"); } IntPtr nextIndex = (IntPtr)(firstRow.RowIndex + 1); var nextRowPtr = NativeQuery.findDirect(_coreQueryHandle, nextIndex); var nextRow = Realm.CreateRowHandle(nextRowPtr, _realm.SharedRealmHandle); if (!nextRow.IsInvalid) { throw new InvalidOperationException("Sequence contains more than one matching element"); } return(Expression.Constant(_realm.MakeObjectForRow(_retType, firstRow))); } } if (m.Method.DeclaringType == typeof(string)) { NativeQuery.Operation <string> queryMethod = null; if (m.Method == Methods.String.Contains.Value) { queryMethod = (q, c, v) => NativeQuery.string_contains(q, c, v, (IntPtr)v.Length); } else if (m.Method == Methods.String.StartsWith.Value) { queryMethod = (q, c, v) => NativeQuery.string_starts_with(q, c, v, (IntPtr)v.Length); } else if (m.Method == Methods.String.EndsWith.Value) { queryMethod = (q, c, v) => NativeQuery.string_ends_with(q, c, v, (IntPtr)v.Length); } if (queryMethod != null) { var member = m.Object as MemberExpression; if (member == null) { throw new NotSupportedException($"The method '{m.Method}' has to be invoked on a RealmObject member"); } var columnIndex = NativeQuery.get_column_index(_coreQueryHandle, member.Member.Name, (IntPtr)member.Member.Name.Length); var argument = ExtractConstantValue(m.Arguments.SingleOrDefault()); if (argument == null || argument.GetType() != typeof(string)) { throw new NotSupportedException($"The method '{m.Method}' has to be invoked with a single string constant argument or closure variable"); } queryMethod(_coreQueryHandle, columnIndex, (string)argument); return(m); } } throw new NotSupportedException($"The method '{m.Method.Name}' is not supported"); }
protected override void Unbind() { IntPtr managedResultsHandle = NativeResults.destroy_notificationtoken(handle); GCHandle.FromIntPtr(managedResultsHandle).Free(); }
protected override void Unbind() { NativeResults.destroy(handle); }