criterias: handle empty Or statements (#39463)

This commit is contained in:
Frédéric Péters 2020-02-02 12:27:03 +01:00
parent d49de08b18
commit 80c8c10718
3 changed files with 4 additions and 0 deletions

View File

@ -874,6 +874,7 @@ def test_select_criteria_or_and():
t = data_class()
t.store()
assert [int(x.id) for x in data_class.select([st.Or([])], order_by='id')] == []
assert [x.id for x in data_class.select([st.Or([st.Less('id', 10)])], order_by='id')] == list(range(1, 10))
assert [x.id for x in data_class.select([st.Or([
st.Less('id', 10), st.Equal('id', 15)])], order_by='id')] == list(range(1, 10)) + [15]

View File

@ -309,6 +309,7 @@ def test_select_criteria_or_and():
assert len(Foobar.select()) == 50
assert [int(x.id) for x in Foobar.select([st.Or([])], order_by='id')] == []
assert [int(x.id) for x in Foobar.select([st.Or([st.Less('value', 10)])], order_by='id')] == list(range(1, 10))
assert [int(x.id) for x in Foobar.select([st.Or([
st.Less('value', 10), st.Equal('value', 15)])], order_by='value')] == list(range(1, 10)) + [15]

View File

@ -176,6 +176,8 @@ class Or(Criteria):
self.criterias.append(sql_element)
def as_sql(self):
if not self.criterias:
return '( FALSE )'
return '( %s )' % ' OR '.join([x.as_sql() for x in self.criterias])
def as_sql_param(self):