xo / usql

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

Add dataTypeFormatter to information schema reader #343

Closed henlue closed 2 years ago

henlue commented 2 years ago

Implements #177

Adds a dataTypeFormatter function to the InformationSchema reader. The function builds the datatype string from a metadata.Column object.

Only working for the postgres driver so far. For the interval type the fields (second, year ...) are not printed. But this is unrelated to this PR.

|         usql Type              |         psql Type              |
+--------------------------------+--------------------------------+
| bit(1)                         | bit(1)                         |
| bit varying(2)                 | bit varying(2)                 |
| character(3)                   | character(3)                   |
| character varying(4)           | character varying(4)           |
| interval                       | interval second(6)             |
| numeric(1,0)                   | numeric(1,0)                   |
| time(4) without time zone      | time(4) without time zone      |
| time(3) with time zone         | time(3) with time zone         |
| timestamp(2) without time zone | timestamp(2) without time zone |
| timestamp(1) with time zone    | timestamp(1) with time zone    |
| bit(1)                         | bit(1)                         |
| bit varying                    | bit varying                    |
| character(1)                   | character(1)                   |
| character varying              | character varying              |
| interval                       | interval                       |
| interval                       | interval second                |
| interval                       | interval year                  |
| numeric                        | numeric                        |
| time(6) without time zone      | time without time zone         |
| time(6) with time zone         | time with time zone            |
| timestamp(6) without time zone | timestamp without time zone    |
| timestamp(6) with time zone    | timestamp with time zone       |
| bigint                         | bigint                         |
| bigint                         | bigint                         |
| boolean                        | boolean                        |
| box                            | box                            |
| bytea                          | bytea                          |
| cidr                           | cidr                           |
| circle                         | circle                         |
| date                           | date                           |
| double precision               | double precision               |
| inet                           | inet                           |
| integer                        | integer                        |
| json                           | json                           |
| jsonb                          | jsonb                          |
| line                           | line                           |
| lseg                           | lseg                           |
| macaddr                        | macaddr                        |
| macaddr8                       | macaddr8                       |
| money                          | money                          |
| path                           | path                           |
| pg_lsn                         | pg_lsn                         |
| pg_snapshot                    | pg_snapshot                    |
| point                          | point                          |
| polygon                        | polygon                        |
| real                           | real                           |
| smallint                       | smallint                       |
| smallint                       | smallint                       |
| integer                        | integer                        |
| text                           | text                           |
| tsvector                       | tsvector                       |
| txid_snapshot                  | txid_snapshot                  |
| uuid                           | uuid                           |
| xml                            | xml                            |

@nineinchnick

henlue commented 2 years ago

Data type formatting is now implemented for postgres, mysql and sqlserver.

I've also included the ReadinessURL fix for the sqlserver tests. On my machine the tests now at least work if I include --cleanup=false. Without the flag they still fail though. The problem is that while the code checks if the sakila schema is there, it is not checking whether all tables have actually been created already.

To fix this properly I would propose to execute the commands from sql-server-sakila-schema.sql directly from the go testing code and not through a separate shell command. Like this it will be obvious to the testing code when the database initialization has finished, and it would reduce complexity, because building docker images for the tests would not be necessary anymore.

Anyways, since this would be a bigger change, I would propose to do this as a separate PR and close this PR in the current state. @nineinchnick