Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Truncate ids in SQL IN clause #49

Open
wants to merge 1 commit into from

2 participants

@lanior

sqlparse place each value in IN clause into separate line making queries generated by prefetch_related and batch_select unreadable.
This patch limits IN clause size to 5 values.

@adamchainz

+1 - big annoyance here too

@coagulant coagulant referenced this pull request from a commit in coagulant/django-devserver
@coagulant coagulant Truncate ids in SQL IN clause eea3fe8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 19, 2011
  1. @lanior

    Truncate ids in SQL IN clause

    lanior authored
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 0 deletions.
  1. +10 −0 devserver/modules/sql.py
View
10 devserver/modules/sql.py
@@ -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)
Something went wrong with that request. Please try again.