问题描述
如何获得esqueleto从 code $!
我也被迫提供一个连接(我通过SQLite获得)甚至尽管应该不需要连接到数据库来生成SQL。
这就是我所得到的。
withSqliteConn:内存:$
\conn - >返回$ toRawSql SELECT
(unsafeCoerce((const mempty)
:: a - > Text.Lazy.Builder.Builder))
(conn,initialIdentState)myFromStatement)
$ b $在发布这个问题之后, esqueleto 已经经历了许多重大的修订。从 2.1.2版和几个较早的版本, QueryType a 参数需要您的 unsafeCoerce 已从 toRawSql ;主要的疣不再是必需的。
目前实施,需要一个连接。我认为,正如类型同义词名称所示, IdentInfo , esqueleto 用于在查询中构建标识符。例如,它可能会添加数据库名称。我没有真正地深入探究资料来源。可以这么说,传递一个假连接(即 undefined )不起作用;我不知道是否可以实施模拟连接。您的解决方案似乎可行。
解决方案的其余部分应该可以正常工作。由于 toRawSql 显式是一个内部函数,因此这里的API似乎是合理的。尽管其他人注意到它应该可以生成一个连接中立的字符串,它出现在 toRawSql 范围之外。
您提到您无法按照建议使用 MonadLogger 。你尝试了什么,发生了什么?
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
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?
这篇关于我如何获得esqueleto来为我生成一个SQL字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!