Repository navigation
Add with_ clause for Common Table Expressions - #1440
Open
SahilRajputdotexe wants to merge 1 commit into
Open
SahilRajputdotexe wants to merge 1 commit into
SahilRajputdotexe wants to merge 1 commit into
Conversation
Adds `CTE`, `CTERef`, `CTEColumn` and `CTEError` to `piccolo.query`, and a `with_` method on `Select`, `Update`, `Delete` and `Insert` which prepends a `WITH` (or `WITH RECURSIVE`) clause to the query.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1439
This adds support for
WITHclauses (Common Table Expressions) onSelect,Update,DeleteandInsertqueries.You build a
CTEfrom a name and a body, which can be a query or aQueryString, and pass it towith_.CTE.recursivemakes aWITH RECURSIVEexpression from a base and a recursive part, andmaterialized=True/Falseadds theMATERIALIZED/NOT MATERIALIZEDhint.CTE.ref()returns aCTEReffor the declaredcolumn_names, so the columns can be referred to by name. Mistakes like an invalid name, a duplicate name or a column count that doesn't match raiseCTEError.The CTE is referred to from the main query through raw SQL like above. I didn't try to add joins against a CTE, to keep this small.
I added a docs page under query clauses, and
tests/query/test_cte.py, which checks the generated SQL and runs select, update, delete and insert against the test database.isort,black,flake8,mypyandslotscheckare clean, and I ran the new tests with SQLite locally and the Postgres and CockroachDB jobs on my fork.