In the discussion to the
previous article,
dezconnect raised an interesting question about the ability of OData to perform recursive queries (by analogy with SQL CTE).
The
OData documentation in section 11.2.4.2 describes the
$ expand query option. This option allows you to get objects along with related objects. For example, you can get information about a company along with all the information about its president:
https://samples.databoom.space/api1/sampledb/collections/companies(company1)?$expand=presidentWithout the $ expand option, the result will only include the company's president id (or a link with other settings).
https://samples.databoom.space/api1/sampledb/collections/companies(company1)')
In the 4th version of OData, the options of the $ expand option for recursive queries are significantly expanded.For example, a person has friends, they, in turn, also have friends and we want to get the whole tree of friends. To make it easier to write such recursive queries, OData introduces an additional
$ level option that defines the depth of the recursion:
http: // yourhost / service / yourdatabase / person (1)? $ expand = friends ($ level = 2)
This query will build a tree (according to the friends connection) to a depth = 2.
It is possible to specify a search without limiting the depth of recursion by specifying $ levels = max.
But in this case, the problem of cyclic bonds arises. The standard does not specify what should happen in the presence of cyclic connections - therefore, in this case, the depth of recursion should be limited to a specific value.