How to change only certain conditions in complex where clause #426
-
I have a sql query with a reasonably complex where clause:
Everywhere where 'XXX[0-9]' is part of a condition, I want to comment out that change that particular condition to 1=1 (for the sake of example). So the desired output is:
I tried playing around with the transform method, but can't figure it out. In other queries where I plan to use this for there can be other constructs/variants. So I need it to be a bit general. Something in the gist of:
Love the library, btw. Better than sqlparse, one reason being that it runs on Windows... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The following seems to work correctly for your example, but I'm not sure if it's what you're after: from sqlglot import exp, parse_one
def transformer(node):
if isinstance(node, exp.Predicate):
if 'XXX' in node.text('this') or 'XXX' in node.text('expression'):
return parse_one("1=1")
return node
print(parse_one(<your query>).transform(transformer).sql(pretty=True)) |
Beta Was this translation helpful? Give feedback.
The following seems to work correctly for your example, but I'm not sure if it's what you're after: