Browse Source

Implements del_relation in LeDatasourceSQL and get_relation modifications

Yann Weber 9 years ago
parent
commit
a960d83da3
2 changed files with 19 additions and 17 deletions
  1. 3
    0
      DataSource/MySQL/MySQL.py
  2. 16
    17
      leobject/datasources/ledatasourcesql.py

+ 3
- 0
DataSource/MySQL/MySQL.py View File

@@ -13,6 +13,9 @@ class MySQL(object):
13 13
     class_table_prefix = 'class_'
14 14
     objects_table_name = 'object'
15 15
     connections = settings.DATABASE_CONNECTIONS
16
+    ## @brief indicates if we want ON DELETE CASCADE on foreign keys
17
+    # @todo implementation in migration handler
18
+    fk_on_delete_cascade = False
16 19
 
17 20
     @classmethod
18 21
     ## @brief gets the table name from class name

+ 16
- 17
leobject/datasources/ledatasourcesql.py View File

@@ -224,22 +224,15 @@ class LeDataSourceSQL(DummyDatasource):
224 224
     # @param id_relation int : The relation identifier
225 225
     # @return bool
226 226
     def del_relation(self, id_relation):
227
-
228
-        #First step : Get the two concerned lodel_id to be able to fetch
229
-        #               each LeObject class and type in order to build 
230
-        #               the attribute table name (if one exists)
231
-
232
-
233
-        if lesup is None or lesub is None:
234
-            raise AttributeError("Missing member(s) of the relation to delete")
235
-        
236
-        delete_params = {'id_sup': lesup.lodel_id, 'id_sub': lesub.lodel_id}
237
-        if nature is not None:
238
-            delete_params['nature'] = nature
239
-
240
-        sql = delete(self.datasource_utils.relations_table_name, delete_params)
241
-
242 227
         with self.connection as cur:
228
+            pk_where = {MySQL.relations_pkname:id_relation}
229
+            if not MySQL.fk_on_delete_cascade and len(lesup._linked_types[lesub.__class__]) > 0:
230
+                #Delete the row in the relation attribute table
231
+                (lesup, lesub, _) = self.get_relation(id_relation, no_attr = False)
232
+                sql = delete(MySQL.relations_table_name, pk_where)
233
+                if cur.execute(sql) != 1:
234
+                    raise RuntimeError("Unknown SQL Error")
235
+            sql = delete(MySQL.relations_table_name, pk_where)
243 236
             if cur.execute(sql) != 1:
244 237
                 raise RuntimeError("Unknown SQL Error")
245 238
 
@@ -247,11 +240,12 @@ class LeDataSourceSQL(DummyDatasource):
247 240
     
248 241
     ## @brief Fetch a relation
249 242
     # @param id_relation int : The relation identifier
243
+    # @param no_attr bool : If true put None in place of relations attributes
250 244
     # @return a tuple(lesup, lesub, dict_attr) or False if no relation exists with this id
251 245
     # @throw Exception if the nature is not NULL
252 246
     #
253 247
     # @todo TESTS
254
-    def get_relation(self, id_relation):
248
+    def get_relation(self, id_relation, no_attr = False):
255 249
         with self.connection as cur:
256 250
             sql = select(MySQL.relation_table_name, {MySQL.relations_pkname: id_relation})
257 251
             if cur.execute(sql) != 1:
@@ -262,11 +256,16 @@ class LeDataSourceSQL(DummyDatasource):
262 256
             if len(res) > 1:
263 257
                 raise RuntimeError("When selecting on primary key, get more than one result. Bailout")
264 258
 
259
+            if res['nature'] != None:
260
+                raise ValueError("The relation with id %d is not a rel2type relation"%id_relation)
261
+
265 262
             leobj = leobject.lefactory.LeFactory.leobj_from_name('LeObject')
266 263
             lesup = leobj.uid2leobj(res['id_sup'])
267 264
             lesub = leobj.uid2leobj(res['id_sub'])
268 265
             
269
-            if len(lesup._linked_types[lesub.__class__]) == 0:
266
+            if no_attr:
267
+                attrs = None
268
+            elif len(lesup._linked_types[lesub.__class__]) == 0:
270 269
                 #No relation attributes
271 270
                 attrs = dict()
272 271
             else:

Loading…
Cancel
Save