Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get esqueleto to generate an SQL string for me?

How can I get esqueleto to generate an SQL string from a from statement?

The documentation of toRawSql says that "you may just turn on query logging of persistent". I tried all possible forms of MonadLogger that I could understand, but it never printed any SQL. The same documentation also says "manually using this function ... is possible but tedious". However, no constructors of the type, nor any functions returning values of the type, QueryType are exported. I managed to get around this by noticing that QueryType is a newtype and using unsafeCoerce!

I was also forced to provide a Connection (which I got via SQLite) even though there should be no need to connect to a database to generate the SQL.

This is what I've got. There must be a better way.

withSqliteConn ":memory:" $     \conn -> return $ toRawSql SELECT                                (unsafeCoerce ((const mempty)                                   :: a -> Text.Lazy.Builder.Builder))                                (conn, initialIdentState) myFromStatement) 

http://hackage.haskell.org/package/esqueleto-1.3.4.2/docs/Database-Esqueleto-Internal-Sql.html

like image 364
Tom Ellis Avatar asked Dec 07 '13 00:12

Tom Ellis


1 Answers

In the time since this question was posted, esqueleto has gone through a number of major revisions. As of version 2.1.2, and several earlier versions, the QueryType a parameter that necessitated your unsafeCoerce has been removed from toRawSql; that major wart is no longer necessary.

As currently implemented, a Connection is required. I believe that, as indicated by the type synonym name, IdentInfo, esqueleto uses this to build identifiers in the query. It may, for example, add the database name. I haven't really plumbed the source in enough depth. Suffice it to say, passing a fake connection (i.e. undefined) doesn't work; I don't know if a mock connection could be implemented. Your solution seems workable.

The rest of your solution should work fine. Since toRawSql is explicitly an internal function, the API here seems reasonable. Although others note that it "should" be possible to generate a connection-neutral string, that appears outside the scope of toRawSql.

You mention that you couldn't use MonadLogger as recommended. What did you try, and what happened?

like image 69
3 revs Avatar answered Sep 28 '22 08:09

3 revs