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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 used in query definitions
  10. _query_operators_map = ['=', '<=', '>=', '!=', '<', '>', ' in ', ' not in ', ' like ', ' not like ']
  11. ## @brief Constructor
  12. # @param target_class LeObject class or childs : The LeObject child class concerned by this query
  13. def __init__(self, target_class):
  14. if not issubclass(target_class, LeObject):
  15. raise TypeError("target_class have to be a child class of LeObject")
  16. self._target_class = target_class
  17. ## @brief Prepares the query by formatting it into a dictionary
  18. # @param datas dict: query parameters
  19. # @return dict : The formatted query
  20. def prepare_query(self, datas=None):
  21. return {}
  22. ## @brief Executes the query
  23. # @return dict : The results of the query
  24. def execute(self):
  25. return {}
  26. ## @brief Handles insert queries
  27. class LeInsertQuery(LeQuery):
  28. # Name of the corresponding action
  29. action = 'insert'
  30. def __init__(self, target_class):
  31. super().__init__(target_class)
  32. if target_class.is_abstract():
  33. raise LeQueryError("Target EmClass cannot be abstract for an InsertQuery")
  34. def prepare_query(self, datas=None):
  35. if datas is None or len(datas.keys()) == 0:
  36. raise LeQueryError("No query datas found")
  37. query = {}
  38. query['action'] = self.__class__.action
  39. query['target'] = self._target_class
  40. for key, value in datas.items():
  41. query[key] = value
  42. return query
  43. ## @brief Handles Le*Query with a query_filter argument
  44. # @see LeGetQuery, LeUpdateQuery, LeDeleteQuery
  45. class LeFilteredQuery(LeQuery):
  46. pass
  47. ## @brief Handles Get queries
  48. class LeGetQuery(LeFilteredQuery):
  49. # Name of the corresponding action
  50. action = 'get'
  51. ## @brief Handles Update queries
  52. class LeUpdateQuery(LeFilteredQuery):
  53. # Name of the corresponding action
  54. action = 'update'
  55. def __init__(self, datas=None):
  56. self.datas = datas if datas is None else locals()
  57. del(self.datas['self'])
  58. ## @brief executes the query, with corresponding hooks
  59. # @return dict
  60. def execute(self):
  61. # LodelHook.call_hook('leapi_update_pre', self, None)
  62. ret = self.update()
  63. return self.after_update(ret)
  64. ## @brief calls before-update hook(s)
  65. # @return dict
  66. # @todo to be implemented
  67. def before_update(self):
  68. return self.datas
  69. ## @brief calls hook(s) after the update process
  70. # @param ret : returned results
  71. # @return bool : True if success
  72. # @todo to be implemented
  73. def after_update(self, ret):
  74. return ret
  75. ## @brief performs the update in the datasource
  76. # @TODO the call to _datasource.update() will be changed when the corresponding method is implemented
  77. # @TODO in case of an error, we should add code to manage it
  78. def update(self):
  79. if 'uid' not in self.datas:
  80. raise LeQueryError("No object uid given to the query. The update can't be performed")
  81. updated_datas = self.prepare_query()
  82. ret = self._datasource.update(self.datas['uid'], **updated_datas)
  83. if ret == 1:
  84. return True
  85. return False
  86. ## @brief checks and prepare datas
  87. # @return dict
  88. def prepare_query(self):
  89. ret_datas = self.check_datas_value(self.datas)
  90. if isinstance(ret_datas, Exception):
  91. raise ret_datas
  92. ret_datas = self.construct_datas(ret_datas)
  93. self.check_datas_consistency(ret_datas)
  94. return ret_datas
  95. def check_datas_value(self, datas):
  96. err_l = dict() # Stores all errors occurring during the process
  97. correct = [] # Valid fields name
  98. mandatory = [] # mandatory fields name
  99. pass
  100. def construct_datas(self, datas):
  101. pass
  102. def check_datas_consistency(self, datas):
  103. pass
  104. ## @brief Handles Delete queries
  105. class LeDeleteQuery(LeFilteredQuery):
  106. # Name of the corresponding action
  107. action = 'delete'