Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature add sql formatter #84

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

astroshot
Copy link

@astroshot astroshot commented Oct 22, 2022

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

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

@gfrlv
Copy link
Member

gfrlv commented Oct 22, 2022

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
Copy link
Author

Hi, I opened a PR to pgcli last month (dbcli/pgcli#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. 😁

Copy link
Contributor

@j-bennet j-bennet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @astroshot, sorry it took forever to review this. I think this change can be useful to multiple CLIs, even if it will need some tweaking in the future. I left a couple of small comments, but otherwise this looks nice.

# -*- coding: utf-8 -*-

supported_formats = (
"sql-insert",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These formats will need documenting.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll add some later

delimiter: Character surrounds table name or column name when it conflicts with sql keywords.
For example, mysql uses ` and postgres uses "
"""
extract_table_func = kwargs.get("extract_tables")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one way to do it. Alternatively, the CLI could pass in tables as part of kwargs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try it

if not isinstance(delimiter, str):
delimiter = '"'

if tables is not None and len(tables) > 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adapter doesn't really make sense for more than one table, correct? Perhaps there should be an error if we have more than one table.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this formatter is convenient is generate INSERTION SQL or UPDATING SQL relates to one table. In those cases when SQL with multiple tables is run, usually queried results earn more concern than considering to importing them. So I thought it was ok to use a fake table name "DUAL" to indicate this situation, and you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should raise an exception in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants