- # provide for negation with a field starting with ~
- negation=False
- if field[0] == '~':
- negation = True
- field = field[1:]
-
- if field not in self.fields:
- raise PLCInvalidArgument, "Invalid filter field '%s'" % field
-
- if isinstance(value, (list, tuple, set)):
- # Turn empty list into (NULL) instead of invalid ()
- if not value:
- value = [None]
-
- operator = "IN"
- value = map(str, map(api.db.quote, value))
- value = "(%s)" % ", ".join(value)
+ # handle negation, numeric comparisons
+ # simple, 1-depth only mechanism
+
+ modifiers={'~' : False,
+ '<' : False, '>' : False,
+ '[' : False, ']' : False,
+ '-' : False,
+ }
+
+ for char in modifiers.keys():
+ if field[0] == char:
+ modifiers[char]=True;
+ field = field[1:]
+ break
+
+ # filter on fields
+ if not modifiers['-']:
+ if field not in self.fields:
+ raise PLCInvalidArgument, "Invalid filter field '%s'" % field
+
+ if isinstance(value, (list, tuple, set)):
+ # Turn empty list into (NULL) instead of invalid ()
+ if not value:
+ value = [None]
+
+ operator = "IN"
+ value = map(str, map(api.db.quote, value))
+ value = "(%s)" % ", ".join(value)
+ else:
+ if value is None:
+ operator = "IS"
+ value = "NULL"
+ elif isinstance(value, StringTypes) and \
+ (value.find("*") > -1 or value.find("%") > -1):
+ operator = "LIKE"
+ value = str(api.db.quote(value.replace("*", "%")))
+ else:
+ operator = "="
+ if modifiers['<']:
+ operator='<'
+ if modifiers['>']:
+ operator='>'
+ if modifiers['[']:
+ operator='<='
+ if modifiers[']']:
+ operator='>='
+ else:
+ value = str(api.db.quote(value))
+
+ clause = "%s %s %s" % (field, operator, value)
+
+ if modifiers['~']:
+ clause = " ( NOT %s ) " % (clause)
+
+ conditionals.append(clause)
+ # sorting and clipping