A semantic search engine for source code https://bitshift.benkurtovic.com/
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

58 wiersze
1.7 KiB

  1. __all__ = ["Tree"]
  2. QUERY_TEMPLATE = """SELECT codelet_id, (codelet_rank + %s) AS score
  3. FROM codelets %s
  4. WHERE %s
  5. GROUP BY codelet_id
  6. ORDER BY score DESC
  7. LIMIT %d OFFSET %d""".replace("\n", " ")
  8. class Tree(object):
  9. """Represents a query tree."""
  10. def __init__(self, root):
  11. self._root = root
  12. def __repr__(self):
  13. return "Tree({0})".format(self._root)
  14. @property
  15. def root(self):
  16. """The root node of the tree."""
  17. return self._root
  18. def sortkey(self):
  19. """Return a string sort key for the query tree."""
  20. return self._root.sortkey()
  21. def serialize(self):
  22. """Create a string representation of the query for caching.
  23. :return: Query string representation.
  24. :rtype: str
  25. """
  26. return repr(self)
  27. def build_query(self, page=1, page_size=10, pretty=False):
  28. """Convert the query tree into a parameterized SQL SELECT statement.
  29. :param page: The page number to get results for.
  30. :type page: int
  31. :param page_size: The number of results per page.
  32. :type page_size: int
  33. :param pretty: Whether to pretty-print the SQL query or not.
  34. :type pretty: bool
  35. :return: SQL query data.
  36. :rtype: 2-tuple of (SQL statement string, query parameter tuple)
  37. """
  38. tables = set()
  39. cond, ranks, arglist = self._root.parameterize(tables)
  40. ranks = ranks or [cond]
  41. score = "((%s) / %d)" % (" + ".join(ranks), len(ranks))
  42. joins = " ".join(tables) # TODO
  43. offset = (page - 1) * page_size
  44. query = QUERY_TEMPLATE % (score, joins, cond, page_size, offset)
  45. return query, tuple(arglist * 2)