2012-04-27 86 views
13

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

类似于awesome_print或甚至Pretty Print

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

目前我只是复制SQL去在线格式化它显然是一个生产力杀手。

我真的很想query.to_sql.pretty_format_sql,看到更好的输出。

谢谢。

+0

如果你正在使用JRuby,你可以考虑为[类似的问题]一些答案(http://stackoverflow.com/q/312552/215168)合影的Java,比如Hibernate的'org.hibernate作为。 jdbc.util.BasicFormatterImpl' – 2012-04-27 14:11:51

+0

哈,使用MRI。 – 2012-04-29 22:25:15

回答

9

试试这个:

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 

我离开炼就你(重构:猴子补丁 - >模块,定制格式等:-))

+2

它应该做的工作,但我真的想避免使用不推荐使用的rails插件(非宝石)。 – 2012-04-27 10:39:37

+0

我觉得它不是一个如此“标准”的库...但它不应该很难提取出好的部分,并使它们成为宝石(我测试了它,它工作得很好,看到我不能找到替代品,为什么重写工作的东西?)...我会尝试! – mdesantis 2012-04-27 17:04:15

7

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 
}