/// <summary> /// Move the iterator to the next related object, starting "before" the first object. /// </summary> /// <remarks> /// Is a factory for RealmObjects, loading a new one and updating <c>Current</c>. /// </remarks> /// <returns>True only if can advance.</returns> public bool MoveNext() { if (_enumeratingResults == null) { return(false); } ++_index; var rowPtr = _enumeratingResults.GetObject(_index); if (rowPtr == IntPtr.Zero) { Current = (T)(object)null; return(false); } Current = (T)(object)_realm.MakeObject(_schema.Name, rowPtr); return(true); }
internal override Expression VisitMethodCall(MethodCallExpression m) { if (m.Method.DeclaringType == typeof(Queryable)) { if (m.Method.Name == nameof(Queryable.Where)) { this.Visit(m.Arguments[0]); var lambda = (LambdaExpression)StripQuotes(m.Arguments[1]); this.Visit(lambda.Body); return(m); } if (m.Method.Name == nameof(Queryable.OrderBy)) { this.Visit(m.Arguments[0]); AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), true, true); return(m); } if (m.Method.Name == nameof(Queryable.OrderByDescending)) { this.Visit(m.Arguments[0]); AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), true, false); return(m); } if (m.Method.Name == nameof(Queryable.ThenBy)) { this.Visit(m.Arguments[0]); AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), false, true); return(m); } if (m.Method.Name == nameof(Queryable.ThenByDescending)) { this.Visit(m.Arguments[0]); AddSort((LambdaExpression)StripQuotes(m.Arguments[1]), false, false); return(m); } if (m.Method.Name == nameof(Queryable.Count)) { RecurseToWhereOrRunLambda(m); var foundCount = CoreQueryHandle.Count(); return(Expression.Constant(foundCount)); } if (m.Method.Name == nameof(Queryable.Any)) { RecurseToWhereOrRunLambda(m); var foundAny = CoreQueryHandle.FindDirect(_realm.SharedRealmHandle) != IntPtr.Zero; return(Expression.Constant(foundAny)); } if (m.Method.Name.StartsWith(nameof(Queryable.First))) { RecurseToWhereOrRunLambda(m); var firstObjectPtr = IntPtr.Zero; if (OptionalSortDescriptorBuilder == null) { firstObjectPtr = CoreQueryHandle.FindDirect(_realm.SharedRealmHandle); } else { using (ResultsHandle rh = _realm.MakeResultsForQuery(CoreQueryHandle, OptionalSortDescriptorBuilder)) { firstObjectPtr = rh.GetObject(0); } } if (firstObjectPtr != IntPtr.Zero) { return(Expression.Constant(_realm.MakeObject(_metadata, firstObjectPtr))); } if (m.Method.Name == nameof(Queryable.First)) { throw new InvalidOperationException("Sequence contains no matching element"); } Debug.Assert(m.Method.Name == nameof(Queryable.FirstOrDefault), $"The method {m.Method.Name} is not supported. We expected {nameof(Queryable.FirstOrDefault)}."); return(Expression.Constant(null)); } /* * // FIXME: See discussion in the test DefaultIfEmptyReturnsDefault * // kept because it shows part of what might be a viable implementation if can work out architectural issues * * if (m.Method.Name == nameof(Queryable.DefaultIfEmpty)) * { * RecurseToWhereOrRunLambda(m); * IntPtr firstObjectPtr = _coreQueryHandle.FindDirect(IntPtr.Zero); * if (firstObjectPtr != IntPtr.Zero) * return m; // as if just a "Where" * var innerType = m.Type.GetGenericArguments()[0]; * var listType = typeof(List<>).MakeGenericType(innerType); * var singleNullItemList = Activator.CreateInstance(listType); * ((IList)singleNullItemList).Add(null); * return Expression.Constant(singleNullItemList); * } */ if (m.Method.Name.StartsWith(nameof(Queryable.Single))) // same as unsorted First with extra checks { RecurseToWhereOrRunLambda(m); var firstObjectPtr = CoreQueryHandle.FindDirect(_realm.SharedRealmHandle); if (firstObjectPtr == IntPtr.Zero) { if (m.Method.Name == nameof(Queryable.Single)) { throw new InvalidOperationException("Sequence contains no matching element"); } Debug.Assert(m.Method.Name == nameof(Queryable.SingleOrDefault), $"The method {m.Method.Name} is not supported. We expected {nameof(Queryable.SingleOrDefault)}."); return(Expression.Constant(null)); } var firstObject = Realm.CreateObjectHandle(firstObjectPtr, _realm.SharedRealmHandle); var nextObjectPtr = CoreQueryHandle.FindNext(firstObject); if (nextObjectPtr != IntPtr.Zero) { throw new InvalidOperationException("Sequence contains more than one matching element"); } return(Expression.Constant(_realm.MakeObject(_metadata, firstObject))); } if (m.Method.Name.StartsWith(nameof(Queryable.Last))) { RecurseToWhereOrRunLambda(m); var lastObjectPtr = IntPtr.Zero; using (ResultsHandle rh = _realm.MakeResultsForQuery(CoreQueryHandle, OptionalSortDescriptorBuilder)) { var lastIndex = rh.Count() - 1; if (lastIndex >= 0) { lastObjectPtr = rh.GetObject(lastIndex); } } if (lastObjectPtr != IntPtr.Zero) { return(Expression.Constant(_realm.MakeObject(_metadata, lastObjectPtr))); } if (m.Method.Name == nameof(Queryable.Last)) { throw new InvalidOperationException("Sequence contains no matching element"); } Debug.Assert(m.Method.Name == nameof(Queryable.LastOrDefault), $"The method {m.Method.Name} is not supported. We expected {nameof(Queryable.LastOrDefault)}."); return(Expression.Constant(null)); } if (m.Method.Name.StartsWith(nameof(Queryable.ElementAt))) { var objectHandle = VisitElementAt(m); if (objectHandle == null || objectHandle.IsInvalid) { if (m.Method.Name == nameof(Queryable.ElementAt)) { throw new ArgumentOutOfRangeException(); } Debug.Assert(m.Method.Name == nameof(Queryable.ElementAtOrDefault), $"The method {m.Method.Name} is not supported. We expected {nameof(Queryable.ElementAtOrDefault)}."); return(Expression.Constant(null)); } return(Expression.Constant(_realm.MakeObject(_metadata, objectHandle))); } } if (m.Method.DeclaringType == typeof(string)) { QueryHandle.Operation <string> queryMethod = null; if (m.Method == Methods.String.Contains.Value) { queryMethod = (q, c, v) => q.StringContains(c, v, caseSensitive: true); } else if (m.Method == Methods.String.StartsWith.Value) { queryMethod = (q, c, v) => q.StringStartsWith(c, v, caseSensitive: true); } else if (m.Method == Methods.String.StartsWithStringComparison.Value) { queryMethod = (q, c, v) => q.StringStartsWith(c, v, GetComparisonCaseSensitive(m)); } else if (m.Method == Methods.String.EndsWith.Value) { queryMethod = (q, c, v) => q.StringEndsWith(c, v, caseSensitive: true); } else if (m.Method == Methods.String.EndsWithStringComparison.Value) { queryMethod = (q, c, v) => q.StringEndsWith(c, v, GetComparisonCaseSensitive(m)); } else if (m.Method == Methods.String.IsNullOrEmpty.Value) { var member = m.Arguments.SingleOrDefault() as MemberExpression; if (member == null) { throw new NotSupportedException($"The method '{m.Method}' has to be invoked with a RealmObject member"); } var columnIndex = CoreQueryHandle.GetColumnIndex(member.Member.Name); CoreQueryHandle.GroupBegin(); CoreQueryHandle.NullEqual(columnIndex); CoreQueryHandle.Or(); CoreQueryHandle.StringEqual(columnIndex, string.Empty, caseSensitive: true); CoreQueryHandle.GroupEnd(); return(m); } else if (m.Method == Methods.String.EqualsMethod.Value) { queryMethod = (q, c, v) => q.StringEqual(c, v, caseSensitive: true); } else if (m.Method == Methods.String.EqualsStringComparison.Value) { queryMethod = (q, c, v) => q.StringEqual(c, v, GetComparisonCaseSensitive(m)); } 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 = CoreQueryHandle.GetColumnIndex(member.Member.Name); object argument; if (!TryExtractConstantValue(m.Arguments[0], out argument) || 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"); }