Escape columns according if using database specific Query classes
See original GitHub issueDescription
I did not saw in the documentation how to escape the columns, and I expected this library to do it in a way or another. In my case I expected it to automatically escape the columns.
Reproduce
Save the following code in a file test.py
from pypika import MySQLQuery, MSSQLQuery, PostgreSQLQuery, Table
table = Table("user")
mysql_query = MySQLQuery.from_(table).select("id", "external_id", "name", "from", "user")
mssql_query = MSSQLQuery.from_(table).select("id", "external_id", "name", "from", "user")
postgresql_query = PostgreSQLQuery.from_(table).select("id", "external_id", "name", "from", "user")
mysql_query_string = mysql_query.get_sql()
mssql_query_string = mssql_query.get_sql()
postgresql_query_string = postgresql_query.get_sql()
print("**** MySQL QUERY ****")
print(mysql_query_string)
print("**** MSSQL QUERY *****")
print(mssql_query_string)
print("**** PostgreSQL QUERY *****")
print(postgresql_query_string)
See the output in console.
python test.py
**** MySQL QUERY ****
SELECT id,external_id,name,from,user FROM user
**** MSSQL QUERY *****
SELECT id,external_id,name,from,user FROM user
**** PostgreSQL QUERY *****
SELECT id,external_id,name,from,user FROM user
Expected result
To see in console the escaped columns.
**** MySQL QUERY ****
SELECT `id`,`external_id`,`name`,`from`,`user` FROM user
**** MSSQL QUERY *****
SELECT [id],[external_id],[name],[from],[user] FROM user
**** PostgreSQL QUERY *****
SELECT "id","external_id","name","from","user" FROM user
Notes
- Maybe the
pypika.Field
should be updated to produce the same output in case we are calling this class only with a column likeField("user")
? - Maybe this could be opt-in in a way or another, meaning if you decide to resolve this issue,
- to make the library to automatically escape the columns, with an option in a defined location (on the class level?) to let the developer to disable this behavior (like
.select("id", "user", escape=False)
) - to make the library not to do anything without the developer to specify wether he want to escape columns or not (like
.select("id", "user", escape=True)
)
- to make the library to automatically escape the columns, with an option in a defined location (on the class level?) to let the developer to disable this behavior (like
- This goes beyond the scope of this issue (for the moment?), but maybe extend this escape feature to table names also?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How do I escape reserved words used as column names ...
Enclose them in the following MySQL specific queries: SET @OLD_SQL_MODE=@@SQL_MODE; SET SESSION SQL_MODE=' ...
Read more >How to write SQL queries with spaces in column names
In this article, we are going to learn how we can write a SQL query with space in the column name. Blanks spaces...
Read more >LIKE (Transact-SQL) - SQL Server - Microsoft Learn
ESCAPE '!' . If ESCAPE and the escape character aren't specified, the Database Engine returns any rows with the string 30!
Read more >Tutorial: Querying and Filtering Rows: Databases for Developers
From Oracle Database 12c you can set the visibility of a column. To view invisible columns you have to list them in the...
Read more >SQL reference for query expressions used in ArcGIS
Databases or enterprise geodatabases use the SQL syntax of the underlying RDBMS, such as, Oracle, SQL Server, PostgreSQL, SAP HANA, and IBM Db2,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
good to hear 😃 thanks for reporting anyway. If you run into any other issues, please don’t hesitate to open another ticket.
It worked, I upgraded to
0.38.*
and it correctly returned the expected output. Got baited by the published tags (did not browsed the last version on PyPI).