|
@@ -3,71 +3,87 @@
|
3
|
3
|
from leobject.datasources.dummy import DummyDatasource
|
4
|
4
|
from mosql.db import Database, all_to_dicts
|
5
|
5
|
from mosql.query import select
|
|
6
|
+from leobject import REL_SUB, REL_SUP
|
6
|
7
|
|
7
|
8
|
from Lodel.utils.mosql import *
|
8
|
9
|
|
9
|
10
|
## SQL DataSource for LeObject
|
10
|
11
|
class LeDataSourceSQL(DummyDatasource):
|
11
|
12
|
|
|
13
|
+ RELATIONS_TABLE_NAME = 'relations'
|
|
14
|
+ RELATIONS_POSITIONS_FIELDS = {REL_SUP:'superior_id', REL_SUB:'subordinate_id'}
|
|
15
|
+
|
12
|
16
|
def __init__(self, module=None, *conn_args, **conn_kargs):
|
13
|
17
|
super(LeDataSourceSQL, self).__init__()
|
14
|
18
|
self.db = Database(self.module, self.conn_args, self.conn_kargs)
|
15
|
19
|
|
16
|
20
|
## @brief update an existing object's data
|
|
21
|
+ # @param letype LeType
|
17
|
22
|
# @param leclass LeClass
|
18
|
23
|
# @param filters list : list of tuples formatted as (FIELD, OPERATOR, VALUE)
|
|
24
|
+ # @param rel_filters list : list of tuples formatted as (('superior'|'subordinate', FIELD), OPERATOR, VALUE)
|
19
|
25
|
# @param data dict
|
20
|
|
- # @param relational_filters list : list of tuples formatted as (('superior'|'subordinate', FIELD), OPERATOR, VALUE)
|
21
|
|
- # @param letype LeType
|
22
|
26
|
# @return bool
|
23
|
|
- def update (self, leclass, filters, data, relational_filters=None, letype=None):
|
|
27
|
+ def update(self, letype, leclass, filters, rel_filters, data):
|
24
|
28
|
pass
|
25
|
29
|
|
26
|
30
|
## @brief create a new object in the datasource
|
27
|
|
- # @param leclass LeClass
|
28
|
31
|
# @param letype LeType
|
|
32
|
+ # @param leclass LeClass
|
29
|
33
|
# @param datas dict : dictionnary of field:value pairs to save
|
30
|
34
|
# @return int : lodel_id of the created object
|
31
|
|
- def insert(self, leclass, letype=None, **datas):
|
|
35
|
+ def insert(self, letype, leclass, **datas):
|
32
|
36
|
pass
|
33
|
37
|
|
34
|
38
|
## @brief delete an existing object
|
|
39
|
+ # @param letype LeType
|
35
|
40
|
# @param leclass LeClass
|
36
|
41
|
# @param filters list : list of tuples formatted as (FIELD, OPERATOR, VALUE)
|
37
|
42
|
# @param relational_filters list : list of tuples formatted as (('superior'|'subordinate', FIELD), OPERATOR, VALUE)
|
38
|
|
- # @param letype LeType
|
39
|
43
|
# @return bool : True on success
|
40
|
|
- def delete(self, leclass, filters, relational_filters=None, letype=None):
|
|
44
|
+ def delete(self, letype, leclass, filters, relational_filters):
|
41
|
45
|
pass
|
42
|
46
|
|
43
|
47
|
## @brief search for a collection of objects
|
44
|
|
- # @param emclass LeClass
|
45
|
|
- # @param emtype LeType
|
|
48
|
+ # @param leclass LeClass
|
|
49
|
+ # @param letype LeType
|
46
|
50
|
# @field_list list
|
47
|
51
|
# @param filters list : list of tuples formatted as (FIELD, OPERATOR, VALUE)
|
48
|
52
|
# @param relational_filters list : list of tuples formatted as (('superior'|'subordinate', FIELD), OPERATOR, VALUE)
|
49
|
|
- def get(self, emclass, emtype, field_list, filters, relational_filters=None):
|
|
53
|
+ # @return list
|
|
54
|
+ def get(self, leclass, letype, field_list, filters, relational_filters=None):
|
50
|
55
|
|
51
|
|
- tablename = emclass.name
|
|
56
|
+ query_table_name = leclass.name
|
52
|
57
|
where_filters = self._prepare_filters(filters)
|
|
58
|
+ join_fields = {}
|
|
59
|
+
|
53
|
60
|
if relational_filters or len(relational_filters) > 0:
|
54
|
61
|
for relational_filter in relational_filters:
|
|
62
|
+ # Parsing the relation_filter
|
55
|
63
|
relational_position = relational_filter[0][0]
|
56
|
64
|
relational_field = relational_filter[0][1]
|
57
|
65
|
relational_operator = relational_filter[1]
|
58
|
66
|
relational_value = relational_filter[2]
|
|
67
|
+ relational_where_filters_key = (relational_field, relational_operator)
|
|
68
|
+ relational_where_filters_value = relational_value
|
59
|
69
|
|
|
70
|
+ # Definition of the join condition
|
|
71
|
+ relation_table_join_field = "%s.%s" % (self.RELATIONS_TABLE_NAME, self.RELATIONS_POSITIONS_FIELDS[relational_position])
|
|
72
|
+ query_table_join_field = "%s.lodel_id" % query_table_name
|
|
73
|
+ join_fields[query_table_join_field] = relation_table_join_field
|
60
|
74
|
|
|
75
|
+ # Adding "where" filters
|
|
76
|
+ where_filters[relational_where_filters_key] = relational_where_filters_value
|
61
|
77
|
|
62
|
|
- tablename = emclass.name
|
63
|
|
- where_filters = self._prepare_filters(filters)
|
64
|
|
- if relational_filters or len(relational_filters) > 0:
|
65
|
|
- rel_filters = self._prepare_filters(relational_filters)
|
66
|
|
- query = select(tablename, where=where_filters, select=field_list, joins=join('relations', {}))
|
|
78
|
+ # Building the query
|
|
79
|
+ query = select(query_table_name, where=where_filters, select=field_list, joins = join(self.RELATIONS_TABLE_NAME, join_fields))
|
67
|
80
|
else:
|
68
|
|
- query = select(tablename, where=where_filters, select=field_list)
|
|
81
|
+ query = select(query_table_name, where=where_filters, select=field_list)
|
|
82
|
+
|
|
83
|
+ # Executing the query
|
69
|
84
|
self.db.execute(query)
|
70
|
85
|
|
|
86
|
+ # Returning it as a list of dict
|
71
|
87
|
return all_to_dicts(self.db)
|
72
|
88
|
|
73
|
89
|
# @brief prepares the filters to be used by the mosql library's functions
|