246
253
if event_manif_where:
247
254
subwhere.extend(event_manif_where)
249
value, negation, wildcard = parse_operators(Subject, Subject.Interpretation, subject_template.interpretation)
250
# Expand subject interpretation children
251
su_interp_where = WhereClause(WhereClause.OR, negation)
252
for child_interp in (Symbol.find_child_uris_extended(value)):
254
su_interp_where.add_text_condition("subj_interpretation",
255
child_interp, like=wildcard, cache=self._interpretation)
257
subwhere.extend(su_interp_where)
259
value, negation, wildcard = parse_operators(Subject, Subject.Manifestation, subject_template.manifestation)
260
# Expand subject manifestation children
261
su_manif_where = WhereClause(WhereClause.OR, negation)
262
for child_manif in (Symbol.find_child_uris_extended(value)):
264
su_manif_where.add_text_condition("subj_manifestation",
265
child_manif, like=wildcard, cache=self._manifestation)
267
subwhere.extend(su_manif_where)
269
# FIXME: Expand mime children as well.
270
# Right now we only do exact matching for mimetypes
271
# thekorn: this will be fixed when wildcards are supported
272
value, negation, wildcard = parse_operators(Subject, Subject.Mimetype, subject_template.mimetype)
274
subwhere.add_text_condition("subj_mimetype",
275
value, wildcard, negation, cache=self._mimetype)
277
256
value, negation, wildcard = parse_operators(Event, Event.Actor, event_template.actor)
279
258
subwhere.add_text_condition("actor", value, wildcard, negation, cache=self._actor)
260
if subject_templates is not None:
261
for subject_template in subject_templates:
262
value, negation, wildcard = parse_operators(Subject, Subject.Interpretation, subject_template.interpretation)
263
# Expand subject interpretation children
264
su_interp_where = WhereClause(WhereClause.OR, negation)
265
for child_interp in (Symbol.find_child_uris_extended(value)):
267
su_interp_where.add_text_condition("subj_interpretation",
268
child_interp, like=wildcard, cache=self._interpretation)
270
subwhere.extend(su_interp_where)
272
value, negation, wildcard = parse_operators(Subject, Subject.Manifestation, subject_template.manifestation)
273
# Expand subject manifestation children
274
su_manif_where = WhereClause(WhereClause.OR, negation)
275
for child_manif in (Symbol.find_child_uris_extended(value)):
277
su_manif_where.add_text_condition("subj_manifestation",
278
child_manif, like=wildcard, cache=self._manifestation)
280
subwhere.extend(su_manif_where)
282
# FIXME: Expand mime children as well.
283
# Right now we only do exact matching for mimetypes
284
# thekorn: this will be fixed when wildcards are supported
285
value, negation, wildcard = parse_operators(Subject, Subject.Mimetype, subject_template.mimetype)
287
subwhere.add_text_condition("subj_mimetype",
288
value, wildcard, negation, cache=self._mimetype)
290
for key in ("uri", "origin", "text"):
291
value = getattr(subject_template, key)
293
value, negation, wildcard = parse_operators(Subject, getattr(Subject, key.title()), value)
294
subwhere.add_text_condition("subj_%s" %key, value, wildcard, negation)
280
295
except KeyError, e:
281
296
# Value not in DB
282
297
log.debug("Unknown entity in query: %s" % e)
283
298
where_or.register_no_result()
286
for key in ("uri", "origin", "text"):
287
value = getattr(subject_template, key)
289
value, negation, wildcard = parse_operators(Subject, getattr(Subject, key.title()), value)
290
subwhere.add_text_condition("subj_%s" %key, value, wildcard, negation)
291
where_or.extend(subwhere)
300
where_or.extend(subwhere)
295
303
def _build_sql_event_filter(self, time_range, templates, storage_state):
350
358
" ORDER BY timestamp ASC",
351
359
" GROUP BY subj_uri ORDER BY timestamp DESC",
352
360
" GROUP BY subj_uri ORDER BY timestamp ASC",
353
" GROUP BY subj_uri ORDER BY COUNT(id) DESC, timestamp DESC",
354
" GROUP BY subj_uri ORDER BY COUNT(id) ASC, timestamp ASC",
355
" GROUP BY actor ORDER BY COUNT(id) DESC, timestamp DESC",
356
" GROUP BY actor ORDER BY COUNT(id) ASC, timestamp ASC",
357
" GROUP BY actor", # implicit: ORDER BY max(timestamp) DESC
358
" ORDER BY timestamp ASC")[order]
361
" GROUP BY subj_uri ORDER BY COUNT(subj_uri) DESC, timestamp DESC",
362
" GROUP BY subj_uri ORDER BY COUNT(subj_uri) ASC, timestamp ASC",
363
" GROUP BY actor ORDER BY COUNT(actor) DESC, timestamp DESC",
364
" GROUP BY actor ORDER BY COUNT(actor) ASC, timestamp ASC",
365
" GROUP BY actor ORDER BY timestamp DESC",
366
" GROUP BY actor ORDER BY timestamp ASC",
367
" GROUP BY subj_origin ORDER BY timestamp DESC",
368
" GROUP BY subj_origin ORDER BY timestamp ASC",
369
" GROUP BY subj_origin ORDER BY COUNT(subj_origin) DESC, timestamp DESC",
370
" GROUP BY subj_origin ORDER BY COUNT(subj_origin) ASC, timestamp ASC")[order]
360
372
if max_events > 0:
361
373
sql += " LIMIT %d" % max_events