ruby,Postgres,在一个漂亮的表格中打印sql结果

时间:2018-04-15 05:15:12

标签: ruby postgresql

在Postgres中,如果您连接到数据库并在命令行中发出此查询,您将获得以下结果:

dvdrental=# SELECT 
dvdrental-#   nspname AS schemaname,relname,reltuples
dvdrental-# FROM pg_class C
dvdrental-# LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
dvdrental-# WHERE 
dvdrental-#   nspname NOT IN ('pg_catalog', 'information_schema') AND
dvdrental-#   relkind='r' 
dvdrental-# ORDER BY reltuples DESC;
schemaname |    relname    | reltuples 
------------+---------------+-----------
public     | rental        |     16044
public     | payment       |     14596
public     | film_actor    |      5462
public     | inventory     |      4581
public     | film_category |      1000
public     | film          |      1000
public     | address       |       603
public     | city          |       600
public     | customer      |       599
public     | actor         |       200
public     | country       |       109
public     | category      |        16
public     | language      |         6
public     | store         |         2
public     | staff         |         2
(15 rows)

这是在Postgres命令行中,现在我在ruby代码中运行上面的查询,我得到了结果,然后我将它转换为哈希数组:

[{"schemaname"=>"public", "relname"=>"rental", "reltuples"=>"16044"}, {"schemaname"=>"public", "relname"=>"payment", "reltuples"=>"14596"}, {"schemaname"=>"public", "relname"=>"film_actor", "reltuples"=>"5462"}, {"schemaname"=>"public", "relname"=>"inventory", "reltuples"=>"4581"}, {"schemaname"=>"public", "relname"=>"film_category", "reltuples"=>"1000"}, {"schemaname"=>"public", "relname"=>"film", "reltuples"=>"1000"}, {"schemaname"=>"public", "relname"=>"address", "reltuples"=>"603"}, {"schemaname"=>"public", "relname"=>"city", "reltuples"=>"600"}, {"schemaname"=>"public", "relname"=>"customer", "reltuples"=>"599"}, {"schemaname"=>"public", "relname"=>"actor", "reltuples"=>"200"}, {"schemaname"=>"public", "relname"=>"country", "reltuples"=>"109"}, {"schemaname"=>"public", "relname"=>"category", "reltuples"=>"16"}, {"schemaname"=>"public", "relname"=>"language", "reltuples"=>"6"}, {"schemaname"=>"public", "relname"=>"store", "reltuples"=>"2"}, {"schemaname"=>"public", "relname"=>"staff", "reltuples"=>"2"}]

有没有什么方法可以打印这些哈希数组,使其与从命令行打印的方式相同? 谢谢。

1 个答案:

答案 0 :(得分:1)

也许这会有所帮助。

pg_array = [{"schemaname"=>"public", "relname"=>"rental", "reltuples"=>"16044"},
                        {"schemaname"=>"public", "relname"=>"payment", "reltuples"=>"14596"},
                        {"schemaname"=>"public", "relname"=>"film_actor", "reltuples"=>"5462"},
                        {"schemaname"=>"public", "relname"=>"inventory", "reltuples"=>"4581"},
                        {"schemaname"=>"public", "relname"=>"film_category", "reltuples"=>"1000"},
                        {"schemaname"=>"public", "relname"=>"film", "reltuples"=>"1000"},
                        {"schemaname"=>"public", "relname"=>"address", "reltuples"=>"603"},
                        {"schemaname"=>"public", "relname"=>"city", "reltuples"=>"600"},
                        {"schemaname"=>"public", "relname"=>"customer", "reltuples"=>"599"},
                        {"schemaname"=>"public", "relname"=>"actor", "reltuples"=>"200"},
                        {"schemaname"=>"public", "relname"=>"country", "reltuples"=>"109"},
                        {"schemaname"=>"public", "relname"=>"category", "reltuples"=>"16"},
                        {"schemaname"=>"public", "relname"=>"language", "reltuples"=>"6"},
                        {"schemaname"=>"public", "relname"=>"store", "reltuples"=>"2"},
                        {"schemaname"=>"public", "relname"=>"staff", "reltuples"=>"2"}]

# split into headers and data
data = pg_array.map(&:values)
headers = pg_array.map(&:keys).uniq[0]


# finde each column width
col_width = []
headers.each {|header| col_width << header.size }

data.each do |row|
  row.each_with_index do |value, index|
    col_width[index] = value.length if value.length > col_width[index]
  end
end

# give 1 space more
col_width.map! {|e| e+1} 


# print the output
def print_row(row_array, col_width)
  row_array.each_with_index do |val, index|
    print "|"
    print val
    print " "*(col_width[index]-val.length)
  end
  print "|\n"
end

print_row(headers, col_width)
print "-"*10 + "\n" # change to print the proper separator
data.each {|row| print_row(row, col_width)}
puts "(#{data.size} rows)"