Make wheres a set instead of list.

This commit is contained in:
voussoir 2017-09-23 11:20:38 -07:00
parent 163e960bfa
commit 855e94ed87

View file

@ -15,14 +15,15 @@ def build_query(
mmf_results=None, mmf_results=None,
): ):
query = ['SELECT * FROM photos'] query = ['SELECT * FROM photos']
wheres = [] wheres = set()
if author_ids: if author_ids:
notnulls.add('author_id') notnulls.add('author_id')
wheres.append('author_id in %s' % helpers.sql_listify(author_ids)) wheres.add('author_id in %s' % helpers.sql_listify(author_ids))
if mmf_results: if mmf_results:
wheres.append('id %s %s' % (mmf_results['operator'], helpers.sql_listify(mmf_results['photoids']))) # "id IN/NOT IN (1, 2, 3)"
wheres.add('id %s %s' % (mmf_results['operator'], helpers.sql_listify(mmf_results['photoids'])))
if orderby: if orderby:
orderby = [o.split('-') for o in orderby] orderby = [o.split('-') for o in orderby]
@ -33,17 +34,20 @@ def build_query(
if column != 'RANDOM()': if column != 'RANDOM()':
notnulls.add(column) notnulls.add(column)
for column in notnulls:
wheres.append(column + ' IS NOT NULL')
for (column, value) in minimums.items(): if minimums:
wheres.append(column + ' >= ' + str(value)) for (column, value) in minimums.items():
wheres.add(column + ' >= ' + str(value))
for (column, value) in maximums.items(): if maximums:
wheres.append(column + ' <= ' + str(value)) for (column, value) in maximums.items():
wheres.add(column + ' <= ' + str(value))
## Assemble ## Assemble
for column in notnulls:
wheres.add(column + ' IS NOT NULL')
if wheres: if wheres:
wheres = 'WHERE ' + ' AND '.join(wheres) wheres = 'WHERE ' + ' AND '.join(wheres)
query.append(wheres) query.append(wheres)