dbcli / cli_helpers

Python helpers for common CLI tasks
http://cli-helpers.rtfd.io/
BSD 3-Clause "New" or "Revised" License
92 stars 29 forks source link

Feature add sql formatter #84

Open astroshot opened 1 year ago

astroshot commented 1 year ago

Description

Add sql-formatter for *clis, which convert output data to sql format like insertion sqls or updating sqls.

For example, in pgcli, you can get sql-insert output like this:

PostgreSQL postgres@127.0.0.1:5432/test ➜ SELECT * FROM "user";
INSERT INTO "user" ("id", "name", "email", "phone", "description", "created_at", "updated_at") VALUES
('1', 'Jackson', 'jackson_test@gmail.com', '132454789', '', '2022-09-09 19:44:32.712343+08', '2022-09-09 19:44:32.712343+08')
;
SELECT 1
Time: 0.004s

Checklist

gfrlv commented 1 year ago

cli-helpers is meant to be a general library, common for all dbcli projects. It would be great to have an sql formatter in the base library, however, it would have to use the correct dialect (mysql vs postgres vs whatever). I don't know how to do it correctly. But to add the mysql-specific format, like you're proposing, seems a little out of place here. Should it not live in mycli?

UPD ah, my bad, I see what you're doing. Interesting.

astroshot commented 1 year ago

Hi, I opened a PR to pgcli last month (https://github.com/dbcli/pgcli/pull/1366) to export output to sqls like mycli and it's suggested to move sql-formatter to cli_helpers.

My idea is that:

  1. Move adapter and register function here,
  2. Call register function from clis by passing different delimiter chars (for example, mycli pass ` and pgcli pass ") and extract_tables function.

For sql output, table name is needed, so parsing function is required. Since cli_helpers only does output format job, so I think it's not good enough to import sqlparse pkg here.

So any suggestions? Thanks for your attention. 😁