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.

datasource.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. import pymongo
  3. from pymongo import MongoClient
  4. from pymongo.errors import BulkWriteError
  5. import urllib
  6. import lodel.datasource.mongodb.utils as utils
  7. class MongoDbDataSourceError(Exception):
  8. pass
  9. class MongoDbDataSource(object):
  10. MANDATORY_CONNECTION_ARGS = ('host', 'port', 'login', 'password', 'dbname')
  11. ## @brief Instanciates a Database object given a connection name
  12. # @param connection_name str
  13. def __init__(self, connection_name='default'):
  14. connection_args = self._get_connection_args(connection_name)
  15. login, password, host, port, dbname = MongoDbDataSource._check_connection_args(connection_args)
  16. # Creating of the connection
  17. connection_string = 'mongodb://%s:%s@%s:%s' % (login, password, host, port)
  18. self.connection = MongoClient(connection_string)
  19. # Getting the database
  20. self.database = self.connection[dbname]
  21. ## @brief Gets the settings given a connection name
  22. # @param connection_name str
  23. # @return dict
  24. # @TODO Change the return value using the Lodel 2 settings module
  25. def _get_connection_args(self, connection_name):
  26. return {
  27. 'host': 'localhost',
  28. 'port': 27017,
  29. 'login': 'login', # TODO modifier la valeur
  30. 'password': 'password', # TODO modifier la valeur
  31. 'dbname': 'lodel'
  32. }
  33. ## @brief checks if the connection args are valid and complete
  34. # @param connection_args dict
  35. # @return bool
  36. # @todo checks on the argument types can be added here
  37. @classmethod
  38. def _check_connection_args(cls, connection_args):
  39. errors = []
  40. for connection_arg in cls.MANDATORY_CONNECTION_ARGS:
  41. if connection_arg not in connection_args:
  42. errors.append("Datasource connection error : %s parameter is missing." % connection_arg)
  43. if len(errors) > 0 :
  44. raise MongoDbDataSourceError("\r\n-".join(errors))
  45. return (connection_args['login'], urllib.quote_plus(connection_args['password']), connection_args['host'],
  46. connection_args['port'], connection_args['dbname'])
  47. ## @brief returns a selection of documents from the datasource
  48. # @param target_cls Emclass
  49. # @param field_list list
  50. def select(self, target_cls, field_list, filters, rel_filters=None, order=None, group=None, limit=None, offset=0,
  51. instanciate=True):
  52. collection_name = utils.object_collection_name(target_cls.__class__)
  53. collection = self.database[collection_name]
  54. query_filters = utils.parse_query_filters(filters)
  55. query_result_ordering = utils.parse_query_order(order) if order is not None else None
  56. results_field_list = None if len(field_list) == 0 else field_list # TODO On peut peut-être utiliser None dans les arguments au lieu d'une liste vide
  57. limit = limit if limit is not None else 0
  58. cursor = collection.find(
  59. filter=query_filters,
  60. projection=results_field_list,
  61. skip=offset,
  62. limit=limit,
  63. sort=query_result_ordering
  64. )
  65. results = list()
  66. for document in cursor:
  67. results.append(document)
  68. return results
  69. ## @brief Deletes one record defined by its uid
  70. # @param target_cls Emclass : class of the record to delete
  71. # @param uid dict|list : a dictionary of fields and values composing the unique identifier of the record or a list of several dictionaries
  72. # @return int : number of deleted records
  73. # @TODO check the content of the result.raw_result property depending on the informations to return
  74. # @TODO Implement the error management
  75. def delete(self, target_cls, uid):
  76. if isinstance(uid, dict):
  77. uid = [uid]
  78. collection_name = utils.object_collection_name(target_cls.__class__)
  79. collection = self.database[collection_name]
  80. result = collection.delete_many(uid)
  81. return result.deleted_count
  82. def update(self, target_cls, uid, **datas):
  83. pass
  84. ## @brief Inserts a record in a given collection
  85. # @param target_cls Emclass : class of the object to insert
  86. # @param datas dict : datas to insert
  87. # @return bool
  88. # @TODO Implement the error management
  89. def insert(self, target_cls, **datas):
  90. collection_name = utils.object_collection_name(target_cls.__class__)
  91. collection = self.database[collection_name]
  92. result = collection.insert_one(datas)
  93. return len(result.inserted_id)
  94. ## @brief Inserts a list of records in a given collection
  95. # @param target_cls Emclass : class of the objects inserted
  96. # @param datas_list
  97. # @return list : list of the inserted records' ids
  98. # @TODO Implement the error management
  99. def insert_multi(self, target_cls, datas_list):
  100. collection_name = utils.object_collection_name(target_cls.__class__)
  101. collection = self.database[collection_name]
  102. result = collection.insert_many(datas_list)
  103. return len(result.inserted_ids)