1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """ Definition of the query language.
21
22 A query is composed of elementary search operations, which are
23 combined by boolean operations:
24
25 >>> query = (AnyWord(u'findme') | Txo('type', item)) & ~ HasField('title')
26
27 """
28
29 from Pyblio import Attribute, Exceptions
30 from Pyblio.Arrays import KeyArray, match_arrays
31
32
34
36
37 return _ANDed(self, other)
38
40
41 return _ORed(self, other)
42
46
47 - def apply (self, fn, *args, **kargs):
48
49 fn(self, *args, ** kargs)
50 return
51
55
56
57
59
61
62 self.a = a
63 return
64
66 return '~ %s' % str(self.a)
67
68 - def apply (self, fn, *args, **kargs):
69
70 self.a.apply(fn, *args, **kargs)
71
80
81
83
84 - def apply(self, fn, *args, **kargs):
85
86 self.a.apply(fn, *args, **kargs)
87 self.b.apply(fn, *args, **kargs)
88 return
89
91
92 return len(self.a) + len(self.b)
93
94
96
98
99 self.a = a
100 self.b = b
101 return
102
104 return '(%s | %s)' % (str(self.a),
105 str(self.b))
106
115
117
119
120 self.a = a
121 self.b = b
122 return
123
125 return '(%s & %s)' % (str(self.a),
126 str(self.b))
127
136
137
138 -class Null(_Constraint):
139 """ Does not search anything, but is useful when programatically
140 constructing a query:
141
142 >>> q = Null()
143 >>> q = q & AnyWord(...)
144
145 """
146
149
152
155
156
158
159 """ Full text searching of a single word """
160
162
163 self.word = word
164 return
165
166
169
172
174 return 'AnyWord(%s)' % repr(self.word)
175
177
178 """ Matches when the record has the specified field."""
179
181 self.field = field
182 return
183
185
186 try:
187 t = schema[self.field]
188
189 except KeyError:
190 raise Exceptions.InvalidQuery('unknown field: %s' % self.field)
191
192 return
193
196
197
198 -class Txo(_Constraint):
199
200 """ Search items that belong to the corresponding txo """
201
202 Attr = Attribute.Txo
203
205
206 self.field = field
207 self.txo = txo
208 return
209
222
225
226
228
229 """ A mixin that provides an (one day optimized) query engine to a store """
230
232 """ Perform a query and return a result set of the matching records. """
233 self._q_check(query)
234
235 r = query.run(self)
236 return self._q_to_rs(r)
237
238
240 """ Perform a query and return the count of matching records. """
241
242 self._q_check(query)
243
244 return len(query.run(self))
245
246
247
248
249
251
252
253 def check(const, schema):
254 const.validate(schema)
255 return
256
257 query.apply(check, self.schema)
258 return
259
260
261
262
264
265 res = KeyArray()
266
267 for e in self.entries.itervalues():
268
269 try:
270 fs = e[q.field]
271
272 except KeyError:
273 continue
274
275 res.add(e.key)
276
277 return res
278
299
301
302 res = KeyArray()
303
304 word = q.word.lower()
305
306 for entry in self.entries.itervalues():
307
308 found = False
309
310 for attrs in entry.values ():
311
312 for attr in attrs:
313 idx = attr.index ()
314
315 if word in idx:
316 found = True
317 break
318
319 if found: break
320
321 if not found: continue
322
323 res.add(entry.key)
324
325 return res
326
327
328
330
331 rs = self.rs.new()
332
333 for key in res:
334 rs.add(key)
335
336 return rs
337
346