cli_helpers icon indicating copy to clipboard operation
cli_helpers copied to clipboard

Feature add sql formatter

Open astroshot opened this issue 3 years ago • 2 comments

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 [email protected]:5432/test ➜ SELECT * FROM "user";
INSERT INTO "user" ("id", "name", "email", "phone", "description", "created_at", "updated_at") VALUES
('1', 'Jackson', '[email protected]', '132454789', '', '2022-09-09 19:44:32.712343+08', '2022-09-09 19:44:32.712343+08')
;
SELECT 1
Time: 0.004s

Checklist

  • [x] I've added this contribution to the CHANGELOG.
  • [x] I've added my name to the AUTHORS file (or it's already there).
  • [x] I installed pre-commit hooks (pip install pre-commit && pre-commit install), and ran black on my code.
  • [x] Please squash merge this pull request (uncheck if you'd like us to merge as multiple commits)

astroshot avatar Oct 22 '22 07:10 astroshot

~~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.

gfrlv avatar Oct 22 '22 17:10 gfrlv

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. 😁

astroshot avatar Oct 23 '22 00:10 astroshot