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.

ledatasourcesql.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #-*- coding: utf-8 -*-
  2. import pymysql
  3. from leobject.datasources.dummy import DummyDatasource
  4. from mosql.db import Database, all_to_dicts
  5. from mosql.query import select, insert, update, delete, join
  6. from leobject.leobject import REL_SUB, REL_SUP
  7. import mosql.mysql
  8. ## SQL DataSource for LeObject
  9. class LeDataSourceSQL(DummyDatasource):
  10. RELATIONS_TABLE_NAME = 'relations'
  11. RELATIONS_POSITIONS_FIELDS = {REL_SUP: 'superior_id', REL_SUB: 'subordinate_id'}
  12. RELATIONS_NATURE_FIELD = 'nature'
  13. def __init__(self, module=pymysql, conn_args={'host': '127.0.0.1', 'user':'lodel', 'passwd':'bruno', 'db': 'lodel2'}):
  14. super(LeDataSourceSQL, self).__init__()
  15. self.module = module
  16. self.connection = Database(pymysql, host=conn_args['host'], user=conn_args['user'], passwd=conn_args['passwd'], db=conn_args['db'])
  17. ## @brief update an existing object's data
  18. # @param letype LeType
  19. # @param leclass LeClass
  20. # @param filters list : list of tuples formatted as (FIELD, OPERATOR, VALUE)
  21. # @param rel_filters list : list of tuples formatted as (('superior'|'subordinate', FIELD), OPERATOR, VALUE)
  22. # @param data dict
  23. # @return bool
  24. # @todo prendre en compte les rel_filters
  25. def update(self, letype, leclass, filters, rel_filters, data):
  26. query_table_name = leclass.name
  27. where_filters = filters
  28. set_data = data
  29. # Building the query
  30. query = update(table=query_table_name, where=where_filters, set=set_data)
  31. # Executing the query
  32. with self.connection as cur:
  33. cur.execute(query)
  34. return True
  35. ## @brief create a new object in the datasource
  36. # @param letype LeType
  37. # @param leclass LeClass
  38. # @param datas dict : dictionnary of field:value pairs to save
  39. # @return int : lodel_id of the created object
  40. def insert(self, letype, leclass, datas):
  41. query_table_name = leclass.name
  42. #building the query
  43. query = insert(query_table_name, datas)
  44. #executing the query
  45. with self.connection as cur:
  46. cur.execute(query)
  47. return True
  48. ## @brief delete an existing object
  49. # @param letype LeType
  50. # @param leclass LeClass
  51. # @param filters list : list of tuples formatted as (FIELD, OPERATOR, VALUE)
  52. # @param relational_filters list : list of tuples formatted as (('superior'|'subordinate', FIELD), OPERATOR, VALUE)
  53. # @return bool : True on success
  54. # @todo prendre en compte les rel_filters
  55. def delete(self, letype, leclass, filters, relational_filters):
  56. query_table_name = leclass.name
  57. query = delete(query_table_name, filters)
  58. with self.connection as cur:
  59. cur.execute(query)
  60. return True
  61. ## @brief search for a collection of objects
  62. # @param leclass LeClass
  63. # @param letype LeType
  64. # @field_list list
  65. # @param filters list : list of tuples formatted as (FIELD, OPERATOR, VALUE)
  66. # @param relational_filters list : list of tuples formatted as (('superior'|'subordinate', FIELD), OPERATOR, VALUE)
  67. # @return list
  68. def get(self, leclass, letype, field_list, filters, relational_filters=None):
  69. query_table_name = leclass.name
  70. where_filters = self._prepare_filters(filters)
  71. join_fields = {}
  72. if relational_filters is not None and len(relational_filters) > 0:
  73. for relational_filter in relational_filters:
  74. # Parsing the relation_filter
  75. relational_position = REL_SUB if relational_filter[0][0] == REL_SUP else REL_SUP
  76. relational_nature = relational_filter[0][1]
  77. relational_operator = relational_filter[1]
  78. relational_condition_key = (self.RELATIONS_POSITIONS_FIELDS[relational_filter[0][0]], relational_operator)
  79. relational_condition_value = relational_filter[2]
  80. # Definition of the join condition
  81. relation_table_join_field = "%s.%s" % (self.RELATIONS_TABLE_NAME, self.RELATIONS_POSITIONS_FIELDS[relational_position])
  82. query_table_join_field = "%s.lodel_id" % query_table_name
  83. join_fields[query_table_join_field] = relation_table_join_field
  84. # Adding "where" filters
  85. where_filters['%s.%s' % (self.RELATIONS_TABLE_NAME, self.RELATIONS_NATURE_FIELD)] = relational_nature
  86. where_filters[relational_condition_key] = relational_condition_value
  87. # Building the query
  88. query = select(query_table_name, where=where_filters, select=field_list, joins=join(self.RELATIONS_TABLE_NAME, join_fields))
  89. else:
  90. query = select(query_table_name, where=where_filters, select=field_list)
  91. # Executing the query
  92. with self.connection as cur:
  93. results = all_to_dicts(cur.execute(query))
  94. # Returning it as a list of dict
  95. return results
  96. ## @brief prepares the filters to be used by the mosql library's functions
  97. # @params filters : (FIELD, OPERATOR, VALUE) tuples
  98. # @return dict : Dictionnary with (FIELD, OPERATOR):VALUE style elements
  99. def _prepare_filters(self, filters):
  100. prepared_filters = {}
  101. for filter_item in filters:
  102. prepared_filter_key = (filter_item[0], filter_item[1])
  103. prepared_filter_value = filter_item[2]
  104. prepared_filters[prepared_filter_key] = prepared_filter_value
  105. return prepared_filters