Skip to content

Commit

Permalink
Add filter data descriptions, Fluent interface and LINQ
Browse files Browse the repository at this point in the history
  • Loading branch information
dacharyc committed Aug 6, 2024
1 parent 13a563e commit e491561
Show file tree
Hide file tree
Showing 14 changed files with 1,153 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
To filter data, call the ``.where()`` function on a collection with a valid
query. Currently, C++ supports only a subset of RQL operators.

Supported Query Operators
`````````````````````````

C++ supports the following query operators:

- Equality (``==``, ``!=``)
- Greater than/less than (``>``, ``>=``, ``<``, ``<=``)
- Compound queries (``||``, ``&&``)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
To query, filter, and sort data in the database, use the SDK query engine.
There are two ways to use the query engine with C#:

- :ref:`LINQ Syntax <sdks-dotnet-linq>`
- :ref:`Realm Query Language (RQL) <realm-query-language>`

Use LINQ syntax for querying when possible, as it provides compile-time checks
for queries and aligns with .NET conventions.

Filter Data with LINQ
`````````````````````

To filter data with LINQ syntax, call the :dotnet-sdk:`Where()
<linqsupport.html#restriction-operators>` operator with a
:dotnet-sdk:`Predicate <linqsupport.html#predicate-operations>` that describes
the subset of data you want to access.

.. literalinclude:: /examples/generated/dotnet/QueryEngineExamples.snippet.logical.cs
:language: csharp

Filter Data with RQL
````````````````````

You can also use the :ref:`Realm Query Language <realm-query-language>` (RQL)
to query realms. RQL is a string-based query language used to access the query
engine. When using RQL, you use the
:dotnet-sdk:`Filter() <reference/Realms.CollectionExtensions.html?q=Filter>`
method:

.. literalinclude:: /examples/generated/dotnet/QueryEngineExamples.snippet.rql.cs
:language: csharp

.. important::

Because :ref:`LINQ <sdks-dotnet-linq>` provides compile-time error checking
of queries, you should use it instead of RQL in most cases. If you require
features beyond LINQ's current capabilities, such as using
:ref:`aggregation <rql-aggregate-operators>`, use RQL.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Filter a collection to retrieve a specific segment of objects with the
:flutter-sdk:`Realm.query() <realm/Realm/query.html>` method. In the
``query()`` method's argument, use RQL to perform filtering.

.. literalinclude:: /examples/generated/flutter/read_write_data_test.snippet.filter.dart
:language: dart

You can use iterable arguments in your filter.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
In Java, you can filter objects with either the Fluent interface or RQL.

Filter Data with Fluent Interface
`````````````````````````````````

To filter data with the Fluent interface, filter data with a
:java-sdk:`RealmQuery <io/realm/RealmQuery.html>`. For more details, refer
to :ref:`Fluent interface <sdks-java-filter-data>`.

In the following example, we use the Fluent interface comparison operators to:

- Find high priority tasks by comparing the value of the ``priority`` property value with a threshold number, above which priority can be considered high.
- Find just-started or short-running tasks by seeing if the ``progressMinutes`` property falls within a certain range.
- Find unassigned tasks by finding tasks where the ``assignee`` property is equal to null.
- Find tasks assigned to specific teammates Ali or Jamie by seeing if the ``assignee`` property is in a list of names.

.. literalinclude:: /examples/generated/java/sync/ReadsTest.snippet.filter-results.java
:language: java
:copyable: false

Filter Data with RQL
````````````````````

To filter data with RQL, use :java-sdk:`RealmQuery.rawPredicate()
<io/realm/RealmQuery.html#rawPredicate-java.lang.String->`.
For more information about syntax, usage and limitations,
refer to the :ref:`Realm Query Language reference <realm-query-language>`.

RQL can use either the class and property names defined in your SDK model
classes or the internal names defined with ``@RealmField``. You can combine
raw predicates with other raw predicates or type-safe predicates created with
``RealmQuery``.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
In Java or Kotlin (Java SDK), you can filter objects with either the
Fluent interface or RQL.

Filter Data with Fluent Interface
`````````````````````````````````

To filter data with the Fluent interface, filter data with a
:java-sdk:`RealmQuery <io/realm/RealmQuery.html>`. For more details, refer
to :ref:`Fluent interface <sdks-java-filter-data>`.

In the following example, we use the Fluent interface comparison operators to:

- Find high priority tasks by comparing the value of the ``priority`` property value with a threshold number, above which priority can be considered high.
- Find just-started or short-running tasks by seeing if the ``progressMinutes`` property falls within a certain range.
- Find unassigned tasks by finding tasks where the ``assignee`` property is equal to null.
- Find tasks assigned to specific teammates Ali or Jamie by seeing if the ``assignee`` property is in a list of names.

.. literalinclude:: /examples/generated/java/sync/ReadsTest.snippet.filter-results.kt
:language: kotlin
:copyable: false

Filter Data with RQL
````````````````````

To filter data with RQL, use :java-sdk:`RealmQuery.rawPredicate()
<io/realm/RealmQuery.html#rawPredicate-java.lang.String->`.
For more information about syntax, usage and limitations,
refer to the :ref:`Realm Query Language reference <realm-query-language>`.

RQL can use either the class and property names defined in your SDK model
classes or the internal names defined with ``@RealmField``. You can combine
raw predicates with other raw predicates or type-safe predicates created with
``RealmQuery``.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
To filter a query, call :js-sdk:`filtered()
<classes/Results.html#filtered>` on the query results collection.
Pass a Realm Query Language query as argument to ``filtered()``.

In the following example, we use RQL comparison operators to:

- Find high priority tasks by comparing the value of the ``priority`` property
value with a threshold number, above which priority can be considered high.
- Find just-started or short-running tasks by seeing if the ``progressMinutes``
property falls within a certain range.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
To filter by property, you can pass Realm Query Language (RQL) filters and
operators, use Kotlin's built-in extension methods or the SDK's convenience
methods, or use a combination.

In the following example, we query a ``Frog`` object type and filter by the
``name`` property.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
To filter results in Objective-C, call :objc-sdk:`-[RLMResults objectsWhere:]
<Classes/RLMResults.html#/c:objc(cs)RLMResults(im)objectsWhere:>`
with a query predicate.

For more details about the supported operators available for Objective-C
queries, refer to :ref:`sdks-nspredicate-query`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Swift has its own query APIs that use either the type-safe ``where`` syntax,
or a string ``NSPredicate`` query. For more details about the supported
operators available for Swift queries, refer to :ref:`sdks-filter-data-swift`.

Type-Safe Queries
`````````````````

To use the :ref:`Realm Swift Query API <ios-realm-swift-query-api>`, call
:swift-sdk:`.where <Structs/Query.html>` with a closure that contains a query
expression as an argument.

This query API provides compile-time type-safe query checking. Prefer using
this API over the older string-based ``NSPredicate`` API.

.. literalinclude:: /examples/generated/code/start/ReadRealmObjects.snippet.where.swift
:language: swift

NSPredicate Queries
```````````````````

To filter with the older string-based ``NSPredicate`` API, call
:swift-sdk:`Results.filter(_:)
<Structs/Results.html#/s:10RealmSwift7ResultsV6filteryACyxGSo11NSPredicateCF>`
with a query predicate.

Prefer using the type-safe query API unless you are using an SDK version
older than 10.19.0. The string-based ``NSPredicate`` query API does not provide
compile-time checking for valid query syntax, and may crash at runtime with
an invalid ``NSPredicate``.
63 changes: 63 additions & 0 deletions source/includes/sdk-examples/crud/read-filter-or-query-objects.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
.. tabs-drivers::

tabs:
- id: cpp-sdk
content: |

.. literalinclude:: /examples/generated/cpp/crud.snippet.filter-using-type-safe-query.cpp
:language: cpp

- id: csharp
content: |

.. literalinclude:: /examples/generated/dotnet/QueryEngineExamples.snippet.rql.cs
:language: csharp

- id: dart
content: |

.. literalinclude:: /examples/generated/flutter/read_write_data_test.snippet.filter-iterable.dart
:language: dart

- id: java
content: |

.. literalinclude:: /examples/generated/java/local/FilterDataTest.snippet.realm-query-language.java
:language: java

- id: java-kotlin
content: |

.. literalinclude:: /examples/generated/java/local/FilterDataTest.snippet.realm-query-language.kt
:language: kotlin

- id: javascript
content: |

.. literalinclude:: /examples/generated/node/read-and-write-data.snippet.read-and-write-filter-queries.js
:language: javascript

- id: kotlin
content: |

.. literalinclude:: /examples/generated/kotlin/ReadTest.snippet.query-by-property.kt
:language: kotlin

- id: objectivec
content: |

.. literalinclude:: /examples/generated/code/start/ReadWriteData.snippet.filter.m
:language: objectivec

- id: swift
content: |

.. literalinclude:: /examples/generated/code/start/ReadRealmObjects.snippet.filter.swift
:language: swift

- id: typescript
content: |

.. literalinclude:: /examples/MissingPlaceholders/example.ts
:language: typescript
:copyable: false
2 changes: 1 addition & 1 deletion source/sdk/crud/query-engines.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ query engines:

- :ref:`realm-query-language`
- :ref:`java-filter-data`
- :ref:`dotnet-linq`
- :ref:`sdks-dotnet-linq`
- :ref:`sdks-filter-data-swift`
Loading

0 comments on commit e491561

Please sign in to comment.