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
     # @param filters list : List of filters (see @ref leobject_filters )
16
     # @param filters list : List of filters (see @ref leobject_filters )
17
     # @param rel_filters list : List of relationnal filters (see @ref leobject_filters )
17
     # @param rel_filters list : List of relationnal filters (see @ref leobject_filters )
18
     # @return a list of LeCrud child classes
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
         pass
20
         pass
21
 
21
 
22
     ## @brief delete lodel editorial components given filters
22
     ## @brief delete lodel editorial components given filters

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

10
 from leapi.lecrud import _LeCrud
10
 from leapi.lecrud import _LeCrud
11
 
11
 
12
 from mosql.db import Database, all_to_dicts, one_to_dict
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
 from mosql.util import raw, or_
14
 from mosql.util import raw, or_
15
 import mosql.mysql
15
 import mosql.mysql
16
 
16
 
35
 
35
 
36
     ## @brief select lodel editorial components using given filters
36
     ## @brief select lodel editorial components using given filters
37
     # @param target_cls LeCrud(class): The component class concerned by the select (a LeCrud child class (not instance !) )
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
     # @param filters list: List of filters (see @ref leobject_filters)
39
     # @param filters list: List of filters (see @ref leobject_filters)
39
     # @param rel_filters list: List of relational filters (see @ref leobject_filters)
40
     # @param rel_filters list: List of relational filters (see @ref leobject_filters)
40
     # @return a list of LeCrud child classes
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
         if rel_filters is not None and len(rel_filters) > 0:
57
         if rel_filters is not None and len(rel_filters) > 0:
51
             rel_filters = self._prepare_rel_filters(rel_filters)
58
             rel_filters = self._prepare_rel_filters(rel_filters)
59
                 where_filters[rel_filter['condition_key']] = rel_filter['condition_value']
66
                 where_filters[rel_filter['condition_key']] = rel_filter['condition_value']
60
 
67
 
61
             # building the query
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
         # Executing the query
77
         # Executing the query
67
         cur = self.datasource_utils.query(self.connection, query)
78
         cur = self.datasource_utils.query(self.connection, query)
68
         results = all_to_dicts(cur)
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
         return results
84
         return results
71
 
85
 
72
     ## @brief delete lodel editorial components given filters
86
     ## @brief delete lodel editorial components given filters
147
     # @return The inserted component's id
161
     # @return The inserted component's id
148
     # @todo should work with LeType, LeClass, and Relations
162
     # @todo should work with LeType, LeClass, and Relations
149
     def insert(self, target_cls, **datas):
163
     def insert(self, target_cls, **datas):
150
-        class_id = ''
151
-        type_id = ''
152
         # it is a LeType
164
         # it is a LeType
153
         if (hasattr(target_cls, '_leclass')):
165
         if (hasattr(target_cls, '_leclass')):
154
             # find main table and main table datas
166
             # find main table and main table datas

+ 4
- 4
leapi/lecrud.py View File

181
         #preparing filters
181
         #preparing filters
182
         filters, relational_filters = cls._prepare_filters(query_filters)
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
     ## @brief Insert a new component
189
     ## @brief Insert a new component
190
     # @param datas dict : The value of object we want to insert
190
     # @param datas dict : The value of object we want to insert
191
     # @return A new id if success else False
191
     # @return A new id if success else False

Loading…
Cancel
Save