1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2026-06-16 23:40:48 +02:00

SQL datasource: insert method for relations

This commit is contained in:
ArnAud 2015-12-17 11:14:00 +01:00
commit 95deb4574e
3 changed files with 21 additions and 12 deletions

View file

@ -180,6 +180,7 @@ class LeDataSourceSQL(DummyDatasource):
main_datas = {'class_id':target_cls._leclass._class_id, 'type_id':target_cls._type_id}
main_fields = common_fields
class_table = self.datasource_utils.get_table_name_from_class(target_cls._leclass.__name__)
fk_name = self.datasource_utils.field_lodel_id
# it is a hierarchy
elif target_cls.is_lehierarch():
main_table = self.datasource_utils.relations_table_name
@ -188,8 +189,12 @@ class LeDataSourceSQL(DummyDatasource):
class_table = False
# it is a relation
elif target_cls.is_lerel2type():
print('TODO', datas)
pass
main_table = self.datasource_utils.relations_table_name
main_datas = {'id_sup':datas['lesup'].lodel_id, 'id_sub':datas['lesub'].lodel_id}
main_fields = relations_common_fields
class_table = self.datasource_utils.get_r2t2table_name(datas['lesup']._leclass.__name__, datas['lesub'].__class__.__name__)
del(datas['lesup'], datas['lesub'])
fk_name = self.datasource_utils.relations_pkname
else:
raise AttributeError("'%s' is not a LeType or a LeRelation, it is not possible to insert it" % target_cls)
@ -206,8 +211,10 @@ class LeDataSourceSQL(DummyDatasource):
if class_table:
# insert in class_table
datas[self.datasource_utils.field_lodel_id] = lodel_id
self.datasource_utils.query(self.connection, insert(class_table, datas))
datas[fk_name] = lodel_id
sql = insert(class_table, datas)
#print (sql)
self.datasource_utils.query(self.connection, sql)
return lodel_id

View file

@ -143,11 +143,11 @@ class _LeHierarch(_LeRelation):
class _LeRel2Type(_LeRelation):
## @brief Stores the list of fieldtypes handling relations attributes
_rel_attr_fieldtypes = dict()
## @brief Delete current instance from DB
def delete(self):
lecrud._LeCrud._delete(self)
## @brief Implements insert for rel2type
# @todo checks when autodetecing the rel2type class
@classmethod
@ -161,8 +161,8 @@ class _LeRel2Type(_LeRelation):
super().insert(datas, classname)
## @brief Given a superior and a subordinate, returns the classname of the give rel2type
# @param lesupclass LeClass : LeClass child class (can be a LeType or a LeClass child)
# @param lesubclass LeType : A LeType child class
# @param lesupclass LeClass : LeClass child class (not an instance) (can be a LeType or a LeClass child)
# @param lesubclass LeType : A LeType child class (not an instance)
# @return a name as string
@staticmethod
def relname(lesupclass, lesubclass):

View file

@ -129,14 +129,16 @@ class _LeType(_LeClass):
# @param leo_tolink LeObject : LeObject child instance to link with
# @param **datas : Relation attributes (if any)
# @return a relation id if success
def link_with(self, leo_tolink, **datas):
def link_with(self, leo_tolink, datas):
# Fetch rel2type leapi class
r2t = self.name2class('LeRel2Type')
r2tcls = self.name2class(r2t.relname(self, leo_tolink))
class_name = r2t.relname(self, leo_tolink.__class__)
r2tcls = self.name2class(class_name)
if not r2tcls:
raise ValueError("No rel2type possible between a '%s' as superior and a '%s' as subordinate" % (self._leclass.__name__, leo_tolink.__class__.__name__))
return r2tcls.insert(lesup = self, lesub = leo_tolink, **datas)
datas['lesup'] = self
datas['lesub'] = leo_tolink
return r2tcls.insert(datas, class_name)
## @brief Get the linked objects lodel_id
# @param letype LeType : Filter the result with LeType child class (not instance)