+ # handle negation, numeric comparisons
+ # simple, 1-depth only mechanism
+
+ modifiers={'~' : False,
+ '<' : False, '>' : False,
+ '[' : False, ']' : False,
+ '-' : False,
+ '&' : False, '|' : False,
+ }
+ def check_modifiers(field):
+ if field[0] in modifiers.keys():
+ modifiers[field[0]] = True
+ field = field[1:]
+ return check_modifiers(field)
+ return field
+ field = check_modifiers(field)
+
+ # filter on fields
+ if not modifiers['-']:
+ if field not in self.fields:
+ raise PLCInvalidArgument, "Invalid filter field '%s'" % field
+
+ # handling array fileds always as compound values
+ if modifiers['&'] or modifiers['|']:
+ if not isinstance(value, (list, tuple, set)):
+ value = [value,]
+
+ def get_op_and_val(value):
+ if value is None:
+ operator = "IS"
+ value = "NULL"
+ elif isinstance(value, StringTypes) and \
+ (value.find("*") > -1 or value.find("%") > -1):
+ operator = "ILIKE"
+ # insert *** in pattern instead of either * or %
+ # we dont use % as requests are likely to %-expansion later on
+ # actual replacement to % done in PostgreSQL.py
+ value = value.replace ('*','***')
+ value = value.replace ('%','***')
+ value = str(api.db.quote(value))
+ else:
+ operator = "="
+ if modifiers['<']:
+ operator='<'
+ if modifiers['>']:
+ operator='>'
+ if modifiers['[']:
+ operator='<='
+ if modifiers[']']:
+ operator='>='
+ value = str(api.db.quote(value))
+ return (operator, value)
+
+ if isinstance(value, (list, tuple, set)):
+ # handling filters like '~slice_id':[]
+ # this should return true, as it's the opposite of 'slice_id':[] which is false
+ # prior to this fix, 'slice_id':[] would have returned ``slice_id IN (NULL) '' which is unknown
+ # so it worked by coincidence, but the negation '~slice_ids':[] would return false too
+ if not value:
+ if modifiers['&'] or modifiers['|']:
+ operator = "="
+ value = "'{}'"
+ else:
+ field=""
+ operator=""
+ value = "FALSE"
+ clause = "%s %s %s" % (field, operator, value)
+ else:
+ vals = {}
+ for val in value:
+ base_op, val = get_op_and_val(val)
+ if base_op in vals:
+ vals[base_op].append(val)
+ else:
+ vals[base_op] = [val]
+ subclauses = []
+ for operator in vals.keys():
+ if operator == '=':
+ if modifiers['&']:
+ subclauses.append("(%s @> ARRAY[%s])" % (field, ",".join(vals[operator])))
+ elif modifiers['|']:
+ subclauses.append("(%s && ARRAY[%s])" % (field, ",".join(vals[operator])))
+ else:
+ subclauses.append("(%s IN (%s))" % (field, ",".join(vals[operator])))
+ elif operator == 'IS':
+ subclauses.append("(%s IS NULL)" % field)
+ else:
+ for value in vals[operator]:
+ subclauses.append("(%s %s %s)" % (field, operator, value))
+ clause = "(" + " OR ".join(subclauses) + ")"
+ else:
+ operator, value = get_op_and_val(value)
+
+ clause = "%s %s %s" % (field, operator, value)
+
+ if modifiers['~']:
+ clause = " ( NOT %s ) " % (clause)
+
+ conditionals.append(clause)
+ # sorting and clipping