question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Escape columns according if using database specific Query classes

See original GitHub issue

Description

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

  1. Maybe the pypika.Field should be updated to produce the same output in case we are calling this class only with a column like Field("user")?
  2. 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))
  3. This goes beyond the scope of this issue (for the moment?), but maybe extend this escape feature to table names also?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
twheyscommented, Jan 9, 2020

good to hear 😃 thanks for reporting anyway. If you run into any other issues, please don’t hesitate to open another ticket.

0reactions
khalyomedecommented, Jan 9, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found