dont query for empty list

This commit is contained in:
j 2019-01-19 14:22:08 +05:30
parent 3a8429b9ac
commit 98bd2f6cf3

View file

@ -107,33 +107,16 @@ class Parser(object):
vk = getattr(self._sort, k) vk = getattr(self._sort, k)
q = operators.eq(vk, v) q = operators.eq(vk, v)
ids = self._model.query.join(self._sort).filter(q).options(load_only('id')) ids = self._model.query.join(self._sort).filter(q).options(load_only('id'))
if not ids: return self.in_ids(ids, exclude)
q = operators.eq(self._model.id, '')
if exclude:
q = ~q
else:
in_op = operators.notin_op if exclude else operators.in_op
q = in_op(self._model.id, ids)
return q
elif k == 'id': elif k == 'id':
if op == '&': if op == '&':
ids = v ids = v
else: else:
ids = [v] ids = [v]
in_op = operators.notin_op if exclude else operators.in_op return self.in_ids(ids, exclude)
q = in_op(self._model.id, ids)
return q
elif k == 'fulltext': elif k == 'fulltext':
ids = find_fulltext(v) ids = find_fulltext(v)
if ids: return self.in_ids(ids, exclude)
in_op = operators.notin_op if exclude else operators.in_op
q = in_op(self._model.id, ids)
else:
# nothing
q = operators.eq(self._model.id, -1)
if exclude:
q = ~q
return q
elif key_type in ("string", "text"): elif key_type in ("string", "text"):
if isinstance(v, str): if isinstance(v, str):
@ -142,9 +125,7 @@ class Parser(object):
if k != '*': if k != '*':
q &= (self._find.key == k) q &= (self._find.key == k)
ids = self._model.query.join(self._find).filter(q).options(load_only('id')) ids = self._model.query.join(self._find).filter(q).options(load_only('id'))
in_op = operators.notin_op if exclude else operators.in_op return self.in_ids(ids, exclude)
q = in_op(self._model.id, ids)
return q
elif k == 'list': elif k == 'list':
nickname, name = v.split(':', 1) nickname, name = v.split(':', 1)
if nickname: if nickname:
@ -175,9 +156,7 @@ class Parser(object):
) )
else: else:
ids = l.get_items().options(load_only('id')) ids = l.get_items().options(load_only('id'))
in_op = operators.notin_op if exclude else operators.in_op return self.in_ids(ids, exclude)
q = in_op(self._model.id, ids)
return q
elif key_type == 'date': elif key_type == 'date':
def parse_date(d): def parse_date(d):
while len(d) < 3: while len(d) < 3:
@ -188,12 +167,18 @@ class Parser(object):
vk = getattr(self._sort, k) vk = getattr(self._sort, k)
q = get_operator(op, 'int')(vk, v) q = get_operator(op, 'int')(vk, v)
ids = self._model.query.join(self._find).filter(q).options(load_only('id')) ids = self._model.query.join(self._find).filter(q).options(load_only('id'))
in_op = operators.notin_op if exclude else operators.in_op return self.in_ids(ids, exclude)
q = in_op(self._model.id, ids)
return q
else: #integer, float, time else: #integer, float, time
q = get_operator(op, 'int')(getattr(self._sort, k), v) q = get_operator(op, 'int')(getattr(self._sort, k), v)
ids = self._model.query.join(self._find).filter(q).options(load_only('id')) ids = self._model.query.join(self._find).filter(q).options(load_only('id'))
return self.in_ids(ids, exclude)
def in_ids(self, ids, exclude):
if isinstance(ids, list) and not ids:
q = operators.eq(self._model.id, '')
if exclude:
q = ~q
else:
in_op = operators.notin_op if exclude else operators.in_op in_op = operators.notin_op if exclude else operators.in_op
q = in_op(self._model.id, ids) q = in_op(self._model.id, ids)
return q return q