@@ -40,6 +40,7 @@ class InvalidQueryError(ParsingError):
40
40
41
41
The query should be a unicode string or a list, which will be space-joined.
42
42
"""
43
+
43
44
def __init__ (self , query , explanation ):
44
45
if isinstance (query , list ):
45
46
query = " " .join (query )
@@ -53,6 +54,7 @@ class InvalidQueryArgumentValueError(ParsingError):
53
54
It exists to be caught in upper stack levels so a meaningful (i.e. with the
54
55
query) InvalidQueryError can be raised.
55
56
"""
57
+
56
58
def __init__ (self , what , expected , detail = None ):
57
59
message = u"'{0}' is not {1}" .format (what , expected )
58
60
if detail :
@@ -63,6 +65,7 @@ def __init__(self, what, expected, detail=None):
63
65
class Query (object ):
64
66
"""An abstract class representing a query into the item database.
65
67
"""
68
+
66
69
def clause (self ):
67
70
"""Generate an SQLite expression implementing the query.
68
71
@@ -95,6 +98,7 @@ class FieldQuery(Query):
95
98
string. Subclasses may also provide `col_clause` to implement the
96
99
same matching functionality in SQLite.
97
100
"""
101
+
98
102
def __init__ (self , field , pattern , fast = True ):
99
103
self .field = field
100
104
self .pattern = pattern
@@ -134,6 +138,7 @@ def __hash__(self):
134
138
135
139
class MatchQuery (FieldQuery ):
136
140
"""A query that looks for exact matches in an item field."""
141
+
137
142
def col_clause (self ):
138
143
return self .field + " = ?" , [self .pattern ]
139
144
@@ -143,6 +148,7 @@ def value_match(cls, pattern, value):
143
148
144
149
145
150
class NoneQuery (FieldQuery ):
151
+ """A query that checks whether a field is null."""
146
152
147
153
def __init__ (self , field , fast = True ):
148
154
super (NoneQuery , self ).__init__ (field , None , fast )
@@ -165,6 +171,7 @@ class StringFieldQuery(FieldQuery):
165
171
"""A FieldQuery that converts values to strings before matching
166
172
them.
167
173
"""
174
+
168
175
@classmethod
169
176
def value_match (cls , pattern , value ):
170
177
"""Determine whether the value matches the pattern. The value
@@ -182,11 +189,12 @@ def string_match(cls, pattern, value):
182
189
183
190
class SubstringQuery (StringFieldQuery ):
184
191
"""A query that matches a substring in a specific item field."""
192
+
185
193
def col_clause (self ):
186
194
pattern = (self .pattern
187
- .replace ('\\ ' , '\\ \\ ' )
188
- .replace ('%' , '\\ %' )
189
- .replace ('_' , '\\ _' ))
195
+ .replace ('\\ ' , '\\ \\ ' )
196
+ .replace ('%' , '\\ %' )
197
+ .replace ('_' , '\\ _' ))
190
198
search = '%' + pattern + '%'
191
199
clause = self .field + " like ? escape '\\ '"
192
200
subvals = [search ]
@@ -204,6 +212,7 @@ class RegexpQuery(StringFieldQuery):
204
212
Raises InvalidQueryError when the pattern is not a valid regular
205
213
expression.
206
214
"""
215
+
207
216
def __init__ (self , field , pattern , fast = True ):
208
217
super (RegexpQuery , self ).__init__ (field , pattern , fast )
209
218
pattern = self ._normalize (pattern )
@@ -231,6 +240,7 @@ class BooleanQuery(MatchQuery):
231
240
"""Matches a boolean field. Pattern should either be a boolean or a
232
241
string reflecting a boolean.
233
242
"""
243
+
234
244
def __init__ (self , field , pattern , fast = True ):
235
245
super (BooleanQuery , self ).__init__ (field , pattern , fast )
236
246
if isinstance (pattern , six .string_types ):
@@ -244,6 +254,7 @@ class BytesQuery(MatchQuery):
244
254
`unicode` equivalently in Python 2. Always use this query instead of
245
255
`MatchQuery` when matching on BLOB values.
246
256
"""
257
+
247
258
def __init__ (self , field , pattern ):
248
259
super (BytesQuery , self ).__init__ (field , pattern )
249
260
@@ -270,6 +281,7 @@ class NumericQuery(FieldQuery):
270
281
Raises InvalidQueryError when the pattern does not represent an int or
271
282
a float.
272
283
"""
284
+
273
285
def _convert (self , s ):
274
286
"""Convert a string to a numeric type (float or int).
275
287
@@ -337,6 +349,7 @@ class CollectionQuery(Query):
337
349
"""An abstract query class that aggregates other queries. Can be
338
350
indexed like a list to access the sub-queries.
339
351
"""
352
+
340
353
def __init__ (self , subqueries = ()):
341
354
self .subqueries = subqueries
342
355
@@ -389,6 +402,7 @@ class AnyFieldQuery(CollectionQuery):
389
402
any field. The individual field query class is provided to the
390
403
constructor.
391
404
"""
405
+
392
406
def __init__ (self , pattern , fields , cls ):
393
407
self .pattern = pattern
394
408
self .fields = fields
@@ -424,6 +438,7 @@ class MutableCollectionQuery(CollectionQuery):
424
438
"""A collection query whose subqueries may be modified after the
425
439
query is initialized.
426
440
"""
441
+
427
442
def __setitem__ (self , key , value ):
428
443
self .subqueries [key ] = value
429
444
@@ -433,6 +448,7 @@ def __delitem__(self, key):
433
448
434
449
class AndQuery (MutableCollectionQuery ):
435
450
"""A conjunction of a list of other queries."""
451
+
436
452
def clause (self ):
437
453
return self .clause_with_joiner ('and' )
438
454
@@ -442,6 +458,7 @@ def match(self, item):
442
458
443
459
class OrQuery (MutableCollectionQuery ):
444
460
"""A conjunction of a list of other queries."""
461
+
445
462
def clause (self ):
446
463
return self .clause_with_joiner ('or' )
447
464
@@ -453,6 +470,7 @@ class NotQuery(Query):
453
470
"""A query that matches the negation of its `subquery`, as a shorcut for
454
471
performing `not(subquery)` without using regular expressions.
455
472
"""
473
+
456
474
def __init__ (self , subquery ):
457
475
self .subquery = subquery
458
476
@@ -481,6 +499,7 @@ def __hash__(self):
481
499
482
500
class TrueQuery (Query ):
483
501
"""A query that always matches."""
502
+
484
503
def clause (self ):
485
504
return '1' , ()
486
505
@@ -490,6 +509,7 @@ def match(self, item):
490
509
491
510
class FalseQuery (Query ):
492
511
"""A query that never matches."""
512
+
493
513
def clause (self ):
494
514
return '0' , ()
495
515
0 commit comments