2 # Thierry Parmentelat - INRIA
4 from types import StringTypes
13 from PLC.Faults import *
14 from PLC.Parameter import Parameter, Mixed, python_type
16 class Filter(Parameter, dict):
18 A type of parameter that represents a filter on one or more
19 columns of a database table.
20 Special features provide support for negation, upper and lower bounds,
21 sorting and clipping and more...
24 fields should be a dictionary of field names and types.
25 As of PLCAPI-4.3-26, we provide support for filtering on
26 sequence types as well, with the special '&' and '|' modifiers.
27 example : fields = {'node_id': Parameter(int, "Node identifier"),
28 'hostname': Parameter(int, "Fully qualified hostname", max = 255),
32 filter should be a dictionary of field names and values
33 representing the criteria for filtering.
34 example : filter = { 'hostname' : '*.edu' , site_id : [34,54] }
39 * a field starting with the ~ character means negation.
40 example : filter = { '~peer_id' : None }
42 * a field starting with < [ ] or > means lower than or greater than
43 < > uses strict comparison
44 [ ] is for using <= or >= instead
45 example : filter = { ']event_id' : 2305 }
46 example : filter = { '>time' : 1178531418 }
47 in this example the integer value denotes a unix timestamp
49 * if a value is a sequence type, then it should represent
50 a list of possible values for that field
51 example : filter = { 'node_id' : [12,34,56] }
53 * a (string) value containing either a * or a % character is
54 treated as a (sql) pattern; * are replaced with % that is the
55 SQL wildcard character.
56 example : filter = { 'hostname' : '*.jp' }
58 * a field starting with '&' or '|' should refer to a sequence type
59 the semantics is then that the object value (expected to be a list)
60 should contain all (&) or any (|) value specified in the corresponding
61 filter value. See other examples below.
62 example : filter = { '|role_ids' : [ 20, 40 ] }
63 example : filter = { '|roles' : ['tech', 'pi'] }
64 example : filter = { '&roles' : ['admin', 'tech'] }
65 example : filter = { '&roles' : 'tech' }
67 * the filter's keys starting with '-' are special and relate to sorting and clipping
68 * '-SORT' : a field name, or an ordered list of field names that are used for sorting
69 these fields may start with + (default) or - for denoting increasing or decreasing order
70 example : filter = { '-SORT' : [ '+node_id', '-hostname' ] }
71 * '-OFFSET' : the number of first rows to be ommitted
72 * '-LIMIT' : the amount of rows to be returned
73 example : filter = { '-OFFSET' : 100, '-LIMIT':25}
75 * similarly the two special keys below allow to change the semantics of multi-keys filters
76 * '-AND' : select rows that match ALL the criteria (default)
77 * '-OR' : select rows that match ANY criteria
78 The value attached to these keys is ignored.
79 Please note however that because a Filter is a dict, you cannot provide two criteria on a given key.
82 Here are a few realistic examples
84 GetNodes ( { 'node_type' : 'regular' , 'hostname' : '*.edu' ,
85 '-SORT' : 'hostname' , '-OFFSET' : 30 , '-LIMIT' : 25 } )
86 would return regular (usual) nodes matching '*.edu' in alphabetical order from 31th to 55th
88 GetNodes ( { '~peer_id' : None } )
89 returns the foreign nodes - that have an integer peer_id
91 GetPersons ( { '|role_ids' : [ 20 , 40] } )
92 would return all persons that have either pi (20) or tech (40) roles
94 GetPersons ( { '&role_ids' : 10 } )
95 GetPersons ( { '&role_ids' : 10 } )
96 GetPersons ( { '|role_ids' : [ 10 ] } )
97 GetPersons ( { '|role_ids' : [ 10 ] } )
98 all 4 forms are equivalent and would return all admin users in the system
104 def __init__(self, fields = {}, filter = {}, doc = "Attribute filter"):
105 # Store the filter in our dict instance
106 dict.__init__(self, filter)
108 # Declare ourselves as a type of parameter that can take
109 # either a value or a list of values for each of the specified
111 self.fields = dict ( [ ( field, Mixed (expected, [expected]))
112 for (field,expected) in fields.iteritems() ] )
114 # Null filter means no filter
115 Parameter.__init__(self, self.fields, doc = doc, nullok = True)
117 def sql(self, api, join_with = "AND"):
119 Returns a SQL conditional that represents this filter.
122 if self.has_key('-AND'):
125 if self.has_key('-OR'):
129 self.join_with=join_with
131 # So that we always return something
132 if join_with == "AND":
133 conditionals = ["True"]
134 elif join_with == "OR":
135 conditionals = ["False"]
137 assert join_with in ("AND", "OR")
143 for field, value in self.iteritems():
144 # handle negation, numeric comparisons
145 # simple, 1-depth only mechanism
147 modifiers={'~' : False,
148 '<' : False, '>' : False,
149 '[' : False, ']' : False,
151 '&' : False, '|' : False,
153 def check_modifiers(field):
154 if field[0] in modifiers.keys():
155 modifiers[field[0]] = True
157 return check_modifiers(field)
159 field = check_modifiers(field)
162 if not modifiers['-']:
163 if field not in self.fields:
164 raise PLCInvalidArgument, "Invalid filter field '%s'" % field
166 # handling array fileds always as compound values
167 if modifiers['&'] or modifiers['|']:
168 if not isinstance(value, (list, tuple, set)):
171 def get_op_and_val(value):
175 elif isinstance(value, StringTypes) and \
176 (value.find("*") > -1 or value.find("%") > -1):
178 # insert *** in pattern instead of either * or %
179 # we dont use % as requests are likely to %-expansion later on
180 # actual replacement to % done in PostgreSQL.py
181 value = value.replace ('*','***')
182 value = value.replace ('%','***')
183 value = str(api.db.quote(value))
194 value = str(api.db.quote(value))
195 return (operator, value)
197 if isinstance(value, (list, tuple, set)):
198 # handling filters like '~slice_id':[]
199 # this should return true, as it's the opposite of 'slice_id':[] which is false
200 # prior to this fix, 'slice_id':[] would have returned ``slice_id IN (NULL) '' which is unknown
201 # so it worked by coincidence, but the negation '~slice_ids':[] would return false too
203 if modifiers['&'] or modifiers['|']:
210 clause = "%s %s %s" % (field, operator, value)
214 base_op, val = get_op_and_val(val)
216 vals[base_op].append(val)
218 vals[base_op] = [val]
220 for operator in vals.keys():
223 subclauses.append("(%s @> ARRAY[%s])" % (field, ",".join(vals[operator])))
225 subclauses.append("(%s && ARRAY[%s])" % (field, ",".join(vals[operator])))
227 subclauses.append("(%s IN (%s))" % (field, ",".join(vals[operator])))
228 elif operator == 'IS':
229 subclauses.append("(%s IS NULL)" % field)
231 for value in vals[operator]:
232 subclauses.append("(%s %s %s)" % (field, operator, value))
233 clause = "(" + " OR ".join(subclauses) + ")"
235 operator, value = get_op_and_val(value)
237 clause = "%s %s %s" % (field, operator, value)
240 clause = " ( NOT %s ) " % (clause)
242 conditionals.append(clause)
243 # sorting and clipping
245 if field not in ('SORT','OFFSET','LIMIT'):
246 raise PLCInvalidArgument, "Invalid filter, unknown sort and clip field %r"%field
249 if not isinstance(value,(list,tuple,set)):
255 elif field[0] == '-':
258 if field not in self.fields:
259 raise PLCInvalidArgument, "Invalid field %r in SORT filter"%field
260 sorts.append("%s %s"%(field,order))
262 elif field == 'OFFSET':
263 clips.append("OFFSET %d"%value)
265 elif field == 'LIMIT' :
266 clips.append("LIMIT %d"%value)
268 where_part = (" %s " % join_with).join(conditionals)
271 clip_part += " ORDER BY " + ",".join(sorts)
273 clip_part += " " + " ".join(clips)
274 if Filter.debug: print 'Filter.sql: where_part=',where_part,'clip_part',clip_part
275 return (where_part,clip_part)