Esqueleto : SELECT COUNT(*) FROM table_name ;

Hi all, Any idea how I would do the equivalent of: SELECT COUNT(*) FROM table_name ; with Esqueleto? I really just want the row count of the table. Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/

At Fri, 23 Nov 2012 20:25:56 +1100, Erik de Castro Lopo wrote:
Any idea how I would do the equivalent of:
SELECT COUNT(*) FROM table_name ;
with Esqueleto? I really just want the row count of the table.
Have you looked at http://hackage.haskell.org/packages/archive/esqueleto/0.2.8/doc/html/Databas...? I didn’t try, but it looks promising. Francesco

Francesco Mazzoli wrote:
Have you looked at http://hackage.haskell.org/packages/archive/esqueleto/0.2.8/doc/html/Databas...? 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 -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/

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/Databas...? 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

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
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/Databas...? 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@haskell.org http://www.haskell.org/mailman/listinfo/web-devel
-- Felipe.

Felipe Almeida Lessa wrote:
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 =).
There are still some problems typing all these and they all still return a list instead of a value whereas the SQL statement returns a single value. Looks like I'll need to dig into the Esqueleto sources. Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/

Erik de Castro Lopo wrote:
There are still some problems typing all these and they all still return a list instead of a value whereas the SQL statement returns a single value.
I stand corrected! This: queryTableNameCount :: SqlPersist IO [Value Int64] queryTableNameCount = select . from $ \(_ :: SqlExpr (Entity TableName)) -> return countRows returns a singleton list with the correct value and no type checker warnings. Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/
participants (4)
-
Erik de Castro Lopo
-
Felipe Almeida Lessa
-
Francesco Mazzoli
-
Gaël Deest