1) Further refactoring of as_sql():

merged very similar QuerySet and AggregateQuerySet methods
2) Fixed some bugs, caused by conditions_as_sql() parameters change
This commit is contained in:
M1ha 2018-12-08 11:57:08 +05:00
parent 3bc5f27cda
commit 95055996ad
2 changed files with 33 additions and 43 deletions

View File

@ -252,6 +252,7 @@ class QuerySet(object):
self._order_by = [] self._order_by = []
self._where_q = Q() self._where_q = Q()
self._prewhere_q = Q() self._prewhere_q = Q()
self._grouping_fields = []
self._fields = model_cls.fields().keys() self._fields = model_cls.fields().keys()
self._limits = None self._limits = None
self._distinct = False self._distinct = False
@ -292,19 +293,34 @@ class QuerySet(object):
qs._limits = (start, stop - start) qs._limits = (start, stop - start)
return qs return qs
def select_fields_as_sql(self):
return comma_join('`%s`' % field for field in self._fields) if self._fields else '*'
def as_sql(self): def as_sql(self):
""" """
Returns the whole query as a SQL string. Returns the whole query as a SQL string.
""" """
distinct = 'DISTINCT ' if self._distinct else '' distinct = 'DISTINCT ' if self._distinct else ''
fields = '*'
if self._fields: params = (distinct, self.select_fields_as_sql(), self._model_cls.table_name())
fields = comma_join('`%s`' % field for field in self._fields) sql = u'SELECT %s%s\nFROM `%s`\n' % params
ordering = '\nORDER BY ' + self.order_by_as_sql() if self._order_by else ''
limit = '\nLIMIT %d, %d' % self._limits if self._limits else '' if self._prewhere_q:
params = (distinct, fields, self._model_cls.table_name(), sql += '\nPREWHERE ' + self.conditions_as_sql(self._prewhere_q)
self.conditions_as_sql(), ordering, limit)
return u'SELECT %s%s\nFROM `%s`\nWHERE %s%s%s' % params if self._where_q:
sql += '\nWHERE ' + self.conditions_as_sql(self._where_q)
if self._grouping_fields:
sql += '\nGROUP BY %s' % comma_join('`%s`' % field for field in self._grouping_fields)
if self._order_by:
sql += '\nORDER BY ' + self.order_by_as_sql()
if self._limits:
sql += '\nLIMIT %d, %d' % self._limits
return
def order_by_as_sql(self): def order_by_as_sql(self):
""" """
@ -333,8 +349,10 @@ class QuerySet(object):
sql = u'SELECT count() FROM (%s)' % self.as_sql() sql = u'SELECT count() FROM (%s)' % self.as_sql()
raw = self._database.raw(sql) raw = self._database.raw(sql)
return int(raw) if raw else 0 return int(raw) if raw else 0
# Simple case # Simple case
return self._database.count(self._model_cls, self.conditions_as_sql()) conditions = self.conditions_as_sql(self._where_q & self._prewhere_q)
return self._database.count(self._model_cls, conditions)
def order_by(self, *field_names): def order_by(self, *field_names):
""" """
@ -498,36 +516,8 @@ class AggregateQuerySet(QuerySet):
""" """
raise NotImplementedError('Cannot re-aggregate an AggregateQuerySet') raise NotImplementedError('Cannot re-aggregate an AggregateQuerySet')
def as_sql(self): def select_fields_as_sql(self):
""" return comma_join(list(self._fields) + ['%s AS %s' % (v, k) for k, v in self._calculated_fields.items()])
Returns the whole query as a SQL string.
"""
distinct = 'DISTINCT ' if self._distinct else ''
fields = comma_join(list(self._fields) + ['%s AS %s' % (v, k) for k, v in self._calculated_fields.items()])
params = dict(
distinct=distinct,
fields=fields,
table=self._model_cls.table_name(),
)
sql = u'SELECT %(distinct)s%(fields)s\nFROM `%(table)s`' % params
if self._prewhere_q:
sql += '\nPREWHERE ' + self.conditions_as_sql(self._prewhere_q)
if self._where_q:
sql += '\nWHERE ' + self.conditions_as_sql(self._where_q)
if self._grouping_fields:
sql += '\nGROUP BY %s' % comma_join('`%s`' % field for field in self._grouping_fields)
if self._order_by:
sql += '\nORDER BY ' + self.order_by_as_sql()
if self._limits:
sql += '\nLIMIT %d, %d' % self._limits
return sql
def __iter__(self): def __iter__(self):
return self._database.select(self.as_sql()) # using an ad-hoc model return self._database.select(self.as_sql()) # using an ad-hoc model

View File

@ -369,13 +369,13 @@ class AggregateTestCase(TestCaseWithData):
the__next__number = Int32Field() the__next__number = Int32Field()
engine = Memory() engine = Memory()
qs = Mdl.objects_in(self.database).filter(the__number=1) qs = Mdl.objects_in(self.database).filter(the__number=1)
self.assertEqual(qs.conditions_as_sql(), 'the__number = 1') self.assertEqual(qs.conditions_as_sql(qs._where_q), 'the__number = 1')
qs = Mdl.objects_in(self.database).filter(the__number__gt=1) qs = Mdl.objects_in(self.database).filter(the__number__gt=1)
self.assertEqual(qs.conditions_as_sql(), 'the__number > 1') self.assertEqual(qs.conditions_as_sql(qs._where_q), 'the__number > 1')
qs = Mdl.objects_in(self.database).filter(the__next__number=1) qs = Mdl.objects_in(self.database).filter(the__next__number=1)
self.assertEqual(qs.conditions_as_sql(), 'the__next__number = 1') self.assertEqual(qs.conditions_as_sql(qs._where_q), 'the__next__number = 1')
qs = Mdl.objects_in(self.database).filter(the__next__number__gt=1) qs = Mdl.objects_in(self.database).filter(the__next__number__gt=1)
self.assertEqual(qs.conditions_as_sql(), 'the__next__number > 1') self.assertEqual(qs.conditions_as_sql(qs._where_q), 'the__next__number > 1')
Color = Enum('Color', u'red blue green yellow brown white black') Color = Enum('Color', u'red blue green yellow brown white black')