我可以在 DBIx::Class 中漂亮地打印 DBIC_TRACE 输出吗?
将 DBIC_TRACE 环境变量设置为 true:
BEGIN { $ENV{DBIC_TRACE} = 1 }
生成非常有用的输出,特别是显示正在执行的 SQL 查询,但 SQL 查询全部在一行中。
有没有办法将它推入某种“sql tidy”例程以更好地格式化它,也许将它分成多行? 如果做不到这一点,任何人都可以给我一个提示,告诉我需要在代码中添加这样一个钩子吗? 接受格式错误的 SQL 查询并推出格式良好的查询的最佳工具是什么?
在这种情况下,“良好的格式”只是意味着比“全部都在一行上”更好。 我并不特别关心格式化查询的特定样式
谢谢!
Setting the DBIC_TRACE environment variable to true:
BEGIN { $ENV{DBIC_TRACE} = 1 }
generates very helpful output, especially showing the SQL query that is being executed, but the SQL query is all on one line.
Is there a way to push it through some kinda "sql tidy" routine to format it better, perhaps breaking it up over multiple lines? Failing that, could anyone give me a nudge into where in the code I'd need to hack to add such a hook? And what the best tool is to accept a badly formatted SQL query and push out a nicely formatted one?
"nice formatting" in this context simply means better than "all on one line". I'm not particularly fussed about specific styles of formatting queries
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 DBIx::Class 0.08124 开始,它是内置的。
只需设置
$ENV{DBIC_TRACE_PROFILE}
到console
或console_monochrome
。As of DBIx::Class 0.08124 it's built in.
Just set
$ENV{DBIC_TRACE_PROFILE}
toconsole
orconsole_monochrome
.来自 DBIx::Class::Storage 的文档
换句话说,您应该将该类中的
debugobj
设置为子类 DBIx::Class::Storage::Statistics。 在您的子类中,您可以按照您想要的方式重新格式化查询。From the documentation of DBIx::Class::Storage
In other words, you should set
debugobj
in that class to an object that subclasses DBIx::Class::Storage::Statistics. In your subclass, you can reformat the query the way you want it to be.首先谢谢各位指点! 部分答案如下......
到目前为止我得到了什么......首先是一些脚手架:
My::DBIx::Class::Storage::Statistics 的定义是:
这解决了如何挂钩的问题获取 SQL 查询以供我“美化”。
然后我运行一个查询:
但是 SQL::Parser 对 DBIx::Class 生成的 SQL 产生了错误:
那么...是否有比 SQL::Parser 更好的解析器来完成这项工作?
First, thanks for the pointers! Partial answer follows ....
What I've got so far ... first some scaffolding:
And the definition of My::DBIx::Class::Storage::Statistics being:
Which solves the problem about how to hook in to get the SQL query for me to "pretty-ify".
Then I run a query:
However SQL::Parser barfs on the SQL generated by DBIx::Class:
So ... is there a better parser than SQL::Parser for the job?