Browse Source

[MongoDbDataSource] Implementation of the connection creation and a first check on the given parameters

- The check method checks, for now, if all the needed parameters are passed to the connection string
- A test class has been written with some bad connection args examples
Roland Haroutiounian 9 years ago
parent
commit
542fdf1ca7
1 changed files with 40 additions and 31 deletions
  1. 40
    31
      lodel/datasource/mongodb/datasource.py

+ 40
- 31
lodel/datasource/mongodb/datasource.py View File

@@ -14,42 +14,51 @@ DEFAULT_CONNECTION = {
14 14
     'dbname': 'lodel'
15 15
 }
16 16
 
17
+
18
+class MongoDbDataSourceError(Exception):
19
+    pass
20
+
21
+
17 22
 class MongoDbDataSource(object):
18 23
 
24
+    MANDATORY_CONNECTION_ARGS = ('host', 'post', 'login', 'password', 'dbname')
25
+
19 26
     def __init__(self, module=pymongo, connection_args=DEFAULT_CONNECTION):
20
-        connection_string = 'mongodb://%s:%s@%s:%s' % (connection_args['login'],
21
-                                                       urllib.quote_plus(connection_args['password']),
22
-                                                       connection_args['host'],
23
-                                                       connection_args['port'])
27
+        login, password, host, port, dbname = MongoDbDataSource._check_connection_args(connection_args)
28
+
29
+        # Creating of the connection
30
+        connection_string = 'mongodb://%s:%s@%s:%s' % (login, password, host, port)
24 31
         self.connection = MongoClient(connection_string)
25
-        self.database = self.connection[connection_args['dbname']]
26
-
27
-    ##@brief Inserts a list of records in a given collection
28
-    #
29
-    # @param collection_name str : name of the MongoDB collection in which we will insert the records
30
-    # @param datas list : list of dictionaries corresponding to the records
31
-    # @throw BulkWriteError : is thrown when an error occurs during the execution of the ordered bulk operations
32
-    def insert(self, collection_name, datas):
33
-        collection = self.database[collection_name]
34
-        bulk = collection.initialize_ordered_bulk_op()
35
-        # TODO check if all the elements of the list are dictionaries
36
-        bulk.insert_many(datas)
37
-        try:
38
-            result = bulk.execute()
39
-        except BulkWriteError as bwe:
40
-            pass  # TODO Add the behavior in case of an exception => bwe.details['writeErrors'], is a list of error info dicts
41
-
42
-        return result['nInserted']
43
-
44
-    def select(self):
32
+        # Getting the database
33
+        self.database = self.connection[dbname]
45 34
 
46
-        pass
47
-    
48
-    def update(self):
35
+    ## @brief checks if the connection args are valid and complete
36
+    # @param connection_args dict
37
+    # @return bool
38
+    # @todo checks on the argument types can be added here
39
+    @classmethod
40
+    def _check_connection_args(cls, connection_args):
41
+        errors = []
42
+        for connection_arg in cls.MANDATORY_CONNECTION_ARGS:
43
+            if connection_arg not in connection_args:
44
+                errors.append("Datasource connection error : %s parameter is missing." % connection_arg)
45
+        if len(errors) > 0 :
46
+            raise MongoDbDataSourceError("\r\n-".join(errors))
47
+
48
+        return (connection_args['login'], urllib.quote_plus(connection_args['password']), connection_args['host'],
49
+                connection_args['port'], connection_args['dbname'])
50
+
51
+    def select(self, target_cls, field_list, filters, rel_filters=None, order=None, group=None, limit=None, offset=0, instanciate=True):
49 52
         pass
50 53
 
51 54
     def delete(self, target_cls, uid):
52
-        uidname = target_cls.uidname
53
-        collection_name = target_cls.collection_name
54
-        result = self.database[collection_name].delete_many({uidname: uid})
55
-        return result.deteled_count
55
+        pass
56
+
57
+    def update(self, target_cls, uid, **datas):
58
+        pass
59
+
60
+    def insert(self, target_cls, **datas):
61
+        pass
62
+
63
+    def insert_multi(self, target_cls, datas_list):
64
+        pass

Loading…
Cancel
Save