bitemyapp / esqueleto

New home of Esqueleto, please file issues so we can get things caught up!
BSD 3-Clause "New" or "Revised" License
375 stars 108 forks source link

Feature Request: Support for representing literal values as a Table #231

Open JonathanLorimer opened 3 years ago

JonathanLorimer commented 3 years ago

It would be amazing to have support for something like this:

WITH foo (col1, col2, col3) AS (
    SELECT * FROM (
        VALUES
            (1, 2, 3),
            (2, 3, 4)
        ) AS TMP
    )

SELECT * FROM foo

I don't know the esqueleto codebase very well, but it seems that it could be added to the From GADT like so:

data From a where
    Values
        :: PersistEntity ent
        => [ent]
        -> From (SqlExpr (Entity ent))
    Table
        :: PersistEntity ent
        => From (SqlExpr (Entity ent))
    SubQuery
        :: ( SqlSelect a r
           , ToAlias a
           , ToAliasReference a
           )
        => SqlQuery a
        -> From a
    ...
belevy commented 3 years ago

So this isn't a sql standard but rather postgresql specific as far as I can tell. I started work on a branch that eliminates the From GADT in favor of a typeclass. This would allow extensions of From to be made.in the backend specific modules.

JonathanLorimer commented 3 years ago

Ahhhh, you are right, I only use postgresql so I just assumed other dbs had this capability. I would be happy to try and implement Values in the postgresql module once the migration to a typeclass has happened.