From 6902de3b94825569a6d2f11a79bbe7af10bce103 Mon Sep 17 00:00:00 2001 From: M1ha Date: Wed, 19 Dec 2018 10:06:57 +0500 Subject: [PATCH] Fix fo conditions_as_sql method by review --- src/infi/clickhouse_orm/query.py | 9 +++++---- tests/test_querysets.py | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/infi/clickhouse_orm/query.py b/src/infi/clickhouse_orm/query.py index b705320..9eee645 100644 --- a/src/infi/clickhouse_orm/query.py +++ b/src/infi/clickhouse_orm/query.py @@ -345,10 +345,10 @@ class QuerySet(object): sql = u'SELECT %s%s\nFROM `%s`%s' % params if self._prewhere_q: - sql += '\nPREWHERE ' + self.conditions_as_sql(self._prewhere_q) + sql += '\nPREWHERE ' + self.conditions_as_sql(prewhere=True) if self._where_q: - sql += '\nWHERE ' + self.conditions_as_sql(self._where_q) + sql += '\nWHERE ' + self.conditions_as_sql(prewhere=False) if self._grouping_fields: sql += '\nGROUP BY %s' % comma_join('`%s`' % field for field in self._grouping_fields) @@ -370,10 +370,11 @@ class QuerySet(object): for field in self._order_by ]) - def conditions_as_sql(self, q_object): + def conditions_as_sql(self, prewhere=False): """ Returns the contents of the query's `WHERE` or `PREWHERE` clause as a string. """ + q_object = self._prewhere_q if prewhere else self._where_q return q_object.to_sql(self._model_cls) def count(self): @@ -387,7 +388,7 @@ class QuerySet(object): return int(raw) if raw else 0 # Simple case - conditions = self.conditions_as_sql(self._where_q & self._prewhere_q) + conditions = (self._where_q & self._prewhere_q).to_sql(self._model_cls) return self._database.count(self._model_cls, conditions) def order_by(self, *field_names): diff --git a/tests/test_querysets.py b/tests/test_querysets.py index f5c20c4..298d859 100644 --- a/tests/test_querysets.py +++ b/tests/test_querysets.py @@ -417,13 +417,13 @@ class AggregateTestCase(TestCaseWithData): the__next__number = Int32Field() engine = Memory() qs = Mdl.objects_in(self.database).filter(the__number=1) - self.assertEqual(qs.conditions_as_sql(qs._where_q), 'the__number = 1') + self.assertEqual(qs.conditions_as_sql(), 'the__number = 1') qs = Mdl.objects_in(self.database).filter(the__number__gt=1) - self.assertEqual(qs.conditions_as_sql(qs._where_q), 'the__number > 1') + self.assertEqual(qs.conditions_as_sql(), 'the__number > 1') qs = Mdl.objects_in(self.database).filter(the__next__number=1) - self.assertEqual(qs.conditions_as_sql(qs._where_q), 'the__next__number = 1') + self.assertEqual(qs.conditions_as_sql(), 'the__next__number = 1') qs = Mdl.objects_in(self.database).filter(the__next__number__gt=1) - self.assertEqual(qs.conditions_as_sql(qs._where_q), 'the__next__number > 1') + self.assertEqual(qs.conditions_as_sql(), 'the__next__number > 1') Color = Enum('Color', u'red blue green yellow brown white black')