xo / usql

Universal command-line interface for SQL databases
MIT License
8.88k stars 347 forks source link

Fix newlines in \d output #362

Open henlue opened 2 years ago

henlue commented 2 years ago

This fixes the output of \d *. There were too many newlines printed at the beginning of the output. And no newlines were printed to separate indices and sequences. E.g.:

pg:postgres@localhost/postgres=> \d *

             table "public.table1"
 Name |       Type        | Nullable | Default
------+-------------------+----------+---------
 id   | integer           | "NO"     |
 name | character varying | "YES"    |
Indexes:
  "table1_pkey" PRIMARY_KEY, UNIQUE, index (id)

             table "public.table2"
 Name |       Type        | Nullable | Default
------+-------------------+----------+---------
 id   | integer           | "NO"     |
 name | character varying | "YES"    |
Indexes:
  "table2_pkey" PRIMARY_KEY, UNIQUE, index (id)

                   Sequence "public.sequence1"
  Type  | Start | Min |         Max         | Increment | Cycles?
--------+-------+-----+---------------------+-----------+---------
 bigint | 1     | 1   | 9223372036854775807 | 1         | "NO"
                   Sequence "public.sequence2"
  Type  | Start | Min |         Max         | Increment | Cycles?
--------+-------+-----+---------------------+-----------+---------
 bigint | 1     | 1   | 9223372036854775807 | 1         | "NO"
Index "public.table1_pkey"
 Name |  Type
------+---------
 id   | integer
primary key, index, for table table1
Index "public.table2_pkey"
 Name |  Type
------+---------
 id   | integer
primary key, index, for table table2

TestWriter now also succeeds for pgsql descTable go test ./drivers/ --dbs pgsql --cleanup=false

I restricted the postgres implementation of Tables() to only return tables, views, materialized views and sequences. Technically this would not be necessary since all calls to Tables() now specifically request the types of database objects they require. But still I think it is cleaner since other implementations (mostly information_schema) behave like this as well.

This PR depends on (PR36)[https://github.com/xo/tblfmt/pull/36] in tblfmt.