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

Truncate ids in SQL IN clause #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions devserver/modules/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ def format(text, *args, **kwargs):
import re
_sql_fields_re = re.compile(r'SELECT .*? FROM')
_sql_aggregates_re = re.compile(r'SELECT .*?(COUNT|SUM|AVERAGE|MIN|MAX).*? FROM')
_sql_in_numbers_re = re.compile(r'IN\s*\(([\d\s,]+)\)')

def truncate_sql(sql, aggregates=True):
def truncate_number_list(match):
numbers = match.group(1).split(',')
result = [x.strip() for x in numbers[:5]]
if len(numbers) > len(result):
result.append('...')
return u'IN (%s)' % (u', '.join(result))
sql = _sql_in_numbers_re.sub(truncate_number_list, sql)

if not aggregates and _sql_aggregates_re.match(sql):
return sql
return _sql_fields_re.sub('SELECT ... FROM', sql)
Expand Down