3 from types import StringTypes
12 from PLC.Faults import *
13 from PLC.Parameter import Parameter, Mixed, python_type
15 class Filter(Parameter, dict):
17 A type of parameter that represents a filter on one or more
18 columns of a database table.
19 Special features provide support for negation, upper and lower bounds,
20 as well as sorting and clipping.
23 fields should be a dictionary of field names and types.
24 As of PLCAPI-4.3-26, we provide support for filtering on
25 sequence types as well, with the special '&' and '|' modifiers.
26 example : fields = {'node_id': Parameter(int, "Node identifier"),
27 'hostname': Parameter(int, "Fully qualified hostname", max = 255),
31 filter should be a dictionary of field names and values
32 representing the criteria for filtering.
33 example : filter = { 'hostname' : '*.edu' , site_id : [34,54] }
34 Whether the filter represents an intersection (AND) or a union (OR)
35 of these criteria is determined by the join_with argument
36 provided to the sql method below
40 * a field starting with '&' or '|' should refer to a sequence type
41 the semantic is then that the object value (expected to be a list)
42 should contain all (&) or any (|) value specified in the corresponding
43 filter value. See other examples below.
44 example : filter = { '|role_ids' : [ 20, 40 ] }
45 example : filter = { '|roles' : ['tech', 'pi'] }
46 example : filter = { '&roles' : ['admin', 'tech'] }
47 example : filter = { '&roles' : 'tech' }
49 * a field starting with the ~ character means negation.
50 example : filter = { '~peer_id' : None }
52 * a field starting with < [ ] or > means lower than or greater than
53 < > uses strict comparison
54 [ ] is for using <= or >= instead
55 example : filter = { ']event_id' : 2305 }
56 example : filter = { '>time' : 1178531418 }
57 in this example the integer value denotes a unix timestamp
59 * if a value is a sequence type, then it should represent
60 a list of possible values for that field
61 example : filter = { 'node_id' : [12,34,56] }
63 * a (string) value containing either a * or a % character is
64 treated as a (sql) pattern; * are replaced with % that is the
65 SQL wildcard character.
66 example : filter = { 'hostname' : '*.jp' }
68 * the filter's keys starting with '-' are special and relate to sorting and clipping
69 * '-SORT' : a field name, or an ordered list of field names that are used for sorting
70 these fields may start with + (default) or - for denoting increasing or decreasing order
71 example : filter = { '-SORT' : [ '+node_id', '-hostname' ] }
72 * '-OFFSET' : the number of first rows to be ommitted
73 * '-LIMIT' : the amount of rows to be returned
74 example : filter = { '-OFFSET' : 100, '-LIMIT':25}
76 Here are a few realistic examples
78 GetNodes ( { 'node_type' : 'regular' , 'hostname' : '*.edu' , '-SORT' : 'hostname' , '-OFFSET' : 30 , '-LIMIT' : 25 } )
79 would return regular (usual) nodes matching '*.edu' in alphabetical order from 31th to 55th
81 GetPersons ( { '|role_ids' : [ 20 , 40] } )
82 would return all persons that have either pi (20) or tech (40) roles
84 GetPersons ( { '&role_ids' : 10 } )
85 GetPersons ( { '&role_ids' : 10 } )
86 GetPersons ( { '|role_ids' : [ 10 ] } )
87 GetPersons ( { '|role_ids' : [ 10 ] } )
88 all 4 forms are equivalent and would return all admin users in the system
91 def __init__(self, fields = {}, filter = {}, doc = "Attribute filter"):
92 # Store the filter in our dict instance
93 dict.__init__(self, filter)
95 # Declare ourselves as a type of parameter that can take
96 # either a value or a list of values for each of the specified
98 self.fields = dict ( [ ( field, Mixed (expected, [expected]))
99 for (field,expected) in fields.iteritems() ] )
101 # Null filter means no filter
102 Parameter.__init__(self, self.fields, doc = doc, nullok = True)
104 def sql(self, api, join_with = "AND"):
106 Returns a SQL conditional that represents this filter.
109 # So that we always return something
110 if join_with == "AND":
111 conditionals = ["True"]
112 elif join_with == "OR":
113 conditionals = ["False"]
115 assert join_with in ("AND", "OR")
121 for field, value in self.iteritems():
122 # handle negation, numeric comparisons
123 # simple, 1-depth only mechanism
125 modifiers={'~' : False,
126 '<' : False, '>' : False,
127 '[' : False, ']' : False,
129 '&' : False, '|' : False,
131 def check_modifiers(field):
132 if field[0] in modifiers.keys():
133 modifiers[field[0]] = True
135 return check_modifiers(field)
137 field = check_modifiers(field)
140 if not modifiers['-']:
141 if field not in self.fields:
142 raise PLCInvalidArgument, "Invalid filter field '%s'" % field
144 # handling array fileds always as compound values
145 if modifiers['&'] or modifiers['|']:
146 if not isinstance(value, (list, tuple, set)):
149 if isinstance(value, (list, tuple, set)):
150 # handling filters like '~slice_id':[]
151 # this should return true, as it's the opposite of 'slice_id':[] which is false
152 # prior to this fix, 'slice_id':[] would have returned ``slice_id IN (NULL) '' which is unknown
153 # so it worked by coincidence, but the negation '~slice_ids':[] would return false too
155 if modifiers['&'] or modifiers['|']:
163 value = map(str, map(api.db.quote, value))
166 value = "ARRAY[%s]" % ", ".join(value)
169 value = "ARRAY[%s]" % ", ".join(value)
172 value = "(%s)" % ", ".join(value)
177 elif isinstance(value, StringTypes) and \
178 (value.find("*") > -1 or value.find("%") > -1):
180 # insert *** in pattern instead of either * or %
181 # we dont use % as requests are likely to %-expansion later on
182 # actual replacement to % done in PostgreSQL.py
183 value = value.replace ('*','***')
184 value = value.replace ('%','***')
185 value = str(api.db.quote(value))
197 value = str(api.db.quote(value))
199 clause = "%s %s %s" % (field, operator, value)
202 clause = " ( NOT %s ) " % (clause)
204 conditionals.append(clause)
205 # sorting and clipping
207 if field not in ('SORT','OFFSET','LIMIT'):
208 raise PLCInvalidArgument, "Invalid filter, unknown sort and clip field %r"%field
211 if not isinstance(value,(list,tuple,set)):
217 elif field[0] == '-':
220 if field not in self.fields:
221 raise PLCInvalidArgument, "Invalid field %r in SORT filter"%field
222 sorts.append("%s %s"%(field,order))
224 elif field == 'OFFSET':
225 clips.append("OFFSET %d"%value)
227 elif field == 'LIMIT' :
228 clips.append("LIMIT %d"%value)
230 where_part = (" %s " % join_with).join(conditionals)
233 clip_part += " ORDER BY " + ",".join(sorts)
235 clip_part += " " + " ".join(clips)
236 # print 'where_part=',where_part,'clip_part',clip_part
237 return (where_part,clip_part)