0

In Postgres if you connect to a database and issued this query in the command line, you will get these results :

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)

This was in Postgres command line, now I run the above query in ruby code, and I got results, then I converted it to an array of hashes :

[{"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"}]

Is there any nice way on how to print these array of hashes to appear exactley the sameway as it was printed from the command line ? Thanks.

2
  • a for loop and put command? Commented Apr 15, 2018 at 5:23
  • I found this solution : stackoverflow.com/questions/16030738/… , but I got : undefined method `join' for nil:NilClass (NoMethodError) Commented Apr 15, 2018 at 5:42

1 Answer 1

1

Maybe this can help.

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)"
Sign up to request clarification or add additional context in comments.

5 Comments

it looks like there is a problem with the method print_row, now it print only (15 rows) |staff |2 | BUT if I add for example puts "k" at the beginning of print_row it works but replace the firsr character of the first column with "k"
"k" "k"-------e |relname |reltuples | "k"blic |rental |16044 |
@Fatna, I got it: just replaced \r with \n. I was testing on SublimeText not in terminal. Updated the code in the post.
Thanks lot, \n mean new line, what \r does ?
Take a look here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.