Re: [sqlalchemy] Mapping a CTE

2011-08-19 Thread Marc DellaVolpe
I am interested in any feedback on the code at https://gist.github.com/1156683 . The code works for me but it's a hacked solution and I'm not sure if this will work generally. I am not sure if there is a better way to handle gather the CTE's, I simply tacked the found CTE onto the compiler object

Re: [sqlalchemy] Mapping a CTE

2011-08-18 Thread Michael Bayer
On Aug 18, 2011, at 2:01 PM, Marc DellaVolpe wrote: > I understand what you are saying about this being an uphill battle on mssql. > > If we ignore the CTE for the moment and pretend that all_dates is a regular > table, shouldn't there be no difference for generation. You can subquery as > n

Re: [sqlalchemy] Mapping a CTE

2011-08-18 Thread Marc DellaVolpe
I understand what you are saying about this being an uphill battle on mssql. If we ignore the CTE for the moment and pretend that all_dates is a regular table, shouldn't there be no difference for generation. You can subquery as needed and all that really needs to happen is to prepend the CTE to

Re: [sqlalchemy] Mapping a CTE

2011-08-18 Thread Michael Bayer
On Aug 18, 2011, at 1:28 PM, Marc DellaVolpe wrote: > There seems to be a problem with the custom compilation. > > >>> print Session.query(Date).order_by(Date.date.desc()) > > WITH all_dates(date) AS ( > SELECT CAST(:start AS DATETIME) AS anon_1 UNION ALL SELECT DATEADD(dd, :step, > date) AS "

Re: [sqlalchemy] Mapping a CTE

2011-08-18 Thread Marc DellaVolpe
There seems to be a problem with the custom compilation. >>> print Session.query(Date).order_by(Date.date.desc()) WITH all_dates(date) AS ( SELECT CAST(:start AS DATETIME) AS anon_1 UNION ALL SELECT DATEADD(dd, :step, date) AS "DATEADD_1" FROM all_dates WHERE DATEADD(dd, :step, date) <= CAST(:sto

Re: [sqlalchemy] Mapping a CTE

2011-08-18 Thread Marc DellaVolpe
>From what I can tell from http://msdn.microsoft.com/en-us/library/ms190766.aspx and experimentally, you can only put CTE's at the top: -- Works WITH all_dates(date) AS ( SELECT CAST('2011-01-01' AS DATETIME) AS anon_1 UNION ALL SELECT DATEADD(dd, 1, date) AS "DATEADD_1" FROM all_dates WHERE DATEA

Re: [sqlalchemy] Mapping a CTE

2011-08-18 Thread Michael Bayer
On Aug 18, 2011, at 11:56 AM, Marc DellaVolpe wrote: > I should have mentioned, I modified the CTE demo to work on SQL Server and I > believe (I will double check this...) that on SQL Server the "with" of the > CTE needs to be at the top of the statement and referenced in subqueries > below.

Re: [sqlalchemy] Mapping a CTE

2011-08-18 Thread Marc DellaVolpe
I should have mentioned, I modified the CTE demo to work on SQL Server and I believe (I will double check this...) that on SQL Server the "with" of the CTE needs to be at the top of the statement and referenced in subqueries below. The generated SQL "SELECT FROM (WITH ...)" is invalid on SQL

Re: [sqlalchemy] Mapping a CTE

2011-08-18 Thread Michael Bayer
On Aug 18, 2011, at 11:23 AM, Marc DellaVolpe wrote: > I found the following CTE demo (http://www.sqlalchemy.org/trac/ > attachment/ticket/1859/cte_demo.py) and I was wondering if there was > any way to map these selects. > > I have built a CTE based select to generate a dates table on the fly >

[sqlalchemy] Mapping a CTE

2011-08-18 Thread Marc DellaVolpe
I found the following CTE demo (http://www.sqlalchemy.org/trac/ attachment/ticket/1859/cte_demo.py) and I was wondering if there was any way to map these selects. I have built a CTE based select to generate a dates table on the fly and I would love to be able to map this and use generative selects