-
-
Notifications
You must be signed in to change notification settings - Fork 711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support recursive CTEs in the query interface #1441
Comments
Hi @myyra, I did not have the time to look at this issue. Please hold on a few more days. |
Hello again @myyra,
If you would share some ideas and failed attempts, maybe I could help. Quite possibly the library needs some inner refactoring in order to make recursive CTEs possible, and it would help to identify the current blockers. |
@groue absolutely, I should have done that in the first place... I'll need some time to reacquaint myself with all the details (and I always need a moment to wrap my head around recursive CTEs to begin with 😅) before providing a more in-depth look at the problems I ran into. But from what I recall, the primary challenge was that I couldn't create an association to a CTE before fully initializing it, even though the association only ever needs the name (through It seems simple enough to solve when put like this, but I'm sure there is a good reason for the current structure and API that I can't recall now, and that was the root issue I ran into, no matter the direction I approached this from.
Most likely, yeah. Given that altering the CTE structure might be a significant change, I'm curious about your thoughts on supporting this functionality. Not every use case needs to be accommodated, even though I'd prefer it 😁 Especially if it comes at the cost of complexity or API design. From my perspective, we'd need a new initializer for |
A slight note about the pairs of dual types, so that the intent is more clear. A few such pairs exist:
In general, the public type is just a thin wrapper around the internal one. Its role is only to add some type-safety with Swift generics, so that we can help the compiler prevent some misuses. For example, a So To sum up: the internal type does all the "dirty" stuff with raw SQL, strings, other runtime values, and eventual runtime errors, while the public type uses as much static information as possible in order to prevent runtime errors from happening. Quite surely, I have performed quite a few layering mistakes 😬. For example,
All right, I'll come back with a more detailed response to this question. Thanks for asking. |
I tried to make this into a draft PR at least, but ran into so many dead ends that I didn't feel like opening something that would've required more fixing than reviewing... So I thought it would be better to first explain what I'm trying to achieve.
What did you do?
Tried to build a recursive CTE using the query interface / without writing SQL manually.
What did you expect to happen?
I could get a hold of the CTE "table", so that I can use it in the query interface and generate the recursive part of the CTE.
Given that it's already possible to get quite close, I hope we can find a way to build the whole thing using the query interface.
What happened instead?
Somewhat obviously, I can't access the CTE before it has been initialized. And since it needs the request in the initializer, it can't be constructed with a query that uses itself. This can be avoided by defining a CTE with the same name and empty request, and using it to build a request before giving it to the actual CTE.
Environment
GRDB flavor(s): GRDB
GRDB version: 6.20.0
Installation method: SPM
Xcode version: 15.0
Swift version: 5.9
Platform(s) running GRDB: All
macOS version running Xcode: 14.0
Demo Project
A minimal test case that can be added to
CommonTableExpressionTests.swift
, demonstrating the closest possible solution that can be achieved currently:A slightly longer test case that is closer to the real-world problem I'm trying to solve:
Querying a DAG like structure
The text was updated successfully, but these errors were encountered: