|
@@ -7,92 +7,121 @@ from .leobject import LeObject, LeApiErrors, LeApiDataCheckError
|
7
|
7
|
class LeQueryError(Exception):
|
8
|
8
|
pass
|
9
|
9
|
|
10
|
|
-
|
11
|
10
|
class LeQuery(object):
|
12
|
11
|
|
13
|
12
|
## @brief The datasource object used for this query
|
14
|
|
- _datasource = None
|
|
13
|
+ datasource = None
|
15
|
14
|
|
16
|
15
|
## @brief The available operators used in query definitions
|
17
|
|
- _query_operators = ['=', '<=', '>=', '!=', '<', '>', ' in ', ' not in ', ' like ', ' not like ']
|
|
16
|
+ query_operators = ['=', '<=', '>=', '!=', '<', '>', ' in ', ' not in ', ' like ', ' not like ']
|
18
|
17
|
|
|
18
|
+ ## @brief Constructor
|
|
19
|
+ # @param target_class EmClass : class of the object to query about
|
19
|
20
|
def __init__(self, target_class):
|
20
|
21
|
if not issubclass(target_class, LeObject):
|
21
|
22
|
raise TypeError("target class has to be a child class of LeObject")
|
22
|
|
- self._target_class = target_class
|
|
23
|
+ self.target_class = target_class
|
23
|
24
|
|
24
|
25
|
|
|
26
|
+## @brief Class representing an Insert query
|
25
|
27
|
class LeInsertQuery(LeQuery):
|
26
|
|
- action = 'insert'
|
27
|
|
-
|
28
|
|
- def __init__(self, target_class, datas, classname=None):
|
29
|
|
- targeted_class = target_class if classname is None else LeObject.name2class(classname)
|
30
|
|
- if not targeted_class:
|
31
|
|
- raise LeQueryError('Error when inserting', {'error': ValueError("The class '%s' was not found" % classname)})
|
32
|
28
|
|
33
|
|
- super().__init__(targeted_class)
|
|
29
|
+ ## @brief Constructor
|
|
30
|
+ # @param target_class EmClass: class corresponding to the inserted object
|
|
31
|
+ # @param datas dict : datas to insert
|
|
32
|
+ def __init__(self, target_class, datas):
|
|
33
|
+ super().__init__(target_class)
|
34
|
34
|
self.datas = datas
|
35
|
35
|
|
36
|
|
- # @todo Reactivate the LodelHooks call when this class is implemented
|
|
36
|
+ ## @brief executes the insert query
|
|
37
|
+ # @return bool
|
|
38
|
+ # @TODO reactivate the LodelHooks call when this class is implemented
|
37
|
39
|
def execute(self):
|
38
|
|
- datas = self.datas # TODO : replace with LodelHooks.call_hook('leapi_insert_pre', self._target_class, self.datas)
|
|
40
|
+ datas = self.datas # LodelHooks.call_hook('leapi_insert_pre', self.target_class, self.datas)
|
39
|
41
|
ret = self.__insert(**datas)
|
40
|
|
- # ret = LodelHook.call_hook('leapi_insert_post', self._target_class, ret)
|
|
42
|
+ # ret = LodelHook.call_hook('leapi_insert_post', self.target_class, ret)
|
41
|
43
|
return ret
|
42
|
44
|
|
43
|
|
- def __insert(self):
|
44
|
|
- insert_datas = self._target_class.prepare_datas(self.datas, complete=True, allow_internal=True)
|
45
|
|
- return self._datasource.insert(self._target_class, **insert_datas)
|
|
45
|
+ ## @brief calls the datasource to perform the insert command
|
|
46
|
+ # @param datas dict : formatted datas corresponding to the insert
|
|
47
|
+ # @return str : the uid of the inserted object
|
|
48
|
+ def __insert(self, **datas):
|
|
49
|
+ insert_datas = self.target_class.prepare_datas(datas, complete=True, allow_internal=True)
|
|
50
|
+ res = self.datasource.insert(self.target_class, **insert_datas)
|
|
51
|
+ return res
|
46
|
52
|
|
47
|
53
|
|
|
54
|
+## @brief Class representing an Abstract Filtered Query
|
48
|
55
|
class LeFilteredQuery(LeQuery):
|
49
|
56
|
|
|
57
|
+ ## @brief Constructor
|
|
58
|
+ # @param target_class EmClass : Object of the query
|
50
|
59
|
def __init__(self, target_class):
|
51
|
60
|
super().__init__(target_class)
|
52
|
61
|
|
|
62
|
+ ## @brief Validates the query filters
|
|
63
|
+ # @param query_filters list
|
|
64
|
+ # @return bool
|
|
65
|
+ # @raise LeQueryError if one of the filter is not valid
|
53
|
66
|
@classmethod
|
54
|
67
|
def validate_query_filters(cls, query_filters):
|
55
|
68
|
for query_filter in query_filters:
|
56
|
|
- if query_filter[1] not in cls._query_operators:
|
|
69
|
+ if query_filter[1] not in cls.query_operators:
|
57
|
70
|
raise LeQueryError("The operator %s is not valid." % query_filter[1])
|
58
|
71
|
return True
|
59
|
72
|
|
|
73
|
+ ## @brief Checks if a field is relational
|
|
74
|
+ # @param field str : Name of the field
|
|
75
|
+ # @return bool
|
60
|
76
|
@classmethod
|
61
|
77
|
def is_relational_field(cls, field):
|
62
|
78
|
return field.startswith('superior.') or field.startswith('subordinate.')
|
63
|
79
|
|
|
80
|
+
|
|
81
|
+## @brief Class representing a Get Query
|
64
|
82
|
class LeGetQuery(LeFilteredQuery):
|
65
|
83
|
|
66
|
|
- def __init__(self, target_class, target_uid, query_filters, field_list=None, order=None, group=None, limit=None, offset=0, instanciate=True):
|
|
84
|
+ ## @brief Constructor
|
|
85
|
+ # @param target_class EmClass : main class
|
|
86
|
+ # @param query_filters
|
|
87
|
+ # @param field_list list
|
|
88
|
+ # @param order list : list of tuples corresponding to the fields used to sort the results
|
|
89
|
+ # @param group list : list of tuples corresponding to the fields used to group the results
|
|
90
|
+ # @param limit int : Maximum number of results to get
|
|
91
|
+ # @param offset int
|
|
92
|
+ # @param instanciate bool : if True, objects will be returned instead of dictionaries
|
|
93
|
+ def __init__(self, target_class, query_filters, field_list=None, order=None, group=None, limit=None, offset=0, instanciate=True):
|
67
|
94
|
super().__init__(target_class)
|
68
|
95
|
self.query_filters = query_filters
|
69
|
96
|
self.default_field_list = []
|
70
|
|
- self.field_list = field_list if field_list is not None else self._target_class.fieldnames()
|
|
97
|
+ self.field_list = field_list if field_list is not None else self.target_class.fieldnames()
|
71
|
98
|
self.order = order
|
72
|
99
|
self.group = group
|
73
|
100
|
self.limit = limit
|
74
|
101
|
self.offset = offset
|
75
|
102
|
self.instanciate = instanciate
|
76
|
|
- self.target_object = None # TODO get an instance of the target_class using a unique id
|
77
|
103
|
|
|
104
|
+ ## @brief executes the query
|
|
105
|
+ # @return list
|
|
106
|
+ # @TODO activate LodelHook calls
|
78
|
107
|
def execute(self):
|
79
|
|
- datas = self.datas # TODO : replace with LodelHook.call_hook('leapi_get_pre', self.target_object, self.datas)
|
|
108
|
+ datas = self.datas # LodelHook.call_hook('leapi_get_pre', self.target_object, self.datas)
|
80
|
109
|
ret = self.__get(**datas)
|
81
|
110
|
# ret = LodelHook.call_hook('leapi_get_post', self.target_object, ret)
|
82
|
111
|
return ret
|
83
|
112
|
|
84
|
|
- def __get(self, **kwargs):
|
85
|
|
- field_list = self.__prepare_field_list(self.field_list) #TODO implement the prepare_field_list method
|
|
113
|
+ def __get(self, **datas):
|
|
114
|
+ field_list = self.__prepare_field_list(self.field_list)
|
86
|
115
|
|
87
|
116
|
query_filters, relational_filters = self.__prepare_filters()
|
88
|
117
|
|
89
|
|
- # Preparing order
|
|
118
|
+ # Preparing the "order" parameters
|
90
|
119
|
if self.order:
|
91
|
120
|
order = self.__prepare_order()
|
92
|
121
|
if isinstance(order, Exception):
|
93
|
|
- raise order # can be buffered and raised later, but _prepare_filters raise when fails
|
|
122
|
+ raise order # can be buffered and raised later, but __prepare_filters raise when fails
|
94
|
123
|
|
95
|
|
- # Preparing group
|
|
124
|
+ # Preparing the "group" parameters
|
96
|
125
|
if self.group:
|
97
|
126
|
group = self.__prepare_order()
|
98
|
127
|
if isinstance(group, Exception):
|
|
@@ -108,12 +137,15 @@ class LeGetQuery(LeFilteredQuery):
|
108
|
137
|
results = self._datasource.select() # TODO add the correct arguments for the datasource's method call
|
109
|
138
|
return results
|
110
|
139
|
|
|
140
|
+ ## @brief prepares the field list
|
|
141
|
+ # @return list
|
|
142
|
+ # @raise LeApiDataCheckError
|
111
|
143
|
def __prepare_field_list(self):
|
112
|
144
|
errors = dict()
|
113
|
145
|
ret_field_list = list()
|
114
|
146
|
for field in self.field_list:
|
115
|
147
|
if self.is_relational(field):
|
116
|
|
- ret = self.__prepare_relational_fields(field)
|
|
148
|
+ ret = self.__prepare_relational_field(field)
|
117
|
149
|
else:
|
118
|
150
|
ret = self.__check_field(field)
|
119
|
151
|
|
|
@@ -127,10 +159,15 @@ class LeGetQuery(LeFilteredQuery):
|
127
|
159
|
|
128
|
160
|
return ret_field_list
|
129
|
161
|
|
130
|
|
- def __prepare_relational_fields(self, field):
|
|
162
|
+ ## @brief prepares a relational field
|
|
163
|
+ def __prepare_relational_field(self, field):
|
131
|
164
|
# TODO Implement the method
|
132
|
165
|
return field
|
133
|
166
|
|
|
167
|
+ ## @brief splits the filter string into a tuple (FIELD, OPERATOR, VALUE)
|
|
168
|
+ # @param filter str
|
|
169
|
+ # @return tuple
|
|
170
|
+ # @raise ValueError
|
134
|
171
|
def __split_filter(self, filter):
|
135
|
172
|
if self.query_re is None:
|
136
|
173
|
self.__compile_query_re()
|
|
@@ -153,11 +190,17 @@ class LeGetQuery(LeFilteredQuery):
|
153
|
190
|
op_re_piece += ')'
|
154
|
191
|
self.query_re = re.compile('^\s*(?P<field>(((superior)|(subordinate))\.)?[a-z_][a-z0-9\-_]*)\s*'+op_re_piece+'\s*(?P<value>[^<>=!].*)\s*$', flags=re.IGNORECASE)
|
155
|
192
|
|
156
|
|
- def __check_field(self, target_object, field):
|
157
|
|
- if field not in self.target_object.fieldnames():
|
158
|
|
- return ValueError("No such field '%s' in %s" % (field, self.target_object.__class__))
|
|
193
|
+ ## @brief checks if a field is in the target class of the query
|
|
194
|
+ # @param field str
|
|
195
|
+ # @return str
|
|
196
|
+ # @raise ValueError
|
|
197
|
+ def __check_field(self, field):
|
|
198
|
+ if field not in self.target_class.fieldnames():
|
|
199
|
+ return ValueError("No such field '%s' in %s" % (field, self.target_class))
|
159
|
200
|
return field
|
160
|
201
|
|
|
202
|
+ ## @brief Prepares the filters (relational and others)
|
|
203
|
+ # @return tuple
|
161
|
204
|
def __prepare_filters(self):
|
162
|
205
|
filters = list()
|
163
|
206
|
errors = dict()
|
|
@@ -173,7 +216,7 @@ class LeGetQuery(LeFilteredQuery):
|
173
|
216
|
|
174
|
217
|
for field, operator, value in filters:
|
175
|
218
|
# TODO check the relation filters
|
176
|
|
- ret = self.__check_field(self.target_object, field)
|
|
219
|
+ ret = self.__check_field(field)
|
177
|
220
|
if isinstance(ret, Exception):
|
178
|
221
|
errors[field] = ret
|
179
|
222
|
else:
|
|
@@ -188,9 +231,11 @@ class LeGetQuery(LeFilteredQuery):
|
188
|
231
|
datas = dict()
|
189
|
232
|
if LeFilteredQuery.validate_query_filters(self.query_filters):
|
190
|
233
|
datas['query_filters'] = self.query_filters
|
191
|
|
- datas['target_object'] = self.target_object
|
|
234
|
+ datas['target_class'] = self.target_class
|
192
|
235
|
return datas
|
193
|
236
|
|
|
237
|
+ ## @brief prepares the "order" parameters
|
|
238
|
+ # @return list
|
194
|
239
|
def __prepare_order(self):
|
195
|
240
|
errors = dict()
|
196
|
241
|
result = []
|
|
@@ -200,14 +245,14 @@ class LeGetQuery(LeFilteredQuery):
|
200
|
245
|
if len(order_field) != 2 or order_field[1].upper() not in ['ASC', 'DESC']:
|
201
|
246
|
errors[order_field] = ValueError("Expected a string or a tuple with (FIELDNAME, ['ASC'|'DESC']) but got : %s" % order_field)
|
202
|
247
|
else:
|
203
|
|
- ret = self._target_class.check_field(order_field[0])
|
|
248
|
+ ret = self.target_class.check_field(order_field[0])
|
204
|
249
|
if isinstance(ret, Exception):
|
205
|
250
|
errors[order_field] = ret
|
206
|
251
|
order_field = (order_field[0], order_field[1].upper())
|
207
|
252
|
result.append(order_field)
|
208
|
253
|
|
209
|
254
|
if len(errors) > 0:
|
210
|
|
- return LeApiErrors("Errors when preparing ordering fields", errors)
|
|
255
|
+ raise LeApiErrors("Errors when preparing ordering fields", errors)
|
211
|
256
|
return result
|
212
|
257
|
|
213
|
258
|
|
|
@@ -217,7 +262,6 @@ class LeUpdateQuery(LeFilteredQuery):
|
217
|
262
|
super().__init__(target_class)
|
218
|
263
|
self.query_filters = query_filters
|
219
|
264
|
self.target_uid = target_uid
|
220
|
|
- self.target_object = None # TODO get an instance of the target_class using a unique id
|
221
|
265
|
|
222
|
266
|
def execute(self):
|
223
|
267
|
# LodelHook.call_hook('leapi_update_pre', self.target_object, None)
|
|
@@ -230,7 +274,7 @@ class LeUpdateQuery(LeFilteredQuery):
|
230
|
274
|
# @TODO change the behavior in case of error in the update process
|
231
|
275
|
def __update(self):
|
232
|
276
|
updated_datas = self.__prepare()
|
233
|
|
- ret = self._datasource.update(self.target_uid, **updated_datas) # TODO add the correct arguments for the datasource's method call
|
|
277
|
+ ret = self.datasource.update(self.target_uid, **updated_datas) # TODO add the correct arguments for the datasource's method
|
234
|
278
|
if ret == 1:
|
235
|
279
|
return True
|
236
|
280
|
else:
|
|
@@ -276,4 +320,4 @@ class LeDeleteQuery(LeFilteredQuery):
|
276
|
320
|
datas['target_uid'] = self.target_uid
|
277
|
321
|
datas['target_class'] = self._target_class
|
278
|
322
|
|
279
|
|
- return datas
|
|
323
|
+ return datas
|