|
@@ -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
|