Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle escaping % character in query #90

Merged
merged 4 commits into from
Mar 14, 2024

Conversation

shounakmk219
Copy link
Contributor

This addresses the issue mentioned in #10

@shounakmk219 shounakmk219 requested a review from xiangfu0 March 14, 2024 13:10
@shounakmk219 shounakmk219 marked this pull request as draft March 14, 2024 13:25
@shounakmk219 shounakmk219 marked this pull request as ready for review March 14, 2024 14:16
pinotdb/db.py Outdated


def escape_operation(value: str) -> str:
return value.replace('%', '%%').replace('%(', '(')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One concern I have is what if users are already handling this escaping while sending the query. Should we handle that with return value.replace('%%', '%')replace('%', '%%').replace('%(', '(') ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in that case maybe you can do

value.replace('%%', '%').replace('%', '%%').replace('%(', '(')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or make it a place holder, then convert it back, e.g.

import re

def convert_percent_correctly(text):
    # First, temporarily replace '%%' with a placeholder to avoid modifying them
    temp_placeholder = "TEMP_DOUBLE_PERCENT"
    text = text.replace('%%', temp_placeholder)
    
    # Then, convert standalone '%' to '%%'
    text = re.sub(r'%(?!\()', '%%', text)
    
    # Finally, replace the placeholder back to '%%'
    text = text.replace(temp_placeholder, '%%')
    
    return text

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's much cleaner!

@xiangfu0 xiangfu0 merged commit db48cae into python-pinot-dbapi:master Mar 14, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants