scala 您如何为以下 Slick 查询打印选择语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14840010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you print the select statements for the following Slick queries?
提问by Hyman
I would like to find out which of the following queries would be the most efficient for getting a row count on a table, so I'm trying to print out the select statements. I know you can add.selectStatementto a Queryable but don't know if this tells me the complete truth because I'll have to remove the result generating code, e.g. .list.lengthand replace it with .selectStatement. Slick probably picks up that you are looking for the length and optimises further, so I want to see the select statement for the whole query, including the SQL that will be generated because of the .list.length, or .count).first
我想找出以下哪个查询对获取表的行数最有效,因此我试图打印出选择语句。我知道您可以添加.selectStatement到 Queryable 中,但不知道这是否告诉了我完整的真相,因为我必须删除结果生成代码,例如.list.length并将其替换为.selectStatement. Slick 可能会发现您正在寻找长度并进一步优化,所以我想查看整个查询的 select 语句,包括由于.list.length, 或.count).first
Query(MyTable).list.length
(for{mt <- MyTable} yield mt).list.length
(for{mt <- MyTable} yield mt.count).first
采纳答案by Hyman
I've not been able to print the select statements using Slick, but Virtualeyes made a good suggestion: Look at the database logs!
我无法使用 Slick 打印选择语句,但 Virtualeyes 提出了一个很好的建议:查看数据库日志!
Well, I test my Slick code in Scala Worksheets, and this is how you go about setting it up - For the worksheets and H2 you need to change the trace level in the database url, e.g.
好吧,我在 Scala Worksheets 中测试了我的 Slick 代码,这就是您设置它的方式 - 对于工作表和 H2,您需要更改数据库 url 中的跟踪级别,例如
implicit val session = Database.forURL(
"jdbc:h2:mem:test1;TRACE_LEVEL_FILE=4",
driver = "org.h2.Driver")
.createSession()
This will tell H2 to log just about everything. Keep in mind though, that you will have to increase the 'maximum number or lines to output' in preferences -> Worksheet.
这将告诉 H2 记录几乎所有内容。但请记住,您必须在首选项 -> 工作表中增加“要输出的最大数量或行数”。
It also turns out that setting Slick at the right level of logging will serve the same purpose.
事实证明,将 Slick 设置在正确的日志记录级别也能达到同样的目的。
Thanks virtualeyes for alerting me to the elephant in the room :-)
感谢 virtualeyes 提醒我注意房间里的大象:-)
回答by user1187983
In play-2.2.1 with slick 2.0.0, in application.conf have:
在带有 2.0.0 的 play-2.2.1 中,在 application.conf 中有:
logger.scala.slick.jdbc.JdbcBackend.statement=DEBUG
回答by Mon Calamari
In Playframework 2.4.xwith Slick 3.0+use following entry:
在Playframework 2.4.x与Slick 3.0+使用下面的条目:
<logger name="slick.jdbc" level="DEBUG"/>
<logger name="slick.jdbc" level="DEBUG"/>
回答by leonidv
In Slick 3.1.0 (and I suppose in 3.0) you can make very cool sql debug:
在 Slick 3.1.0(我想在 3.0 中)中,您可以进行非常酷的 sql 调试:
[DEBUG] - slick.jdbc.JdbcBackend.statement - Preparing statement: select "id", "email", "name", "password" from "users" where ("email" = '[email protected]') and ("password" = ext.crypt('123456',"password"))
[DEBUG] - slick.jdbc.JdbcBackend.benchmark - Execution of prepared statement took 56ms
[DEBUG] - slick.jdbc.StatementInvoker.result - /----------------------+---------------+-------+----------------------\
[DEBUG] - slick.jdbc.StatementInvoker.result - | 1 | 2 | 3 | 4 |
[DEBUG] - slick.jdbc.StatementInvoker.result - | id | email | name | password |
[DEBUG] - slick.jdbc.StatementInvoker.result - |----------------------+---------------+-------+----------------------|
[DEBUG] - slick.jdbc.StatementInvoker.result - | 4fe6e5c3-af74-40f... | [email protected] | petya | a$WyOrBy7p48... |
[DEBUG] - slick.jdbc.StatementInvoker.result - \----------------------+---------------+-------+----------------------/
I use only logback configuration for logging, so it's very easy turn on:
我只使用 logback 配置进行日志记录,所以很容易打开:
<logger name="slick" level="INFO" />
<logger name="slick.jdbc" level="DEBUG" />
回答by jazmit
In Slick 3.0 you can now directly get the SQL for execution directly
在 Slick 3.0 中,您现在可以直接获取 SQL 以直接执行
val q = coffees.filter(_.supID === 15)
val action = q.delete
val affectedRowsCount: Future[Int] = db.run(action)
val sql = action.statements.head
See http://slick.typesafe.com/doc/3.0.0/queries.html#querying
请参阅http://slick.typesafe.com/doc/3.0.0/queries.html#querying
回答by moatra
If you have a logging framework set up, you can set scala.slick.session=DEBUGto log connection pool events and queries.
如果您设置了日志记录框架,则可以设置scala.slick.session=DEBUG为记录连接池事件和查询。
(Note: Setting scala.slick=DEBUGwill drown you with information from the query compiler)
(注意:设置scala.slick=DEBUG会让你被查询编译器的信息淹没)

