doctrine / dbal

Doctrine Database Abstraction Layer
https://www.doctrine-project.org/projects/dbal.html
MIT License
9.48k stars 1.34k forks source link

Add WITH clause (Common Table Expressions/CTE) support to SQL Query Builder #5018

Open 0xPaul opened 3 years ago

0xPaul commented 3 years ago

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

$queryBuilder->addWith('name', $this->createQueryBuilder()->select(...));

References

morozov commented 3 years ago

WITH seems to be a special case of a sub-query from the building/syntax standpoint. Linking https://github.com/doctrine/dbal/issues/2305 for reference.

ianef commented 2 years ago

SQL Server also supports CTEs: https://learn.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-ver16

I'd love to see this included. I have a lot DB views and TVFs that use CTEs and it would be a lot simpler to maintain these as queries in my repository classes. 👍

derrabus commented 2 years ago

Someone has to build this feature so we can include it. Do you want to give it a try?

ianef commented 2 years ago

I’d love to but I have a lot of work at present. Hopefully December will be quieter and I can have a look then.

Cmrickels commented 9 months ago

Has there been any traction on bringing CTEs into query builder ?

derrabus commented 9 months ago

No. Do you want to work on the feature?

n0099 commented 1 month ago

An wrapper library for DBAL level native SQL: https://github.com/somnambulist-tech/cte-builder

ianef commented 1 month ago

Thanks @n0099, that looks like an interesting project. Sadly for me that would mean a rewrite of all my queries as it doesn't look like it supports DQL.

derrabus commented 1 month ago

This is the DBAL repository. DQL is out of scope here anyway. The ORM has its own query builder for DQL.

nio-dtp commented 6 days ago

I made a PR for this. Could I have a review on implementation before adding doc and tests ?

morozov commented 5 days ago

@nio-dtp, thank you for the PR. Reviewing the code w/o being able to run it (by means of unit and integration tests) is challenging. Please add the tests.