|
@@ -1,26 +1,23 @@
|
1
|
1
|
#-*- coding: utf-8 -*-
|
2
|
2
|
|
3
|
|
-import sqlite3
|
4
|
3
|
from leobject.datasources.dummy import DummyDatasource
|
5
|
4
|
from mosql.db import Database, all_to_dicts
|
6
|
|
-from mosql.query import select, insert, update, delete
|
7
|
|
-from leobject import REL_SUB, REL_SUP
|
8
|
|
-
|
9
|
|
-from Lodel.utils.mosql import *
|
|
5
|
+from mosql.query import select, insert, update, delete, join
|
|
6
|
+from leobject.leobject import REL_SUB, REL_SUP
|
10
|
7
|
|
11
|
8
|
## SQL DataSource for LeObject
|
12
|
9
|
class LeDataSourceSQL(DummyDatasource):
|
13
|
10
|
|
14
|
11
|
RELATIONS_TABLE_NAME = 'relations'
|
15
|
|
- RELATIONS_POSITIONS_FIELDS = {REL_SUP:'superior_id', REL_SUB:'subordinate_id'}
|
|
12
|
+ RELATIONS_POSITIONS_FIELDS = {REL_SUP: 'superior_id', REL_SUB: 'subordinate_id'}
|
16
|
13
|
RELATIONS_NATURE_FIELD = 'nature'
|
17
|
14
|
|
18
|
15
|
MODULE = 'sqlite3'
|
19
|
16
|
|
20
|
17
|
def __init__(self, module=None, *conn_args, **conn_kargs):
|
21
|
|
- self.module = self.MODULE if module == None else module
|
|
18
|
+ self.module = self.MODULE if module is None else module
|
22
|
19
|
super(LeDataSourceSQL, self).__init__()
|
23
|
|
- self.db = Database(self.module, self.conn_args, self.conn_kargs)
|
|
20
|
+ self.connection = Database(self.module, self.conn_args, self.conn_kargs)
|
24
|
21
|
|
25
|
22
|
## @brief update an existing object's data
|
26
|
23
|
# @param letype LeType
|
|
@@ -39,7 +36,7 @@ class LeDataSourceSQL(DummyDatasource):
|
39
|
36
|
# Building the query
|
40
|
37
|
query = update(table=query_table_name, where=where_filters, set=set_data)
|
41
|
38
|
# Executing the query
|
42
|
|
- with self.db as cur:
|
|
39
|
+ with self.connection as cur:
|
43
|
40
|
cur.execute(query)
|
44
|
41
|
return True
|
45
|
42
|
|
|
@@ -53,7 +50,7 @@ class LeDataSourceSQL(DummyDatasource):
|
53
|
50
|
#building the query
|
54
|
51
|
query = insert(query_table_name, datas)
|
55
|
52
|
#executing the query
|
56
|
|
- with self.db as cur:
|
|
53
|
+ with self.connection as cur:
|
57
|
54
|
cur.execute(query)
|
58
|
55
|
return True
|
59
|
56
|
|
|
@@ -67,7 +64,7 @@ class LeDataSourceSQL(DummyDatasource):
|
67
|
64
|
def delete(self, letype, leclass, filters, relational_filters):
|
68
|
65
|
query_table_name = leclass.name
|
69
|
66
|
query = delete(query_table_name, filters)
|
70
|
|
- with self.db as cur:
|
|
67
|
+ with self.connection as cur:
|
71
|
68
|
cur.execute(query)
|
72
|
69
|
return True
|
73
|
70
|
|
|
@@ -99,11 +96,8 @@ class LeDataSourceSQL(DummyDatasource):
|
99
|
96
|
query_table_join_field = "%s.lodel_id" % query_table_name
|
100
|
97
|
join_fields[query_table_join_field] = relation_table_join_field
|
101
|
98
|
|
102
|
|
- # Adding to "where" filters
|
103
|
|
- where_filters['%s.%s' % (self.RELATIONS_TABLE_NAME,self.RELATIONS_NATURE_FIELD_NAME ]
|
104
|
|
-
|
105
|
99
|
# Adding "where" filters
|
106
|
|
- where_filters['%s.%s' % (self.RELATIONS_TABLE_NAME, self.RELATIONS_NATURE_FIELD)] = relational_field
|
|
100
|
+ where_filters['%s.%s' % (self.RELATIONS_TABLE_NAME, self.RELATIONS_NATURE_FIELD)] = relational_nature
|
107
|
101
|
where_filters[relational_condition_key] = relational_condition_value
|
108
|
102
|
|
109
|
103
|
# Building the query
|
|
@@ -112,7 +106,7 @@ class LeDataSourceSQL(DummyDatasource):
|
112
|
106
|
query = select(query_table_name, where=where_filters, select=field_list)
|
113
|
107
|
|
114
|
108
|
# Executing the query
|
115
|
|
- with self.db as cur:
|
|
109
|
+ with self.connection as cur:
|
116
|
110
|
results = cur.execute(query)
|
117
|
111
|
|
118
|
112
|
# Returning it as a list of dict
|
|
@@ -123,9 +117,9 @@ class LeDataSourceSQL(DummyDatasource):
|
123
|
117
|
# @return dict : Dictionnary with (FIELD, OPERATOR):VALUE style elements
|
124
|
118
|
def _prepare_filters(self, filters):
|
125
|
119
|
prepared_filters = {}
|
126
|
|
- for filter in filters:
|
127
|
|
- prepared_filter_key = (filter[0], filter[1])
|
128
|
|
- prepared_filter_value = filter[2]
|
|
120
|
+ for filter_item in filters:
|
|
121
|
+ prepared_filter_key = (filter_item[0], filter_item[1])
|
|
122
|
+ prepared_filter_value = filter_item[2]
|
129
|
123
|
prepared_filters[prepared_filter_key] = prepared_filter_value
|
130
|
124
|
|
131
|
|
- return prepared_filters
|
|
125
|
+ return prepared_filters
|