Closed mtagle closed 8 months ago
Opeartors are parsed here currently. https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/keywords.py#L105 can you propose a regex that would parse your operator?
We too at Odoo have the problem, we would like to use sqlparse to pretty-print SQL queries generated by our ORM but at the moment the situation is sub-optimal:
SELECT "ir_module_module"."id",
"ir_module_module"."name",
"ir_module_module"."category_id",
"ir_module_module"."shortdesc"->>'en_US',
"ir_module_module"."summary"->>'en_US',
"ir_module_module"."description"->>'en_US',
"ir_module_module"."author",
"ir_module_module"."maintainer",
"ir_module_module"."contributors",
"ir_module_module"."website"
FROM "ir_module_module"
WHERE ("ir_module_module"."id" IN (...)) -- ids striped for github
We use JSON objects to store text indexed by lang (for i18n purpose), so we have pretty much those arrows in all our queries.
It looks like the last sqlparse.keywords.SQL_REGEX
could be
(r'->>|->|([+/@#%^&|^-]+)', tokens.Operator)
Not sure that it is enough but it solves the issue for our use case. Still need to test it a little.
Here is what we are planning to do in our code base waiting for a fix
from sqlparse import keywords, tokens
import re
fixed_regex = r'->>|->|([+/@#%^&|^-]+)'
if callable(keywords.SQL_REGEX[-1][0]): # sqlparse version 0.4.1
FLAGS = re.IGNORECASE | re.UNICODE
keywords.SQL_REGEX[-1] = (re.compile(fixed_regex, FLAGS).match, tokens.Operator)
else: # curent sqlparse master (7fdb2da82d51a9a02baaefb5c7fe5cbbaac4329e)
keywords.SQL_REGEX[-1] = (fixed_regex, tokens.Operator)
I'm aware of at least 2 sql dialects that have
->
("arrow") operators. presto/athena use them as part of lambda expressions (https://prestodb.io/docs/current/functions/lambda.html) and postgres uses them to get things out of a json blob: https://www.postgresql.org/docs/9.4/functions-json.htmlRight now, however, sqlparse does not seem to recognize
->
as an operator so if you format sql with a->
in it withuse_space_around_operators=True
, you will end up breaking the arrow and making your sql invalid.ie:
will print:
(note the
- >
) Obviously a workaround is to not useuse_space_around_operators
, but it would be nice if sqlparse were aware of this as an operator.