漂亮的Ruby中的打印SQL

时间:2012-04-27 08:57:45

标签: sql ruby-on-rails ruby pretty-print

在(rails 3)控制台中有一种简单的漂亮打印随机SQL的方法吗?

类似于awesome_print,甚至可能是Pretty Print

它不必了解所有可能的方言或超级先进 我真正想要的是更容易检查ActiveRecord生成的SQL

目前我只是复制SQL上线来格式化它,这显然是一种生产力杀手。

我真的想要query.to_sql.pretty_format_sql并看到更好的输出。

感谢。

3 个答案:

答案 0 :(得分:11)

试试这个:

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 

我给你留下精炼(重构:猴子修补 - &gt;模块,自定义格式等等:-))

答案 1 :(得分:9)

first answeranbt-sql-formatteravailable as a 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
}

答案 2 :(得分:0)

六年后,这里是另一个选择:https://github.com/kvokka/pp_sql

“用anbt-sql-formatter gem替换标准ActiveRecord#to_sql方法,以便在控制台中输出漂亮的SQL代码。Rails日志也将被格式化。”

在后台使用anbt-sql-formatter,但将其设置为.to_sql的默认行为