[web-devel] Esqueleto : SELECT COUNT(*) FROM table_name ;

Felipe Almeida Lessa felipe.lessa at gmail.com
Fri Nov 23 13:22:05 CET 2012


You may just return countRows as Gaël said.  However, there's a catch:
GHC won't have enough information to typecheck the code and infer
which table you're talking about.  You'll probably need something such
as:

  count <-
    select $
    from \(foo :: SqlExpr (Entity Foo)) ->
    return countRows

Alternatively, you could constraint the type via an unused expression:

  count <-
    select $
    from \foo ->
    let _ = foo ^. FooId -- constraint the type of foo
    return countRows

Both code snippets are untested.  Have fun =).

Cheers,

On Fri, Nov 23, 2012 at 9:16 AM, Gaël Deest <gael.deest at gmail.com> wrote:
> Le 23/11/12 12:04, Erik de Castro Lopo a écrit :
>
>> Francesco Mazzoli wrote:
>>
>>> Have you looked at
>>>
>>> <http://hackage.haskell.org/packages/archive/esqueleto/0.2.8/doc/html/Database-Esqueleto.html#v:countRows>?
>>> I didn’t try, but it looks promising.
>>
>> The examples in that section still suggest that the SELECT statement
>> that is run against the database returns an element for each row
>> whereas the what I'm hoping for is a single element returned, the
>> row count.
>>
>> Erik
>
> Never played with Esqueleto, but it looks to me like the following would
> work:
>
> count <- select $ from \foo -> do
>   return countRows
>
>
>
> _______________________________________________
> web-devel mailing list
> web-devel at haskell.org
> http://www.haskell.org/mailman/listinfo/web-devel



-- 
Felipe.



More information about the web-devel mailing list