Browse Source

SQL datasource: implement get() from LeObject

ArnAud 9 years ago
parent
commit
9d16c6a4c8
3 changed files with 30 additions and 18 deletions
  1. 1
    1
      leapi/datasources/dummy.py
  2. 25
    13
      leapi/datasources/ledatasourcesql.py
  3. 4
    4
      leapi/lecrud.py

+ 1
- 1
leapi/datasources/dummy.py View File

@@ -16,7 +16,7 @@ class DummyDatasource(object):
16 16
     # @param filters list : List of filters (see @ref leobject_filters )
17 17
     # @param rel_filters list : List of relationnal filters (see @ref leobject_filters )
18 18
     # @return a list of LeCrud child classes
19
-    def select(self, target_cls, filters, rel_filters):
19
+    def select(self, target_cls, field_list, filters, rel_filters):
20 20
         pass
21 21
 
22 22
     ## @brief delete lodel editorial components given filters

+ 25
- 13
leapi/datasources/ledatasourcesql.py View File

@@ -10,7 +10,7 @@ from leapi.leobject import REL_SUB, REL_SUP
10 10
 from leapi.lecrud import _LeCrud
11 11
 
12 12
 from mosql.db import Database, all_to_dicts, one_to_dict
13
-from mosql.query import select, insert, update, delete, join
13
+from mosql.query import select, insert, update, delete, join, left_join
14 14
 from mosql.util import raw, or_
15 15
 import mosql.mysql
16 16
 
@@ -35,17 +35,24 @@ class LeDataSourceSQL(DummyDatasource):
35 35
 
36 36
     ## @brief select lodel editorial components using given filters
37 37
     # @param target_cls LeCrud(class): The component class concerned by the select (a LeCrud child class (not instance !) )
38
+    # @param field_list list: List of field to fetch
38 39
     # @param filters list: List of filters (see @ref leobject_filters)
39 40
     # @param rel_filters list: List of relational filters (see @ref leobject_filters)
40 41
     # @return a list of LeCrud child classes
41
-    def select(self, target_cls, filters, rel_filters=None):
42
-        if target_cls is None:
43
-            query_table_name = self.datasource_utils.objects_table_name
44
-        else:
45
-            query_table_name = self.datasource_utils.get_table_name_from_class(target_cls.__name__)
42
+    # @todo this only works with LeObject.get()
43
+    # @todo for speed get rid of all_to_dicts
44
+    def select(self, target_cls, field_list, filters, rel_filters=None):
46 45
 
47
-        where_filters = self._prepare_filters(filters, query_table_name)
48
-        join_fields = {}
46
+        joins = []
47
+        # it is a LeObject, query only on main table
48
+        if target_cls.__name__ == 'LeObject':
49
+            main_table = self.datasource_utils.objects_table_name
50
+        # it is a LeType, query on main and class table
51
+        elif (hasattr(target_cls, '_leclass')):
52
+            # find main table and main table datas
53
+            main_table = self.datasource_utils.objects_table_name
54
+            class_table = self.datasource_utils.get_table_name_from_class(target_cls._leclass.__name__)
55
+            joins = [left_join(class_table, self.datasource_utils.field_lodel_id)]
49 56
 
50 57
         if rel_filters is not None and len(rel_filters) > 0:
51 58
             rel_filters = self._prepare_rel_filters(rel_filters)
@@ -59,14 +66,21 @@ class LeDataSourceSQL(DummyDatasource):
59 66
                 where_filters[rel_filter['condition_key']] = rel_filter['condition_value']
60 67
 
61 68
             # building the query
62
-            query = select(query_table_name, where=where_filters, joins=join(self.datasource_utils.relations_table_name, join_fields))
63
-        else:
64
-            query = select(query_table_name, where=where_filters)
69
+            #query = select(query_table_name, where=where_filters, joins=join(self.datasource_utils.relations_table_name, join_fields))
70
+        #else:
71
+            #query = select(query_table_name, where=where_filters)
72
+
73
+        wheres = {(name, op):value for name,op,value in filters}
74
+        query = select(main_table, select=field_list, where=wheres, joins=joins)
75
+        #print ('SQL', query)
65 76
 
66 77
         # Executing the query
67 78
         cur = self.datasource_utils.query(self.connection, query)
68 79
         results = all_to_dicts(cur)
69 80
 
81
+        results = [target_cls.uid2leobj(datas['type_id'])(**datas) for datas in results]
82
+        #print('results', results)
83
+
70 84
         return results
71 85
 
72 86
     ## @brief delete lodel editorial components given filters
@@ -147,8 +161,6 @@ class LeDataSourceSQL(DummyDatasource):
147 161
     # @return The inserted component's id
148 162
     # @todo should work with LeType, LeClass, and Relations
149 163
     def insert(self, target_cls, **datas):
150
-        class_id = ''
151
-        type_id = ''
152 164
         # it is a LeType
153 165
         if (hasattr(target_cls, '_leclass')):
154 166
             # find main table and main table datas

+ 4
- 4
leapi/lecrud.py View File

@@ -181,11 +181,11 @@ class _LeCrud(object):
181 181
         #preparing filters
182 182
         filters, relational_filters = cls._prepare_filters(query_filters)
183 183
 
184
-        #Fetching datas from datasource
185
-        db_datas = cls._datasource.select(cls, field_list, filters, relational_filters)
184
+        #Fetching editorial components from datasource
185
+        results = cls._datasource.select(cls, field_list, filters, relational_filters)
186
+
187
+        return results
186 188
 
187
-        return [ cls(**datas) for datas in db_datas]
188
-    
189 189
     ## @brief Insert a new component
190 190
     # @param datas dict : The value of object we want to insert
191 191
     # @return A new id if success else False

Loading…
Cancel
Save