Skip to content
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

Allow for ad-hoc table CTEs with parameterized data #304

Conversation

freakingawesome
Copy link
Contributor

This addresses the request in #152 by adding a With(...) variation that takes a list of column names and a list of list of values to create a CTE representation of that data. That CTE can then be used in subsequent SELECT statements to, for example, query efficiently against a number of composite keys.

Here is an example for creating a CTE with two rows of parameterized data:

var query = new Query("rows").With("rows",
    new[] { "a", "b", "c" },
    new object[][] {
        new object[] { 1, 2, 3 },
        new object[] { 4, 5, 6 },
    });

This conceptually results in a CTE whose contents are:

a b c
1 2 3
4 5 6

The default SQL implementation uses a number of SELECT statements joined by UNION ALL:

Postgres Output:

WITH "rows" AS (
    SELECT 1 AS "a", 2 AS "b", 3 AS "c"
    UNION ALL
    SELECT 4 AS "a", 5 AS "b", 6 AS "c"
)
SELECT *
FROM "rows"

SQL Server output uses its own VALUES (...), (...) syntax:

WITH [rows] AS (
    SELECT [a], [b], [c]
    FROM (
        VALUES
            (1, 2, 3),
            (4, 5, 6)
    ) AS tbl ([a], [b], [c])
)
SELECT *
FROM [rows]

Note: The initial issue #152 showed that Postgres has its own syntax for ad-hoc tables in a CTE, but I did not implement that specific flavor because it would involve more substantial changes to the actual WITH portion of the CTE, e.g. WITH cte (a, b, c) AS (...). Postgres instead uses the UNION ALL flavor

@mathijs-dumon
Copy link
Contributor

just a +1
I'd really like to see this land as well!

@ahmad-moussawi ahmad-moussawi force-pushed the master branch 6 times, most recently from f9a6417 to a30bb49 Compare January 1, 2020 11:39
@eyaldar
Copy link

eyaldar commented Jul 14, 2020

Any plans to push this one?

@ahmad-moussawi ahmad-moussawi force-pushed the master branch 7 times, most recently from 838f8ca to 47633a3 Compare March 14, 2021 14:51
@ahmad-moussawi ahmad-moussawi force-pushed the master branch 2 times, most recently from 748c2bf to bad6733 Compare June 12, 2021 03:48
@mnsrulz
Copy link
Contributor

mnsrulz commented Sep 23, 2021

any plans to push? I see some conflicts are there, I can spend some time fixing the conflicts if needed.

@ahmad-moussawi
Copy link
Contributor

@mnsrulz Yes please ping once resolving the conflict to merge it with the master branch
Thanks.

@mnsrulz
Copy link
Contributor

mnsrulz commented Sep 25, 2021

@ahmad-moussawi #524 created with conflicts resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants