Exemplo n.º 1
0
	def _build_sql_event_filter(self, time_range, templates, storage_state):
		
		where = WhereClause(WhereClause.AND)
		
		# thekorn: we are using the unary operator here to tell sql to not use
		# the index on the timestamp column at the first place. This `fix` for
		# (LP: #672965) is based on some benchmarks, which suggest a performance
		# win, but we might not oversee all implications.
		# (see http://www.sqlite.org/optoverview.html section 6.0)
		min_time, max_time = time_range
		if min_time != 0:
			where.add("+timestamp >= ?", min_time)
		if max_time != sys.maxint:
			where.add("+timestamp <= ?", max_time)
		
		if storage_state in (StorageState.Available, StorageState.NotAvailable):
			where.add("(subj_storage_state = ? OR subj_storage_state IS NULL)",
				storage_state)
		elif storage_state != StorageState.Any:
			raise ValueError, "Unknown storage state '%d'" % storage_state
		
		where.extend(self._build_sql_from_event_templates(templates))
		
		return where
Exemplo n.º 2
0
	def _build_sql_from_event_templates(self, templates):
	
		where_or = WhereClause(WhereClause.OR)
		
		for template in templates:
			event_template = Event((template[0], [], None))
			if template[1]:
				subject_templates = [Subject(data) for data in template[1]]
			else:
				subject_templates = None
			
			subwhere = WhereClause(WhereClause.AND)
			
			if event_template.id:
				subwhere.add("id = ?", event_template.id)
			
			try:
				value, negation, wildcard = parse_operators(Event, Event.Interpretation, event_template.interpretation)
				# Expand event interpretation children
				event_interp_where = WhereClause(WhereClause.OR, negation)
				for child_interp in (Symbol.find_child_uris_extended(value)):
					if child_interp:
						event_interp_where.add_text_condition("interpretation",
						                       child_interp, like=wildcard, cache=self._interpretation)
				if event_interp_where:
					subwhere.extend(event_interp_where)
				
				value, negation, wildcard = parse_operators(Event, Event.Manifestation, event_template.manifestation)
				# Expand event manifestation children
				event_manif_where = WhereClause(WhereClause.OR, negation)
				for child_manif in (Symbol.find_child_uris_extended(value)):
					if child_manif:
						event_manif_where.add_text_condition("manifestation",
						                      child_manif, like=wildcard, cache=self._manifestation)
				if event_manif_where:
					subwhere.extend(event_manif_where)
				
				value, negation, wildcard = parse_operators(Event, Event.Actor, event_template.actor)
				if value:
					subwhere.add_text_condition("actor", value, wildcard, negation, cache=self._actor)
				
				value, negation, wildcard = parse_operators(Event, Event.Origin, event_template.origin)
				if value:
					subwhere.add_text_condition("origin", value, wildcard, negation)
				
				if subject_templates is not None:
					for subject_template in subject_templates:
						value, negation, wildcard = parse_operators(Subject, Subject.Interpretation, subject_template.interpretation)
						# Expand subject interpretation children
						su_interp_where = WhereClause(WhereClause.OR, negation)
						for child_interp in (Symbol.find_child_uris_extended(value)):
							if child_interp:
								su_interp_where.add_text_condition("subj_interpretation",
													child_interp, like=wildcard, cache=self._interpretation)
						if su_interp_where:
							subwhere.extend(su_interp_where)
						
						value, negation, wildcard = parse_operators(Subject, Subject.Manifestation, subject_template.manifestation)
						# Expand subject manifestation children
						su_manif_where = WhereClause(WhereClause.OR, negation)
						for child_manif in (Symbol.find_child_uris_extended(value)):
							if child_manif:
								su_manif_where.add_text_condition("subj_manifestation",
												   child_manif, like=wildcard, cache=self._manifestation)
						if su_manif_where:
							subwhere.extend(su_manif_where)
						
						value, negation, wildcard = parse_operators(Subject, Subject.Mimetype, subject_template.mimetype)
						if value:
							subwhere.add_text_condition("subj_mimetype",
										 value, wildcard, negation, cache=self._mimetype)
				
						for key in ("uri", "origin", "text"):
							value = getattr(subject_template, key)
							if value:
								value, negation, wildcard = parse_operators(Subject, getattr(Subject, key.title()), value)
								subwhere.add_text_condition("subj_%s" % key, value, wildcard, negation)
						
						if subject_template.current_uri:
							value, negation, wildcard = parse_operators(Subject,
								Subject.CurrentUri, subject_template.current_uri)
							subwhere.add_text_condition("subj_current_uri", value, wildcard, negation)
						
						if subject_template.storage:
							subwhere.add_text_condition("subj_storage", subject_template.storage)
						
			except KeyError, e:
				# Value not in DB
				log.debug("Unknown entity in query: %s" % e)
				where_or.register_no_result()
				continue
			where_or.extend(subwhere)