No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

query.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #-*- coding: utf-8 -*-
  2. from .leobject import LeObject
  3. class LeQueryError(Exception):
  4. pass
  5. ## @brief Handle CRUD operations on datasource
  6. class LeQuery(object):
  7. ## @brief The datasource object used for this Query class
  8. _datasource = None
  9. ## @brief the operators map
  10. # assigns the right operator for a string based key (ex: "lte" => "<=" )
  11. _query_operators_map = {}
  12. ## @brief Constructor
  13. # @param target_class LeObject class or childs : The LeObject child class concerned by this query
  14. def __init__(self, target_class):
  15. if not issubclass(target_class, LeObject):
  16. raise TypeError("target_class have to be a child class of LeObject")
  17. self._target_class = target_class
  18. ## @brief Prepares the query by formatting it into a dictionary
  19. # @param datas dict: query parameters
  20. # @return dict : The formatted query
  21. def prepare_query(self, datas=None):
  22. return {}
  23. ## @brief Executes the query
  24. # @return dict : The results of the query
  25. def execute_query(self):
  26. return {}
  27. ## @brief Handles insert queries
  28. class LeInsertQuery(LeQuery):
  29. # Name of the corresponding action
  30. action = 'insert'
  31. def __init__(self, target_class):
  32. super().__init__(target_class)
  33. if target_class.is_abstract():
  34. raise LeQueryError("Target EmClass cannot be abstract for an InsertQuery")
  35. def prepare_query(self, datas=None):
  36. if datas is None or len(datas.keys()) == 0:
  37. raise LeQueryError("No query datas found")
  38. query = {}
  39. query['action'] = self.__class__.action
  40. query['target'] = self._target_class
  41. for key, value in datas.items():
  42. query[key] = value
  43. return query
  44. ## @brief Handles Le*Query with a query_filter argument
  45. # @see LeGetQuery, LeUpdateQuery, LeDeleteQuery
  46. class LeFilteredQuery(LeQuery):
  47. pass
  48. ## @brief Handles Get queries
  49. class LeGetQuery(LeFilteredQuery):
  50. # Name of the corresponding action
  51. action = 'get'
  52. ## @brief Handles Update queries
  53. class LeUpdateQuery(LeFilteredQuery):
  54. # Name of the corresponding action
  55. action = 'update'
  56. ## @brief Handles Delete queries
  57. class LeDeleteQuery(LeFilteredQuery):
  58. # Name of the corresponding action
  59. action = 'delete'