Browse Source

sql datasource: update() for rel2type

ArnAud 9 years ago
parent
commit
a084678831
1 changed files with 30 additions and 23 deletions
  1. 30
    23
      DataSource/MySQL/leapidatasource.py

+ 30
- 23
DataSource/MySQL/leapidatasource.py View File

@@ -68,13 +68,14 @@ class LeDataSourceSQL(DummyDatasource):
68 68
             main_table = utils.common_tables['relation']
69 69
             fields = [(main_table, target_cls.name2class('LeRelation').fieldlist())]
70 70
         elif target_cls.is_lerel2type():
71
+            # find main table
71 72
             main_table = utils.common_tables['relation']
72
-
73
+            # find relational table
73 74
             class_table = utils.r2t_table_name(target_cls._superior_cls.__name__, target_cls._subordinate_cls.__name__)
74 75
             relation_fieldname = target_cls.name2class('LeRelation').uidname()
75 76
             main_relation_id = utils.column_prefix(main_table, relation_fieldname)
76 77
             class_relation_id = utils.column_prefix(class_table, relation_fieldname)
77
-
78
+            # do the joins
78 79
             lodel_id = target_cls.name2class('LeObject').uidname()
79 80
             joins = [
80 81
                 left_join(class_table, {main_relation_id:class_relation_id}),
@@ -106,12 +107,11 @@ class LeDataSourceSQL(DummyDatasource):
106 107
         if offset:
107 108
             kwargs['offset'] = offset
108 109
 
109
-       # @todo implement relational filters
110
+        # @todo implement relational filters
110 111
 
111 112
         # prefix filters'' column names, and prepare dict for mosql where {(fieldname, op): value}
112 113
         wheres = {(utils.find_prefix(name, fields), op):value for name,op,value in filters}
113 114
         query = select(main_table, select=prefixed_field_list, where=wheres, joins=joins, **kwargs)
114
-        #print ('SQL', query)
115 115
 
116 116
         # Executing the query
117 117
         cur = utils.query(self.connection, query)
@@ -174,30 +174,38 @@ class LeDataSourceSQL(DummyDatasource):
174 174
     # @return the number of updated components
175 175
     # @todo implement other filters than lodel_id
176 176
     def update(self, target_cls, filters, rel_filters, **datas):
177
-        #print(target_cls, filters, rel_filters, datas)
177
+        class_table = False
178
+
178 179
         # it is a LeType
179
-        if not target_cls.is_letype():
180
-            raise AttributeError("'%s' is not a LeType, it is not possible to update it" % target_cls)
180
+        if target_cls.is_letype():
181
+            # find main table and main table datas
182
+            main_table = utils.common_tables['object']
183
+            main_datas = {target_cls.uidname(): raw(target_cls.uidname())} #  be sure to have one SET clause
184
+            main_fields = target_cls.name2class('LeObject').fieldlist()
185
+            class_table = utils.object_table_name(target_cls.leo_class().__name__)
186
+        elif target_cls.is_lerel2type():
187
+            main_table = utils.common_tables['relation']
188
+            le_relation = target_cls.name2class('LeRelation')
189
+            main_datas = {le_relation.uidname(): raw(le_relation.uidname())} #  be sure to have one SET clause
190
+            main_fields = le_relation.fieldlist()
191
+
192
+            class_table = utils.r2t_table_name(target_cls._superior_cls.__name__, target_cls._subordinate_cls.__name__)
193
+        else:
194
+            raise AttributeError("'%s' is not a LeType or a LeRelation, it is not possible to insert it" % target_cls)
181 195
 
182
-        # find main table and main table datas
183
-        main_table = self.datasource_utils.objects_table_name
184
-        main_datas = {self.datasource_utils.field_lodel_id: raw(self.datasource_utils.field_lodel_id)} #  be sure to have one SET clause
185
-        for main_column_name in common_fields:
196
+        for main_column_name in main_fields:
186 197
             if main_column_name in datas:
187 198
                 main_datas[main_column_name] = datas[main_column_name]
188 199
                 del(datas[main_column_name])
189 200
 
190 201
         wheres = {(name, op):value for name,op,value in filters}
191
-        query = update(main_table, wheres, main_datas)
192
-        #print(query)
193
-        self.datasource_utils.query(self.connection, query)
202
+        sql = update(main_table, wheres, main_datas)
203
+        utils.query(self.connection, sql)
194 204
 
195 205
         # update on class table
196
-        if datas:
197
-            class_table = self.datasource_utils.get_table_name_from_class(target_cls._leclass.__name__)
198
-            query = update(class_table, wheres, datas)
199
-            #print(query)
200
-            self.datasource_utils.query(self.connection, query)
206
+        if class_table and datas:
207
+            sql = update(class_table, wheres, datas)
208
+            utils.query(self.connection, sql)
201 209
 
202 210
         return True
203 211
 
@@ -207,10 +215,12 @@ class LeDataSourceSQL(DummyDatasource):
207 215
     # @return The inserted component's id
208 216
     # @todo should work with LeType, LeClass, and Relations
209 217
     def insert(self, target_cls, **datas):
218
+        class_table = False
219
+
210 220
         # it is a LeType
211 221
         if target_cls.is_letype():
212 222
             main_table = utils.common_tables['object']
213
-            main_datas = {'class_id':target_cls._leclass._class_id, 'type_id':target_cls._type_id}
223
+            main_datas = {'class_id':target_cls.leo_class()._class_id, 'type_id':target_cls._type_id}
214 224
             main_fields = target_cls.name2class('LeObject').fieldlist()
215 225
 
216 226
             class_table = utils.object_table_name(target_cls.leo_class().__name__)
@@ -223,7 +233,6 @@ class LeDataSourceSQL(DummyDatasource):
223 233
                 utils.column_name(target_cls._subordinate_field_name): datas[target_cls._subordinate_field_name].lodel_id
224 234
             }
225 235
             main_fields = target_cls.name2class('LeRelation').fieldlist()
226
-            class_table = False
227 236
         # it is a relation
228 237
         elif target_cls.is_lerel2type():
229 238
             main_table = utils.common_tables['relation']
@@ -247,7 +256,6 @@ class LeDataSourceSQL(DummyDatasource):
247 256
                 del(datas[main_column_name])
248 257
 
249 258
         sql = insert(main_table, main_datas)
250
-        #print (sql)
251 259
         cur = utils.query(self.connection, sql)
252 260
         lodel_id = cur.lastrowid
253 261
 
@@ -255,7 +263,6 @@ class LeDataSourceSQL(DummyDatasource):
255 263
             # insert in class_table
256 264
             datas[fk_name] = lodel_id
257 265
             sql = insert(class_table, datas)
258
-            #print (sql)
259 266
             utils.query(self.connection, sql)
260 267
 
261 268
         return lodel_id

Loading…
Cancel
Save