noborus / trdsql

CLI tool that can execute SQL queries on CSV, LTSV, JSON, YAML and TBLN. Can output to various formats.
https://noborus.github.io/trdsql/
MIT License
1.93k stars 72 forks source link

feature request: automatically quote columns #233

Open momiji opened 10 months ago

momiji commented 10 months ago

Hello,

I've jsut discovered this great tool, which is incredible, except for this ugly ` quote character required when column names are not fully lowercased, which is really awful in our bash scripts...

I wonder if it was possible to have some sort of generic way of quoting columns, that can be automatically converted to the good column quote depending on the DB driver.

The idea is to allow something like SELECT [Id], [Name] FROM something and have it converted automatically to " or `.

Maybe the [...] is not the good solution, if some drivers support it, so we may want to switch to something else like {...} or |...|. It should be something that can be used for all known DB drivers.

The complex thing is to parse the SQL string to detect what is inside strings and what is not, to perform the replacement only on the SQL part of the query.

What do you think?

noborus commented 10 months ago

Thank you for the issue.

I would like to implement automatic rewriting if no other problems occur, but in reality unexpected problems do occur. Therefore, I believe that it is best to pass the SQL statements written by the user as is.

In most cases, the database engine used by the user is known, and the user will write SQL statements appropriate for that database.

For example, SQLite3, which is used by many people, uses backquotes, so arguments are enclosed in single quotes and string literals are enclosed in double quotes.

trdsql -driver sqlite3 -ih -oh 'SELECT `Aaa`,`Bbb`,`Ccc`, "literal" as string FROM test.csv'