def query(self): """ Returns a valid query suitable for execution, or raises an exception. """ if self._query: return self._query if not self.load: operations = LoadOp('') else: operations = self.load if self.qualifiers: qualifiers = iter(self.qualifiers) bool_op = query_parser.parse(qualifiers.next()) for qualifier in qualifiers: bool_op = And(bool_op, query_parser.parse(qualifier)) operations = SelectionOp(operations, bool_op) #operations.append(SelectionOp(bool_op)) operations = query_parser.parse_select(operations, self.column_exps) if self.grouping or self.has_aggregates(operations): operations = GroupByOp(operations, *self.grouping) if self.ordering: # todo: eleminate ordering if it matches # the grouping since we already sort #operations.append(self.ordering) operations = OrderByOp(operations, *self.ordering) if self.stop is not None or self.start is not None: if self.start and self.stop: stop = self.start + self.stop else: stop = self.stop operations = SliceOp(operations, self.start, stop) self._query = Query(self.dataset, operations) return self._query
def _run_once(self): self._do_load() put_start = self.wait_idx for i in range(self.wait_idx, len(self.wait_put)): line = self.wait_put[i] q = query_parser.parse(self.parse_fn, line) if not q: continue if not self._safe_put(q): break self.wait_idx += 1 return self.wait_idx - put_start
def join(self, clause, on=None): if not self.load: raise ValueError('Specify at least one relation with frm(...) before using join') if isinstance(clause, QueryBuilder): load = JoinOp(self.load, clause.operations) else: load = query_parser.parse_join(clause, self.load) if on: load.bool_op = query_parser.parse(on) return self.new(load = load )
def perform_search(self, request, resultcount): searcher_identifier = self.searcher.identifier all_query = request.GET.copy() offset = request.GET.get('from', '') or request.POST.get('from', '') or "0" query, params = parse(request,searcher_identifier) result,args = self.searcher.search(query, params, offset, resultcount) results = result.images def resultpart(image): if isinstance(image, ResultRecord): return { "is_record": True, "thumb_url": image.record.get_thumbnail_url(), "title": image.record.title, "record_url": image.record.get_absolute_url(), "identifier": image.record.id } else: return { "thumb_url": image.thumb, "title": image.name, "record_url": image.infourl, "identifier": image.identifier, "content_provider" : image.content_provider } prev_off = hasattr(self.searcher, "previousOffset") and self.searcher.previousOffset(offset, resultcount) prev = None if int(offset)>0 : prev_off =int(offset)-resultcount if prev_off > int(result.total): prev_off = result.total-len(result.images)-resultcount if prev_off <0: prev_off=0 all_query.update({'from':prev_off}) prev = self.__url_search_(all_query) nextPage = None firstPage = None lastPage = None if int(offset)>0: all_query.update({'from':0}) firstPage = self.__url_search_(all_query) if (int(result.nextoffset)<int(result.total)): all_query.update({'from':result.nextoffset}) nextPage = self.__url_search_(all_query) if (nextPage): num_lastPageResult = result.total%resultcount if num_lastPageResult==0: num_lastPageResult=resultcount lastOffset = result.total-num_lastPageResult all_query.update({'from':lastOffset}) lastPage = self.__url_search_(all_query) query_language = "" if "simple_keywords" in args: query_language = args["simple_keywords"] if "query_string" in args: query_language = args["query_string"] return { 'results': map(resultpart, results), 'select_url': self.url_select(), 'next_page': nextPage, 'previous_page': prev, 'first_page': firstPage, 'last_page' :lastPage, 'hits': result.total, 'searcher_name': self.searcher.name, 'searcher_logo': self.searcher.get_logo(), 'searcher_url': self.searcher.get_searcher_page(), 'html_parameters': self.htmlparams(args), 'query': query_language }
def test_number_cannot_be_float(self): with self.assertRaises(ValueError): parse('3.2')
def assertIsNone(self, query, vars={}): self.assertIs(parse(query, vars), None)
def assertEqual(self, query, expected, vars={}): unittest.TestCase.assertEqual( self, parse(query, vars), expected )
def assertFalse(self, query, vars={}): self.assertIs(parse(query, vars), False)
def assertTrue(self, query, vars={}): self.assertIs(parse(query, vars), True)
def test_evaluate(self, query, expected=True, vars={}): """{} evaluates to {}""" self.assertIs(parse(query, vars), expected)
def perform_search(self, request, resultcount): searcher_identifier = self.searcher.identifier all_query = request.GET.copy() #query = request.GET.get('q', '') or request.POST.get('q', '') offset = request.GET.get('from', '') or request.POST.get('from', '') or "0" """ params = {} for key in request.GET: if key.startswith("i_"): params.update({key[2:]:request.GET[key]}) """ query, params = parse(request, searcher_identifier) result, args = self.searcher.search(query, params, offset, resultcount) results = result.images def resultpart(image): if isinstance(image, ResultRecord): return { "is_record": True, "thumb_url": image.record.get_thumbnail_url(), "title": image.record.title, "record_url": image.record.get_absolute_url(), "identifier": image.record.id } else: return { "thumb_url": image.thumb, "title": image.name, "record_url": image.infourl, "identifier": image.identifier, "content_provider": image.content_provider } prev_off = hasattr(self.searcher, "previousOffset") and self.searcher.previousOffset( offset, resultcount) prev = None if int(offset) > 0: prev_off = int(offset) - resultcount if prev_off > int(result.total): prev_off = result.total - len(result.images) - resultcount if prev_off < 0: prev_off = 0 all_query.update({'from': prev_off}) prev = self.__url_search_(all_query) nextPage = None firstPage = None lastPage = None if int(offset) > 0: all_query.update({'from': 0}) firstPage = self.__url_search_(all_query) if (int(result.nextoffset) < int(result.total)): all_query.update({'from': result.nextoffset}) nextPage = self.__url_search_(all_query) if (nextPage): num_lastPageResult = result.total % resultcount if num_lastPageResult == 0: num_lastPageResult = resultcount lastOffset = result.total - num_lastPageResult all_query.update({'from': lastOffset}) lastPage = self.__url_search_(all_query) query_language = "" if "simple_keywords" in args: query_language = args["simple_keywords"] return { 'results': map(resultpart, results), 'select_url': self.url_select(), 'next_page': nextPage, 'previous_page': prev, 'first_page': firstPage, 'last_page': lastPage, 'hits': result.total, 'searcher_name': self.searcher.name, 'searcher_logo': self.searcher.get_logo(), 'searcher_url': self.searcher.get_searcher_page(), 'html_parameters': self.htmlparams(args), 'query': query_language }