有没有一种简单的方法可以在(rails 3)控制台中漂亮地打印随机 SQL?
类似于awesome_print,甚至可能是Pretty Print。
它不必了解所有可能的方言或超级高级。
我真正想要的只是更轻松地检查 ActiveRecord 生成的 SQL。
目前我只是将 SQL 复制到网上进行格式化,这显然是一个生产力杀手。
我真的很想query.to_sql.pretty_format_sql
看到更好的输出。
谢谢。
有没有一种简单的方法可以在(rails 3)控制台中漂亮地打印随机 SQL?
类似于awesome_print,甚至可能是Pretty Print。
它不必了解所有可能的方言或超级高级。
我真正想要的只是更轻松地检查 ActiveRecord 生成的 SQL。
目前我只是将 SQL 复制到网上进行格式化,这显然是一个生产力杀手。
我真的很想query.to_sql.pretty_format_sql
看到更好的输出。
谢谢。
尝试这个:
git clone https://github.com/sonota/anbt-sql-formatter
cd anbt-sql-formatter
rails setup.rb
然后,在 Rails 初始化程序中:
# config/initializers/pretty_format_sql.rb
class String
def pretty_format_sql
require "anbt-sql-formatter/formatter"
rule = AnbtSql::Rule.new
rule.keyword = AnbtSql::Rule::KEYWORD_UPPER_CASE
%w(count sum substr date).each{|func_name|
rule.function_names << func_name.upcase
}
rule.indent_string = " "
formatter = AnbtSql::Formatter.new(rule)
formatter.format(self)
end
end
测试:
rails console
# Some complex SQL
puts Recipe.joins(:festivity).where(['? BETWEEN festivities.starts_at AND festivities.ends_at', Time.utc(0,Time.now.month,Time.now.day,12,0,0)]).to_sql.pretty_format_sql
SELECT
"recipes" . *
FROM
"recipes" INNER JOIN "festivities"
ON "festivities" . "id" = "recipes" . "festivity_id"
WHERE
(
'0000-04-27 12:00:00.000000' BETWEEN festivities.starts_at AND festivities.ends_at
)
=> nil
我把改进留给你(重构:猴子补丁 -> 模块,自定义格式等 :-))
第一个答案anbt-sql-formatter
的可作为 gem 使用,您可以使用以下方式安装它:
gem install anbt-sql-formatter
这里是一个用法示例:
require "anbt-sql-formatter/formatter"
rule = AnbtSql::Rule.new
formatter = AnbtSql::Formatter.new(rule)
[
"SELECT `col1`, `col2` FROM `table` WHERE ((`col1` = 1) AND (`col2` = 5))",
"SELECT `col1`, `col2` FROM `table` WHERE (`col1` = 1) AND (`col2` = 5)",
"SELECT `col1` FROM `table` WHERE (`col1` IN (SELECT * FROM `table21` WHERE (`col2` = 5)))",
"SELECT `col1` FROM `table` INNER JOIN `tab2` ON (`tab1`.`id` = `tab2`.`id1`) WHERE ((`id` >= 1) AND (`id` <= 5))",
].each{|sql_cmd|
puts "======"
puts sql_cmd
puts formatter.format(sql_cmd)
}
结果:
======
SELECT `col1`, `col2` FROM `table` WHERE ((`col1` = 1) AND (`col2` = 5))
SELECT
`col1`
,`col2`
FROM
`table`
WHERE
(
(
`col1` = 1
)
AND (
`col2` = 5
)
)
======
SELECT `col1`, `col2` FROM `table` WHERE (`col1` = 1) AND (`col2` = 5)
SELECT
`col1`
,`col2`
FROM
`table`
WHERE
(
`col1` = 1
)
AND (
`col2` = 5
)
======
SELECT `col1` FROM `table` WHERE (`col1` IN (SELECT * FROM `table21` WHERE (`col2` = 5)))
SELECT
`col1`
FROM
`table`
WHERE
(
`col1` IN (
SELECT
*
FROM
`table21`
WHERE
(
`col2` = 5
)
)
)
======
SELECT `col1` FROM `table` INNER JOIN `tab2` ON (`tab1`.`id` = `tab2`.`id1`) WHERE ((`id` >= 1) AND (`id` <= 5))
SELECT
`col1`
FROM
`table` INNER JOIN `tab2`
ON (
`tab1`.`id` = `tab2`.`id1`
)
WHERE
(
(
`id` >= 1
)
AND (
`id` <= 5
)
)
还有可能扩展规则,例如
# User defined additional functions:
%w(count sum substr date coalesce).each{|func_name|
rule.function_names << func_name.upcase
}
六年后,这是另一种选择:https ://github.com/kvokka/pp_sql
“用 anbt-sql-formatter gem 替换标准的 ActiveRecord#to_sql 方法,以便在控制台中输出漂亮的 SQL 代码。Rails 日志也将被格式化。”
在后台使用 anbt-sql-formatter,但使其成为 .to_sql 的默认行为
有prettier-plugin-sql有一个很好的输出,例如
SELECT
DISTINCT "events".*
FROM
"events"
INNER JOIN "approvals" ON "approvals"."event_id" = "events"."id"
LEFT OUTER JOIN "attendances" ON "attendances"."event_id" = "events"."id"
WHERE
(
"approvals"."status" = ?
OR "events"."user_id" = ?
)
AND (
"attendances"."user_id" = ?
OR "events"."user_id" = ?
)
如果你在做之后复制你的 sql 查询puts Event.something.to_sql
,你可以这样做:
pbpaste | prettier --parser sql