Browse Source

First implementation of the abstract class select queries

Roland Haroutiounian 8 years ago
parent
commit
d32756695a
1 changed files with 56 additions and 49 deletions
  1. 56
    49
      plugins/mongodb_datasource/datasource.py

+ 56
- 49
plugins/mongodb_datasource/datasource.py View File

@@ -91,56 +91,63 @@ class MongoDbDatasource(object):
91 91
     #@return list
92 92
     #@todo Implement the relations
93 93
     def select(self, target, field_list, filters = None, rel_filters=None, order=None, group=None, limit=None, offset=0):
94
-        # Default arg init
95
-        filters = [] if filters is None else filters
96
-        rel_filters = [] if rel_filters is None else rel_filters
97
-
98
-        collection_name = object_collection_name(target)
99
-        collection = self.database[collection_name]
100
-
101
-        query_filters = self.__process_filters(
102
-            target, filters, rel_filters)
103
-        query_result_ordering = None
104
-        if order is not None:
105
-            query_result_ordering = utils.parse_query_order(order)
106
-        results_field_list = None if len(field_list) == 0 else field_list
107
-        limit = limit if limit is not None else 0
108
-
109
-        if group is None:
110
-            cursor = collection.find(
111
-                filter=query_filters, projection=results_field_list,
112
-                skip=offset, limit=limit, sort=query_result_ordering)
113
-        else:
114
-            pipeline = list()
115
-            unwinding_list = list()
116
-            grouping_dict = OrderedDict()
117
-            sorting_list = list()
118
-            for group_param in group:
119
-                field_name = group_param[0]
120
-                field_sort_option = group_param[1]
121
-                sort_option = MONGODB_SORT_OPERATORS_MAP[field_sort_option]
122
-                unwinding_list.append({'$unwind': '$%s' % field_name})
123
-                grouping_dict[field_name] = '$%s' % field_name
124
-                sorting_list.append((field_name, sort_option))
125
-
126
-            sorting_list.extends(query_result_ordering)
127
-
128
-            pipeline.append({'$match': query_filters})
129
-            if results_field_list is not None:
130
-                pipeline.append({
131
-                    '$project': SON([{field_name: 1}
132
-                    for field_name in field_list])})
133
-            pipeline.extend(unwinding_list)
134
-            pipeline.append({'$group': grouping_dict})
135
-            pipeline.extend({'$sort': SON(sorting_list)})
136
-            if offset > 0:
137
-                pipeline.append({'$skip': offset})
138
-            if limit is not None:
139
-                pipeline.append({'$limit': limit})
140
-
141 94
         results = list()
142
-        for document in cursor:
143
-            results.append(document)
95
+        if target.abstract:
96
+            target_childs = target.child_classes()
97
+            for target_child in target_childs:
98
+                results.append(self.select(target=target, field_list=field_list, filters=filters,
99
+                                           rel_filters=rel_filters, order=order, group=group, limit=limit,
100
+                                           offset=offset))
101
+        else:
102
+            # Default arg init
103
+            filters = [] if filters is None else filters
104
+            rel_filters = [] if rel_filters is None else rel_filters
105
+
106
+            collection_name = object_collection_name(target)
107
+            collection = self.database[collection_name]
108
+
109
+            query_filters = self.__process_filters(target, filters, rel_filters)
110
+            query_result_ordering = None
111
+            if order is not None:
112
+                query_result_ordering = utils.parse_query_order(order)
113
+            results_field_list = None if len(field_list) == 0 else field_list
114
+            limit = limit if limit is not None else 0
115
+
116
+            if group is None:
117
+                cursor = collection.find(
118
+                    filter=query_filters, projection=results_field_list,
119
+                    skip=offset, limit=limit, sort=query_result_ordering)
120
+            else:
121
+                pipeline = list()
122
+                unwinding_list = list()
123
+                grouping_dict = OrderedDict()
124
+                sorting_list = list()
125
+                for group_param in group:
126
+                    field_name = group_param[0]
127
+                    field_sort_option = group_param[1]
128
+                    sort_option = MONGODB_SORT_OPERATORS_MAP[field_sort_option]
129
+                    unwinding_list.append({'$unwind': '$%s' % field_name})
130
+                    grouping_dict[field_name] = '$%s' % field_name
131
+                    sorting_list.append((field_name, sort_option))
132
+
133
+                sorting_list.extends(query_result_ordering)
134
+
135
+                pipeline.append({'$match': query_filters})
136
+                if results_field_list is not None:
137
+                    pipeline.append({
138
+                        '$project': SON([{field_name: 1}
139
+                        for field_name in field_list])})
140
+                pipeline.extend(unwinding_list)
141
+                pipeline.append({'$group': grouping_dict})
142
+                pipeline.extend({'$sort': SON(sorting_list)})
143
+                if offset > 0:
144
+                    pipeline.append({'$skip': offset})
145
+                if limit is not None:
146
+                    pipeline.append({'$limit': limit})
147
+
148
+            #results = list()
149
+            for document in cursor:
150
+                results.append(document)
144 151
 
145 152
         return results
146 153
 

Loading…
Cancel
Save